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

Support opt-in restrictions on 'module' and 'exports' usage alongside ES6 import/export. #6309

Merged
merged 4 commits into from Sep 26, 2017

Conversation

loganfsmyth
Copy link
Member

Q                       A
Fixed Issues
Patch: Bug Fix? N
Major: Breaking Change? N
Minor: New Feature?
Tests Added/Pass?
Spec Compliancy?
License MIT
Doc PR
Any Dependency Changes?

Not breaking since it is opt in, but we should probably make this the default once we settle on a plan for #6242.

This injects code to throw an exception if you try to use module or exports inside an ES6 module. Webpack already does this, so hopefully it won't be too much of an upgrade issue.

@loganfsmyth loganfsmyth force-pushed the assert-module-exports-usage branch 2 times, most recently from 3832842 to a122577 Compare September 26, 2017 19:36
@babel-bot
Copy link
Collaborator

babel-bot commented Sep 26, 2017

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/5061/

const { scope, bindingNames } = this;

const arg = path.get("argument");
if (!arg.isIdentifier()) return;
Copy link
Member

Choose a reason for hiding this comment

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

This would be super (😉, you'll get it in a second) helpful for super.prop++ and this.#foo++, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

At the moment this only accepts a list of identifier names, so that'd be tough, but we could certainly expand that functionality later.

) {
// ++i => (i += 1);
path.replaceWith(
t.assignmentExpression("+=", arg.node, t.numericLiteral(1)),
Copy link
Member

Choose a reason for hiding this comment

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

Can we transform into a get and set? super.prop and this.#foo again.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's just translating into this to avoid code duplication. The AssignmentExpression visitor should still run on cases like this after this replacement happens, AFAIK, which will replace it with a get and a set.

@@ -0,0 +1,22 @@
import "foo";
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a test with a locally defined exports?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we do. I'll add some.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

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

LGTM, just a few readme things

@@ -0,0 +1,18 @@
# babel-helper-simple-assignment

There are many cases where it is hard to perform transformations because a
Copy link
Member

Choose a reason for hiding this comment

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

code using -> code is using


It is difficult to work with.

This helper can handle converting these to simple access patterns of
Copy link
Member

Choose a reason for hiding this comment

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

of what? :)

@loganfsmyth loganfsmyth merged commit 508597a into babel:master Sep 26, 2017
@loganfsmyth loganfsmyth deleted the assert-module-exports-usage branch September 26, 2017 23:42
`);
const exportsAssertion = template(`
(function(){
throw new Error("The CommonJS 'exports' variable is not available in ES6 modules.");
Copy link
Member

Choose a reason for hiding this comment

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

This is also the case for __dirname, __filename, require et al but we can't really ban those until node gets a standard import.meta shape

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I'd be up for doing this for those too, but we can always do it behind a separate flag. For now at least this PR allows us to get closer.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants