Skip to content

Commit 5754639

Browse files
committed
Support synthesized SourceFile parent in getOrCreateEmitNode (#24709)
getOrCreateEmitNode() assumes that the SourceFile of node that is part of a parse tree will also be a parse tree node. This assumption is not valid for a transformed SourceFile. disposeEmitNodes() already handles this case by getting the original SourceFile node if the provided node is synthesized, so do the same in getOrCreateEmitNode(). This results in the test case in #24709 to run without error.
1 parent e2100cd commit 5754639

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/compiler/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2819,7 +2819,7 @@ namespace ts {
28192819
return node.emitNode = { annotatedNodes: [node] } as EmitNode;
28202820
}
28212821

2822-
const sourceFile = getSourceFileOfNode(node);
2822+
const sourceFile = getSourceFileOfNode(getParseTreeNode(getSourceFileOfNode(node)));
28232823
getOrCreateEmitNode(sourceFile).annotatedNodes!.push(node);
28242824
}
28252825

src/testRunner/unittests/transform.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,46 @@ namespace Foo {
400400
}
401401
}).outputText;
402402
});
403+
404+
// https://github.com/Microsoft/TypeScript/issues/24709
405+
testBaseline("issue24709", () => {
406+
const fs = vfs.createFromFileSystem(Harness.IO, /*caseSensitive*/ true);
407+
const transformed = transform(createSourceFile("source.ts", "class X { echo(x: string) { return x; } }", ScriptTarget.ES3), [transformSourceFile]);
408+
const transformedSourceFile = transformed.transformed[0];
409+
transformed.dispose();
410+
const host = new fakes.CompilerHost(fs);
411+
host.getSourceFile = () => transformedSourceFile;
412+
const program = createProgram(["source.ts"], {
413+
target: ScriptTarget.ES3,
414+
module: ModuleKind.None,
415+
noLib: true
416+
}, host);
417+
program.emit(transformedSourceFile, (_p, s, b) => host.writeFile("source.js", s, b));
418+
return host.readFile("source.js")!.toString();
419+
420+
function transformSourceFile(context: TransformationContext) {
421+
const visitor: Visitor = (node) => {
422+
if (isMethodDeclaration(node)) {
423+
return updateMethod(
424+
node,
425+
node.decorators,
426+
node.modifiers,
427+
node.asteriskToken,
428+
createIdentifier("foobar"),
429+
node.questionToken,
430+
node.typeParameters,
431+
node.parameters,
432+
node.type,
433+
node.body,
434+
);
435+
}
436+
return visitEachChild(node, visitor, context);
437+
};
438+
return (node: SourceFile) => visitNode(node, visitor);
439+
}
440+
441+
});
442+
403443
});
404444
}
405445

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var X = /** @class */ (function () {
2+
function X() {
3+
}
4+
X.prototype.foobar = function (x) { return x; };
5+
return X;
6+
}());

0 commit comments

Comments
 (0)