Skip to content

Commit 098c21d

Browse files
rmgarayitsfarseen
authored andcommittedOct 6, 2024
support exports that rename classes
1 parent 61a6f8c commit 098c21d

6 files changed

+68
-7
lines changed
 

‎tests/api/api.test.ts

+59-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This module tests that the classes generated in out.ts match the CSL API
22
// at the method level
33
import fs from "node:fs";
4-
import { ClassInfo, MethodInfo, ParamInfo, SomeType } from "../test_types";
4+
import { ClassInfo, ClassRename, MethodInfo, ParamInfo, SomeType } from "../test_types";
55
import grammar, { TypeDefsSemantics } from "./grammar.ohm-bundle"
66
import { describe, test } from "@jest/globals";
77

@@ -134,6 +134,51 @@ semantics.addOperation<string>("attrName()", {
134134
}
135135
return classes;
136136
}
137+
// These operations are unrelated to the previous ones. They are only used
138+
// for parsing renaming exports.
139+
}).addOperation<ClassRename>("rename()", {
140+
Rename(originalName, _as, newName) {
141+
// console.log("Rename");
142+
return { originalName: originalName.sourceString, newName: newName.sourceString };
143+
}
144+
}).addOperation<Array<ClassRename> | undefined>("renames_maybe()", {
145+
OtherExport(otherExportNode) {
146+
// console.log("OtherExport");
147+
if (otherExportNode.ctorName == "OtherExport_export_rename") {
148+
return otherExportNode.renames();
149+
} else {
150+
return undefined;
151+
}
152+
},
153+
Import(_0, _1, _2, _3, _4, _5, _6) {
154+
// console.log("Import");
155+
return undefined;
156+
},
157+
ClassDecl(_0, _1, _2, _3, _4, _5, _6) {
158+
// console.log("ClassDecl");
159+
return undefined
160+
}
161+
}).addOperation<Array<ClassRename>>("renames()", {
162+
TopLevel(topLevelNodes) {
163+
// console.log("TopLevel");
164+
let renames: Array<ClassRename> = [];
165+
for (const node of topLevelNodes.children) {
166+
const rename: Array<ClassRename> | undefined = node.renames_maybe();
167+
if (rename) {
168+
renames = renames.concat(rename);
169+
}
170+
}
171+
return renames;
172+
},
173+
OtherExport_export_rename(_export, _braceOpen, renamesList, _braceClose, _semicolon) {
174+
// console.log("OtherExport_export_rename")
175+
let renames: Array<ClassRename> = [];
176+
for (const renameNode of renamesList.asIteration().children) {
177+
const rename: ClassRename = renameNode.rename();
178+
renames.push(rename);
179+
}
180+
return renames
181+
},
137182
});
138183

139184

@@ -143,6 +188,8 @@ let cslClassesMap: Map<string, MethodInfo[]> = new Map(cslClasses.map((cls) => [
143188
console.log("Traversing parse tree of CDL type definitions...");
144189
const cdlClasses: Array<ClassInfo> = semantics(cdlMatch).classes();
145190
let cdlClassesMap: Map<string, MethodInfo[]> = new Map(cdlClasses.map((cls) => [cls.name, cls.methods]))
191+
console.log("Extracting renaming exports from CDL type definitions...");
192+
const classRenames: Array<ClassRename> = semantics(cdlMatch).renames();
146193

147194
// We trace all the classes as parsed
148195
if (traceClassInfos) {
@@ -153,7 +200,17 @@ if (traceClassInfos) {
153200
}
154201
}
155202
}
156-
// First we filter out the ignored classes from clsClassesMap based on the classInfo file.
203+
204+
// Before filtering, we rename all classes that are re-exported with a different name
205+
for(const rename of classRenames) {
206+
const clsValue = cdlClassesMap.get(rename.originalName)
207+
if(clsValue) {
208+
cdlClassesMap.delete(rename.originalName);
209+
cdlClassesMap.set(rename.newName, clsValue);
210+
}
211+
}
212+
213+
// We filter out the ignored classes from clsClassesMap based on the classInfo file.
157214
// We don't want to fail when checking methods if cdlClassesMap does not contain these classes.
158215
const classInfo: { ignore: Array<string> } = JSON.parse(fs.readFileSync("csl-types/class-info.json", "utf-8"));
159216
for (const cls of classInfo.ignore) {

‎tests/api/grammar.ohm

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ TypeDefs {
33
// Imports
44
Import =
55
"import" "{" ListOf<wordlike, ","> "}" "from" path ";"?
6-
// Other exports (we don't care about these)
6+
// Rename exports
7+
// Other exports (we don't generally care about these)
78
OtherExport =
8-
"export" Block ";"? -- export
9+
"export" "{" ListOf<Rename, ","> "}" ";"? -- export_rename
910
| "export" "declare"? "enum" identifier Block -- export_enum
1011
| "export" "declare"? "function" identifier Parens (":" wordlike )? ";"? -- export_function
1112
| "export" "type" identifier "=" ListOf<Block, "|"> ";"? -- export_type
13+
Rename = identifier "as" identifier
1214
ClassDecl = "export" "declare"? "class" identifier "{" (MethodDecl | DataDecl)* "}"
1315
// Methods
1416
MethodDecl =

‎tests/api/grammar.ohm-bundle.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import {
1414
export interface TypeDefsActionDict<T> extends BaseActionDict<T> {
1515
TopLevel?: (this: NonterminalNode, arg0: IterationNode) => T;
1616
Import?: (this: NonterminalNode, arg0: TerminalNode, arg1: TerminalNode, arg2: NonterminalNode, arg3: TerminalNode, arg4: TerminalNode, arg5: NonterminalNode, arg6: IterationNode) => T;
17-
OtherExport_export?: (this: NonterminalNode, arg0: TerminalNode, arg1: NonterminalNode, arg2: IterationNode) => T;
17+
OtherExport_export_rename?: (this: NonterminalNode, arg0: TerminalNode, arg1: TerminalNode, arg2: NonterminalNode, arg3: TerminalNode, arg4: IterationNode) => T;
1818
OtherExport_export_enum?: (this: NonterminalNode, arg0: TerminalNode, arg1: IterationNode, arg2: TerminalNode, arg3: NonterminalNode, arg4: NonterminalNode) => T;
1919
OtherExport_export_function?: (this: NonterminalNode, arg0: TerminalNode, arg1: IterationNode, arg2: TerminalNode, arg3: NonterminalNode, arg4: NonterminalNode, arg5: IterationNode, arg6: IterationNode, arg7: IterationNode) => T;
2020
OtherExport_export_type?: (this: NonterminalNode, arg0: TerminalNode, arg1: TerminalNode, arg2: NonterminalNode, arg3: TerminalNode, arg4: NonterminalNode, arg5: IterationNode) => T;
2121
OtherExport?: (this: NonterminalNode, arg0: NonterminalNode) => T;
22+
Rename?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode, arg2: NonterminalNode) => T;
2223
ClassDecl?: (this: NonterminalNode, arg0: TerminalNode, arg1: IterationNode, arg2: TerminalNode, arg3: NonterminalNode, arg4: TerminalNode, arg5: IterationNode, arg6: TerminalNode) => T;
2324
MethodDecl_static?: (this: NonterminalNode, arg0: TerminalNode, arg1: NonterminalNode) => T;
2425
MethodDecl_instance?: (this: NonterminalNode, arg0: NonterminalNode) => T;

0 commit comments

Comments
 (0)
Please sign in to comment.