Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
fix: isFileIgnored should call isDirectoryIgnored
Browse files Browse the repository at this point in the history
In order to determine if a file should be ignored, we also need to check
if the directory is ignored first.

To be more consistent with previous behavior, this also now returns
true from isDirectoryIgnored when a directory is outside of the
basepath.
  • Loading branch information
nzakas committed Oct 3, 2022
1 parent 0bd81f5 commit 270d359
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/config-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,20 @@ export class ConfigArray extends Array {

// next check to see if the file should be ignored

// check if this should be ignored due to its directory
if (this.isDirectoryIgnored(path.dirname(filePath))) {
debug(`Ignoring ${filePath} based on directory pattern`);

// cache and return result - finalConfig is undefined at this point
cache.set(filePath, finalConfig);
return finalConfig;
}

// TODO: Maybe move elsewhere?
const relativeFilePath = path.relative(this.basePath, filePath);

if (shouldIgnoreFilePath(this.ignores, filePath, relativeFilePath)) {
debug(`Ignoring ${filePath}`);
debug(`Ignoring ${filePath} based on file pattern`);

// cache and return result - finalConfig is undefined at this point
cache.set(filePath, finalConfig);
Expand Down Expand Up @@ -736,17 +745,17 @@ export class ConfigArray extends Array {
* while a pattern such as `/foo/**` is not considered to ignore the
* directory because it is matching files.
* @param {string} directoryPath The complete path of a directory to check.
* @returns {boolean} True if the directory is ignored, false if not.
* @returns {boolean} True if the directory is ignored, false if not. Will
* return true for any directory that is not inside of `basePath`.
* @throws {Error} When the `ConfigArray` is not normalized.
* @throws {Error} When `directoryPath` is outside of `basePath`.
*/
isDirectoryIgnored(directoryPath) {

assertNormalized(this);

const relativeDirectoryPath = path.relative(this.basePath, directoryPath) + "/";
if (relativeDirectoryPath.startsWith("..")) {
throw new Error(`Directory ${directoryPath} is outside of the basePath ${this.basePath}`);
return true;
}

// first check the cache
Expand Down
23 changes: 19 additions & 4 deletions tests/config-array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,23 @@ describe('ConfigArray', () => {
expect(configs.isFileIgnored(path.join(basePath, 'foo.test.js'))).to.be.false;
});

it('should return false when file is inside of ignored directory', () => {
configs = new ConfigArray([
{
ignores: ['ignoreme']
},
{
files: ['**/*.js']
}
], {
basePath
});

configs.normalizeSync();

expect(configs.isFileIgnored(path.join(basePath, 'ignoreme/foo.js'))).to.be.true;
});

});

describe("isDirectoryIgnored()", () => {
Expand Down Expand Up @@ -1059,7 +1076,7 @@ describe('ConfigArray', () => {
}).throws(/normalized/);
});

it("should throw an error when the directory is outside of the basePath", () => {
it("should return true when the directory is outside of the basePath", () => {
configs = new ConfigArray([
{
ignores: ['foo/bar']
Expand All @@ -1070,9 +1087,7 @@ describe('ConfigArray', () => {

configs.normalizeSync();

expect(() => {
configs.isDirectoryIgnored("/usr/fake/foo/bar");
}).throws(/outside/);
expect(configs.isDirectoryIgnored(path.resolve(basePath, '../foo/bar'))).to.be.true;
});

});
Expand Down

0 comments on commit 270d359

Please sign in to comment.