Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: glob processing (fixes #11940) #11986

Merged
merged 3 commits into from Jul 21, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -289,31 +289,32 @@ class FileEnumerator {
* @param {string} pattern The glob pattern to iterate files.
* @returns {IterableIterator<FileEntry>} The found files.
*/
_iterateFiles(pattern) {
*_iterateFiles(pattern) {
const { cwd, globInputPaths } = internalSlotsMap.get(this);
const absolutePath = path.resolve(cwd, pattern);
const isDot = dotfilesPattern.test(pattern);

if (globInputPaths && isGlobPattern(pattern)) {
return this._iterateFilesWithGlob(
absolutePath,
dotfilesPattern.test(pattern)
);
let found = false;

for (const entry of this._iterateFilesWithGlob(absolutePath, isDot)) {
found = true;
yield entry;
}

// If the glob didn't match any files, fall it through as a filename.
if (found) {
return;
}
}

const stat = statSafeSync(absolutePath);

if (stat && stat.isDirectory()) {
return this._iterateFilesWithDirectory(
absolutePath,
dotfilesPattern.test(pattern)
);
}

if (stat && stat.isFile()) {
return this._iterateFilesWithFile(absolutePath);
yield* this._iterateFilesWithDirectory(absolutePath, isDot);
} else if (stat && stat.isFile()) {
yield* this._iterateFilesWithFile(absolutePath);
}

return [];
}

/**
@@ -3454,6 +3454,46 @@ describe("CLIEngine", () => {
assert.strictEqual(report.results[0].messages[0].message, "ok");
});
});

describe("glob pattern '[ab].js'", () => {
const root = getFixturePath("cli-engine/unmatched-glob");

it("should match 'a.js' and 'b.js', but not '[ab].js'.", () => {
CLIEngine = defineCLIEngineWithInMemoryFileSystem({
cwd: () => root,
files: {
"a.js": "",
"b.js": "",
"ab.js": "",
"[ab].js": "",
".eslintrc.yml": "root: true"
}
}).CLIEngine;
engine = new CLIEngine();

const { results } = engine.executeOnFiles(["[ab].js"]);
const filenames = results.map(r => path.basename(r.filePath));

assert.deepStrictEqual(filenames, ["a.js", "b.js"]);
This conversation was marked as resolved by mysticatea

This comment has been minimized.

Copy link
@aladdin-add

aladdin-add Jul 14, 2019

Member

what if the user want to match "[ab].js" in this case?

just my 2c. :-) it seems more straightforward to match like following steps:

  1. '[ab].js' as a filename
  2. '[ab].js' as a glob pattern (if the file not exist).

This comment has been minimized.

Copy link
@mysticatea

mysticatea Jul 16, 2019

Author Member

At first, I thought so, but it's different from what shell does.

root@387125dc75aa:~# ls
[ab].js  a.js  b.js
root@387125dc75aa:~# echo [ab].js
a.js b.js

I think that it's better if the behavior is similar to popular tools.

This comment has been minimized.

Copy link
@aladdin-add

aladdin-add Jul 16, 2019

Member

the question is the user cannot lint "[ab].js", even quoting eslint "[ab].js".

I agreed we should be as close to popular tools as possible, but probably exceptions can be made here.

This comment has been minimized.

Copy link
@mysticatea

mysticatea Jul 16, 2019

Author Member

Yes. It's the problem that ESLint cannot distinguish eslint "foo" and eslint foo.

I'm considering that Windows should have the same behavior as Linux about eslint [ab].js. Hmm.

});

it("should match '[ab].js' if both 'a.js' and 'b.js' didn't exist.", () => {
CLIEngine = defineCLIEngineWithInMemoryFileSystem({
cwd: () => root,
files: {
"ab.js": "",
"[ab].js": "",
".eslintrc.yml": "root: true"
}
}).CLIEngine;
engine = new CLIEngine();

const { results } = engine.executeOnFiles(["[ab].js"]);
const filenames = results.map(r => path.basename(r.filePath));

assert.deepStrictEqual(filenames, ["[ab].js"]);
});
});
});

describe("getConfigForFile", () => {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.