-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow filename patterns for rule no-extraneous-dependencies. #527
Allow filename patterns for rule no-extraneous-dependencies. #527
Conversation
"import/no-extraneous-dependencies": ["error", {"devDependencies": /test|spec/}] | ||
``` | ||
|
||
When using a regular expression the result of running [`test`] against the name of the file being linted is used as the boolean value. For example, the above configurations will allow the import of `devDependencies` in files whose names include `test` or `spec`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using a regular expression
--> When using a regular expression,
(+ comma)
the result of running [test] against the name of the file being linted is used as the boolean value.
--> the setting will be activated if the name of the file being linted matches the given regular expression.
Hi @knpwrs! Thanks a lot for this, it looks great! I'm asking myself now whether a glob configuration would not more appropriate (as we're adding (sorry for being a party-pooper 😞) |
Yeah, I saw the discussion about glob vs regular expression but didn't see if it went anywhere. I could see it going either way. I've used globs for everything filename related until webpack came along. Might be overkill, but maybe it would be worth supporting both? Would it be safe to assume that a string bookended by
|
'peerDependencies': { 'type': 'boolean' }, | ||
'devDependencies': { 'type': ['boolean', 'string', 'object'] }, | ||
'optionalDependencies': { 'type': ['boolean', 'string', 'object'] }, | ||
'peerDependencies': { 'type': ['boolean', 'string', 'object'] }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're allowing object
, because a regular expression value is an object, right? Just want confirmation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, AFAIK the schema is checked with typeof
, and typeof /foo/ === 'object'
.
I like globs as you can decompose your allowance rules And thanks for doing this, the bikeshedding may continue, but at least this is going forward :) |
For two of those examples ( |
True, didn't think them through much ^^ test/
A/
a.js
a2.js
fixtures/
no-dev-deps.js
B/
b.js
b2.js
fixtures/
no-dev-deps.js If you have a lot of them, it gets tedious to add a Anyway, you'd make a clearer selection using globs than regexes IMO. |
Kaboom. No kill like overkill! Looks like everything new is covered. |
"import/no-extraneous-dependencies": ["error", {"devDependencies": [/test/, '/spec/', '*.test.js', '*.spec.js']}] | ||
``` | ||
|
||
When using an array of globs or regular expressions, the setting will be activated if the name of the file being linted matches a single glob or regular expression in the array. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
matches a single glob or regular expression in the array
Hmm... Not sure this part works. Globs as an array allows you to specify multiple things ['test/**/*.js', 'src/**/*.js']
, but also to forbid elements allowed by previous "expressions": ['test/**/*.js', 'src/**/*.js', '!fixtures']
. Here <root>/test/fixtures/a.js
should not work, whereas from your docs (and guessing implementation), it would.
(This is a glob right? Or am I confusing terms?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang it. Back to the drawing board.
Maybe we should only support arrays for globs? But then what is excluded? Just things that contain an exclamation mark? In the case of ['*.test.js', '*.spec.js']
, I'd want that to be additive. In the case of ['*.test.js', '!**/fixtures/**']
, I'd want that to be subtractive.
This is approaching a point of complexity that may not be best for this plugin to handle. In your multiple test and fixture directory example we're already approaching a status of, "really, now?" At least IMHO.
A thought occurs. Maybe allow a function for configuration? Pass the name to the function and expect a boolean back. This plugin could support the most basic use cases and then people who need more complex functionality can implement it themselves.
Re: your closing question... Is what a glob? The string containing <root>
? Not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also worth noting that passing a function obviously requires a JavaScript configuration file. I think that's a reasonable requirement if somebody requires super-specific rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
['.test.js', '.spec.js'] I'd want that to be additive. In the case of ['.test.js', '!__/fixtures/*'], I'd want that to be subtractive.
That is the case. Imagine
[
'**/*.test.js', // All the file ending with '.test.js'
'test/**/*.js', // + All the files in test/
'!**/fixtures/**' // Take all the files selected by the previous two selectors,
// and remove those in a fixtures (sub-)folder
]
This is approaching a point of complexity that may not be best for this plugin to handle.
I'm not sure I agree, but that's a subjective point a view ;)
Maybe allow a function for configuration
The function is doable, but then we're pushing the responsibility of doing the glob/regex logic to the user. I'd use functions as a last last resort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These aren't ignore files - I don't think they need to be subtractive. There's nothing wrong with requiring people to nest .eslintrc
files for things that can easily be achieved that way, but that would otherwise complicate the config.
I think we've found the cost of both is in the design discussion. 😅 I think it should be one or the other. There is precedent for both at this point (pending the PR @jfmengels referenced). My gut says it should be globs, going forward, whenever we're defining configuration for sets of files. My suspicion is that one or two globs will be sufficient in 99% of the cases where this is used, i.e. So, I propose that it be an array of globs, handled by |
So let's say we drop regex, how do we want to handle the additive / subtractive problem with globs? |
@knpwrs Sorry, what problem? |
I'm just currently thinking in the mindset of how I implemented the array functionality using |
That all seems overcomplicated. If you want to exempt a path, either use a nested |
Unless there's something about glob composition I'm missing. |
Right, okay, so that's going to a point I was making at #527 (comment). So is the current idea just to roll this back to nothing but matching |
That's what makes the most sense to me. |
Cool, I'll knock it out when I get home. 😄 |
ɯooqɐʞ |
You can also use globs instead of literal booleans: | ||
|
||
```js | ||
"import/no-extraneous-dependencies": ["error", {"devDependencies": "*.test.js"}] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems like it'd be simpler to just require this to always be an array - eslint can enforce it, and that way the user would get an immediate indication that multiple globs were possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll go ahead and do that.
How's this looking now? |
LGTM 😄 |
@benmosher Filename: |
if it's deep, might need to be |
if that works, let me know--we should update the example in the docs to that |
@benmosher works 👍 - can confirm that glob |
Following the suggestion in #470 (comment).
Fixes #470.