-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New: Add eslintIgnore support to package.json (fixes #8458) #8690
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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("the package.json file eslintIgnore property requires an array of paths"); | ||
} | ||
} | ||
} | ||
} catch (e) { | ||
debug("Could not find package.json to check eslintIgnore property"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if we should have multiple try/catch and error logs, in order to distinguish between file not being found vs file was not valid JSON vs whatever other issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added the try-catch for possible invalid json in package.json |
||
throw e; | ||
} | ||
} | ||
|
||
if (options.ignorePattern) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "mypackage", | ||
"version": "0.0.1", | ||
"eslintConfig": { | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
} | ||
}, | ||
"eslintIgnore": "hello.js" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "mypackage", | ||
"version": "0.0.1", | ||
"eslintConfig": { | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
} | ||
}, | ||
"eslintIgnore": ["hello.js", "world.js"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "mypackage", | ||
"version": "0.0.1", | ||
"eslintConfig": { | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
} | ||
}, | ||
"eslintIgnore": ["hello.js", "world.js"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add an assertion that the .eslintignore file does get used in this case? assert.isTrue(ignoredPaths.contains("sampleignorepattern")); |
||
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); | ||
}, "the package.json file eslintIgnore property requires an array of paths"); | ||
}); | ||
}); | ||
|
||
describe("caching file reads", () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: The other error messages begin with a capital letter - maybe this one should also, for consistency's sake?