Skip to content

Commit

Permalink
Fix omit and intersection (#245)
Browse files Browse the repository at this point in the history
* Fix omit and intersection

* fix ci

* fix bugs

* fix omit intersection
  • Loading branch information
lucasavila00 authored Dec 14, 2024
1 parent 95620e9 commit b1dafa3
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 10 deletions.
8 changes: 8 additions & 0 deletions e2e-tests/standalone-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# node-server

## 1.0.61

### Patch Changes

- Updated dependencies
- @beff/cli@0.0.63
- @beff/client@0.0.64

## 1.0.60

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/standalone-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "standalone-parser",
"version": "1.0.60",
"version": "1.0.61",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.62",
"@beff/cli": "workspace:^0.0.63",
"@beff/client": "workspace:^",
"vitest": "^0.34.4",
"zod": "^3.23.5"
Expand Down
6 changes: 6 additions & 0 deletions packages/beff-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @beff/cli

## 0.0.63

### Patch Changes

- fix omit and intersection

## 0.0.62

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/beff-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beff/cli",
"version": "0.0.62",
"version": "0.0.63",
"description": "",
"bin": {
"beff": "./bin/index.js"
Expand Down
8 changes: 8 additions & 0 deletions packages/beff-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @beff/client

## 0.0.64

### Patch Changes

- fix omit and intersection
- Updated dependencies
- @beff/cli@0.0.63

## 0.0.63

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/beff-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beff/client",
"version": "0.0.63",
"version": "0.0.64",
"description": "",
"main": "dist/cjs/index.js",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.62",
"@beff/cli": "workspace:^0.0.63",
"zod": "^3.23.5"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/beff-core/src/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{BffFileName, ParsedModule};

#[derive(Debug, Clone)]
pub enum DiagnosticInfoMessage {
ObjectHasConflictingKeyValueInIntersection,
CannotResolveNamedImport,
EnumMemberNotFound,
TplLitTypeUnsupported,
Expand Down Expand Up @@ -461,6 +462,9 @@ impl DiagnosticInfoMessage {
DiagnosticInfoMessage::CannotResolveNamedImport => {
"Cannot resolve named import".to_string()
}
DiagnosticInfoMessage::ObjectHasConflictingKeyValueInIntersection => {
"Object has conflicting key value in intersection".to_string()
}
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion packages/beff-core/src/type_to_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,31 @@ impl<'a, 'b, R: FileManager> TypeToSchema<'a, 'b, R> {
let map = self.components.get(r).and_then(|it| it.as_ref()).cloned();
match map {
Some(Validator { schema, .. }) => self.extract_object(&schema, span),
_ => self.error(span, DiagnosticInfoMessage::ShouldHaveObjectAsTypeArgument),
None => self.error(span, DiagnosticInfoMessage::ShouldHaveObjectAsTypeArgument),
}
}
JsonSchema::AllOf(vs) => {
let mut acc = BTreeMap::new();

for v in vs {
let extracted = self.extract_object(v, span)?;

// check that if items have the same key, they have the same value

for (k, v) in &extracted {
if let Some(existing) = acc.get(k) {
if existing != v {
return self
.error(span, DiagnosticInfoMessage::ObjectHasConflictingKeyValueInIntersection);
}
}
}

acc.extend(extracted);
}

Ok(acc)
}
_ => self.error(span, DiagnosticInfoMessage::ShouldHaveObjectAsTypeArgument),
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/beff-core/tests/print_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,17 @@ mod tests {
export type ABC = {}
export type KABC = keyof ABC
parse.buildParsers<{ KABC: KABC }>();
"#));
}
#[test]
fn ok_omit_intersection() {
insta::assert_snapshot!(ok(r#"
export type A = {a: string}
export type B = {b: string}
export type KABC = Omit<A & B, 'a'>
parse.buildParsers<{ KABC: KABC }>();
"#));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: packages/beff-core/tests/print_parser.rs
expression: "ok(r#\"\n export type A = {a: string}\n export type B = {b: string}\n\n export type KABC = Omit<A & B, 'a'>\n \n parse.buildParsers<{ KABC: KABC }>();\n \"#)"
---
type A = { "a": string };
type B = { "b": string };
type KABC = { "b": string };
type KABC = KABC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"parser": "./src/parser.ts",
"outputDir": "./src/generated"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated/
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import parse from "./generated/parser";
type A = { a: string };
type A2 = { a: number; b: number };
type IX3 = A & A2;

parse.buildParsers<{ IX3: Omit<IX3, "a"> }>();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Error: Object has conflicting key value in intersection
/errors/conflict-object-kvs/src/parser.ts:6:27
4 | type IX3 = A & A2;
5 |
> 6 | parse.buildParsers<{ IX3: Omit<IX3, "a"> }>();
| ^^^^ Object has conflicting key value in intersection
7 |

Found 1 error
Empty file.
2 changes: 1 addition & 1 deletion packages/beff-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"devDependencies": {
"@babel/code-frame": "^7.22.13",
"@beff/cli": "workspace:^0.0.62",
"@beff/cli": "workspace:^0.0.63",
"@types/babel__code-frame": "^7.0.4",
"@types/node": "^20.6.2",
"@types/vscode": "^1.73.0",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b1dafa3

Please sign in to comment.