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
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 42cc83a commit e0a0368
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
30 changes: 18 additions & 12 deletions lib/ignored-paths.js
Expand Up @@ -48,31 +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 @@ -146,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 @@ -180,7 +185,8 @@ 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);

Expand All @@ -192,7 +198,7 @@ class IgnoredPaths {
}
}
} 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"]
}
}
9 changes: 8 additions & 1 deletion tests/lib/ignored-paths.js
Expand Up @@ -165,12 +165,19 @@ describe("IgnoredPaths", () => {
);
});

it("should load package.json's array of files to ignore in eslintIgnore", () => {
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"));
});
});

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

0 comments on commit e0a0368

Please sign in to comment.