diff --git a/.travis.yml b/.travis.yml index e71d76b16c..952d64b35c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,6 @@ node_js: - 0.12 - 4 - stable -env: - - NODE_PATH=./lib install: - npm -g install npm@2 @@ -14,7 +12,7 @@ install: - "for resolver in ./resolvers/*; do cd $resolver && npm install && cd ../..; done" script: - - "npm run-script ci-test" + - "npm test" - "for resolver in ./resolvers/*; do cd $resolver && npm test && cd ../..; done" after_success: diff --git a/appveyor.yml b/appveyor.yml index a2859cf6dd..875871b7c7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,5 @@ # Test against this version of Node.js environment: - NODE_PATH: ./lib - # NODE_PATH: ./lib nodejs_version: "4" # killing build matrix in the interest of turnaround time # matrix: @@ -33,13 +31,14 @@ install: # Post-install test scripts. test_script: + # Output useful info for debugging. - node --version - npm --version - # Lint - - ./node_modules/.bin/eslint ./src + # core tests - - ./node_modules/.bin/gulp test + - npm test + # resolver tests - cd .\resolvers\webpack && npm test && cd ..\.. - cd .\resolvers\node && npm test && cd ..\.. diff --git a/docs/rules/no-unresolved.md b/docs/rules/no-unresolved.md index f19b81baa0..2c25ec8f56 100644 --- a/docs/rules/no-unresolved.md +++ b/docs/rules/no-unresolved.md @@ -53,6 +53,21 @@ define(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not foun require(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not found ``` +#### `ignore` + +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`]. + +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: + +```js +/*eslint import/no-unresolved: [2, { ignore: ['\.img$'] }]*/ + +import { x } from './mod' // may be reported, if not resolved to a module + +import coolImg from '../../img/coolImg.img' // will not be reported, even if not found +``` + + ## When Not To Use It 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. - [Resolver plugins](../../README.md#resolver-plugins) - [Node resolver](https://npmjs.com/package/eslint-import-resolver-node) (default) - [Webpack resolver](https://npmjs.com/package/eslint-import-resolver-webpack) +- [`import/ignore`] global setting + +[`import/ignore`]: ../../README.md#import/ignore diff --git a/package.json b/package.json index 7edf24a97d..35730468cf 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,18 @@ { "name": "eslint-plugin-import", - "version": "1.0.4", + "version": "1.1.0", "description": "Import with sanity.", "main": "lib/index.js", "directories": { "test": "tests" }, "scripts": { - "watch": "NODE_PATH=./lib gulp watch-test", - "cover": "gulp pretest && NODE_PATH=./lib istanbul cover --dir reports/coverage _mocha tests/lib/ -- --recursive -R progress", + "watch": "cross-env NODE_PATH=./lib gulp watch-test", + "cover": "gulp pretest && cross-env NODE_PATH=./lib istanbul cover --dir reports/coverage _mocha tests/lib/ -- --recursive -R progress", "posttest": "eslint ./src", - "test": "NODE_PATH=./lib gulp test", - "ci-test": "eslint ./src && gulp pretest && istanbul cover --report lcovonly --dir reports/coverage _mocha tests/lib/ -- --recursive --reporter dot", - "debug": "NODE_PATH=./lib mocha debug --recursive --reporter dot tests/lib/", + "test": "cross-env NODE_PATH=./lib gulp test", + "ci-test": "eslint ./src && gulp pretest && cross-env NODE_PATH=./lib istanbul cover --report lcovonly --dir reports/coverage _mocha tests/lib/ -- --recursive --reporter dot", + "debug": "cross-env NODE_PATH=./lib mocha debug --recursive --reporter dot tests/lib/", "prepublish": "gulp prepublish", "coveralls": "cat ./reports/coverage/lcov.info | coveralls" }, @@ -36,15 +36,15 @@ }, "homepage": "https://github.com/benmosher/eslint-plugin-import", "devDependencies": { - "babel": "6.5.1", - "babel-eslint": "^5.0.0", + "babel-eslint": "next", "babel-plugin-transform-runtime": "6.5.2", "babel-preset-es2015": "6.5.0", "babel-preset-react": "6.5.0", "babel-preset-stage-1": "6.5.0", "chai": "^3.4.0", "coveralls": "^2.11.4", - "eslint": "2.2.x", + "cross-env": "^1.0.7", + "eslint": "2.x", "eslint-import-resolver-node": "file:./resolvers/node", "eslint-import-resolver-webpack": "file:./resolvers/webpack", "glob": "^6.0.2", diff --git a/src/rules/no-unresolved.js b/src/rules/no-unresolved.js index 0e32369c34..9020461806 100644 --- a/src/rules/no-unresolved.js +++ b/src/rules/no-unresolved.js @@ -6,9 +6,16 @@ import resolve from '../core/resolve' module.exports = function (context) { + let ignoreRegExps = [] + if (context.options[0] != null && context.options[0].ignore != null) { + ignoreRegExps = context.options[0].ignore.map(p => new RegExp(p)) + } + function checkSourceValue(source) { if (source == null) return + if (ignoreRegExps.some(re => re.test(source.value))) return + if (resolve(source.value, context) === undefined) { context.report(source, 'Unable to resolve path to module \'' + source.value + '\'.') @@ -80,6 +87,12 @@ module.exports.schema = [ 'properties': { 'commonjs': { 'type': 'boolean' }, 'amd': { 'type': 'boolean' }, + 'ignore': { + 'type': 'array', + 'minItems': 1, + 'items': { 'type': 'string' }, + 'uniqueItems': true, + }, }, 'additionalProperties': false, }, diff --git a/tests/files/test.giffy b/tests/files/test.giffy new file mode 100644 index 0000000000..abadbacd0a --- /dev/null +++ b/tests/files/test.giffy @@ -0,0 +1 @@ +boo! \ No newline at end of file diff --git a/tests/src/rules/no-unresolved.js b/tests/src/rules/no-unresolved.js index 1e4fa45676..cdbdf44699 100644 --- a/tests/src/rules/no-unresolved.js +++ b/tests/src/rules/no-unresolved.js @@ -245,3 +245,41 @@ ruleTester.run('no-unresolved (webpack-specific)', rule, { }), ], }) + + +ruleTester.run('no-unresolved ignore list', rule, { + valid: [ + test({ + code: 'import "./malformed.js"', + options: [{ ignore: ['\.png$', '\.gif$']}], + }), + test({ + code: 'import "./test.giffy"', + options: [{ ignore: ['\.png$', '\.gif$']}], + }), + + test({ + code: 'import "./test.gif"', + options: [{ ignore: ['\.png$', '\.gif$']}], + }), + + test({ + code: 'import "./test.png"', + options: [{ ignore: ['\.png$', '\.gif$']}], + }), + ], + + invalid:[ + test({ + code: 'import "./test.gif"', + options: [{ ignore: ['\.png$']}], + errors: [ "Unable to resolve path to module './test.gif'." ], + }), + + test({ + code: 'import "./test.png"', + options: [{ ignore: ['\.gif$']}], + errors: [ "Unable to resolve path to module './test.png'." ], + }), + ], +})