Skip to content

Commit

Permalink
Extend no-extraneous-dependencies to support globs and arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
knpwrs committed Aug 28, 2016
1 parent 1be4720 commit 8ff322d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
18 changes: 15 additions & 3 deletions docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ You can set the options like this:
"import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false, "peerDependencies": false}]
```

You can also use regular expressions instead of literal booleans:
You can also use globs instead of literal booleans:

```js
"import/no-extraneous-dependencies": ["error", {"devDependencies": "test|spec"}]
"import/no-extraneous-dependencies": ["error", {"devDependencies": "*.test.js"}]
```

You can also use regular expressions instead of literal booleans or globs (note: the string must begin and end with `/`):

```js
"import/no-extraneous-dependencies": ["error", {"devDependencies": "/test|spec/"}]
```

If you are using JavaScript configuration (e.g., `.eslintrc.js`), then you can use a RegExp literal instead of a string:
Expand All @@ -31,7 +37,13 @@ If you are using JavaScript configuration (e.g., `.eslintrc.js`), then you can u
"import/no-extraneous-dependencies": ["error", {"devDependencies": /test|spec/}]
```

When using a regular expression, the setting will be activated if the name of the file being linted matches the given regular expression. For example, the above configurations will allow the import of `devDependencies` in files whose names include `test` or `spec`.
When using a glob or regular expression, the setting will be activated if the name of the file being linted matches the given glob or regular expression. In addition, you can use an array to specify multiple globs or regular expressions:

```js
"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.

## Rule Details

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"lodash.endswith": "^4.0.1",
"lodash.find": "^4.3.0",
"lodash.findindex": "^4.3.0",
"minimatch": "^3.0.3",
"object-assign": "^4.0.1",
"pkg-dir": "^1.0.0",
"pkg-up": "^1.0.0"
Expand Down
25 changes: 20 additions & 5 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import pkgUp from 'pkg-up'
import minimatch from 'minimatch'
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'

Expand Down Expand Up @@ -72,11 +73,25 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
}

function testConfig(config, filename) {
// Simplest configuration first
if (typeof config === 'boolean' || typeof config === 'undefined') {
return config
}
const pattern = new RegExp(config) // `config` is either a string or RegExp
return pattern.test(filename)
// Account for the possibility of an array for multiple configuration
return [].concat(config).some(c => {
if (typeof c === 'object') {
// `c` is a literal RegExp
return c.test(filename)
}
// By this point `c` must be a string.
if (/^\/.+\/$/.test(c)) {
// `c` is a string representation of a RegExp.
const pattern = new RegExp(c.substring(1, c.length - 1))
return pattern.test(filename)
}
// `c` must be a string representing a glob
return minimatch(filename, c)
})
}

module.exports = function (context) {
Expand Down Expand Up @@ -111,9 +126,9 @@ module.exports.schema = [
{
'type': 'object',
'properties': {
'devDependencies': { 'type': ['boolean', 'string', 'object'] },
'optionalDependencies': { 'type': ['boolean', 'string', 'object'] },
'peerDependencies': { 'type': ['boolean', 'string', 'object'] },
'devDependencies': { 'type': ['boolean', 'string', 'object', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'string', 'object', 'array'] },
'peerDependencies': { 'type': ['boolean', 'string', 'object', 'array'] },
},
'additionalProperties': false,
},
Expand Down
41 changes: 37 additions & 4 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,27 @@ ruleTester.run('no-extraneous-dependencies', rule, {
test({
code: 'import chai from "chai"',
options: [{devDependencies: /test|spec/}],
filename: 'glob.test.js',
filename: 'foo.test.js',
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: 'test|spec'}],
filename: 'glob.spec.js',
options: [{devDependencies: '/test|spec/'}],
filename: 'foo.spec.js',
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: '*.spec.js'}],
filename: 'foo.spec.js',
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: ['*.test.js', '*.spec.js']}],
filename: 'foo.spec.js',
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: [/\.test\.js$/, /\.spec\.js$/]}],
filename: 'foo.spec.js',
}),
],
invalid: [
Expand Down Expand Up @@ -110,7 +125,25 @@ ruleTester.run('no-extraneous-dependencies', rule, {
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: 'test|spec'}],
options: [{devDependencies: '/test|spec/'}],
filename: 'foo.tes.js',
errors: [{
ruleId: 'no-extraneous-dependencies',
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: '*.test.js'}],
filename: 'foo.tes.js',
errors: [{
ruleId: 'no-extraneous-dependencies',
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{devDependencies: ['*.test.js', /test\.js$/, '/test/']}],
filename: 'foo.tes.js',
errors: [{
ruleId: 'no-extraneous-dependencies',
Expand Down

0 comments on commit 8ff322d

Please sign in to comment.