Skip to content

Commit

Permalink
New: WIP add eslintIgnore support to package.json (fixes #8458)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom committed Jun 12, 2017
1 parent 34c4020 commit b79a04b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
16 changes: 16 additions & 0 deletions docs/user-guide/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 47 additions & 6 deletions lib/ignored-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const fs = require("fs"),

const debug = require("debug")("eslint:ignored-paths");


//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
Expand All @@ -37,25 +36,42 @@ const DEFAULT_OPTIONS = {
cwd: process.cwd()
};


//------------------------------------------------------------------------------
// 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
Expand Down Expand Up @@ -157,6 +173,31 @@ 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
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");
}

if (packageJSONOptions.eslintIgnore && Array.isArray(packageJSONOptions.eslintIgnore)) {
packageJSONOptions.eslintIgnore.forEach(pattern => {
addPattern(this.ig.custom, pattern);
addPattern(this.ig.default, pattern);
});
}
}
} catch (e) {
debug("Could not find package.json to check eslintIgnore property");
}
}

if (options.ignorePattern) {
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/ignored-paths/package-json-ignore/package.json
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"]
}
11 changes: 11 additions & 0 deletions tests/fixtures/ignored-paths/package.json
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"]
}
15 changes: 15 additions & 0 deletions tests/lib/ignored-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ 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"));
});
});

describe("initialization with ignorePattern", () => {
Expand Down

0 comments on commit b79a04b

Please sign in to comment.