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 ac7140b commit 8f8c788
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
37 changes: 23 additions & 14 deletions lib/ignored-paths.js
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
}
}

Expand Down
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 8f8c788

Please sign in to comment.