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
This commit is contained in:
Wesley Wigham 2017-09-06 21:58:04 -07:00 committed by GitHub
parent 5c779b1edb
commit ed61d2d803
3 changed files with 60 additions and 10 deletions

View file

@ -208,6 +208,24 @@ namespace ts {
* @param node The node to visit.
*/
function sourceElementVisitorWorker(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.ExportAssignment:
case SyntaxKind.ExportDeclaration:
return visitEllidableStatement(<ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration>node);
default:
return visitorWorker(node);
}
}
function visitEllidableStatement(node: ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration): VisitResult<Node> {
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(<ImportDeclaration>node);
@ -218,7 +236,7 @@ namespace ts {
case SyntaxKind.ExportDeclaration:
return visitExportDeclaration(<ExportDeclaration>node);
default:
return visitorWorker(node);
Debug.fail("Unhandled ellided statement");
}
}

View file

@ -57,7 +57,7 @@ namespace ts {
testBaseline("types", () => {
return transformSourceFile(`let a: () => void`, [
context => file => visitNode(file, function visitor(node: Node): VisitResult<Node> {
context => file => visitNode(file, function visitor(node: Node): VisitResult<Node> {
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<T extends ts.Node>(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);
}
};
}
});
});
}

View file

@ -0,0 +1 @@
export { x as x } from './helper';