From 5f81a68a3904a559764872e3f0c7453865a6c6dc Mon Sep 17 00:00:00 2001 From: Victor Hom Date: Thu, 15 Jun 2017 10:52:46 -0400 Subject: [PATCH] New: Add eslintIgnore support to package.json (fixes #8458) (#8690) --- docs/user-guide/configuring.md | 16 +++++ lib/ignored-paths.js | 58 +++++++++++++++++-- .../bad-package-json-ignore/package.json | 11 ++++ .../package-json-ignore/package.json | 11 ++++ tests/fixtures/ignored-paths/package.json | 11 ++++ tests/lib/ignored-paths.js | 21 +++++++ 6 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/ignored-paths/bad-package-json-ignore/package.json create mode 100644 tests/fixtures/ignored-paths/package-json-ignore/package.json create mode 100644 tests/fixtures/ignored-paths/package.json diff --git a/docs/user-guide/configuring.md b/docs/user-guide/configuring.md index 3f59e867237..86469a5af4f 100644 --- a/docs/user-guide/configuring.md +++ b/docs/user-guide/configuring.md @@ -775,6 +775,22 @@ You can also use your `.gitignore` file: Any file that follows the standard ignore file format can be used. Keep in mind that specifying `--ignore-path` means that any existing `.eslintignore` file will not be used. Note that globbing rules in `.eslintignore` follow those of `.gitignore`. +### Using eslintIgnore in package.json + +If an `.eslintignore` file is not found and an alternate file is not specified, eslint will look in package.json for an `eslintIgnore` key to check for files to ignore. + + { + "name": "mypackage", + "version": "0.0.1", + "eslintConfig": { + "env": { + "browser": true, + "node": true + } + }, + "eslintIgnore": ["hello.js", "world.js"] + } + ### Ignored File Warnings When you pass directories to ESLint, files and directories are silently ignored. If you pass a specific file to ESLint, then you will see a warning indicating that the file was skipped. For example, suppose you have an `.eslintignore` file that looks like this: diff --git a/lib/ignored-paths.js b/lib/ignored-paths.js index d2d89bd9895..2923ee1a672 100644 --- a/lib/ignored-paths.js +++ b/lib/ignored-paths.js @@ -16,7 +16,6 @@ const fs = require("fs"), const debug = require("debug")("eslint:ignored-paths"); - //------------------------------------------------------------------------------ // Constants //------------------------------------------------------------------------------ @@ -41,20 +40,38 @@ const DEFAULT_OPTIONS = { // Helpers //------------------------------------------------------------------------------ - /** - * Find an ignore file in the current directory. + * Find a file in the current directory. * @param {string} cwd Current working directory + * @param {string} name File name * @returns {string} Path of ignore file or an empty string. */ -function findIgnoreFile(cwd) { +function findFile(cwd, name) { cwd = cwd || DEFAULT_OPTIONS.cwd; - const ignoreFilePath = path.resolve(cwd, ESLINT_IGNORE_FILENAME); + const ignoreFilePath = path.resolve(cwd, name); return fs.existsSync(ignoreFilePath) && fs.statSync(ignoreFilePath).isFile() ? ignoreFilePath : ""; } +/** + * Find an ignore file in the current directory. + * @param {string} cwd Current working directory + * @returns {string} Path of ignore file or an empty string. + */ +function findIgnoreFile(cwd) { + return findFile(cwd, ESLINT_IGNORE_FILENAME); +} + +/** + * Find an package.json file in the current directory. + * @param {string} cwd Current working directory + * @returns {string} Path of package.json file or an empty string. + */ +function findPackageJSONFile(cwd) { + return findFile(cwd, "package.json"); +} + /** * Merge options with defaults * @param {Object} options Options to merge with DEFAULT_OPTIONS constant @@ -146,6 +163,37 @@ class IgnoredPaths { this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath)); this.addIgnoreFile(this.ig.custom, ignorePath); this.addIgnoreFile(this.ig.default, ignorePath); + } else { + try { + + // if the ignoreFile does not exist, check package.json for eslintIgnore + const packageJSONPath = findPackageJSONFile(options.cwd); + + if (packageJSONPath) { + let packageJSONOptions; + + try { + packageJSONOptions = JSON.parse(fs.readFileSync(packageJSONPath, "utf8")); + } catch (e) { + debug("Could not read package.json file to check eslintIgnore property"); + throw e; + } + + if (packageJSONOptions.eslintIgnore) { + if (Array.isArray(packageJSONOptions.eslintIgnore)) { + packageJSONOptions.eslintIgnore.forEach(pattern => { + addPattern(this.ig.custom, pattern); + addPattern(this.ig.default, pattern); + }); + } else { + throw new Error("Package.json eslintIgnore property requires an array of paths"); + } + } + } + } catch (e) { + debug("Could not find package.json to check eslintIgnore property"); + throw e; + } } if (options.ignorePattern) { diff --git a/tests/fixtures/ignored-paths/bad-package-json-ignore/package.json b/tests/fixtures/ignored-paths/bad-package-json-ignore/package.json new file mode 100644 index 00000000000..37174a8499f --- /dev/null +++ b/tests/fixtures/ignored-paths/bad-package-json-ignore/package.json @@ -0,0 +1,11 @@ +{ + "name": "mypackage", + "version": "0.0.1", + "eslintConfig": { + "env": { + "browser": true, + "node": true + } + }, + "eslintIgnore": "hello.js" +} diff --git a/tests/fixtures/ignored-paths/package-json-ignore/package.json b/tests/fixtures/ignored-paths/package-json-ignore/package.json new file mode 100644 index 00000000000..05436bdb5cd --- /dev/null +++ b/tests/fixtures/ignored-paths/package-json-ignore/package.json @@ -0,0 +1,11 @@ +{ + "name": "mypackage", + "version": "0.0.1", + "eslintConfig": { + "env": { + "browser": true, + "node": true + } + }, + "eslintIgnore": ["hello.js", "world.js"] +} diff --git a/tests/fixtures/ignored-paths/package.json b/tests/fixtures/ignored-paths/package.json new file mode 100644 index 00000000000..05436bdb5cd --- /dev/null +++ b/tests/fixtures/ignored-paths/package.json @@ -0,0 +1,11 @@ +{ + "name": "mypackage", + "version": "0.0.1", + "eslintConfig": { + "env": { + "browser": true, + "node": true + } + }, + "eslintIgnore": ["hello.js", "world.js"] +} diff --git a/tests/lib/ignored-paths.js b/tests/lib/ignored-paths.js index 1631d37f41c..1ef1f29cdf1 100644 --- a/tests/lib/ignored-paths.js +++ b/tests/lib/ignored-paths.js @@ -165,7 +165,28 @@ describe("IgnoredPaths", () => { ); }); + it("should use package.json's eslintIgnore files if no specified .eslintignore file", () => { + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("package-json-ignore") }); + assert.isTrue(ignoredPaths.contains("hello.js")); + assert.isTrue(ignoredPaths.contains("world.js")); + }); + + it("should not use package.json's eslintIgnore files if specified .eslintignore file", () => { + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() }); + + assert.isFalse(ignoredPaths.contains("hello.js")); + assert.isFalse(ignoredPaths.contains("world.js")); + assert.isTrue(ignoredPaths.contains("sampleignorepattern")); + }); + + it("should error if package.json's eslintIgnore is not an array of file paths", () => { + assert.throws(() => { + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("bad-package-json-ignore") }); + + assert.ok(ignoredPaths); + }, "Package.json eslintIgnore property requires an array of paths"); + }); }); describe("caching file reads", () => {