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

Add allow-require option to no-commonjs rule #880

Merged
merged 4 commits into from
Apr 1, 2018

Conversation

futpib
Copy link
Contributor

@futpib futpib commented Jun 23, 2017

This fixes #548

@coveralls
Copy link

coveralls commented Jun 23, 2017

Coverage Status

Coverage increased (+0.006%) to 95.969% when pulling d8dfc3b on futpib:no-commonjs-allow-require into 8101d39 on benmosher:master.

If `allow-require` is provided as an option, `require` calls are valid:

```js
/*eslint no-commonjs: [2, "allow-require"]*/
Copy link
Member

Choose a reason for hiding this comment

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

Let's use an object schema instead, so it's extensible - like { allowRequire: true, allowPrimitiveModules: true }.

We should move away from string schemas (but continue to support the existing ones, ofc)


but `module.exports` is reported as usual.

This is useful for conditional requires.
Copy link
Member

Choose a reason for hiding this comment

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

When you have this rule enabled, wouldn't you use import() for conditionals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, you could. I guess it's for compatibility only then.

Copy link
Contributor Author

@futpib futpib Jun 23, 2017

Choose a reason for hiding this comment

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

Actually, I just looked though some babel-plugin-dynamic-import-node tests, the new dynamic import is async and thus is not a drop-in replacement for require. So I guess adding a hint like this would suffice:

This is useful for conditional requires. If you don't rely on synchronous module loading, check out dynamic import.

Copy link
Member

Choose a reason for hiding this comment

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

True enough; your suggestion is a good addition.

Copy link
Member

Choose a reason for hiding this comment

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

This was my knee jerk reaction as well, but I think the sync argument tracks.

@futpib futpib force-pushed the no-commonjs-allow-require branch 2 times, most recently from 6fbd7b7 to 759efd4 Compare June 23, 2017 19:10
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Can we add a schema to this rule so eslint can validate it?

@coveralls
Copy link

coveralls commented Jun 23, 2017

Coverage Status

Coverage increased (+0.01%) to 95.976% when pulling 11d6d51 on futpib:no-commonjs-allow-require into 8101d39 on benmosher:master.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great!


schema: {
anyOf: [
{
Copy link
Member

Choose a reason for hiding this comment

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

i think this first anyOf item can just be schemaString by itself?

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 don't think so, options are always an array of things.

@coveralls
Copy link

coveralls commented Jun 23, 2017

Coverage Status

Coverage increased (+0.04%) to 96.008% when pulling 759efd4 on futpib:no-commonjs-allow-require into 8101d39 on benmosher:master.

4 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 96.008% when pulling 759efd4 on futpib:no-commonjs-allow-require into 8101d39 on benmosher:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 96.008% when pulling 759efd4 on futpib:no-commonjs-allow-require into 8101d39 on benmosher:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 96.008% when pulling 759efd4 on futpib:no-commonjs-allow-require into 8101d39 on benmosher:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 96.008% when pulling 759efd4 on futpib:no-commonjs-allow-require into 8101d39 on benmosher:master.

@coveralls
Copy link

coveralls commented Jun 23, 2017

Coverage Status

Coverage increased (+0.01%) to 96.47% when pulling c438d3e on futpib:no-commonjs-allow-require into 48d0a8a on benmosher:master.

@futpib
Copy link
Contributor Author

futpib commented Jul 19, 2017

I'm so waiting for this to release. Is there anything else to do for me?

@ljharb
Copy link
Member

ljharb commented Jul 19, 2017

@futpib i'd like @benmosher or @jfmengels to review it prior to merging.

@benmosher
Copy link
Member

Hi @futpib, sorry it has been literally forever since you submitted this and I'm finally getting back to it. 😅

I just upgraded this to semver-major because it changes the options on this rule. If you'd like to get it merged into the next 2.x release, can you make the options match the existing rule?

i.e. 'allow-require' instead of allowRequire: true. This is not my first choice; I like the flags-based options you've implemented better, but again, they are a breaking change to the existing rule.

Copy link
Member

@benmosher benmosher left a comment

Choose a reason for hiding this comment

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

see my note in the main PR conversation thread, but tl;dr: extend the existing allow-primitive-modules enum to be set as allow-requires.

I think it's okay--or even ideal--if they are mutually exclusive, as IIRC enabling both would turn off the rule and is therefore a nonsense config state.

Copy link
Member

@benmosher benmosher left a comment

Choose a reason for hiding this comment

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

nvm, I totally read this wrong and you are fine 😅

I misread the new tests as changes, but you left the existing tests in place so obviously it's semver-minor, my bad!

@futpib
Copy link
Contributor Author

futpib commented Mar 30, 2018

@benmosher still got semver-major label though

@ljharb ljharb force-pushed the no-commonjs-allow-require branch 2 times, most recently from 78bc94b to c438d3e Compare April 1, 2018 16:44
@ljharb ljharb merged commit ee15fa4 into import-js:master Apr 1, 2018
benmosher added a commit that referenced this pull request Apr 9, 2018
benmosher added a commit that referenced this pull request Apr 9, 2018
@benmosher benmosher mentioned this pull request Apr 9, 2018
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Allow require() but forbid module.exports
4 participants