From 8f8c7887895258123f289da0aaa3e7a397b15e3f Mon Sep 17 00:00:00 2001 From: Victor Hom Date: Tue, 6 Jun 2017 12:46:21 -0400 Subject: [PATCH] New: WIP add eslintIgnore support to package.json (fixes #8458) Added tests for when eslintignore file exists and doesn't exist Abstracted the findFile function for finding ignore file and package.json --- lib/ignored-paths.js | 37 ++++++++++++++--------- tests/fixtures/ignored-paths/package.json | 11 +++++++ tests/lib/ignored-paths.js | 13 ++++++-- 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 tests/fixtures/ignored-paths/package.json diff --git a/lib/ignored-paths.js b/lib/ignored-paths.js index 140beb7f2d54..cbecedf84c58 100644 --- a/lib/ignored-paths.js +++ b/lib/ignored-paths.js @@ -48,29 +48,36 @@ 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) { - cwd = cwd || DEFAULT_OPTIONS.cwd; - - const ignoreFilePath = path.resolve(cwd, ESLINT_PACKAGE_JSON); - return fs.existsSync(ignoreFilePath) && fs.statSync(ignoreFilePath).isFile() ? ignoreFilePath : ""; + return findFile(cwd, ESLINT_PACKAGE_JSON); } /** @@ -144,7 +151,7 @@ class IgnoredPaths { addPattern(this.ig.default, this.defaultPatterns); if (options.ignore !== false) { - let ignorePath, packageJSONPath; + let ignorePath; if (options.ignorePath) { debug("Using specific ignore file"); @@ -178,18 +185,20 @@ class IgnoredPaths { try { // if the ignoreFile does not exist, check package.json for eslintIgnore - packageJSONPath = findPackageJSONFile(options.cwd); + const packageJSONPath = findPackageJSONFile(options.cwd); + if (packageJSONPath) { const configOptions = config.load(packageJSONPath, configContext); if (configOptions.eslintIgnore) { - const filesToIgnore = configOptions.eslintIgnore; - - debug(filesToIgnore); + configOptions.eslintIgnore.forEach(pattern => { + addPattern(this.ig.custom, pattern); + addPattern(this.ig.default, pattern); + }); } } } catch (e) { - debug("Could not find eslintIgnore in package.json"); + debug("Could not find package.json to check eslintIgnore property"); } } diff --git a/tests/fixtures/ignored-paths/package.json b/tests/fixtures/ignored-paths/package.json new file mode 100644 index 000000000000..f093887ea3d8 --- /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"] + } +} \ No newline at end of file diff --git a/tests/lib/ignored-paths.js b/tests/lib/ignored-paths.js index 48048457cff5..81fa1ee8b148 100644 --- a/tests/lib/ignored-paths.js +++ b/tests/lib/ignored-paths.js @@ -165,9 +165,18 @@ describe("IgnoredPaths", () => { ); }); - it("shoud load package.json's array of files to ignore in eslintIgnore", () => { - const expectedIgnoreFile = getFixturePath("package-json-ignore/package.json"); + it("should load package.json's eslintIgnore files if no specified ignore 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 load package.json's eslintIgnore files if specified ignore file", () => { + const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() }); + + assert.isFalse(ignoredPaths.contains("hello.js")); + assert.isFalse(ignoredPaths.contains("world.js")); }); });