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: Ensure shared references in rule configs are separated. #17666

Merged
merged 6 commits into from Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
91 changes: 55 additions & 36 deletions lib/config/flat-config-schema.js
Expand Up @@ -5,6 +5,16 @@

"use strict";

//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

/*
* Note: This can be removed in ESLint v9 because structuredClone is available globally
* starting in Node.js v17.
*/
const structuredClone = require("@ungap/structured-clone").default;

//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -119,7 +129,7 @@ function normalizeRuleOptions(ruleOptions) {
: [ruleOptions];

finalOptions[0] = ruleSeverities.get(finalOptions[0]);
return finalOptions;
return finalOptions.map(value => structuredClone(value));
nzakas marked this conversation as resolved.
Show resolved Hide resolved
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -378,48 +388,57 @@ const rulesSchema = {
...second
};

for (const ruleId of Object.keys(result)) {

// avoid hairy edge case
if (ruleId === "__proto__") {

/* eslint-disable-next-line no-proto -- Though deprecated, may still be present */
delete result.__proto__;
continue;
}

result[ruleId] = normalizeRuleOptions(result[ruleId]);

/*
* If either rule config is missing, then the correct
* config is already present and we just need to normalize
* the severity.
*/
if (!(ruleId in first) || !(ruleId in second)) {
continue;
}

const firstRuleOptions = normalizeRuleOptions(first[ruleId]);
const secondRuleOptions = normalizeRuleOptions(second[ruleId]);
for (const ruleId of Object.keys(result)) {

/*
* If the second rule config only has a severity (length of 1),
* then use that severity and keep the rest of the options from
* the first rule config.
*/
if (secondRuleOptions.length === 1) {
result[ruleId] = [secondRuleOptions[0], ...firstRuleOptions.slice(1)];
continue;
try {

// avoid hairy edge case
if (ruleId === "__proto__") {

/* eslint-disable-next-line no-proto -- Though deprecated, may still be present */
delete result.__proto__;
continue;
}

result[ruleId] = normalizeRuleOptions(result[ruleId]);

/*
* If either rule config is missing, then the correct
* config is already present and we just need to normalize
* the severity.
*/
if (!(ruleId in first) || !(ruleId in second)) {
continue;
}

const firstRuleOptions = normalizeRuleOptions(first[ruleId]);
const secondRuleOptions = normalizeRuleOptions(second[ruleId]);

/*
* If the second rule config only has a severity (length of 1),
* then use that severity and keep the rest of the options from
* the first rule config.
*/
if (secondRuleOptions.length === 1) {
result[ruleId] = [secondRuleOptions[0], ...firstRuleOptions.slice(1)];
continue;
}

/*
* In any other situation, then the second rule config takes
* precedence. That means the value at `result[ruleId]` is
* already correct and no further work is necessary.
*/
} catch (ex) {
throw new Error(`Key "${ruleId}": ${ex.message}`, { cause: ex });
}

/*
* In any other situation, then the second rule config takes
* precedence. That means the value at `result[ruleId]` is
* already correct and no further work is necessary.
*/
}

return result;


},

validate(value) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -64,9 +64,10 @@
"@eslint-community/regexpp": "^4.6.1",
"@eslint/eslintrc": "^2.1.2",
"@eslint/js": "8.51.0",
"@humanwhocodes/config-array": "^0.11.11",
"@humanwhocodes/config-array": "^0.11.12",
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
"@humanwhocodes/module-importer": "^1.0.1",
"@nodelib/fs.walk": "^1.2.8",
"@ungap/structured-clone": "^1.2.0",
"ajv": "^6.12.4",
"chalk": "^4.0.0",
"cross-spawn": "^7.0.2",
Expand Down
61 changes: 61 additions & 0 deletions tests/lib/config/flat-config-array.js
Expand Up @@ -1987,4 +1987,65 @@ describe("FlatConfigArray", () => {
});

});

// https://github.com/eslint/eslint/issues/12592
describe("Shared references between rule configs", () => {

it("shared rule config should not cause a rule validation error", () => {

const ruleConfig = ["error", {}];

const configs = new FlatConfigArray([{
rules: {
camelcase: ruleConfig,
"default-case": ruleConfig
}
}]);

configs.normalizeSync();

const config = configs.getConfig("foo.js");

assert.deepStrictEqual(config.rules, {
camelcase: [2, {
ignoreDestructuring: false,
ignoreGlobals: false,
ignoreImports: false
}],
"default-case": [2, {}]
});

});


it("should throw rule validation error for camelcase", async () => {

const ruleConfig = ["error", {}];

const configs = new FlatConfigArray([{
rules: {
camelcase: ruleConfig,
"default-case": ruleConfig,

/* eslint-disable-next-line no-dupe-keys -- needed for testing */
camelcase: [
"error",
{
ignoreDestructuring: Date
}

]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
camelcase: ruleConfig,
"default-case": ruleConfig,
/* eslint-disable-next-line no-dupe-keys -- needed for testing */
camelcase: [
"error",
{
ignoreDestructuring: Date
}
]
"default-case": ruleConfig,
camelcase: [
"error",
{
ignoreDestructuring: Date
}
]

I believe duplicate keys don't affect this testing, the previous one is just overwritten in the created object. Perhaps the configuration was intended to have two config objects? (though the test might be even better with one to make sure we're always cloning rule configs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this from the tests that were originally submitted for fixing this bug. Adding a second config would make sense. 👍

}
}]);

configs.normalizeSync();

// exact error may differ based on structuredClone implementation so just test prefix
assert.throws(() => {
configs.getConfig("foo.js");
}, /Key "rules": Key "camelcase":/u);

});

});
});