Skip to content

Commit

Permalink
fix: nested extends broken in rulesets (#1465)
Browse files Browse the repository at this point in the history
* fix(ruleset): nested extends not working correctly

* Update src/rule.ts

Co-authored-by: Domagoj Kriskovic <dom@stoplight.io>

Co-authored-by: Domagoj Kriskovic <dom@stoplight.io>
  • Loading branch information
P0lip and Domagoj Kriskovic committed Mar 8, 2021
1 parent fa93c64 commit 5b8c6f5
Show file tree
Hide file tree
Showing 36 changed files with 501 additions and 915 deletions.
1 change: 1 addition & 0 deletions setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const removeAllRulesBut = (spectral: Spectral, ruleName: string) => {
const patchedRule = Object.assign(rule1, {
recommended: true,
severity: rawRule.severity,
enabled: true,
}) as IRule;

const rules: RuleCollection = {};
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ responses:: !!foo
message: spectral.rules['operation-tag-defined'].message ?? '',
description: spectral.rules['operation-tag-defined'].description ?? '',
severity: 'off',
enabled: false,
},
});

Expand Down
14 changes: 7 additions & 7 deletions src/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { JSONPathExpression } from 'nimma';

import { IDocument } from './document';
import { DEFAULT_SEVERITY_LEVEL, getDiagnosticSeverity } from './rulesets/severity';
import { IGivenNode, IRule, IThen, SpectralDiagnosticSeverity } from './types';
import { IGivenNode, IProcessedRule, IThen } from './types';
import { hasIntersectingElement } from './utils';
import { DiagnosticSeverity } from '@stoplight/types';

export class Rule {
public readonly name: string;
public readonly description: string | null;
public readonly message: string | null;
public readonly severity: SpectralDiagnosticSeverity;
public readonly severity: DiagnosticSeverity;
public readonly resolved: boolean;
public readonly formats: Optional<string[]>;

public readonly then: IThen[];
public readonly given: string[];

public get enabled(): boolean {
return this.severity !== -1;
}
public readonly enabled: boolean;

constructor(name: string, rule: IRule) {
constructor(name: string, rule: IProcessedRule) {
this.name = name;
this.enabled = rule.enabled ?? true;
this.description = rule.description ?? null;
this.message = rule.message ?? null;
this.severity = rule.severity === void 0 ? DEFAULT_SEVERITY_LEVEL : getDiagnosticSeverity(rule.severity);
Expand All @@ -49,7 +49,7 @@ function stub(): void {
export class OptimizedRule extends Rule {
public readonly expressions: JSONPathExpression[];

constructor(name: string, rule: IRule) {
constructor(name: string, rule: IProcessedRule) {
super(name, rule);
this.expressions = this.given.map(given => {
const expr = new JSONPathExpression(given, stub, stub);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"functionsDir": "./customFunctions",
"functions": [
"bar",
"truthy"
],
"rules": {
"bar-rule": {
"message": "should be OK",
"given": "$.info",
"recommended": true,
"then": {
"function": "bar"
}
},
"truthy-rule": {
"message": "should be OK",
"given": "$.x",
"recommended": true,
"then": {
"function": "truthy"
}
}
}
}
12 changes: 12 additions & 0 deletions src/rulesets/__tests__/__fixtures__/formats/oas2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"formats": ["oas2"],
"rules": {
"oas2-valid-rule": {
"message": "should be OK",
"given": "$.info",
"then": {
"function": "truthy"
}
}
}
}
12 changes: 12 additions & 0 deletions src/rulesets/__tests__/__fixtures__/formats/oas3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"formats": ["oas3"],
"rules": {
"oas3-valid-rule": {
"message": "should be OK",
"given": "$.info",
"then": {
"function": "truthy"
}
}
}
}
13 changes: 13 additions & 0 deletions src/rulesets/__tests__/__fixtures__/formats/ruleset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": ["./oas2.json", "./oas3.json"],
"formats": ["oas2", "oas3"],
"rules": {
"generic-valid-rule": {
"message": "should be OK",
"given": "$.info",
"then": {
"function": "truthy"
}
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions src/rulesets/__tests__/__fixtures__/npm/plain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["example-spectral-ruleset"]
}
3 changes: 3 additions & 0 deletions src/rulesets/__tests__/__fixtures__/npm/versioned.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["example-spectral-ruleset@0.0.3"]
}
6 changes: 6 additions & 0 deletions src/rulesets/__tests__/__fixtures__/severity/all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [["./shared.json", "all"]],
"rules": {
"description-matches-stoplight": "off"
}
}
3 changes: 3 additions & 0 deletions src/rulesets/__tests__/__fixtures__/severity/implicit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./shared.json"
}
3 changes: 3 additions & 0 deletions src/rulesets/__tests__/__fixtures__/severity/off-proxy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./off.json"
}
6 changes: 6 additions & 0 deletions src/rulesets/__tests__/__fixtures__/severity/off.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [["./shared.json", "off"]],
"rules": {
"overridable-rule": true
}
}
3 changes: 3 additions & 0 deletions src/rulesets/__tests__/__fixtures__/severity/recommended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": [["shared.json", "recommended"]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description-matches-stoplight": {
"message": "Description must contain Stoplight",
"given": "$.info",
"type": "style",
"recommended": true,
"severity": "error",
"then": {
Expand All @@ -17,7 +16,6 @@
"title-matches-stoplight": {
"message": "Title must contain Stoplight",
"given": "$.info",
"type": "style",
"then": {
"field": "title",
"function": "pattern",
Expand All @@ -29,7 +27,6 @@
"contact-name-matches-stoplight": {
"message": "Contact name must contain Stoplight",
"given": "$.info.contact",
"type": "style",
"recommended": false,
"then": {
"field": "name",
Expand All @@ -38,6 +35,12 @@
"match": "Stoplight"
}
}
},
"overridable-rule": {
"given": "$",
"then": {
"function": "truthy"
}
}
}
}
10 changes: 10 additions & 0 deletions src/rulesets/__tests__/__fixtures__/valid-flat-ruleset-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"rules": {
"valid-rule-2": {
"given": "$.info",
"then": {
"function": "truthy"
}
}
}
}
9 changes: 0 additions & 9 deletions src/rulesets/__tests__/__fixtures__/valid-flat-ruleset.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
{
"rules": {
"valid-rule": {
"message": "should be OK",
"given": "$.info",
"then": {
"function": "truthy"
}
},
"valid-rule-recommended": {
"message": "should be OK",
"given": "$.info",
"recommended": true,
"then": {
"function": "truthy"
}
}
}
}
12 changes: 12 additions & 0 deletions src/rulesets/__tests__/formats/oas2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"formats": ["oas2"],
"rules": {
"oas2-valid-rule": {
"message": "should be OK",
"given": "$.info",
"then": {
"function": "truthy"
}
}
}
}
12 changes: 12 additions & 0 deletions src/rulesets/__tests__/formats/oas3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"formats": ["oas3"],
"rules": {
"oas3-valid-rule": {
"message": "should be OK",
"given": "$.info",
"then": {
"function": "truthy"
}
}
}
}
13 changes: 13 additions & 0 deletions src/rulesets/__tests__/formats/ruleset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": ["./oas2.json", "./oas3.json"],
"formats": ["oas2", "oas3"],
"rules": {
"generic-valid-rule": {
"message": "should be OK",
"given": "$.info",
"then": {
"function": "truthy"
}
}
}
}
Loading

0 comments on commit 5b8c6f5

Please sign in to comment.