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: Use cwd constructor option as config basePath in Linter #17705

Merged
merged 1 commit into from Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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