UNPKG

5.15 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.addOriginalCreateProgramTransformer = exports.createProgramExportFiles = void 0;
7const typescript_1 = __importDefault(require("typescript"));
8const system_1 = require("../../system");
9/* ****************************************************************************************************************** */
10// region: Config
11/* ****************************************************************************************************************** */
12exports.createProgramExportFiles = [
13 /* TS < 5.4 */
14 'src/typescript/_namespaces/ts.ts',
15 /* TS >= 5.4 */
16 'src/server/_namespaces/ts.ts',
17 'src/typescript/typescript.ts'
18];
19// endregion
20/* ****************************************************************************************************************** */
21// region: Utils
22/* ****************************************************************************************************************** */
23function addOriginalCreateProgramTransformer(context) {
24 const { factory } = context;
25 let patchSuccess = false;
26 return (sourceFile) => {
27 if (!exports.createProgramExportFiles.includes(sourceFile.fileName))
28 throw new Error('Wrong emitter file sent to transformer! This should be unreachable.');
29 const res = factory.updateSourceFile(sourceFile, typescript_1.default.visitNodes(sourceFile.statements, visitNodes));
30 if (!patchSuccess)
31 throw new system_1.PatchError('Failed to patch typescript originalCreateProgram!');
32 return res;
33 function visitNodes(node) {
34 /* Handle: __export({ ... }) */
35 if (typescript_1.default.isExpressionStatement(node) &&
36 typescript_1.default.isCallExpression(node.expression) &&
37 node.expression.expression.getText() === '__export') {
38 const exportObjectLiteral = node.expression.arguments[1];
39 if (typescript_1.default.isObjectLiteralExpression(exportObjectLiteral)) {
40 const originalCreateProgramProperty = factory.createPropertyAssignment('originalCreateProgram', factory.createArrowFunction(undefined, undefined, [], undefined, factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), factory.createIdentifier('originalCreateProgram')));
41 const updatedExportObjectLiteral = factory.updateObjectLiteralExpression(exportObjectLiteral, [...exportObjectLiteral.properties, originalCreateProgramProperty]);
42 const updatedNode = factory.updateExpressionStatement(node, factory.updateCallExpression(node.expression, node.expression.expression, undefined, [node.expression.arguments[0], updatedExportObjectLiteral]));
43 patchSuccess = true;
44 return updatedNode;
45 }
46 }
47 /* Handle: 1 && (module.exports = { ... }) (ts5.5+) */
48 if (typescript_1.default.isExpressionStatement(node) && typescript_1.default.isBinaryExpression(node.expression) &&
49 node.expression.operatorToken.kind === typescript_1.default.SyntaxKind.AmpersandAmpersandToken &&
50 typescript_1.default.isParenthesizedExpression(node.expression.right) &&
51 typescript_1.default.isBinaryExpression(node.expression.right.expression) &&
52 node.expression.right.expression.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken &&
53 typescript_1.default.isPropertyAccessExpression(node.expression.right.expression.left) &&
54 node.expression.right.expression.left.expression.getText() === 'module' &&
55 node.expression.right.expression.left.name.getText() === 'exports' &&
56 typescript_1.default.isObjectLiteralExpression(node.expression.right.expression.right)) {
57 // Add originalCreateProgram to the object literal
58 const originalCreateProgramProperty = factory.createShorthandPropertyAssignment('originalCreateProgram');
59 const updatedObjectLiteral = factory.updateObjectLiteralExpression(node.expression.right.expression.right, [...node.expression.right.expression.right.properties, originalCreateProgramProperty]);
60 // Update the node
61 const updatedNode = factory.updateExpressionStatement(node, factory.updateBinaryExpression(node.expression, node.expression.left, node.expression.operatorToken, factory.updateParenthesizedExpression(node.expression.right, factory.updateBinaryExpression(node.expression.right.expression, node.expression.right.expression.left, node.expression.right.expression.operatorToken, updatedObjectLiteral))));
62 patchSuccess = true;
63 return updatedNode;
64 }
65 return node;
66 }
67 };
68}
69exports.addOriginalCreateProgramTransformer = addOriginalCreateProgramTransformer;
70// endregion
71//# sourceMappingURL=add-original-create-program.js.map
\No newline at end of file