Skip to content

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

Merged
nicolo-ribaudo merged 5 commits intobabel:mainfrom
overlookmotel:module-import-assignments
Jun 22, 2021
Merged

Fix const violations in ESM imports when transformed to CJS#13258
nicolo-ribaudo merged 5 commits intobabel:mainfrom
overlookmotel:module-import-assignments

Conversation

@overlookmotel
Copy link
Copy Markdown
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
Copy Markdown

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
}
}
if (imported.get(name) && !importConstViolationName) {
importConstViolationName = name;
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 overlookmotel force-pushed the module-import-assignments branch from 125cc47 to f76a694 Compare June 22, 2021 15:46
@overlookmotel
Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
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
Copy Markdown
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.

3 participants