Skip to content

Commit 362e74f

Browse files
committed
feat(core): support end-user extensions in the rule definitions
1 parent d0595e6 commit 362e74f

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

packages/core/src/ruleset/__tests__/ruleset.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,30 @@ describe('Ruleset', () => {
315315
`);
316316
});
317317

318+
it('should respect extensions', async () => {
319+
const ruleset = {
320+
rules: {
321+
'foo-rule': {
322+
given: '$',
323+
then: {
324+
function() {
325+
return;
326+
},
327+
},
328+
extensions: {
329+
foo: 'bar',
330+
},
331+
},
332+
},
333+
};
334+
335+
const rulesetInstance = new Ruleset(ruleset);
336+
337+
expect(rulesetInstance.rules['foo-rule'].extensions).toEqual({
338+
foo: 'bar',
339+
});
340+
});
341+
318342
it('should include parserOptions', async () => {
319343
const { parserOptions } = await loadRuleset(import('./__fixtures__/parser-options-ruleset'));
320344

packages/core/src/ruleset/meta/rule.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
"enum": ["style", "validation"],
7878
"type": "string",
7979
"errorMessage": "allowed types are \"style\" and \"validation\""
80+
},
81+
"extensions": {
82+
"type": "object"
8083
}
8184
},
8285
"required": ["given", "then"],

packages/core/src/ruleset/rule.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ export interface IRule {
2424
documentationUrl: string | null;
2525
then: IRuleThen[];
2626
given: string[];
27+
extensions: Record<string, unknown> | null;
2728
}
2829

29-
type RuleJson = Omit<IRule, 'then'> & {
30+
type RuleJson = Omit<IRule, 'then' | 'extensions'> & {
3031
name: string;
3132
then: (Pick<IRuleThen, 'field'> & { function: string; functionOptions?: string })[];
3233
owner: number;
@@ -43,6 +44,7 @@ export class Rule implements IRule {
4344
#enabled: boolean;
4445
public recommended: boolean;
4546
public documentationUrl: string | null;
47+
public extensions: Record<string, unknown> | null;
4648
#then!: IRuleThen[];
4749
#given!: string[];
4850

@@ -61,6 +63,7 @@ export class Rule implements IRule {
6163
this.formats = 'formats' in definition ? new Formats(definition.formats) : null;
6264
this.then = definition.then;
6365
this.given = definition.given;
66+
this.extensions = definition.extensions ?? null;
6467
}
6568

6669
public overrides?: { rulesetSource: string; definition: Map<string, Map<string, DiagnosticSeverity | -1>> };

packages/core/src/ruleset/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export type RuleDefinition = {
4242
resolved?: boolean;
4343

4444
then: IRuleThen | IRuleThen[];
45+
46+
// User extensions/metadata point
47+
extensions?: Record<string, unknown>;
4548
};
4649

4750
export interface IRuleThen {

0 commit comments

Comments
 (0)