Skip to content

Commit a574894

Browse files
committed
no-unresolved: ignore option (close #89)
1 parent 16a3719 commit a574894

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

docs/rules/no-unresolved.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ define(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not foun
5353
require(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not found
5454
```
5555

56+
#### `ignore`
57+
58+
This rule has its own ignore list, separate from [`import/ignore`]. This is because you may want to know whether a module can be located, regardless of whether it can be parsed for exports: `node_modules`, CoffeeScript files, etc. are all good to resolve properly, but will not be parsed if configured as such via [`import/ignore`].
59+
60+
To suppress errors from files that may not be properly resolved by your [resolver settings](../../README.md#resolver-plugins), you may add an `ignore` key with an array of `RegExp` pattern strings:
61+
62+
```js
63+
/*eslint import/no-unresolved: [2, { ignore: ['\.img$'] }]*/
64+
65+
import { x } from './mod' // may be reported, if not resolved to a module
66+
67+
import coolImg from '../../img/coolImg.img' // will not be reported, even if not found
68+
```
69+
70+
5671
## When Not To Use It
5772

5873
If you're using a module bundler other than Node or Webpack, you may end up with
@@ -63,3 +78,6 @@ a lot of false positive reports of missing dependencies.
6378
- [Resolver plugins](../../README.md#resolver-plugins)
6479
- [Node resolver](https://npmjs.com/package/eslint-import-resolver-node) (default)
6580
- [Webpack resolver](https://npmjs.com/package/eslint-import-resolver-webpack)
81+
- [`import/ignore`] global setting
82+
83+
[`import/ignore`]: ../../README.md#import/ignore

src/rules/no-unresolved.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import resolve from '../core/resolve'
66

77
module.exports = function (context) {
88

9+
let ignoreRegExps = []
10+
if (context.options[0] != null && context.options[0].ignore != null) {
11+
ignoreRegExps = context.options[0].ignore.map(p => new RegExp(p))
12+
}
13+
914
function checkSourceValue(source) {
1015
if (source == null) return
1116

17+
if (ignoreRegExps.some(re => re.test(source.value))) return
18+
1219
if (resolve(source.value, context) === undefined) {
1320
context.report(source,
1421
'Unable to resolve path to module \'' + source.value + '\'.')
@@ -80,6 +87,12 @@ module.exports.schema = [
8087
'properties': {
8188
'commonjs': { 'type': 'boolean' },
8289
'amd': { 'type': 'boolean' },
90+
'ignore': {
91+
'type': 'array',
92+
'minItems': 1,
93+
'items': { 'type': 'string' },
94+
'uniqueItems': true,
95+
},
8396
},
8497
'additionalProperties': false,
8598
},

tests/files/test.giffy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boo!

tests/src/rules/no-unresolved.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,41 @@ ruleTester.run('no-unresolved (webpack-specific)', rule, {
245245
}),
246246
],
247247
})
248+
249+
250+
ruleTester.run('no-unresolved ignore list', rule, {
251+
valid: [
252+
test({
253+
code: 'import "./malformed.js"',
254+
options: [{ ignore: ['\.png$', '\.gif$']}],
255+
}),
256+
test({
257+
code: 'import "./test.giffy"',
258+
options: [{ ignore: ['\.png$', '\.gif$']}],
259+
}),
260+
261+
test({
262+
code: 'import "./test.gif"',
263+
options: [{ ignore: ['\.png$', '\.gif$']}],
264+
}),
265+
266+
test({
267+
code: 'import "./test.png"',
268+
options: [{ ignore: ['\.png$', '\.gif$']}],
269+
}),
270+
],
271+
272+
invalid:[
273+
test({
274+
code: 'import "./test.gif"',
275+
options: [{ ignore: ['\.png$']}],
276+
errors: [ "Unable to resolve path to module './test.gif'." ],
277+
}),
278+
279+
test({
280+
code: 'import "./test.png"',
281+
options: [{ ignore: ['\.gif$']}],
282+
errors: [ "Unable to resolve path to module './test.png'." ],
283+
}),
284+
],
285+
})

0 commit comments

Comments
 (0)