Transforming ImportDeclaration or ExportDeclaration causes type specifiers to be output in js files
See original GitHub issueType imports/exports which are regularly excluded in compiled JS output get included when updateImportDeclaration
/ updateExportDeclaration
are called during transform.
TypeScript Version: 3+
Related Issues: https://github.com/microsoft/TypeScript/issues/31446 (this is the same issue, but it was closed prematurely by the OP)
The aforementioned issue details the problem well. Unfortunately, the OP closed it before it could be investigated because of finding a workaround. The workaround, however, was an internal method (ts.updateNode
) which has been removed in TS 4.0+.
Playground Link: Reproduction: https://repl.it/@nonara/ts-bug#index.ts
Search Terms: updateImportDeclaration, createImportDeclaration
Code:
a.ts
export type A = string;
index.ts
import { A } from './a'
export { A } from './a'
transformer.ts
function transformer(ctx: ts.TransformationContext) {
const { factory } = ctx;
return (s: ts.SourceFile) => ts.visitEachChild(s, visitor, ctx);
function visitor(node: ts.Node): ts.Node {
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier))
return factory.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
node.importClause,
ts.createStringLiteral(node.moduleSpecifier.text)
);
if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier))
return factory.updateExportDeclaration(
node,
node.decorators,
node.modifiers,
node.isTypeOnly,
node.exportClause,
ts.createStringLiteral(node.moduleSpecifier.text)
);
return node;
}
}
Expected Behaviour: Should match output without transformer…
index.js
export {};
Actual Behaviour: (Output with transformer)
index.js
import { A } from "./a";
export { A } from "./a";
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:13 (5 by maintainers)
Top GitHub Comments
It fixes
But it hack?
Also happening for ts-transform-paths.
EDIT: solved in 2.0.2 by using an
after
transform instead ofbefore