8000 fix: allow `RuleTester` to test files inside `node_modules/` (#19499) · eslint/eslint@f4e9c5f · GitHub
[go: up one dir, main page]

Skip to content

Commit f4e9c5f

Browse files
fiskermdjermanovic
andauthored
fix: allow RuleTester to test files inside node_modules/ (#19499)
* test: add failed tests * fix: fix issue * style: linting * Apply suggestions from code review Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> --------- Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent 9c5c6ee commit f4e9c5f

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

lib/config/default-config.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ const Rules = require("../rules");
1515
// Helpers
1616
//-----------------------------------------------------------------------------
1717

18+
const sharedDefaultConfig = [
19+
20+
// intentionally empty config to ensure these files are globbed by default
21+
{
22+
files: ["**/*.js", "**/*.mjs"]
23+
},
24+
{
25+
files: ["**/*.cjs"],
26+
languageOptions: {
27+
sourceType: "commonjs",
28+
ecmaVersion: "latest"
29+
}
30+
}
31+
];
32+
1833
exports.defaultConfig = Object.freeze([
1934
{
2035
plugins: {
@@ -55,15 +70,11 @@ exports.defaultConfig = Object.freeze([
5570
]
5671
},
5772

58-
// intentionally empty config to ensure these files are globbed by default
59-
{
60-
files: ["**/*.js", "**/*.mjs"]
61-
},
62-
{
63-
files: ["**/*.cjs"],
64-
languageOptions: {
65-
sourceType: "commonjs",
66-
ecmaVersion: "latest"
67-
}
68-
}
73+
...sharedDefaultConfig
74+
]);
75+
76+
exports.defaultRuleTesterConfig = Object.freeze([
77+
{ files: ["**"] }, // Make sure the default config matches for all files
78+
79+
...sharedDefaultConfig
6980
]);

lib/rule-tester/rule-tester.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const
2222
stringify = require("json-stable-stringify-without-jsonify");
2323

2424
const { FlatConfigArray } = require("../config/flat-config-array");
25-
const { defaultConfig } = require("../config/default-config");
25+
const { defaultConfig, defaultRuleTesterConfig } = require("../config/default-config");
2626

2727
const ajv = require("../shared/ajv")({ strictDefaults: true });
2828

@@ -577,7 +577,6 @@ class RuleTester {
577577
}
578578

579579
const baseConfig = [
580-
{ files: ["**"] }, // Make sure the default config matches for all files
581580
{
582581
plugins: {
583582

@@ -622,7 +621,7 @@ class RuleTester {
622621
},
623622
language: defaultConfig[0].language
624623
},
625-
...defaultConfig.slice(1)
624+
...defaultRuleTesterConfig
626625
];
627626

628627
/**

tests/lib/rule-tester/rule-tester.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,24 +2211,34 @@ describe("RuleTester", () => {
22112211
}, /Fixable rules must set the `meta\.fixable` property/u);
22122212
});
22132213

2214-
// https://github.com/eslint/eslint/issues/17962
2215-
it("should not throw an error in case of absolute paths", () => {
2214+
it("should allow testing of any file", () => {
2215+
const filenames = [
2216+
2217+
/*
2218+
* Ignored by default
2219+
* https://github.com/eslint/eslint/issues/19471
2220+
*/
2221+
"node_modules/foo.js",
2222+
".git/foo.js",
2223+
2224+
/*
2225+
* Absolute paths
2226+
* https://github.com/eslint/eslint/issues/17962
2227+
*/
2228+
"/an-absolute-path/foo.js",
2229+
"C:\\an-absolute-path\\foo.js"
2230+
];
2231+
22162232
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
2217-
valid: [
2218-
"Eval(foo)"
2219-
],
2220-
invalid: [
2221-
{
2222-
code: "eval(foo)",
2223-
filename: "/an-absolute-path/foo.js",
2224-
errors: [{ message: "eval sucks.", type: "CallExpression" }]
2225-
},
2226-
{
2227-
code: "eval(bar)",
2228-
filename: "C:\\an-absolute-path\\foo.js",
2229-
errors: [{ message: "eval sucks.", type: "CallExpression" }]
2230-
}
2231-
]
2233+
valid: filenames.map((filename, index) => ({
2234+
code: `Eval(foo${index})`,
2235+
filename
2236+
})),
2237+
invalid: filenames.map((filename, index) => ({
2238+
code: `eval(foo${index})`,
2239+
errors: [{ message: "eval sucks.", type: "CallExpression" }],
2240+
filename
2241+
}))
22322242
});
22332243
});
22342244

0 commit comments

Comments
 (0)
0