|
| 1 | +import { parse, ParserOptions } from "@babel/parser"; |
| 2 | +import generate from "@babel/generator"; |
| 3 | +import { |
| 4 | + ExpressionStatement, |
| 5 | + FunctionDeclaration, |
| 6 | + is, |
| 7 | + Node, |
| 8 | + VariableDeclaration, |
| 9 | +} from "@babel/types"; |
| 10 | + |
| 11 | +export { generate }; |
| 12 | + |
| 13 | +export class Tower<T extends Node> { |
| 14 | + public ast: Node; |
| 15 | + constructor(stringOrAST: string | T, options?: Partial<ParserOptions>) { |
| 16 | + if (typeof stringOrAST === "string") { |
| 17 | + const parsedThing = parse(stringOrAST, { |
| 18 | + sourceType: "module", |
| 19 | + ...options, |
| 20 | + }); |
| 21 | + this.ast = parsedThing.program; |
| 22 | + } else { |
| 23 | + this.ast = stringOrAST; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // Get all the given types at the current scope |
| 28 | + private getType<T extends Node>(type: string, name: string): Tower<T> { |
| 29 | + const body = this.extractBody(this.ast); |
| 30 | + const ast = body.find((node) => { |
| 31 | + if (node.type === type) { |
| 32 | + if (is("FunctionDeclaration", node)) { |
| 33 | + return node.id?.name === name; |
| 34 | + } |
| 35 | + |
| 36 | + if (is("VariableDeclaration", node)) { |
| 37 | + const variableDeclarator = node.declarations[0]; |
| 38 | + if (!is("VariableDeclarator", variableDeclarator)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + const identifier = variableDeclarator.id; |
| 43 | + if (!is("Identifier", identifier)) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + return identifier.name === name; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return false; |
| 52 | + }); |
| 53 | + if (!ast) { |
| 54 | + throw new Error(`No AST found with name ${name}`); |
| 55 | + } |
| 56 | + |
| 57 | + assertIsType<T>(ast); |
| 58 | + return new Tower<T>(ast); |
| 59 | + } |
| 60 | + |
| 61 | + public getFunction(name: string): Tower<FunctionDeclaration> { |
| 62 | + return this.getType("FunctionDeclaration", name); |
| 63 | + } |
| 64 | + |
| 65 | + public getVariable(name: string): Tower<VariableDeclaration> { |
| 66 | + return this.getType("VariableDeclaration", name); |
| 67 | + } |
| 68 | + |
| 69 | + public getCalls(callSite: string): Array<Tower<ExpressionStatement>> { |
| 70 | + const body = this.extractBody(this.ast); |
| 71 | + const calls = body.filter((node) => { |
| 72 | + if (is("ExpressionStatement", node)) { |
| 73 | + const expression = node.expression; |
| 74 | + if (is("CallExpression", expression)) { |
| 75 | + const callee = expression.callee; |
| 76 | + |
| 77 | + switch (callee.type) { |
| 78 | + case "Identifier": |
| 79 | + return callee.name === callSite; |
| 80 | + case "MemberExpression": |
| 81 | + return generate(callee).code === callSite; |
| 82 | + default: |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (is("VariableDeclarator", node)) { |
| 89 | + const init = node.init; |
| 90 | + if (is("CallExpression", init)) { |
| 91 | + const callee = init.callee; |
| 92 | + |
| 93 | + switch (callee.type) { |
| 94 | + case "Identifier": |
| 95 | + return callee.name === callSite; |
| 96 | + case "MemberExpression": |
| 97 | + return generate(callee).code === callSite; |
| 98 | + default: |
| 99 | + return true; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | + }); |
| 106 | + assertIsType<ExpressionStatement[]>(calls); |
| 107 | + return calls.map((call) => new Tower<ExpressionStatement>(call)); |
| 108 | + } |
| 109 | + |
| 110 | + private extractBody(ast: Node): Node[] { |
| 111 | + switch (ast.type) { |
| 112 | + case "Program": |
| 113 | + return ast.body; |
| 114 | + case "FunctionDeclaration": |
| 115 | + return ast.body.body; |
| 116 | + case "VariableDeclaration": |
| 117 | + return ast.declarations; |
| 118 | + case "ArrowFunctionExpression": |
| 119 | + // eslint-disable-next-line no-case-declarations |
| 120 | + const blockStatement = ast.body; |
| 121 | + if (is("BlockStatement", blockStatement)) { |
| 122 | + return blockStatement.body; |
| 123 | + } |
| 124 | + |
| 125 | + throw new Error(`Unimplemented for ${ast.type}`); |
| 126 | + default: |
| 127 | + throw new Error(`Unimplemented for ${ast.type}`); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public get generate(): string { |
| 132 | + return generate(this.ast).code; |
| 133 | + } |
| 134 | + |
| 135 | + public get compact(): string { |
| 136 | + return generate(this.ast, { compact: true }).code; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +function assertIsType<T extends Node | Node[]>( |
| 141 | + ast: Node | Node[], |
| 142 | +): asserts ast is T {} |
0 commit comments