microsoft / microsoft/TypeScript

Invalid CommonJS output when transforming a single import declaration into multiple

Open
#53,115 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

I've created a TypeScript transformer that transforms a single barrel import declaration into multiple deep import declarations.

Eg changing this:

import { A, B } from 'pkg';
console.log(A, B);

Into something like this:

import { A } from 'pkg/a';
import { B } from 'pkg/b';
console.log(A, B);

This works great when emitting ES modules but not when emitting CommonJS.

🔎 Search Terms

Transform, import, esm, cjs

🕗 Version & Regression Information

Tested with 4.9.5 but I suspect that most versions have this behaviour.


⏯ Playground Link

Since Playground doesn't have support for transforms I don't have a playground link.

💻 Code

Not sure what to post here, any custom transform that replaces a single import declaration into multiple behave this way.

🙁 Actual behavior

When calling ts.setOriginalNode(newNode, node) for all new import declarations when emitted CJS code uses the same variable for all statements like this:

var pkg_1 = require('pkg/a');
var pkg_1 = require('pkg/b');
console.log(pkg_1.A, pkg_1.B);

That is, multiple new nodes having the same, single node as their original node.

When not calling ts.setOriginalNode() at all the ES output is fine but the CJS output is even more broken with something like:

var a_1 = require('pkg/a');
var b_1 = require('pkg/b');
console.log(pkg_1.A, pkg_1.B);
🙂 Expected behavior

I'm not sure how this should be handled but I would have liked to be able to connect multiple new nodes to a single original node and have the CommonJS transform see that and use different require variables. With output that looks something like this:

var pkg_1 = require('pkg/a');
var pkg_2 = require('pkg/b');
console.log(pkg_1.A, pkg_2.B);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the custom transformer that splits one import into multiple declarations, then compare CommonJS and ES module emission while tracing how ts.setOriginalNode() affects the generated require bindings. Done means the CommonJS output uses distinct require variables and updates each imported reference consistently, without breaking the ES module output.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.