Debian Codegraph @

cli/tools-cli/src/scripts/transformer.ts

raw symbols SCIP
1 import {
2 factory,
3 isVariableStatement,
4 Node,
5 SyntaxKind,
6 TransformerFactory,
7 VariableStatement,
8 visitEachChild,
9 visitNode,
10 } from "typescript";
11
12 // The name of the spec variable
13 const SPEC_NAME = "completionSpec";
14 const EXPORT_KEYWORD = SyntaxKind.ExportKeyword;
15
16 /**
17 * The TypeScript transformer to rename the export spec variable name to SPEC_NAME,
18 * and remove the export keyword.
19 *
20 * @param context The current context
21 */
22 export const specTransformer: TransformerFactory<Node> = (context) => (sourceFile) => {
23 let updated = false;
24 const visitor = (node: Node): Node => {
25 if (!updated && isVariableStatement(node)) {
26 const { declarationList, modifiers } = node as VariableStatement;
27 // Only process if there is a modifier, and if this modifier
28 // is an export.
29 if (modifiers && modifiers.length === 1 && modifiers[0].kind === EXPORT_KEYWORD) {
30 const variableNode = declarationList.declarations[0];
31
32 updated = true;
33
34 // Update the variable name to SPEC_NAME
35 const newVariableNode = factory.updateVariableDeclaration(
36 variableNode,
37 factory.createIdentifier(SPEC_NAME),
38 variableNode.exclamationToken,
39 variableNode.type,
40 variableNode.initializer
41 );
42 const newDeclarationlist = factory.updateVariableDeclarationList(declarationList, [
43 newVariableNode,
44 ]);
45
46 // Remove the export keyword
47 return factory.updateVariableStatement(node, [], newDeclarationlist);
48 }
49 }
50 return visitEachChild(node, visitor, context);
51 };
52 return visitNode(sourceFile, visitor);
53 };