Skip to content
This repository was archived by the owner on Mar 20, 2020. It is now read-only.

Commit ed61d2d

Browse files
authored
Emit updated export declarations when transformed from export * (microsoft#18017)
* Failing test for missing transform output * dont elide all export stars * Remove comment from test * Refuse to perform ellision on transformed nodes
1 parent 5c779b1 commit ed61d2d

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

src/compiler/transformers/ts.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ namespace ts {
208208
* @param node The node to visit.
209209
*/
210210
function sourceElementVisitorWorker(node: Node): VisitResult<Node> {
211+
switch (node.kind) {
212+
case SyntaxKind.ImportDeclaration:
213+
case SyntaxKind.ImportEqualsDeclaration:
214+
case SyntaxKind.ExportAssignment:
215+
case SyntaxKind.ExportDeclaration:
216+
return visitEllidableStatement(<ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration>node);
217+
default:
218+
return visitorWorker(node);
219+
}
220+
}
221+
222+
function visitEllidableStatement(node: ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration): VisitResult<Node> {
223+
const parsed = getParseTreeNode(node);
224+
if (parsed !== node) {
225+
// If the node has been transformed by a `before` transformer, perform no ellision on it
226+
// As the type information we would attempt to lookup to perform ellision is potentially unavailable for the synthesized nodes
227+
return node;
228+
}
211229
switch (node.kind) {
212230
case SyntaxKind.ImportDeclaration:
213231
return visitImportDeclaration(<ImportDeclaration>node);
@@ -218,7 +236,7 @@ namespace ts {
218236
case SyntaxKind.ExportDeclaration:
219237
return visitExportDeclaration(<ExportDeclaration>node);
220238
default:
221-
return visitorWorker(node);
239+
Debug.fail("Unhandled ellided statement");
222240
}
223241
}
224242

src/harness/unittests/transform.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace ts {
5757

5858
testBaseline("types", () => {
5959
return transformSourceFile(`let a: () => void`, [
60-
context => file => visitNode(file, function visitor(node: Node): VisitResult<Node> {
60+
context => file => visitNode(file, function visitor(node: Node): VisitResult<Node> {
6161
return visitEachChild(node, visitor, context);
6262
})
6363
]);
@@ -91,14 +91,14 @@ namespace ts {
9191
class C { foo = 10; static bar = 20 }
9292
namespace C { export let x = 10; }
9393
`, {
94-
transformers: {
95-
before: [forceNamespaceRewrite],
96-
},
97-
compilerOptions: {
98-
target: ts.ScriptTarget.ESNext,
99-
newLine: NewLineKind.CarriageReturnLineFeed,
100-
}
101-
}).outputText;
94+
transformers: {
95+
before: [forceNamespaceRewrite],
96+
},
97+
compilerOptions: {
98+
target: ts.ScriptTarget.ESNext,
99+
newLine: NewLineKind.CarriageReturnLineFeed,
100+
}
101+
}).outputText;
102102
});
103103

104104
testBaseline("synthesizedClassAndNamespaceCombination", () => {
@@ -138,6 +138,37 @@ namespace ts {
138138
}
139139
};
140140
}
141+
142+
testBaseline("transformAwayExportStar", () => {
143+
return ts.transpileModule("export * from './helper';", {
144+
transformers: {
145+
before: [expandExportStar],
146+
},
147+
compilerOptions: {
148+
target: ts.ScriptTarget.ESNext,
149+
newLine: NewLineKind.CarriageReturnLineFeed,
150+
}
151+
}).outputText;
152+
153+
function expandExportStar(context: ts.TransformationContext) {
154+
return (sourceFile: ts.SourceFile): ts.SourceFile => {
155+
return visitNode(sourceFile);
156+
157+
function visitNode<T extends ts.Node>(node: T): T {
158+
if (node.kind === ts.SyntaxKind.ExportDeclaration) {
159+
const ed = node as ts.Node as ts.ExportDeclaration;
160+
const exports = [{ name: "x" }];
161+
const exportSpecifiers = exports.map(e => ts.createExportSpecifier(e.name, e.name));
162+
const exportClause = ts.createNamedExports(exportSpecifiers);
163+
const newEd = ts.updateExportDeclaration(ed, ed.decorators, ed.modifiers, exportClause, ed.moduleSpecifier);
164+
165+
return newEd as ts.Node as T;
166+
}
167+
return ts.visitEachChild(node, visitNode, context);
168+
}
169+
};
170+
}
171+
});
141172
});
142173
}
143174

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { x as x } from './helper';

0 commit comments

Comments
 (0)