Skip to content

Commit

Permalink
Fix export declare bug (#177)
Browse files Browse the repository at this point in the history
* Fix export declare bug

* version bump

* clippy
  • Loading branch information
lucasavila00 authored May 1, 2024
1 parent 1a43261 commit 600c6bb
Show file tree
Hide file tree
Showing 23 changed files with 254 additions and 44 deletions.
8 changes: 8 additions & 0 deletions examples/standalone-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# node-server

## 1.0.30

### Patch Changes

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

## 1.0.29

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions examples/standalone-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "standalone-parser",
"version": "1.0.29",
"version": "1.0.30",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.32",
"@beff/cli": "workspace:^0.0.33",
"@beff/client": "workspace:^",
"vitest": "^0.34.4",
"zod": "^3.23.5"
Expand Down
3 changes: 3 additions & 0 deletions examples/standalone-parser/src/generated/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const buildParsersInput = {
"Arr2C": function(ctx, input, required = true) {
return validators.Arr2(ctx, input, required);
},
"Arr3": function(ctx, input, required = true) {
return validators.Arr3(ctx, input, required);
},
"DiscriminatedUnion": function(ctx, input, required = true) {
return validators.DiscriminatedUnion(ctx, input, required);
},
Expand Down
6 changes: 6 additions & 0 deletions examples/standalone-parser/src/generated/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"C"
]
},
"Arr3": {
"enum": [
"X",
"Y"
]
},
"AvatarSize": {
"type": "string"
},
Expand Down
7 changes: 7 additions & 0 deletions examples/standalone-parser/src/generated/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ function decodeConst(ctx, input, required, constValue) {
}


