Skip to content

Commit 3cbeaad

Browse files
authored
fix: Use cwd constructor option as config basePath in Linter (#17705)
1 parent d245326 commit 3cbeaad

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed

lib/linter/linter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ class Linter {
14221422
verify(textOrSourceCode, config, filenameOrOptions) {
14231423
debug("Verify");
14241424

1425-
const { configType } = internalSlotsMap.get(this);
1425+
const { configType, cwd } = internalSlotsMap.get(this);
14261426

14271427
const options = typeof filenameOrOptions === "string"
14281428
? { filename: filenameOrOptions }
@@ -1441,7 +1441,7 @@ class Linter {
14411441
let configArray = config;
14421442

14431443
if (!Array.isArray(config) || typeof config.getConfig !== "function") {
1444-
configArray = new FlatConfigArray(config);
1444+
configArray = new FlatConfigArray(config, { basePath: cwd });
14451445
configArray.normalizeSync();
14461446
}
14471447

tests/lib/linter/linter.js

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9831,6 +9831,117 @@ describe("Linter with FlatConfigArray", () => {
98319831
});
98329832
});
98339833

9834+
// https://github.com/eslint/eslint/issues/17669
9835+
it("should use `cwd` constructor option as config `basePath` when config is not an instance of FlatConfigArray", () => {
9836+
const rule = {
9837+
create(context) {
9838+
return {
9839+
Program(node) {
9840+
context.report({ node, message: "Bad program." });
9841+
}
9842+
};
9843+
}
9844+
};
9845+
9846+
const code = "foo";
9847+
const config = [
9848+
{
9849+
plugins: {
9850+
test: {
9851+
rules: {
9852+
"test-rule-1": rule,
9853+
"test-rule-2": rule,
9854+
"test-rule-3": rule
9855+
}
9856+
}
9857+
}
9858+
},
9859+
{
9860+
rules: {
9861+
"test/test-rule-1": 2
9862+
}
9863+
},
9864+
{
9865+
files: ["**/*.ts"],
9866+
rules: {
9867+
"test/test-rule-2": 2
9868+
}
9869+
},
9870+
{
9871+
files: ["bar/file.ts"],
9872+
rules: {
9873+
"test/test-rule-3": 2
9874+
}
9875+
}
9876+
];
9877+
9878+
const linterWithOptions = new Linter({
9879+
configType: "flat",
9880+
cwd: "/foo"
9881+
});
9882+
9883+
let messages;
9884+
9885+
messages = linterWithOptions.verify(code, config, "/file.js");
9886+
assert.strictEqual(messages.length, 1);
9887+
assert.deepStrictEqual(messages[0], {
9888+
ruleId: null,
9889+
severity: 1,
9890+
message: "No matching configuration found for /file.js.",
9891+
line: 0,
9892+
column: 0,
9893+
nodeType: null
9894+
});
9895+
9896+
messages = linterWithOptions.verify(code, config, "/file.ts");
9897+
assert.strictEqual(messages.length, 1);
9898+
assert.deepStrictEqual(messages[0], {
9899+
ruleId: null,
9900+
severity: 1,
9901+
message: "No matching configuration found for /file.ts.",
9902+
line: 0,
9903+
column: 0,
9904+
nodeType: null
9905+
});
9906+
9907+
messages = linterWithOptions.verify(code, config, "/bar/foo/file.js");
9908+
assert.strictEqual(messages.length, 1);
9909+
assert.deepStrictEqual(messages[0], {
9910+
ruleId: null,
9911+
severity: 1,
9912+
message: "No matching configuration found for /bar/foo/file.js.",
9913+
line: 0,
9914+
column: 0,
9915+
nodeType: null
9916+
});
9917+
9918+
messages = linterWithOptions.verify(code, config, "/bar/foo/file.ts");
9919+
assert.strictEqual(messages.length, 1);
9920+
assert.deepStrictEqual(messages[0], {
9921+
ruleId: null,
9922+
severity: 1,
9923+
message: "No matching configuration found for /bar/foo/file.ts.",
9924+
line: 0,
9925+
column: 0,
9926+
nodeType: null
9927+
});
9928+
9929+
messages = linterWithOptions.verify(code, config, "/foo/file.js");
9930+
assert.strictEqual(messages.length, 1);
9931+
assert.strictEqual(messages[0].ruleId, "test/test-rule-1");
9932+
9933+
messages = linterWithOptions.verify(code, config, "/foo/file.ts");
9934+
assert.strictEqual(messages.length, 2);
9935+
assert.strictEqual(messages[0].ruleId, "test/test-rule-1");
9936+
assert.strictEqual(messages[1].ruleId, "test/test-rule-2");
9937+
9938+
messages = linterWithOptions.verify(code, config, "/foo/bar/file.ts");
9939+
assert.strictEqual(messages.length, 3);
9940+
assert.strictEqual(messages[0].ruleId, "test/test-rule-1");
9941+
assert.strictEqual(messages[1].ruleId, "test/test-rule-2");
9942+
assert.strictEqual(messages[2].ruleId, "test/test-rule-3");
9943+
});
9944+
98349945
describe("Plugins", () => {
98359946

98369947
it("should not load rule definition when rule isn't used", () => {
@@ -11976,7 +12087,7 @@ describe("Linter with FlatConfigArray", () => {
1197612087
...baseConfig
1197712088
};
1197812089

11979-
linterWithOption.verify(code, config);
12090+
linterWithOption.verify(code, config, `${cwd}/file.js`);
1198012091
assert(spy && spy.calledOnce);
1198112092
});
1198212093

@@ -12059,7 +12170,7 @@ describe("Linter with FlatConfigArray", () => {
1205912170
...baseConfig
1206012171
};
1206112172

12062-
linterWithOption.verify(code, config);
12173+
linterWithOption.verify(code, config, `${cwd}/file.js`);
1206312174
assert(spy && spy.calledOnce);
1206412175
});
1206512176

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy