Skip to content

Commit

Permalink
fix: handle absolute file paths in FlatRuleTester (#18064)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Feb 1, 2024
1 parent f4a1fe2 commit 2196d97
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rule-tester/flat-rule-tester.js
Expand Up @@ -13,6 +13,7 @@
const
assert = require("assert"),
util = require("util"),
path = require("path"),
equal = require("fast-deep-equal"),
Traverser = require("../shared/traverser"),
{ getRuleOptionsSchema } = require("../config/flat-config-helpers"),
Expand Down Expand Up @@ -592,7 +593,15 @@ class FlatRuleTester {
* @private
*/
function runRuleForItem(item) {
const configs = new FlatConfigArray(testerConfig, { baseConfig });
const flatConfigArrayOptions = {
baseConfig
};

if (item.filename) {
flatConfigArrayOptions.basePath = path.parse(item.filename).root;
}

const configs = new FlatConfigArray(testerConfig, flatConfigArrayOptions);

/*
* Modify the returned config so that the parser is wrapped to catch
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rule-tester/flat-rule-tester.js
Expand Up @@ -1808,6 +1808,22 @@ describe("FlatRuleTester", () => {
}, /Fixable rules must set the `meta\.fixable` property/u);
});

// https://github.com/eslint/eslint/issues/17962
it("should not throw an error in case of absolute paths", () => {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{
code: "eval(foo)",
filename: "/an-absolute-path/foo.js",
errors: [{ message: "eval sucks.", type: "CallExpression" }]
}
]
});
});

describe("suggestions", () => {
it("should pass with valid suggestions (tested using desc)", () => {
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/suggestions").basic, {
Expand Down

0 comments on commit 2196d97

Please sign in to comment.