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

feat(core): support end-user extensions in the rule definitions #2345

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/core/src/ruleset/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,30 @@ describe('Ruleset', () => {
`);
});

it('should respect extensions', async () => {
const ruleset = {
rules: {
'foo-rule': {
given: '$',
then: {
function() {
return;
},
},
extensions: {
foo: 'bar',
},
},
},
};

const rulesetInstance = new Ruleset(ruleset);

expect(rulesetInstance.rules['foo-rule'].extensions).toEqual({
foo: 'bar',
});
});

it('should include parserOptions', async () => {
const { parserOptions } = await loadRuleset(import('./__fixtures__/parser-options-ruleset'));

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/ruleset/meta/rule.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"enum": ["style", "validation"],
"type": "string",
"errorMessage": "allowed types are \"style\" and \"validation\""
},
"extensions": {
"type": "object"
}
},
"required": ["given", "then"],
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/ruleset/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export interface IRule {
documentationUrl: string | null;
then: IRuleThen[];
given: string[];
extensions: Record<string, unknown> | null;
}

type RuleJson = Omit<IRule, 'then'> & {
type RuleJson = Omit<IRule, 'then' | 'extensions'> & {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type RuleJson = Omit<IRule, 'then' | 'extensions'> & {
type RuleJson = Omit<IRule, 'then'> & {

Let's have it included :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks good to me! One minor request - could you please add extensions to

public toJSON(): Stringifable<RuleJson> {

?

       resolved: this.resolved,
       formats: this.formats,
+      extensions: this.extensions,
       then: this.then.map(then => ({

As extensions type is Record<string, unknown> we don't know what comes as a value here, it's unknown... it might be not a valid "JSON" in this sense.

Alternatively, we can allow only primitives as values in this object to make sure it's a JSON compatible object.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's unknown... it might be not a valid "JSON" in this sense

Well, it's going to be JSON for JSON rulesets, but yeah, could be a non-serializable value in the case of JS rulesets.

Alternatively, we can allow only primitives as values in this object to make sure it's a JSON compatible object.

objects and arrays are both JSON compatible objects too.
I believe we can leave it as is - JSON.stringify will just drop any invalid values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

name: string;
then: (Pick<IRuleThen, 'field'> & { function: string; functionOptions?: string })[];
owner: number;
Expand All @@ -43,6 +44,7 @@ export class Rule implements IRule {
#enabled: boolean;
public recommended: boolean;
public documentationUrl: string | null;
public extensions: Record<string, unknown> | null;
#then!: IRuleThen[];
#given!: string[];

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

public overrides?: { rulesetSource: string; definition: Map<string, Map<string, DiagnosticSeverity | -1>> };
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/ruleset/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export type RuleDefinition = {
resolved?: boolean;

then: IRuleThen | IRuleThen[];

// User extensions/metadata point
extensions?: Record<string, unknown>;
};

export interface IRuleThen {
Expand Down