8000 fix: Ensure globbing doesn't include subdirectories (#16272) · eslint/eslint@c6900f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c6900f8

Browse files
nzakasjugalthakkarmdjermanovic
authored
fix: Ensure globbing doesn't include subdirectories (#16272)
* fix: Ensure globbing doesn't include subdirectories Fixes #16260 * docs: copy & use main package version in docs on release (#16252) * docs: update docs package ver from main on release fixes #16212 * docs: read docs package ver instead of main fixes #16212 * docs: add newline to updated docs package json Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * docs: update docs package json version Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * chore: enable linting `.eleventy.js` again (#16274) * Fix filtering of globs * Enable partial matching of globs Co-authored-by: Jugal Thakkar <2640821+jugalthakkar@users.noreply.github.com> Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent e098b5f commit c6900f8

File tree

7 files changed

+101
-4
lines changed

7 files changed

+101
-4
lines changed

lib/eslint/eslint-helpers.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const fsp = fs.promises;
1515
const isGlob = require("is-glob");
1616
const globby = require("globby");
1717
const hash = require("../cli-engine/hash");
18+
const minimatch = require("minimatch");
1819

1920
//-----------------------------------------------------------------------------
2021
// Errors
@@ -126,7 +127,7 @@ async function findFiles({
126127
const filePaths = patterns.map(filePath => path.resolve(cwd, filePath));
127128
const stats = await Promise.all(
128129
filePaths.map(
129-
filePath => fsp.stat(filePath).catch(() => {})
130+
filePath => fsp.stat(filePath).catch(() => { })
130131
)
131132
);
132133

@@ -157,6 +158,11 @@ async function findFiles({
157158
return false;
158159
}
159160

161+
// patterns starting with ** always apply
162+
if (filePattern.startsWith("**")) {
163+
return true;
164+
}
165+
160166
// patterns ending with * are not used for file search
161167
if (filePattern.endsWith("*")) {
162168
return false;
@@ -167,11 +173,27 @@ async function findFiles({
167173
return false;
168174
}
169175

170-
// check if the pattern would be inside the cwd or not
176+
// check if the pattern would be inside the config base path or not
171177
const fullFilePattern = path.join(cwd, filePattern);
172-
const relativeFilePattern = path.relative(configs.basePath, fullFilePattern);
178+
const patternRelativeToConfigBasePath = path.relative(configs.basePath, fullFilePattern);
179+
180+
if (patternRelativeToConfigBasePath.startsWith("..")) {
181+
return false;
182+
}
183+
184+
// check if the pattern matches
185+
if (minimatch(filePath, path.dirname(fullFilePattern), { partial: true })) {
186+
return true;
187+
}
188+
189+
// check if the pattern is inside the directory or not
190+
const patternRelativeToFilePath = path.relative(filePath, fullFilePattern);
191+
192+
if (patternRelativeToFilePath.startsWith("..")) {
193+
return false;
194+
}
173195

174-
return !relativeFilePattern.startsWith("..");
196+
return true;
175197
})
176198
.map(filePattern => {
177199
if (filePattern.startsWith("**")) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = [
2+
{
3+
files: ["subdir/*.js"]
4+
}
5+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = /* intentional syntax error */
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function( {} // intentional syntax error
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = true;

tests/lib/eslint/flat-eslint.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,72 @@ describe("FlatESLint", () => {
784784
assert.strictEqual(results[0].suppressedMessages.length, 0);
785785
});
786786

787+
// https://github.com/eslint/eslint/issues/16260
788+
describe("Globbing based on configs", () => {
789+
it("should report zero messages when given a directory with a .js and config file specifying a subdirectory", async () => {
790+
eslint = new FlatESLint({
791+
ignore: false,
792+
cwd: getFixturePath("shallow-glob")
793+
});
794+
const results = await eslint.lintFiles(["target-dir"]);
795+
796+
assert.strictEqual(results.length, 1);
797+
assert.strictEqual(results[0].messages.length, 0);
798+
assert.strictEqual(results[0].suppressedMessages.length, 0);
799+
});
800+
801+
it("should glob for .jsx file in a subdirectory of the passed-in directory and not glob for any other patterns", async () => {
802+
eslint = new FlatESLint({
803+
ignore: false,
804+
overrideConfigFile: true,
805+
overrideConfig: {
806+
files: ["subdir/**/*.jsx", "target-dir/*.js"],
807+
languageOptions: {
808+
parserOptions: {
809+
jsx: true
810+
}
811+
}
812+
},
813+
cwd: getFixturePath("shallow-glob")
814+
});
815+
const results = await eslint.lintFiles(["subdir/subsubdir"]);
816+
817+
assert.strictEqual(results.length, 2);
818+
assert.strictEqual(results[0].messages.length, 1);
819+
assert(results[0].messages[0].fatal, "Fatal error expected.");
820+
assert.strictEqual(results[0].suppressedMessages.length, 0);
821+
assert.strictEqual(results[1].messages.length, 0);
822+
assert.strictEqual(results[1].suppressedMessages.length, 0);
823+
});
824+
825+
it("should glob for all files in subdir when passed-in on the command line with a partial matching glob", async () => {
826+
eslint = new FlatESLint({
827+
ignore: false,
828+
overrideConfigFile: true,
829+
overrideConfig: {
830+
files: ["s*/subsubdir/*.jsx", "target-dir/*.js"],
831+
languageOptions: {
832+
parserOptions: {
833+
jsx: true
834+
}
835+
}
836+
},
837+
cwd: getFixturePath("shallow-glob")
838+
});
839+
const results = await eslint.lintFiles(["subdir"]);
840+
841+
assert.strictEqual(results.length, 3);
842+
assert.strictEqual(results[0].messages.length, 1);
843+
assert(results[0].messages[0].fatal, "Fatal error expected.");
844+
assert.strictEqual(results[0].suppressedMessages.length, 0);
845+
assert.strictEqual(results[1].messages.length, 1);
846+
assert(results[0].messages[0].fatal, "Fatal error expected.");
847+
assert.strictEqual(results[1].suppressedMessages.length, 0);
848+
assert.strictEqual(results[2].messages.length, 0);
849+
assert.strictEqual(results[2].suppressedMessages.length, 0);
850+
});
851+
});
852+
787853
it("should report zero messages when given a '**' pattern with a .js and a .js2 file", async () => {
788854
eslint = new FlatESLint({
789855
ignore: false,

0 commit comments

Comments
 (0)
0