Skip to content

Commit

Permalink
fix: improve error message for invalid rule config (#18147)
Browse files Browse the repository at this point in the history
* fix: improve error message for invalid rule config

* refactor: update indentation & use more strict checks
  • Loading branch information
snitin315 committed Mar 1, 2024
1 parent c49ed63 commit e37153f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/config/rule-validator.js
Expand Up @@ -167,9 +167,22 @@ class RuleValidator {
validateRule(ruleOptions.slice(1));

if (validateRule.errors) {
throw new Error(`Key "rules": Key "${ruleId}": ${
throw new Error(`Key "rules": Key "${ruleId}":\n${
validateRule.errors.map(
error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`
error => {
if (
error.keyword === "additionalProperties" &&
error.schema === false &&
typeof error.parentSchema?.properties === "object" &&
typeof error.params?.additionalProperty === "string"
) {
const expectedProperties = Object.keys(error.parentSchema.properties).map(property => `"${property}"`);
return `\tValue ${JSON.stringify(error.data)} ${error.message}.\n\t\tUnexpected property "${error.params.additionalProperty}". Expected properties: ${expectedProperties.join(", ")}.\n`;
}
return `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`;
}
).join("")
}`);
}
Expand Down
110 changes: 110 additions & 0 deletions tests/lib/config/flat-config-array.js
Expand Up @@ -44,6 +44,81 @@ const baseConfig = {
baz: {

},
"prefer-const": {
meta: {
schema: [
{
type: "object",
properties: {
destructuring: { enum: ["any", "all"], default: "any" },
ignoreReadBeforeAssign: { type: "boolean", default: false }
},
additionalProperties: false
}
]
}
},
"prefer-destructuring": {
meta: {
schema: [
{
oneOf: [
{
type: "object",
properties: {
VariableDeclarator: {
type: "object",
properties: {
array: {
type: "boolean"
},
object: {
type: "boolean"
}
},
additionalProperties: false
},
AssignmentExpression: {
type: "object",
properties: {
array: {
type: "boolean"
},
object: {
type: "boolean"
}
},
additionalProperties: false
}
},
additionalProperties: false
},
{
type: "object",
properties: {
array: {
type: "boolean"
},
object: {
type: "boolean"
}
},
additionalProperties: false
}
]
},
{
type: "object",
properties: {
enforceForRenamedProperties: {
type: "boolean"
}
},
additionalProperties: false
}
]
}
},

// old-style
boom() {},
Expand Down Expand Up @@ -2157,6 +2232,41 @@ describe("FlatConfigArray", () => {
}
}));

it("should error show expected properties", async () => {

await assertInvalidConfig([
{
rules: {
"prefer-const": ["error", { destruct: true }]
}
}
], "Unexpected property \"destruct\". Expected properties: \"destructuring\", \"ignoreReadBeforeAssign\"");

await assertInvalidConfig([
{
rules: {
"prefer-destructuring": ["error", { obj: true }]
}
}
], "Unexpected property \"obj\". Expected properties: \"VariableDeclarator\", \"AssignmentExpression\"");

await assertInvalidConfig([
{
rules: {
"prefer-destructuring": ["error", { obj: true }]
}
}
], "Unexpected property \"obj\". Expected properties: \"array\", \"object\"");

await assertInvalidConfig([
{
rules: {
"prefer-destructuring": ["error", { object: true }, { enforceRenamedProperties: true }]
}
}
], "Unexpected property \"enforceRenamedProperties\". Expected properties: \"enforceForRenamedProperties\"");
});

});

describe("Invalid Keys", () => {
Expand Down

0 comments on commit e37153f

Please sign in to comment.