Skip to content

Handle synthetic nodes correctly as namespace identifiers in system transform #19623

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

Merged
merged 5 commits into from
Nov 9, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4318,7 +4318,7 @@ namespace ts {
const namespaceDeclaration = getNamespaceDeclarationNode(node);
if (namespaceDeclaration && !isDefaultImport(node)) {
const name = namespaceDeclaration.name;
return isGeneratedIdentifier(name) ? name : createIdentifier(getSourceTextOfNodeFromSourceFile(sourceFile, namespaceDeclaration.name));
return isGeneratedIdentifier(name) ? name : createIdentifier(getSourceTextOfNodeFromSourceFile(sourceFile, name) || idText(name));
}
if (node.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node).importClause) {
return getGeneratedNameForNode(node);
Expand Down
32 changes: 32 additions & 0 deletions src/harness/unittests/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,38 @@ namespace ts {
};
}
});

// https://github.com/Microsoft/TypeScript/issues/19618
testBaseline("transformAddImportStar", () => {
return ts.transpileModule("", {
transformers: {
before: [transformAddImportStar],
},
compilerOptions: {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.System,
newLine: NewLineKind.CarriageReturnLineFeed,
}
}).outputText;

function transformAddImportStar(_context: ts.TransformationContext) {
return (sourceFile: ts.SourceFile): ts.SourceFile => {
return visitNode(sourceFile);
};
function visitNode(sf: ts.SourceFile) {
// produce `import * as i0 from './comp';
const importStar = ts.createImportDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*importClause*/ ts.createImportClause(
/*name*/ undefined,
ts.createNamespaceImport(ts.createIdentifier("i0"))
),
/*moduleSpecifier*/ ts.createLiteral("./comp1"));
return ts.updateSourceFileNode(sf, [importStar]);
}
}
});
});
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
System.register(["./comp1"], function (exports_1, context_1) {
var __moduleName = context_1 && context_1.id;
var i0;
return {
setters: [
function (i0_1) {
i0 = i0_1;
}
],
execute: function () {
}
};
});