Skip to content

Commit

Permalink
handled class declarations in dts files; initialized context for dts …
Browse files Browse the repository at this point in the history
…modules
  • Loading branch information
azizghuloum committed Dec 25, 2024
1 parent ba1f7db commit 8bb78b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/library-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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}'`);
}
}

Expand Down Expand Up @@ -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];
}
Expand Down
15 changes: 14 additions & 1 deletion src/parse-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function parse_statements(statements: TS.NodeArray<TS.Statement>): 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) {
Expand Down Expand Up @@ -244,6 +244,17 @@ function parse_statements(statements: TS.NodeArray<TS.Statement>): 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:
Expand All @@ -260,6 +271,8 @@ function parse_statements(statements: TS.NodeArray<TS.Statement>): 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]}'`);
}
Expand Down

0 comments on commit 8bb78b0

Please sign in to comment.