-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
rule.ts
265 lines (224 loc) · 7.68 KB
/
rule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { isString } from 'lodash';
import { DiagnosticSeverity, JsonPath, Optional } from '@stoplight/types';
import { dirname, relative } from '@stoplight/path';
import { pathToPointer } from '@stoplight/json';
import { printValue } from '@stoplight/spectral-runtime';
import { DEFAULT_SEVERITY_LEVEL, getDiagnosticSeverity } from './utils/severity';
import { Ruleset } from './ruleset';
import { Format } from './format';
import type {
HumanReadableDiagnosticSeverity,
IRuleThen,
RuleDefinition,
RulesetAliasesDefinition,
RulesetScopedAliasDefinition,
Stringifable,
} from './types';
import { minimatch } from './utils/minimatch';
import { Formats } from './formats';
import { isSimpleAliasDefinition } from './utils/guards';
const ALIAS = /^#([A-Za-z0-9_-]+)/;
export interface IRule {
description: string | null;
message: string | null;
severity: DiagnosticSeverity;
resolved: boolean;
formats: Formats | null;
enabled: boolean;
recommended: boolean;
documentationUrl: string | null;
then: IRuleThen[];
given: string[];
}
export type StringifiedRule = Omit<IRule, 'formats' | 'then'> & {
name: string;
formats: string[] | null;
then: (Pick<IRuleThen, 'field'> & { function: string; functionOptions?: string })[];
owner: number;
};
export class Rule implements IRule {
public description: string | null;
public message: string | null;
#severity!: DiagnosticSeverity;
public resolved: boolean;
public formats: Formats | null;
#enabled: boolean;
public recommended: boolean;
public documentationUrl: string | null;
#then!: IRuleThen[];
#given!: string[];
constructor(
public readonly name: string,
public readonly definition: RuleDefinition,
public readonly owner: Ruleset,
) {
this.recommended = definition.recommended !== false;
this.#enabled = this.recommended;
this.description = definition.description ?? null;
this.message = definition.message ?? null;
this.documentationUrl = definition.documentationUrl ?? null;
this.severity = definition.severity;
this.resolved = definition.resolved !== false;
this.formats = 'formats' in definition ? new Formats(definition.formats) : null;
this.then = definition.then;
this.given = definition.given;
}
public overrides?: { rulesetSource: string; definition: Map<string, Map<string, DiagnosticSeverity | -1>> };
public get enabled(): boolean {
return this.#enabled || this.overrides !== void 0;
}
public set enabled(enabled: boolean) {
this.#enabled = enabled;
}
public getSeverityForSource(source: string, path: JsonPath): DiagnosticSeverity | -1 {
if (this.overrides === void 0 || this.overrides.definition.size === 0) {
return this.severity;
}
const relativeSource = relative(dirname(this.overrides.rulesetSource), source);
const relevantOverrides: Map<string, DiagnosticSeverity | -1>[] = [];
for (const [source, override] of this.overrides.definition.entries()) {
if (minimatch(relativeSource, source)) {
relevantOverrides.push(override);
}
}
if (relevantOverrides.length === 0) {
return this.severity;
}
let severity: DiagnosticSeverity = this.severity;
let closestPointer = '';
const pointer = pathToPointer(path);
for (const relevantOverride of relevantOverrides) {
for (const [overridePath, overrideSeverity] of relevantOverride.entries()) {
if (overridePath.length >= closestPointer.length && pointer.startsWith(overridePath)) {
closestPointer = overridePath;
severity = overrideSeverity;
}
}
}
return severity;
}
public get severity(): DiagnosticSeverity {
return this.#severity;
}
public set severity(severity: Optional<HumanReadableDiagnosticSeverity | DiagnosticSeverity>) {
if (severity === void 0) {
this.#severity = DEFAULT_SEVERITY_LEVEL;
} else {
this.#severity = getDiagnosticSeverity(severity);
}
}
public get then(): IRuleThen[] {
return this.#then;
}
public set then(then: RuleDefinition['then']) {
this.#then = Array.isArray(then) ? then : [then];
}
public get given(): string[] {
return this.#given;
}
public set given(given: RuleDefinition['given']) {
const actualGiven = Array.isArray(given) ? given : [given];
this.#given = this.owner.hasComplexAliases
? actualGiven
: actualGiven.flatMap(expr => Rule.#resolveAlias(this.owner.aliases, expr, null, new Set())).filter(isString);
}
public getGivenForFormats(formats: Set<Format> | null): string[] {
return this.owner.hasComplexAliases
? this.#given.flatMap(expr => Rule.#resolveAlias(this.owner.aliases, expr, formats, new Set()))
: this.#given;
}
static #resolveAlias(
aliases: RulesetAliasesDefinition | null,
expr: string,
formats: Set<Format> | null,
stack: Set<string>,
): string[] {
const resolvedExpressions: string[] = [];
if (expr.startsWith('#')) {
const alias = ALIAS.exec(expr)?.[1];
if (alias === void 0 || alias === null) {
throw new ReferenceError(`"${this.name}" rule references an invalid alias`);
}
if (stack.has(alias)) {
const _stack = [...stack, alias];
throw new ReferenceError(`Alias "${_stack[0]}" is circular. Resolution stack: ${_stack.join(' -> ')}`);
}
stack.add(alias);
if (aliases === null || !(alias in aliases)) {
throw new ReferenceError(`Alias "${alias}" does not exist`);
}
const aliasValue = aliases[alias];
let actualAliasValue: string[] | null;
if (isSimpleAliasDefinition(aliasValue)) {
actualAliasValue = aliasValue;
} else {
actualAliasValue = Rule.#resolveAliasForFormats(aliasValue, formats);
}
if (actualAliasValue !== null) {
resolvedExpressions.push(
...actualAliasValue.flatMap(item =>
Rule.#resolveAlias(aliases, item + expr.slice(alias.length + 1), formats, new Set([...stack])),
),
);
}
} else {
resolvedExpressions.push(expr);
}
return resolvedExpressions;
}
static #resolveAliasForFormats(
{ targets }: RulesetScopedAliasDefinition,
formats: Set<Format> | null,
): string[] | null {
if (formats === null || formats.size === 0) {
return null;
}
// we start from the end to be consistent with overrides etc. - we generally tend to pick the "last" value.
for (let i = targets.length - 1; i >= 0; i--) {
const target = targets[i];
for (const format of target.formats) {
if (formats.has(format)) {
return target.given;
}
}
}
return null;
}
public matchesFormat(formats: Set<Format> | null): boolean {
if (this.formats === null) {
return true;
}
if (formats === null) {
return false;
}
for (const format of formats) {
if (this.formats.has(format)) {
return true;
}
}
return false;
}
public clone(): Rule {
return new Rule(this.name, this.definition, this.owner);
}
public toJSON(): Stringifable<StringifiedRule> {
return {
name: this.name,
recommended: this.recommended,
enabled: this.enabled,
description: this.description,
message: this.message,
documentationUrl: this.documentationUrl,
severity: this.severity,
resolved: this.resolved,
formats: this.formats,
then: this.then.map(then => ({
...then.function,
function: then.function.name,
...('functionOptions' in then ? { functionOptions: printValue(then.functionOptions) } : null),
})),
given: Array.isArray(this.definition.given) ? this.definition.given : [this.definition.given],
owner: this.owner.id,
};
}
}