Skip to content

Commit

Permalink
feat: Update RuleDefinition with meta.defaultOptions (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored Jan 31, 2025
1 parent 4964322 commit e9a987e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ export interface RulesMeta<
*/
schema?: JSONSchema4 | JSONSchema4[] | false | undefined;

/** Any default options to be recursively merged on top of any user-provided options. */
defaultOptions?: unknown[];

/**
* The messages that the rule can report.
*/
Expand Down
47 changes: 32 additions & 15 deletions packages/core/tests/types/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ const testRule: RuleDefinition<{
},
],
},
schema: [
{
type: "object",
properties: {
foo: {
type: "string",
},
bar: {
type: "integer",
},
},
additionalProperties: false,
},
],
defaultOptions: [{ foo: "always", bar: 5 }],
messages: {
badFoo: "change this foo",
wrongBar: "fix this bar",
Expand All @@ -227,21 +242,23 @@ const testRule: RuleDefinition<{
return {
Foo(node: TestNode) {
// node.type === "Foo"
context.report({
messageId: "badFoo",
loc: {
start: { line: node.start, column: 1 },
end: { line: node.start + 1, column: Infinity },
},
fix(fixer: RuleTextEditor): RuleTextEdit {
return fixer.replaceText(
node,
context.languageOptions.howMuch === "yes"
? "πŸ‘"
: "πŸ‘Ž",
);
},
});
if (context.options[0].foo === "always") {
context.report({
messageId: "badFoo",
loc: {
start: { line: node.start, column: 1 },
end: { line: node.start + 1, column: Infinity },
},
fix(fixer: RuleTextEditor): RuleTextEdit {
return fixer.replaceText(
node,
context.languageOptions.howMuch === "yes"
? "πŸ‘"
: "πŸ‘Ž",
);
},
});
}
},
Bar(node: TestNode) {
// node.type === "Bar"
Expand Down

0 comments on commit e9a987e

Please sign in to comment.