Skip to content

Commit

Permalink
feat(core): support end-user extensions in the rule definitions (#2345)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelkornev authored Nov 30, 2022
1 parent a0294d9 commit 365fced
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/core/src/ruleset/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand Down Expand Up @@ -322,6 +323,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 Expand Up @@ -581,6 +606,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: false,
extensions: null,
formats: null,
given: ['$.info.contact'],
message: 'Contact name must contain Stoplight',
Expand All @@ -600,6 +626,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$.info'],
message: 'Description must contain Stoplight',
Expand All @@ -619,6 +646,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$.info'],
message: 'Title must contain Stoplight',
Expand Down Expand Up @@ -1144,6 +1172,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['#PathItem'],
message: null,
Expand All @@ -1163,6 +1192,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['#Name', '#Description'],
message: null,
Expand All @@ -1182,6 +1212,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['#Info.contact'],
message: null,
Expand Down Expand Up @@ -1606,6 +1637,7 @@ describe('Ruleset', () => {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['#Id'],
message: null,
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
4 changes: 4 additions & 0 deletions packages/core/src/ruleset/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IRule {
documentationUrl: string | null;
then: IRuleThen[];
given: string[];
extensions: Record<string, unknown> | null;
}

type RuleJson = Omit<IRule, 'then'> & {
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 Expand Up @@ -188,6 +191,7 @@ export class Rule implements IRule {
})),
given: Array.isArray(this.definition.given) ? this.definition.given : [this.definition.given],
owner: this.owner.id,
extensions: this.extensions,
};
}
}
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
6 changes: 6 additions & 0 deletions packages/ruleset-bundler/src/loader/__tests__/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export default {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -134,6 +135,7 @@ export default {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -152,6 +154,7 @@ export default {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand Down Expand Up @@ -189,6 +192,7 @@ export default {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -207,6 +211,7 @@ export default {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -225,6 +230,7 @@ export default {
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export {default} from '/-/lodash.uppercase@v4.3.0-Ghj8UDzvgbRFVHwnUM53/dist=es20
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -66,6 +67,7 @@ export {default} from '/-/lodash.uppercase@v4.3.0-Ghj8UDzvgbRFVHwnUM53/dist=es20
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -84,6 +86,7 @@ export {default} from '/-/lodash.uppercase@v4.3.0-Ghj8UDzvgbRFVHwnUM53/dist=es20
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand Down Expand Up @@ -121,6 +124,7 @@ export {default} from '/-/lodash.uppercase@v4.3.0-Ghj8UDzvgbRFVHwnUM53/dist=es20
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -139,6 +143,7 @@ export {default} from '/-/lodash.uppercase@v4.3.0-Ghj8UDzvgbRFVHwnUM53/dist=es20
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand All @@ -157,6 +162,7 @@ export {default} from '/-/lodash.uppercase@v4.3.0-Ghj8UDzvgbRFVHwnUM53/dist=es20
description: null,
documentationUrl: null,
enabled: true,
extensions: null,
formats: null,
given: ['$'],
message: null,
Expand Down

0 comments on commit 365fced

Please sign in to comment.