From ed61d2d803a99e8891fa306face099a6a4290b29 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 6 Sep 2017 21:58:04 -0700 Subject: [PATCH] Emit updated export declarations when transformed from export * (#18017) * Failing test for missing transform output * dont elide all export stars * Remove comment from test * Refuse to perform ellision on transformed nodes --- src/compiler/transformers/ts.ts | 20 +++++++- src/harness/unittests/transform.ts | 49 +++++++++++++++---- ...sformsCorrectly.transformAwayExportStar.js | 1 + 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 tests/baselines/reference/transformApi/transformsCorrectly.transformAwayExportStar.js diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 692c758bc7..42c25ca34a 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -208,6 +208,24 @@ namespace ts { * @param node The node to visit. */ function sourceElementVisitorWorker(node: Node): VisitResult { + switch (node.kind) { + case SyntaxKind.ImportDeclaration: + case SyntaxKind.ImportEqualsDeclaration: + case SyntaxKind.ExportAssignment: + case SyntaxKind.ExportDeclaration: + return visitEllidableStatement(node); + default: + return visitorWorker(node); + } + } + + function visitEllidableStatement(node: ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration): VisitResult { + const parsed = getParseTreeNode(node); + if (parsed !== node) { + // If the node has been transformed by a `before` transformer, perform no ellision on it + // As the type information we would attempt to lookup to perform ellision is potentially unavailable for the synthesized nodes + return node; + } switch (node.kind) { case SyntaxKind.ImportDeclaration: return visitImportDeclaration(node); @@ -218,7 +236,7 @@ namespace ts { case SyntaxKind.ExportDeclaration: return visitExportDeclaration(node); default: - return visitorWorker(node); + Debug.fail("Unhandled ellided statement"); } } diff --git a/src/harness/unittests/transform.ts b/src/harness/unittests/transform.ts index 27e41a96df..bcdca3e3b6 100644 --- a/src/harness/unittests/transform.ts +++ b/src/harness/unittests/transform.ts @@ -57,7 +57,7 @@ namespace ts { testBaseline("types", () => { return transformSourceFile(`let a: () => void`, [ - context => file => visitNode(file, function visitor(node: Node): VisitResult { + context => file => visitNode(file, function visitor(node: Node): VisitResult { return visitEachChild(node, visitor, context); }) ]); @@ -91,14 +91,14 @@ namespace ts { class C { foo = 10; static bar = 20 } namespace C { export let x = 10; } `, { - transformers: { - before: [forceNamespaceRewrite], - }, - compilerOptions: { - target: ts.ScriptTarget.ESNext, - newLine: NewLineKind.CarriageReturnLineFeed, - } - }).outputText; + transformers: { + before: [forceNamespaceRewrite], + }, + compilerOptions: { + target: ts.ScriptTarget.ESNext, + newLine: NewLineKind.CarriageReturnLineFeed, + } + }).outputText; }); testBaseline("synthesizedClassAndNamespaceCombination", () => { @@ -138,6 +138,37 @@ namespace ts { } }; } + + testBaseline("transformAwayExportStar", () => { + return ts.transpileModule("export * from './helper';", { + transformers: { + before: [expandExportStar], + }, + compilerOptions: { + target: ts.ScriptTarget.ESNext, + newLine: NewLineKind.CarriageReturnLineFeed, + } + }).outputText; + + function expandExportStar(context: ts.TransformationContext) { + return (sourceFile: ts.SourceFile): ts.SourceFile => { + return visitNode(sourceFile); + + function visitNode(node: T): T { + if (node.kind === ts.SyntaxKind.ExportDeclaration) { + const ed = node as ts.Node as ts.ExportDeclaration; + const exports = [{ name: "x" }]; + const exportSpecifiers = exports.map(e => ts.createExportSpecifier(e.name, e.name)); + const exportClause = ts.createNamedExports(exportSpecifiers); + const newEd = ts.updateExportDeclaration(ed, ed.decorators, ed.modifiers, exportClause, ed.moduleSpecifier); + + return newEd as ts.Node as T; + } + return ts.visitEachChild(node, visitNode, context); + } + }; + } + }); }); } diff --git a/tests/baselines/reference/transformApi/transformsCorrectly.transformAwayExportStar.js b/tests/baselines/reference/transformApi/transformsCorrectly.transformAwayExportStar.js new file mode 100644 index 0000000000..7a05a90c1f --- /dev/null +++ b/tests/baselines/reference/transformApi/transformsCorrectly.transformAwayExportStar.js @@ -0,0 +1 @@ +export { x as x } from './helper';