From ccfc9dbc78b3b3a2bb6dc734310f0186cbe7b610 Mon Sep 17 00:00:00 2001 From: Victor Hom Date: Mon, 5 Jun 2017 21:50:17 -0400 Subject: [PATCH] New: WIP add eslintIgnore support to package.json (fixes #8458) --- conf/config-schema.json | 3 +- lib/ignored-paths.js | 34 +++++++++++++++++-- .../package-json-ignore/package.json | 11 ++++++ tests/lib/ignored-paths.js | 5 +++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/ignored-paths/package-json-ignore/package.json diff --git a/conf/config-schema.json b/conf/config-schema.json index c3676bde9252..54ce0b937287 100644 --- a/conf/config-schema.json +++ b/conf/config-schema.json @@ -9,7 +9,8 @@ "settings": { "type": "object" }, "extends": { "type": ["string", "array"] }, "rules": { "type": "object" }, - "parserOptions": { "type": "object" } + "parserOptions": { "type": "object" }, + "eslintIgnore": { "type": ["array"] } }, "additionalProperties": false } diff --git a/lib/ignored-paths.js b/lib/ignored-paths.js index 0d9152495eca..20040209c6ca 100644 --- a/lib/ignored-paths.js +++ b/lib/ignored-paths.js @@ -12,7 +12,12 @@ const fs = require("fs"), path = require("path"), ignore = require("ignore"), - pathUtil = require("./util/path-util"); + pathUtil = require("./util/path-util"), + config = require("./config/config-file"), + Linter = require("../lib/linter"), + Config = require("../lib/config"); + +const configContext = new Config({}, new Linter()); const debug = require("debug")("eslint:ignored-paths"); @@ -22,6 +27,7 @@ const debug = require("debug")("eslint:ignored-paths"); //------------------------------------------------------------------------------ const ESLINT_IGNORE_FILENAME = ".eslintignore"; +const ESLINT_PACKAGE_JSON = "package.json"; /** * Adds `"*"` at the end of `"node_modules/"`, @@ -52,7 +58,18 @@ function findIgnoreFile(cwd) { cwd = cwd || DEFAULT_OPTIONS.cwd; const ignoreFilePath = path.resolve(cwd, ESLINT_IGNORE_FILENAME); + return fs.existsSync(ignoreFilePath) && fs.statSync(ignoreFilePath).isFile() ? ignoreFilePath : ""; +} +/** + * 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) { + cwd = cwd || DEFAULT_OPTIONS.cwd; + + const ignoreFilePath = path.resolve(cwd, ESLINT_PACKAGE_JSON); return fs.existsSync(ignoreFilePath) && fs.statSync(ignoreFilePath).isFile() ? ignoreFilePath : ""; } @@ -127,7 +144,7 @@ class IgnoredPaths { addPattern(this.ig.default, this.defaultPatterns); if (options.ignore !== false) { - let ignorePath; + let ignorePath, packageJSONPath; if (options.ignorePath) { debug("Using specific ignore file"); @@ -157,6 +174,19 @@ class IgnoredPaths { this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath)); addIgnoreFile(this.ig.custom, ignorePath); addIgnoreFile(this.ig.default, ignorePath); + } else { + try { + // if the ignoreFile does not exist, check package.json for eslintIgnore + packageJSONPath = findPackageJSONFile(options.cwd) + if (packageJSONPath) { + const configOptions = config.load(packageJSONPath, configContext); + if (configOptions.eslintIgnore) { + const filesToIgnore = configOptions.eslintIgnore; + } + } + } catch (e) { + debug("Could not find eslintIgnore in package.json"); + } } if (options.ignorePattern) { 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 000000000000..f093887ea3d8 --- /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"] + } +} \ No newline at end of file diff --git a/tests/lib/ignored-paths.js b/tests/lib/ignored-paths.js index 07d96672d0b0..48048457cff5 100644 --- a/tests/lib/ignored-paths.js +++ b/tests/lib/ignored-paths.js @@ -164,6 +164,11 @@ describe("IgnoredPaths", () => { ) ); }); + + it("shoud load package.json's array of files to ignore in eslintIgnore", () => { + const expectedIgnoreFile = getFixturePath("package-json-ignore/package.json"); + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("package-json-ignore") }); + }); }); describe("initialization with ignorePattern", () => {