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:
parent
5c779b1edb
commit
ed61d2d803
3 changed files with 60 additions and 10 deletions
|
@ -208,6 +208,24 @@ namespace ts {
|
||||||
* @param node The node to visit.
|
* @param node The node to visit.
|
||||||
*/
|
*/
|
||||||
function sourceElementVisitorWorker(node: Node): VisitResult<Node> {
|
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) {
|
switch (node.kind) {
|
||||||
case SyntaxKind.ImportDeclaration:
|
case SyntaxKind.ImportDeclaration:
|
||||||
return visitImportDeclaration(<ImportDeclaration>node);
|
return visitImportDeclaration(<ImportDeclaration>node);
|
||||||
|
@ -218,7 +236,7 @@ namespace ts {
|
||||||
case SyntaxKind.ExportDeclaration:
|
case SyntaxKind.ExportDeclaration:
|
||||||
return visitExportDeclaration(<ExportDeclaration>node);
|
return visitExportDeclaration(<ExportDeclaration>node);
|
||||||
default:
|
default:
|
||||||
return visitorWorker(node);
|
Debug.fail("Unhandled ellided statement");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ namespace ts {
|
||||||
|
|
||||||
testBaseline("types", () => {
|
testBaseline("types", () => {
|
||||||
return transformSourceFile(`let a: () => void`, [
|
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);
|
return visitEachChild(node, visitor, context);
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
@ -91,14 +91,14 @@ namespace ts {
|
||||||
class C { foo = 10; static bar = 20 }
|
class C { foo = 10; static bar = 20 }
|
||||||
namespace C { export let x = 10; }
|
namespace C { export let x = 10; }
|
||||||
`, {
|
`, {
|
||||||
transformers: {
|
transformers: {
|
||||||
before: [forceNamespaceRewrite],
|
before: [forceNamespaceRewrite],
|
||||||
},
|
},
|
||||||
compilerOptions: {
|
compilerOptions: {
|
||||||
target: ts.ScriptTarget.ESNext,
|
target: ts.ScriptTarget.ESNext,
|
||||||
newLine: NewLineKind.CarriageReturnLineFeed,
|
newLine: NewLineKind.CarriageReturnLineFeed,
|
||||||
}
|
}
|
||||||
}).outputText;
|
}).outputText;
|
||||||
});
|
});
|
||||||
|
|
||||||
testBaseline("synthesizedClassAndNamespaceCombination", () => {
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
export { x as x } from './helper';
|
Loading…
Reference in a new issue