Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash when transforming functions in modules. #34513

Merged
merged 1 commit into from Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/compiler/transformers/ts.ts
Expand Up @@ -2452,7 +2452,12 @@ namespace ts {
*
* @param node The module declaration node.
*/
function shouldEmitModuleDeclaration(node: ModuleDeclaration) {
function shouldEmitModuleDeclaration(nodeIn: ModuleDeclaration) {
const node = getParseTreeNode(nodeIn, isModuleDeclaration);
if (!node) {
// If we can't find a parse tree node, assume the node is instantiated.
return true;
}
return isInstantiatedModule(node, !!compilerOptions.preserveConstEnums || !!compilerOptions.isolatedModules);
}

Expand Down
29 changes: 29 additions & 0 deletions src/testRunner/unittests/transform.ts
Expand Up @@ -447,6 +447,35 @@ namespace Foo {
}).outputText;
});

testBaseline("transformUpdateModuleMember", () => {
return transpileModule(`
module MyModule {
const myVariable = 1;
function foo(param: string) {}
}
`, {
transformers: {
before: [renameVariable],
},
compilerOptions: {
target: ScriptTarget.ES2015,
newLine: NewLineKind.CarriageReturnLineFeed,
}
}).outputText;

function renameVariable(context: TransformationContext) {
return (sourceFile: SourceFile): SourceFile => {
return visitNode(sourceFile, rootTransform, isSourceFile);
};
function rootTransform<T extends Node>(node: T): Node {
if (isVariableDeclaration(node)) {
return updateVariableDeclaration(node, createIdentifier("newName"), /* type */ undefined, node.initializer);
}
return visitEachChild(node, rootTransform, context);
}
}
});

// https://github.com/Microsoft/TypeScript/issues/24709
testBaseline("issue24709", () => {
const fs = vfs.createFromFileSystem(Harness.IO, /*caseSensitive*/ true);
Expand Down
@@ -0,0 +1,5 @@
var MyModule;
(function (MyModule) {
const newName = 1;
function foo(param) { }
})(MyModule || (MyModule = {}));