Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swc reuses _variable name when importing different modules with same filename in CommonJS #1787

Closed
Tracked by #2228
cspotcode opened this issue Jun 4, 2021 · 4 comments · Fixed by #1830
Closed
Tracked by #2228
Assignees
Labels
Milestone

Comments

@cspotcode
Copy link
Contributor

Describe the bug

When a single file imports multiple different files with the same filenames, and swc uses interopRequireDefault, it reuses the same local variable to store both, when it should use a different local variable.

For example when a single file imports ./foo/bar and ./baz/bar, they are both named bar so the local variable used for both is _bar.

Input code

I can reproduce using the JS API:

const swc = require('@swc/wasm');
swc.transformSync(`
  import A from "./foo/a";
  import B from "./bar/a"
`, {
  module: {type: 'commonjs'}
})

The output is:

"use strict";
var _a = _interopRequireDefault(require("./foo/a"));
var _a = _interopRequireDefault(require("./bar/a"));
function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}

Config

N/A

Expected behavior

Since the 2 imported modules are different, the local variable should be different as well. tsc emits code like this:

const a_1 = __importDefault(require("./foo/a"));
const a_2 = __importDefault(require("./bar/a"));

Version
The version of @swc/wasm: 1.2.58

Additional context
Add any other context about the problem here.

@cspotcode
Copy link
Contributor Author

Does swc today have a mechanism to request unused variable names in top-level scope? Can this be accomplished with the hygiene pass? It looks like hygiene is concerned with finding collisions between identifiers in the input code, whereas imports are stored on hidden variables that do not appear anywhere in the input source.

@kdy1
Copy link
Member

kdy1 commented Jun 17, 2021

You can use private_name! macro to create a such identifier.
hygiene pass will convert it to actual unused identifier at the end.

@cspotcode
Copy link
Contributor Author

I see, thanks. It looks like the identifiers are being created today with local_name_for_src. Sometimes it is being wrapped in private_ident!; sometimes not. I'll try adding the missing private_ident! calls and see if that fixes it.

@cspotcode cspotcode mentioned this issue Jun 17, 2021
@kdy1 kdy1 added this to the v1.2.62 milestone Jun 18, 2021
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 23, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants