Skip to content

Commit

Permalink
New: WIP add eslintIgnore support to package.json (fixes eslint#8458)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom committed Jun 7, 2017
1 parent 34c4020 commit aad0c79
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion conf/config-schema.json
Expand Up @@ -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
}
34 changes: 32 additions & 2 deletions lib/ignored-paths.js
Expand Up @@ -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");

Expand All @@ -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/"`,
Expand Down Expand Up @@ -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 : "";
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions 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"]
}
}
5 changes: 5 additions & 0 deletions tests/lib/ignored-paths.js
Expand Up @@ -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", () => {
Expand Down

0 comments on commit aad0c79

Please sign in to comment.