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
Added tests for when eslintignore file exists and doesn't exist
Abstracted the findFile function for finding ignore file and
package.json
  • Loading branch information
VictorHom committed Jun 7, 2017
1 parent 21c73bf commit 3993289
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
3 changes: 1 addition & 2 deletions conf/config-schema.json
Expand Up @@ -9,8 +9,7 @@
"settings": { "type": "object" },
"extends": { "type": ["string", "array"] },
"rules": { "type": "object" },
"parserOptions": { "type": "object" },
"eslintIgnore": { "type": ["array"] }
"parserOptions": { "type": "object" }
},
"additionalProperties": false
}
50 changes: 26 additions & 24 deletions lib/ignored-paths.js
Expand Up @@ -12,16 +12,10 @@
const fs = require("fs"),
path = require("path"),
ignore = require("ignore"),
pathUtil = require("./util/path-util"),
config = require("./config/config-file"),
Linter = require("../lib/linter"),
Config = require("../lib/config");

const configContext = new Config({}, new Linter());
pathUtil = require("./util/path-util");

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


//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
Expand All @@ -43,34 +37,40 @@ 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) {
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);
}

/**
Expand Down Expand Up @@ -144,7 +144,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");
Expand Down Expand Up @@ -178,18 +178,20 @@ class IgnoredPaths {
try {

// if the ignoreFile does not exist, check package.json for eslintIgnore
packageJSONPath = findPackageJSONFile(options.cwd);
if (packageJSONPath) {
const configOptions = config.load(packageJSONPath, configContext);
const packageJSONPath = findPackageJSONFile(options.cwd);

if (configOptions.eslintIgnore) {
const filesToIgnore = configOptions.eslintIgnore;
if (packageJSONPath) {
const packageJSONOptions = JSON.parse(fs.readFileSync(packageJSONPath, "utf8"));

debug(filesToIgnore);
if (packageJSONOptions.eslintIgnore) {
packageJSONOptions.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");
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/ignored-paths/package-json-ignore/package.json
Expand Up @@ -5,7 +5,7 @@
"env": {
"browser": true,
"node": true
},
"eslintIgnore": ["hello.js", "world.js"]
}
}
}
},
"eslintIgnore": ["hello.js", "world.js"]
}
11 changes: 11 additions & 0 deletions 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"]
}
13 changes: 11 additions & 2 deletions tests/lib/ignored-paths.js
Expand Up @@ -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"));
});
});

Expand Down

0 comments on commit 3993289

Please sign in to comment.