From 8bb78b05eb974889aa5b99e16de451792c1b3261 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ghuloum Date: Wed, 25 Dec 2024 07:59:26 +0300 Subject: [PATCH] handled class declarations in dts files; initialized context for dts modules --- src/library-manager.ts | 11 ++++++++--- src/parse-dts.ts | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/library-manager.ts b/src/library-manager.ts index a75def9..bcb6301 100644 --- a/src/library-manager.ts +++ b/src/library-manager.ts @@ -256,6 +256,7 @@ abstract class Module implements imported_module { assert(this.state.type === "fresh"); const context = this.state.context; const binding = context[name]; + if (!binding) throw new Error(`binding missing for ${name} in ${this.path}`); switch (binding.type) { case "lexical": return { type: "imported_lexical", cuid: this.state.cid, name: binding.name }; @@ -268,7 +269,7 @@ abstract class Module implements imported_module { clauses: binding.clauses, }; default: - throw new Error(`unhandled binding type ${binding.type}`); + throw new Error(`unhandled binding type ${binding.type} for label '${name}'`); } } @@ -447,10 +448,14 @@ class DtsModule extends Module { case "local": { const res: import_resolution[] = []; if (binding.is_type) { - res.push({ type: "type", label: { cuid: cid, name: `t.${binding.name}` } }); + const label = `t.${binding.name}`; + context[label] = { type: "type", name: binding.name }; + res.push({ type: "type", label: { cuid: cid, name: label } }); } if (binding.is_lexical) { - res.push({ type: "lexical", label: { cuid: cid, name: `l.${binding.name}` } }); + const label = `l.${binding.name}`; + context[label] = { type: "lexical", name: binding.name }; + res.push({ type: "lexical", label: { cuid: cid, name: label } }); } return [name, res]; } diff --git a/src/parse-dts.ts b/src/parse-dts.ts index 2f6c531..25b7155 100644 --- a/src/parse-dts.ts +++ b/src/parse-dts.ts @@ -74,7 +74,7 @@ function parse_statements(statements: TS.NodeArray): ts_exports { function handle_main_definition(global: boolean) { function push_lexical(name: string) { - assert(!lexicals[name]); + assert(!lexicals[name], `lexical '${name}' is multiply defined`); lexicals[name] = { global }; } function push_type(name: string) { @@ -244,6 +244,17 @@ function parse_statements(statements: TS.NodeArray): ts_exports { } decl.declarationList.declarations.forEach(handle_decl); } + function handle_class_declaration(decl: TS.ClassDeclaration) { + const name = decl.name?.text; + const exported = decl.modifiers?.some((x) => x.kind === TS.SyntaxKind.ExportKeyword) ?? false; + assert(!exported); + const declared = + decl.modifiers?.some((x) => x.kind === TS.SyntaxKind.DeclareKeyword) ?? false; + assert(declared); + assert(name !== undefined); + push_lexical(name); + push_type(name); + } function handle_statement(stmt: TS.Statement) { switch (stmt.kind) { case TS.SyntaxKind.ImportDeclaration: @@ -260,6 +271,8 @@ function parse_statements(statements: TS.NodeArray): ts_exports { return handle_function_declaration(stmt as TS.FunctionDeclaration); case TS.SyntaxKind.VariableStatement: return handle_variable_statement(stmt as TS.VariableStatement); + case TS.SyntaxKind.ClassDeclaration: + return handle_class_declaration(stmt as TS.ClassDeclaration); default: throw new Error(`unhandled statement in d.ts file '${TS.SyntaxKind[stmt.kind]}'`); }