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

Fix const violations in ESM imports when transformed to CJS #13258

Merged
merged 5 commits into from Jun 22, 2021

Conversation

overlookmotel
Copy link
Contributor

@overlookmotel overlookmotel commented May 4, 2021

Q                       A
Fixed Issues? No issue filed
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link n/a
Any Dependency Changes? No
License MIT

This PR fixes similar cases to #13248 in plugin-transform-modules-commonjs relating to const violations with import statements.

e.g.:

import x from 'foo';
x = 1;

This PR:

  1. Fixes compilation error if import x from 'foo'; x &&= 1;
  2. Adds const violation errors for import x from 'foo'; for ( x in {} ) {}
  3. Fixes a small bug with for loops without a block statement e.g. export let x = 1; for ( x in {} ) x();
  4. Adds more tests for assignments

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 4, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f76a694:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo nicolo-ribaudo added area: modules PR: Spec Compliance 👓 A type of pull request used for our changelog categories labels May 5, 2021
packages/babel-helper-simple-access/src/index.ts Outdated Show resolved Hide resolved
}
}
if (imported.get(name) && !importConstViolationName) {
importConstViolationName = name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we record all const violation names? e.g.

import { x, y } from 'foo';
for ( [x, y] of [] );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's enough to report the first one, since we only inject the error for the first one (the others are not observable anyway).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @nicolo-ribaudo is correct. Only the first is necessary as that will throw an error before the 2nd is evaluated.

@overlookmotel
Copy link
Contributor Author

@JLHwung @nicolo-ribaudo Thanks for the comments. Just to let you know, I'm flat out next few weeks, so will have to leave this until my project is done. But I'll come back to this and make the changes requested next month.

@overlookmotel
Copy link
Contributor Author

@JLHwung Changes requested have been made. I think this is ready to merge if you're happy with it.

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nicolo-ribaudo nicolo-ribaudo changed the title Fix const violations in ES module imports when transformed to CommonJS Fix const violations in ESM imports when transformed to CJS Jun 22, 2021
@nicolo-ribaudo nicolo-ribaudo merged commit 5ac5e35 into babel:main Jun 22, 2021
@overlookmotel
Copy link
Contributor Author

Thanks very much for swift merge.

@overlookmotel overlookmotel deleted the module-import-assignments branch June 23, 2021 11:16
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Jul 30, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Sep 23, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: modules outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Spec Compliance 👓 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants