Skip to content

Commit

Permalink
fix: correctly iterate files matched by glob patterns (#16831)
Browse files Browse the repository at this point in the history
* fix: correctly iterate files matched by glob patterns

* test: add a test case for #14742

* test: improve test case

* chore: remove .only

* chore: revert unwanted changes

* test: fix root path

* chore: fix lint

* fix: correctly iterate files matched by glob patterns for new config system

* test: add case for flat config

* chore: remove .only
  • Loading branch information
snitin315 committed Feb 28, 2023
1 parent 89d9844 commit 92c1943
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 6 deletions.
10 changes: 6 additions & 4 deletions lib/cli-engine/file-enumerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class FileEnumerator {
return this._iterateFilesWithFile(absolutePath);
}
if (globInputPaths && isGlobPattern(pattern)) {
return this._iterateFilesWithGlob(absolutePath, isDot);
return this._iterateFilesWithGlob(pattern, isDot);
}

return [];
Expand Down Expand Up @@ -398,15 +398,17 @@ class FileEnumerator {
_iterateFilesWithGlob(pattern, dotfiles) {
debug(`Glob: ${pattern}`);

const directoryPath = path.resolve(getGlobParent(pattern));
const globPart = pattern.slice(directoryPath.length + 1);
const { cwd } = internalSlotsMap.get(this);
const directoryPath = path.resolve(cwd, getGlobParent(pattern));
const absolutePath = path.resolve(cwd, pattern);
const globPart = absolutePath.slice(directoryPath.length + 1);

/*
* recursive if there are `**` or path separators in the glob part.
* Otherwise, patterns such as `src/*.js`, it doesn't need recursive.
*/
const recursive = /\*\*|\/|\\/u.test(globPart);
const selector = new Minimatch(pattern, minimatchOpts);
const selector = new Minimatch(absolutePath, minimatchOpts);

debug(`recursive? ${recursive}`);

Expand Down
4 changes: 2 additions & 2 deletions lib/eslint/eslint-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,9 @@ async function findFiles({
}

// save patterns for later use based on whether globs are enabled
if (globInputPaths && isGlobPattern(filePath)) {
if (globInputPaths && isGlobPattern(pattern)) {

const basePath = globParent(filePath);
const basePath = path.resolve(cwd, globParent(pattern));

// group in cwd if possible and split out others
if (isPathInside(basePath, cwd)) {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/{curly-path}/client/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { rules: { "no-console": "off" } };
1 change: 1 addition & 0 deletions tests/fixtures/{curly-path}/client/src/one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("one");
1 change: 1 addition & 0 deletions tests/fixtures/{curly-path}/server/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { rules: { "no-console": "warn" } };
1 change: 1 addition & 0 deletions tests/fixtures/{curly-path}/server/src/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("two");
68 changes: 68 additions & 0 deletions tests/lib/cli-engine/file-enumerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const path = require("path");
const os = require("os");
const { assert } = require("chai");
const sh = require("shelljs");
const sinon = require("sinon");
const {
Legacy: {
CascadingConfigArrayFactory
Expand Down Expand Up @@ -182,6 +183,73 @@ describe("FileEnumerator", () => {
});
});

// https://github.com/eslint/eslint/issues/14742
describe("with 5 directories ('{lib}', '{lib}/client', '{lib}/client/src', '{lib}/server', '{lib}/server/src') that contains two files '{lib}/client/src/one.js' and '{lib}/server/src/two.js'", () => {
const root = path.join(os.tmpdir(), "eslint/file-enumerator");
const files = {
"{lib}/client/src/one.js": "console.log('one.js');",
"{lib}/server/src/two.js": "console.log('two.js');",
"{lib}/client/.eslintrc.json": JSON.stringify({
rules: {
"no-console": "error"
},
env: {
mocha: true
}
}),
"{lib}/server/.eslintrc.json": JSON.stringify({
rules: {
"no-console": "off"
},
env: {
mocha: true
}
})
};
const { prepare, cleanup, getPath } = createCustomTeardown({
cwd: root,
files
});

/** @type {FileEnumerator} */
let enumerator;

beforeEach(async () => {
await prepare();
enumerator = new FileEnumerator({
cwd: path.resolve(getPath("{lib}/server"))
});
});

afterEach(cleanup);

describe("when running eslint in the server directory", () => {
it("should use the config '{lib}/server/.eslintrc.json' for '{lib}/server/src/two.js'.", () => {
const spy = sinon.spy(fs, "readdirSync");

const list = [
...enumerator.iterateFiles(["src/**/*.{js,json}"])
];

// should enter the directory '{lib}/server/src' directly
assert.strictEqual(spy.getCall(0).firstArg, path.join(root, "{lib}/server/src"));
assert.strictEqual(list.length, 1);
assert.strictEqual(list[0].config.length, 2);
assert.strictEqual(list[0].config[0].name, "DefaultIgnorePattern");
assert.strictEqual(list[0].config[1].filePath, getPath("{lib}/server/.eslintrc.json"));
assert.deepStrictEqual(
list.map(entry => entry.filePath),
[
path.join(root, "{lib}/server/src/two.js")
]
);

// destroy the spy
sinon.restore();
});
});
});

// This group moved from 'tests/lib/util/glob-utils.js' when refactoring to keep the cumulated test cases.
describe("with 'tests/fixtures/glob-utils' files", () => {
let fixtureDir;
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,23 @@ describe("FlatESLint", () => {
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

// https://github.com/eslint/eslint/issues/14742
it("should run", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("{curly-path}", "server")
});
const results = await eslint.lintFiles(["src/**/*.{js,json}"]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].messages.length, 1);
assert.strictEqual(results[0].messages[0].ruleId, "no-console");
assert.strictEqual(
results[0].filePath,
getFixturePath("{curly-path}/server/src/two.js")
);
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

// https://github.com/eslint/eslint/issues/16265
describe("Dot files in searches", () => {

Expand Down

0 comments on commit 92c1943

Please sign in to comment.