1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.addOriginalCreateProgramTransformer = exports.createProgramExportFiles = void 0;
|
7 | const typescript_1 = __importDefault(require("typescript"));
|
8 | const system_1 = require("../../system");
|
9 |
|
10 |
|
11 |
|
12 | exports.createProgramExportFiles = [
|
13 |
|
14 | 'src/typescript/_namespaces/ts.ts',
|
15 |
|
16 | 'src/server/_namespaces/ts.ts',
|
17 | 'src/typescript/typescript.ts'
|
18 | ];
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function 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 |
|
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 |
|
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 |
|
58 | const originalCreateProgramProperty = factory.createShorthandPropertyAssignment('originalCreateProgram');
|
59 | const updatedObjectLiteral = factory.updateObjectLiteralExpression(node.expression.right.expression.right, [...node.expression.right.expression.right.properties, originalCreateProgramProperty]);
|
60 |
|
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 | }
|
69 | exports.addOriginalCreateProgramTransformer = addOriginalCreateProgramTransformer;
|
70 |
|
71 |
|
\ | No newline at end of file |