Skip to content

Commit

Permalink
Set up meta+schema merging unit tests for flat (passing) and legacy (…
Browse files Browse the repository at this point in the history
…failing)
  • Loading branch information
JoshuaKGoldberg committed Feb 15, 2024
1 parent 6b9b8ac commit 0dc4810
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/config/rule-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class RuleValidator {
if (validateRule) {

const slicedOptions = ruleOptions.slice(1);
const mergedOptions = deepMergeArrays(rule.meta && rule.meta.defaultOptions, slicedOptions);
const mergedOptions = deepMergeArrays(rule.meta?.defaultOptions, slicedOptions);

validateRule(mergedOptions);

Expand Down
59 changes: 56 additions & 3 deletions tests/lib/linter/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6763,7 +6763,7 @@ var a = "test2";
});

describe("options", () => {
it("rules should apply meta.defaultOptions and ignore schema defaults", () => {
it("rules should apply meta.defaultOptions on top of schema defaults", () => {
linter.defineRule("my-rule", {
meta: {
defaultOptions: [{
Expand All @@ -6783,7 +6783,10 @@ var a = "test2";
create(context) {
return {
Program(node) {
context.report({ node, message: JSON.stringify(context.options[0]) });
context.report({
message: JSON.stringify(context.options[0]),
node
});
}
};
}
Expand All @@ -6800,7 +6803,7 @@ var a = "test2";

assert.deepStrictEqual(
JSON.parse(messages[0].message),
{ inBoth: "from-default-options", inDefaultOptions: "from-default-options" }
{ inBoth: "from-default-options", inDefaultOptions: "from-default-options", inSchema: "from-schema" }
);
});
});
Expand Down Expand Up @@ -15451,6 +15454,56 @@ var a = "test2";
});
});

describe("options", () => {
it("rules should apply meta.defaultOptions on top of schema defaults", () => {
const config = {
plugins: {
test: {
rules: {
checker: {
meta: {
defaultOptions: [{
inBoth: "from-default-options",
inDefaultOptions: "from-default-options"
}],
schema: [{
type: "object",
properties: {
inBoth: { default: "from-schema", type: "string" },
inDefaultOptions: { type: "string" },
inSchema: { default: "from-schema", type: "string" }
},
additionalProperties: false
}]
},
create(context) {
return {
Program(node) {
context.report({
message: JSON.stringify(context.options[0]),
node
});
}
};
}
}
}
}
},
rules: {
"test/checker": "error"
}
};

const messages = linter.verify("foo", config, filename);

assert.deepStrictEqual(
JSON.parse(messages[0].message),
{ inBoth: "from-default-options", inDefaultOptions: "from-default-options", inSchema: "from-schema" }
);
});
});

describe("processors", () => {
let receivedFilenames = [];
let receivedPhysicalFilenames = [];
Expand Down

0 comments on commit 0dc4810

Please sign in to comment.