Skip to content

Commit

Permalink
Typeof enum (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasavila00 authored Nov 1, 2024
1 parent 6a33a81 commit 8195ca6
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 12 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.55

### Patch Changes

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

## 1.0.54

### 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.54",
"version": "1.0.55",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.57",
"@beff/cli": "workspace:^0.0.58",
"@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.58

### Patch Changes

- typeof enum

## 0.0.57

### 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.57",
"version": "0.0.58",
"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.58

### Patch Changes

- typeof enum
- Updated dependencies
- @beff/cli@0.0.58

## 0.0.57

### 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.57",
"version": "0.0.58",
"description": "",
"main": "dist/cjs/index.js",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.57",
"@beff/cli": "workspace:^0.0.58",
"zod": "^3.23.5"
},
"devDependencies": {
Expand Down
2 changes: 2 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 {
EnumMemberNotFound,
TplLitTypeUnsupported,
TwoCallsToBuildSchemas,
CannotResolveRefInJsonSchemaToTplLit,
Expand Down Expand Up @@ -459,6 +460,7 @@ impl DiagnosticInfoMessage {
DiagnosticInfoMessage::TplLitTypeUnsupported => {
"Template literal type is not supported".to_string()
}
DiagnosticInfoMessage::EnumMemberNotFound => "Enum member not found".to_string(),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/beff-core/src/sym_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ impl<'a, R: FileManager> TypeResolver<'a, R> {
return Ok(ResolvedLocalSymbol::Expr(alias.clone()));
}

if let Some(enum_) = self.get_current_file().locals.enums.get(k) {
return Ok(ResolvedLocalSymbol::TsEnumDecl(enum_.clone()));
}

if let Some(exported) = self
.get_current_file()
.symbol_exports
Expand Down
36 changes: 33 additions & 3 deletions packages/beff-core/src/type_to_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,38 @@ impl<'a, 'b, R: FileManager> TypeToSchema<'a, 'b, R> {
Expr::TsSatisfies(c) => self.typeof_expr(&c.expr, as_const),
Expr::Member(m) => {
let mut ctx = SemTypeContext::new();
if let Expr::Ident(i) = &m.obj.as_ref() {
let s =
TypeResolver::new(self.files, &self.current_file).resolve_local_value(i)?;
if let ResolvedLocalSymbol::TsEnumDecl(decl) = s {
// check if enum contains the member
let prop = match &m.prop {
MemberProp::Ident(i) => Some(i.sym.to_string()),
MemberProp::Computed(c) => match self.typeof_expr(&c.expr, as_const)? {
JsonSchema::Const(JsonSchemaConst::String(s)) => Some(s.clone()),
_ => None,
},
MemberProp::PrivateName(_) => None,
};
let prop_str = prop.unwrap_or("".to_string());
let found = decl.members.iter().find(|it| {
//
match &it.id {
TsEnumMemberId::Ident(i2) => {
i2.sym.to_string() == prop_str.as_str()
}
TsEnumMemberId::Str(_) => unreachable!(),
}
});

if let Some(_found) = found {
// return the enum's type
return self.convert_enum_decl(&decl);
}
return self
.error(&m.prop.span(), DiagnosticInfoMessage::EnumMemberNotFound);
}
}
let obj = self.typeof_expr(&m.obj, as_const)?;
if let JsonSchema::Object { vs, rest: _ } = &obj {
// try to do it syntatically to preserve aliases
Expand Down Expand Up @@ -1507,9 +1539,7 @@ impl<'a, 'b, R: FileManager> TypeToSchema<'a, 'b, R> {
self.collect_value_exports(&file_name, &mut acc)?;
Ok(JsonSchema::object(acc, None))
}
ResolvedLocalSymbol::TsEnumDecl(_) => {
self.error(span, DiagnosticInfoMessage::TypeofTsEnumNotSupported)
}
ResolvedLocalSymbol::TsEnumDecl(enum_decl) => self.convert_enum_decl(&enum_decl),
ResolvedLocalSymbol::TsBuiltin(_) => {
self.error(span, DiagnosticInfoMessage::TypeOfTsBuiltinNotSupported)
}
Expand Down
26 changes: 26 additions & 0 deletions packages/beff-core/tests/print_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,32 @@ mod tests {
"#));
}
#[test]
fn ok_repro10() {
insta::assert_snapshot!(ok(r#"
enum E {
A="A",
B="B"
};
export const ABC = {a: E.B, } as const satisfies Record<string, string>;
export type AllTs = (keyof typeof ABC);
parse.buildParsers<{ AllTs: AllTs }>();
"#));
}
#[test]
fn ok_repro11() {
insta::assert_snapshot!(ok(r#"
enum E {
A="A",
B="B"
};
const val = E.B as const;
export type AllTs = typeof val;
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 enum E {\n A=\"A\",\n B=\"B\"\n };\n export const ABC = {a: E.B, } as const satisfies Record<string, string>;\n export type AllTs = (keyof typeof ABC);\n\n parse.buildParsers<{ AllTs: AllTs }>();\n \"#)"
---
type AllTs = "a";
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 enum E {\n A=\"A\",\n B=\"B\"\n };\n const val = E.B as const;\n export type AllTs = typeof val;\n\n parse.buildParsers<{ AllTs: AllTs }>();\n \"#)"
---
type AllTs = "A" | "B";
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.57",
"@beff/cli": "workspace:^0.0.58",
"@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 8195ca6

Please sign in to comment.