Skip to content

Transforming ImportDeclaration or ExportDeclaration causes type specifiers to be output in js files #40603

@nonara

Description

@nonara

Type imports/exports which are regularly excluded in compiled JS output get included when updateImportDeclaration / updateExportDeclaration are called during transform.

TypeScript Version: 3+

Related 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";

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions