question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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: 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
uasancommented, Feb 14, 2021

It fixes

node.moduleSpecifier = context.factory.createStringLiteral(newPath);

sourceFile.resolvedModules.set(
  newPath,
  sourceFile.resolvedModules.get(oldPath)
);

But it hack?

1reaction
squidfunkcommented, Jan 11, 2021

Also happening for ts-transform-paths.

EDIT: solved in 2.0.2 by using an after transform instead of before

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
Write Code to Rewrite Your Code: jscodeshift - Toptal
Now run this command to run the codemod. jscodeshift -t ./deprecated.js ./deprecated.input.js -d -p. You should see output indicating the transform ...
Read more >
Eslint rule to put a new line inside import - Stack Overflow
I was looking for such a rule for both import and export declaration. As a result I've made a plugin with autofix. So...
Read more >
@babel/plugin-transform-block-scoped-functions | Yarn - Package ...
@babel/plugin-transform-block-scoped-functions ... The compiler for writing next generation JavaScript. Gitpod ready-to-code.
Read more >
Documentation - TypeScript 3.8
TypeScript 3.8 adds a new syntax for type-only imports and exports. ... TypeScript 3.8 supports JavaScript files by turning on the allowJs flag,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found