UNPKG

2.75 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.patchEmitterTransformer = void 0;
7const typescript_1 = __importDefault(require("typescript"));
8/* ****************************************************************************************************************** */
9// region: Utils
10/* ****************************************************************************************************************** */
11function patchEmitterTransformer(context) {
12 const { factory } = context;
13 let patchSuccess = false;
14 return (sourceFile) => {
15 if (sourceFile.fileName !== 'src/compiler/watch.ts')
16 throw new Error('Wrong emitter file sent to transformer! This should be unreachable.');
17 const res = factory.updateSourceFile(sourceFile, typescript_1.default.visitNodes(sourceFile.statements, visitRootNodes));
18 if (!patchSuccess)
19 throw new Error('Failed to patch emitFilesAndReportErrors function!');
20 return res;
21 function visitRootNodes(node) {
22 if (typescript_1.default.isFunctionDeclaration(node) && node.name && node.name.getText() === 'emitFilesAndReportErrors') {
23 const newBodyStatements = typescript_1.default.visitNodes(node.body.statements, visitEmitterNodes);
24 return factory.updateFunctionDeclaration(node, node.modifiers, node.asteriskToken, node.name, node.typeParameters, node.parameters, node.type, factory.updateBlock(node.body, newBodyStatements));
25 }
26 return node;
27 }
28 function visitEmitterNodes(node) {
29 if (typescript_1.default.isVariableStatement(node) &&
30 node.declarationList.declarations.some((declaration) => typescript_1.default.isVariableDeclaration(declaration) && declaration.name.getText() === 'emitResult')) {
31 // tsp.diagnosticMap.set(program, allDiagnostics);
32 const insertedMapSetterNode = factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createPropertyAccessExpression(factory.createIdentifier('tsp'), factory.createIdentifier('diagnosticMap')), factory.createIdentifier('set')), undefined, [
33 factory.createIdentifier('program'),
34 factory.createIdentifier('allDiagnostics')
35 ]));
36 patchSuccess = true;
37 return [insertedMapSetterNode, node];
38 }
39 return node;
40 }
41 };
42}
43exports.patchEmitterTransformer = patchEmitterTransformer;
44// endregion
45//# sourceMappingURL=patch-emitter.js.map
\No newline at end of file