Skip to content

Commit

Permalink
fix: Use cwd constructor option as config basePath in Linter (#17705
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mdjermanovic committed Nov 7, 2023
1 parent d245326 commit 3cbeaad
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/linter/linter.js
Expand Up @@ -1422,7 +1422,7 @@ class Linter {
verify(textOrSourceCode, config, filenameOrOptions) {
debug("Verify");

const { configType } = internalSlotsMap.get(this);
const { configType, cwd } = internalSlotsMap.get(this);

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

if (!Array.isArray(config) || typeof config.getConfig !== "function") {
configArray = new FlatConfigArray(config);
configArray = new FlatConfigArray(config, { basePath: cwd });
configArray.normalizeSync();
}

Expand Down
115 changes: 113 additions & 2 deletions tests/lib/linter/linter.js
Expand Up @@ -9831,6 +9831,117 @@ describe("Linter with FlatConfigArray", () => {
});
});

// https://github.com/eslint/eslint/issues/17669
it("should use `cwd` constructor option as config `basePath` when config is not an instance of FlatConfigArray", () => {
const rule = {
create(context) {
return {
Program(node) {
context.report({ node, message: "Bad program." });
}
};
}
};

const code = "foo";
const config = [
{
plugins: {
test: {
rules: {
"test-rule-1": rule,
"test-rule-2": rule,
"test-rule-3": rule
}
}
}
},
{
rules: {
"test/test-rule-1": 2
}
},
{
files: ["**/*.ts"],
rules: {
"test/test-rule-2": 2
}
},
{
files: ["bar/file.ts"],
rules: {
"test/test-rule-3": 2
}
}
];

const linterWithOptions = new Linter({
configType: "flat",
cwd: "/foo"
});

let messages;

messages = linterWithOptions.verify(code, config, "/file.js");
assert.strictEqual(messages.length, 1);
assert.deepStrictEqual(messages[0], {
ruleId: null,
severity: 1,
message: "No matching configuration found for /file.js.",
line: 0,
column: 0,
nodeType: null
});

messages = linterWithOptions.verify(code, config, "/file.ts");
assert.strictEqual(messages.length, 1);
assert.deepStrictEqual(messages[0], {
ruleId: null,
severity: 1,
message: "No matching configuration found for /file.ts.",
line: 0,
column: 0,
nodeType: null
});

messages = linterWithOptions.verify(code, config, "/bar/foo/file.js");
assert.strictEqual(messages.length, 1);
assert.deepStrictEqual(messages[0], {
ruleId: null,
severity: 1,
message: "No matching configuration found for /bar/foo/file.js.",
line: 0,
column: 0,
nodeType: null
});

messages = linterWithOptions.verify(code, config, "/bar/foo/file.ts");
assert.strictEqual(messages.length, 1);
assert.deepStrictEqual(messages[0], {
ruleId: null,
severity: 1,
message: "No matching configuration found for /bar/foo/file.ts.",
line: 0,
column: 0,
nodeType: null
});

messages = linterWithOptions.verify(code, config, "/foo/file.js");
assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].ruleId, "test/test-rule-1");

messages = linterWithOptions.verify(code, config, "/foo/file.ts");
assert.strictEqual(messages.length, 2);
assert.strictEqual(messages[0].ruleId, "test/test-rule-1");
assert.strictEqual(messages[1].ruleId, "test/test-rule-2");

messages = linterWithOptions.verify(code, config, "/foo/bar/file.ts");
assert.strictEqual(messages.length, 3);
assert.strictEqual(messages[0].ruleId, "test/test-rule-1");
assert.strictEqual(messages[1].ruleId, "test/test-rule-2");
assert.strictEqual(messages[2].ruleId, "test/test-rule-3");
});

describe("Plugins", () => {

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

linterWithOption.verify(code, config);
linterWithOption.verify(code, config, `${cwd}/file.js`);
assert(spy && spy.calledOnce);
});

Expand Down Expand Up @@ -12059,7 +12170,7 @@ describe("Linter with FlatConfigArray", () => {
...baseConfig
};

linterWithOption.verify(code, config);
linterWithOption.verify(code, config, `${cwd}/file.js`);
assert(spy && spy.calledOnce);
});

Expand Down

0 comments on commit 3cbeaad

Please sign in to comment.