Skip to content

Commit

Permalink
Typeof bin expr and spread (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasavila00 authored Nov 1, 2024
1 parent 89496d3 commit 8840be1
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 14 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.57

### Patch Changes

- Updated dependencies
- @beff/cli@0.0.60
- @beff/client@0.0.60

## 1.0.56

### 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.56",
"version": "1.0.57",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.59",
"@beff/cli": "workspace:^0.0.60",
"@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.60

### Patch Changes

- fix bugs

## 0.0.59

### 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.59",
"version": "0.0.60",
"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.60

### Patch Changes

- fix bugs
- Updated dependencies
- @beff/cli@0.0.60

## 0.0.59

### 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.59",
"version": "0.0.60",
"description": "",
"main": "dist/cjs/index.js",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.59",
"@beff/cli": "workspace:^0.0.60",
"zod": "^3.23.5"
},
"devDependencies": {
Expand Down
28 changes: 23 additions & 5 deletions packages/beff-core/src/type_to_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,11 +1270,19 @@ impl<'a, 'b, R: FileManager> TypeToSchema<'a, 'b, R> {

for it in &lit.props {
match it {
PropOrSpread::Spread(_) => {
return self.error(
&it.span(),
DiagnosticInfoMessage::TypeofObjectUnsupportedSpread,
)
PropOrSpread::Spread(sp) => {
let spread_ty = self.typeof_expr(&sp.expr, as_const)?;

if let JsonSchema::Object { vs: spread_vs, .. } = spread_ty {
for (k, v) in spread_vs {
vs.insert(k, v);
}
} else {
return self.error(
&it.span(),
DiagnosticInfoMessage::TypeofObjectUnsupportedSpread,
);
}
}
PropOrSpread::Prop(p) => match p.as_ref() {
Prop::KeyValue(p) => {
Expand Down Expand Up @@ -1452,6 +1460,16 @@ impl<'a, 'b, R: FileManager> TypeToSchema<'a, 'b, R> {
self.convert_sem_type(access_st, &mut ctx, &m.prop.span())
}
Expr::Arrow(_a) => Ok(JsonSchema::Function),
Expr::Bin(e) => {
let left = self.typeof_expr(&e.left, as_const)?;
let right = self.typeof_expr(&e.right, as_const)?;

match (left, right) {
(JsonSchema::Number, JsonSchema::Number) => Ok(JsonSchema::Number),
(JsonSchema::String, JsonSchema::String) => Ok(JsonSchema::String),
_ => self.error(&e.span(), DiagnosticInfoMessage::CannotConvertExprToSchema),
}
}
_ => {
dbg!(&e);
self.error(&e.span(), DiagnosticInfoMessage::CannotConvertExprToSchema)
Expand Down
19 changes: 19 additions & 0 deletions packages/beff-core/tests/print_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,25 @@ mod tests {
"#));
}
#[test]
fn ok_repro12() {
insta::assert_snapshot!(ok(r#"
const val = 1 + 1;
export type AllTs = typeof val;
parse.buildParsers<{ AllTs: AllTs }>();
"#));
}
#[test]
fn ok_repro13() {
insta::assert_snapshot!(ok(r#"
const val = {a: 1} as const;
const spread = {...val, b: 2} as const;
export type AllTs = typeof spread;
parse.buildParsers<{ AllTs: AllTs }>();
"#));
}
#[test]
fn ok_void() {
insta::assert_snapshot!(ok(r#"
export type IX = void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: packages/beff-core/tests/print_parser.rs
expression: "ok(r#\"\n const val = 1 + 1;\n export type AllTs = typeof val;\n\n parse.buildParsers<{ AllTs: AllTs }>();\n \"#)"
---
type AllTs = number;
type AllTs = AllTs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: packages/beff-core/tests/print_parser.rs
expression: "ok(r#\"\n const val = {a: 1} as const;\n const spread = {...val, b: 2} as const;\n export type AllTs = typeof spread;\n\n parse.buildParsers<{ AllTs: AllTs }>();\n \"#)"
---
type AllTs = { "a": 1; "b": 2 };
type AllTs = AllTs;
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.59",
"@beff/cli": "workspace:^0.0.60",
"@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 8840be1

Please sign in to comment.