function DecodeArr3(ctx, input, required = true) {
return decodeAnyOfConsts(ctx, input, required, [
"X",
"Y"
]);
}
function DecodeOmitSettings(ctx, input, required = true) {
return decodeObject(ctx, input, required, {
"d": (ctx, input)=>(decodeObject(ctx, input, true, {
Expand Down Expand Up @@ -541,6 +547,7 @@ function DecodeUnionWithEnumAccess(ctx, input, required = true) {
});
}
const validators = {
Arr3: DecodeArr3,
OmitSettings: DecodeOmitSettings,
Settings: DecodeSettings,
PartialObject: DecodePartialObject,
Expand Down
3 changes: 2 additions & 1 deletion examples/standalone-parser/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from "./generated/parser";
import { Arr2, OtherEnum, ValidCurrency, OtherEnum2 } from "./types";
import { Arr2, OtherEnum, ValidCurrency, OtherEnum2, Arr3 } from "./types";

export const ALL_TYPES = [
"OmitSettings",
Expand Down Expand Up @@ -171,6 +171,7 @@ export const {
ValidCurrency: ValidCurrencyCodec,
UnionWithEnumAccess,
} = parse.buildParsers<{
Arr3: Arr3;
OmitSettings: OmitSettings;
RequiredPartialObject: RequiredPartialObject;
LevelAndDSettings: LevelAndDSettings;
Expand Down
2 changes: 1 addition & 1 deletion examples/standalone-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export type Arr2 = (typeof ARR2)[number];

export { ValidCurrency } from "./types2";

export { OtherEnum2 } from "./types2";
export { OtherEnum2, Arr3 } from "./types2";
4 changes: 4 additions & 0 deletions examples/standalone-parser/src/types3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export enum OtherEnum2 {
C = "c",
D = "d",
}

const ARR3 = ["X", "Y"] as const;

export type Arr3 = (typeof ARR3)[number];
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.33

### Patch Changes

- fix export declare bug

## 0.0.32

### 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.32",
"version": "0.0.33",
"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.33

### Patch Changes

- fix export declare bug
- Updated dependencies
- @beff/cli@0.0.33

## 0.0.32

### 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.32",
"version": "0.0.33",
"description": "",
"main": "dist/cjs/index.js",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.32"
"@beff/cli": "workspace:^0.0.33"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.7.2",
Expand Down
8 changes: 8 additions & 0 deletions packages/beff-core/src/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{BffFileName, ParsedModule};

#[derive(Debug, Clone)]
pub enum DiagnosticInfoMessage {
CannotUseExprDeclAsQualified,
CannotResolveNamespaceTypeExprDecl,
CannotResolveNamespaceTypeNamespaceSymbol,
CannotResolveNamespaceTypeValueExpr,
CannotResolveNamespaceTypeSomethingOfOtherFile,
Expand Down Expand Up @@ -365,6 +367,12 @@ impl DiagnosticInfoMessage {
DiagnosticInfoMessage::CannotResolveNamespaceTypeSomethingOfOtherFile => {
"Cannot resolve namespace type something of other file".to_string()
}
DiagnosticInfoMessage::CannotResolveNamespaceTypeExprDecl => {
"Cannot resolve namespace type expression declaration".to_string()
}
DiagnosticInfoMessage::CannotUseExprDeclAsQualified => {
"Cannot use expression declaration as qualified".to_string()
}
}
}
}
Expand Down
26 changes: 24 additions & 2 deletions packages/beff-core/src/import_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl<'a, R: FsModuleResolver> Visit for ImportsVisitor<'a, R> {
Rc::new(SymbolExport::TsInterfaceDecl {
decl: Rc::new(*n.clone()),
span: n.span,
original_file: self.current_file.clone(),
}),
);
}
Expand All @@ -131,6 +132,7 @@ impl<'a, R: FsModuleResolver> Visit for ImportsVisitor<'a, R> {
Rc::new(SymbolExport::TsEnumDecl {
decl: Rc::new(*decl.clone()),
span: decl.span,
original_file: self.current_file.clone(),
}),
);
}
Expand All @@ -149,22 +151,39 @@ impl<'a, R: FsModuleResolver> Visit for ImportsVisitor<'a, R> {
name: id.sym.clone(),
params: type_params.as_ref().map(|it| it.as_ref().clone().into()),
span: *span,
original_file: self.current_file.clone(),
}),
);
}
Decl::Var(decl) => {
for it in &decl.decls {
Decl::Var(var_decl) => {
for it in &var_decl.decls {
if let Some(expr) = &it.init {
if let Pat::Ident(it) = &it.name {
let name = it.sym.clone();
let export = Rc::new(SymbolExport::ValueExpr {
expr: Rc::new(*expr.clone()),
name: name.clone(),
span: it.span,
original_file: self.current_file.clone(),
});
self.symbol_exports.insert_value(name, export);
}
}

if var_decl.declare {
if let Pat::Ident(it) = &it.name {
if let Some(ann) = &it.type_ann {
let name = it.sym.clone();
let export = Rc::new(SymbolExport::ExprDecl {
ty: Rc::new(*ann.type_ann.clone()),
name: name.clone(),
span: it.span,
original_file: self.current_file.clone(),
});
self.symbol_exports.insert_value(name, export);
}
}
}
}
}

Expand Down Expand Up @@ -309,6 +328,7 @@ pub fn parse_and_bind<R: FsModuleResolver>(
name: k.0,
params: params.clone(),
span: ty.span(),
original_file: file_name.clone(),
}),
);
continue;
Expand All @@ -320,6 +340,7 @@ pub fn parse_and_bind<R: FsModuleResolver>(
Rc::new(SymbolExport::TsEnumDecl {
decl: enum_.clone(),
span: enum_.span(),
original_file: file_name.clone(),
}),
);
continue;
Expand All @@ -331,6 +352,7 @@ pub fn parse_and_bind<R: FsModuleResolver>(
Rc::new(SymbolExport::TsInterfaceDecl {
decl: intf.clone(),
span: intf.span(),
original_file: file_name.clone(),
}),
);
continue;
Expand Down
41 changes: 32 additions & 9 deletions packages/beff-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,29 @@ pub enum SymbolExport {
ty: Rc<TsType>,
name: JsWord,
span: Span,
original_file: BffFileName,
},
TsInterfaceDecl {
decl: Rc<TsInterfaceDecl>,
span: Span,
original_file: BffFileName,
},
TsEnumDecl {
decl: Rc<TsEnumDecl>,
span: Span,
original_file: BffFileName,
},
ValueExpr {
expr: Rc<Expr>,
name: JsWord,
span: Span,
original_file: BffFileName,
},
ExprDecl {
name: JsWord,
span: Span,
original_file: BffFileName,
ty: Rc<TsType>,
},
StarOfOtherFile {
reference: Rc<ImportReference>,
Expand All @@ -77,12 +87,13 @@ pub enum SymbolExport {
impl SymbolExport {
pub fn span(&self) -> Span {
match self {
SymbolExport::TsType { span, .. } => *span,
SymbolExport::TsInterfaceDecl { span, .. } => *span,
SymbolExport::ValueExpr { span, .. } => *span,
SymbolExport::StarOfOtherFile { span, .. } => *span,
SymbolExport::SomethingOfOtherFile { span, .. } => *span,
SymbolExport::TsEnumDecl { span, .. } => *span,
SymbolExport::TsType { span, .. }
| SymbolExport::TsInterfaceDecl { span, .. }
| SymbolExport::ValueExpr { span, .. }
| SymbolExport::StarOfOtherFile { span, .. }
| SymbolExport::SomethingOfOtherFile { span, .. }
| SymbolExport::TsEnumDecl { span, .. }
| SymbolExport::ExprDecl { span, .. } => *span,
}
}
}
Expand Down Expand Up @@ -238,6 +249,7 @@ pub struct ParsedModuleLocals {
pub enums: HashMap<(JsWord, SyntaxContext), Rc<TsEnumDecl>>,

pub exprs: HashMap<(JsWord, SyntaxContext), Rc<Expr>>,
pub exprs_decls: HashMap<(JsWord, SyntaxContext), Rc<TsType>>,
}
impl ParsedModuleLocals {
pub fn new() -> ParsedModuleLocals {
Expand All @@ -246,6 +258,7 @@ impl ParsedModuleLocals {
interfaces: HashMap::new(),
enums: HashMap::new(),
exprs: HashMap::new(),
exprs_decls: HashMap::new(),
}
}
}
Expand Down Expand Up @@ -275,9 +288,8 @@ impl ParserOfModuleLocals {
match it {
ModuleItem::Stmt(Stmt::Decl(decl)) => {
// add expr to self.content

if let Decl::Var(decls) = decl {
for it in &decls.decls {
if let Decl::Var(var_decl) = decl {
for it in &var_decl.decls {
if let Some(expr) = &it.init {
if let Pat::Ident(id) = &it.name {
self.content.exprs.insert(
Expand All @@ -286,6 +298,17 @@ impl ParserOfModuleLocals {
);
}
}

if var_decl.declare {
if let Pat::Ident(id) = &it.name {
if let Some(ann) = &id.type_ann {
self.content.exprs_decls.insert(
(id.sym.clone(), id.span.ctxt),
Rc::new(*ann.type_ann.clone()),
);
}
}
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/beff-core/src/sym_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ impl<'a, R: FileManager> TypeResolver<'a, R> {
}
.into(),
}),
SymbolExport::ExprDecl { .. } => Err(self
.make_err(
&i.span,
DiagnosticInfoMessage::CannotResolveNamespaceTypeExprDecl,
)
.into()),
}
}
pub fn resolve_namespace_symbol(
Expand Down Expand Up @@ -252,6 +258,7 @@ impl<'a, R: FileManager> TypeResolver<'a, R> {
SymbolExport::TsType { .. }
| SymbolExport::TsEnumDecl { .. }
| SymbolExport::TsInterfaceDecl { .. }
| SymbolExport::ExprDecl { .. }
| SymbolExport::StarOfOtherFile { .. }
| SymbolExport::SomethingOfOtherFile { .. } => {}
}
Expand Down
Loading

0 comments on commit 600c6bb

Please sign in to comment.