-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRapLPCustomSpectral.ts
More file actions
109 lines (103 loc) · 3.84 KB
/
RapLPCustomSpectral.ts
File metadata and controls
109 lines (103 loc) · 3.84 KB
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
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2
import * as SpectralCore from '@stoplight/spectral-core';
import { ISpectralDiagnostic } from '@stoplight/spectral-core';
import spectralCore from '@stoplight/spectral-core';
const { Spectral, Document } = spectralCore;
import { RapLPCustomSpectralDiagnostic } from './RapLPCustomSpectralDiagnostic.js';
import { buildRuleHelpUrl } from '../rulesets/util/rules-doc.config.js';
class RapLPCustomSpectral {
private spectral: SpectralCore.Spectral;
private rules: Record<string, any>;
private enabledRules: EnabledRules = {
rules: {},
};
private instanceCategoryMap: Map<string, any>;
constructor() {
this.spectral = new Spectral();
this.rules = {};
this.instanceCategoryMap = new Map<string, any>();
}
setRuleset(enabledRules: Record<string, any>): void {
this.enabledRules.rules = enabledRules;
this.spectral.setRuleset(this.enabledRules);
}
setCategorys(instanceCategoryMap: Map<string, any>): void {
this.instanceCategoryMap = instanceCategoryMap;
}
async run(document: any): Promise<RapLPCustomSpectralDiagnostic[]> {
const spectralResults = await this.spectral.run(document);
return this.modifyResults(spectralResults);
}
async runSemanticValidation(document: any): Promise<ISpectralDiagnostic[]> {
return await this.spectral.run(document);
}
private modifyRuleset(enabledRules: EnabledRules): Record<string, any> {
return enabledRules.rules;
}
private modifyResults(results: ISpectralDiagnostic[]): RapLPCustomSpectralDiagnostic[] {
const customResults: RapLPCustomSpectralDiagnostic[] = []; // Initialize
for (const result of results) {
const ruleName = result.code as string;
for (const ruleObject of Object.values(this.enabledRules)) {
if (ruleObject && Object.keys(ruleObject).includes(ruleName)) {
const ruleInstance = ruleObject[ruleName];
const ruleClass = this.instanceCategoryMap.get(ruleName);
if (ruleClass && typeof ruleClass.getCustomProperties === 'function') {
// Check for existance
const customProperties = ruleClass.getCustomProperties;
const ruleId = ruleClass.customProperties.id;
const customResult: RapLPCustomSpectralDiagnostic = {
id: ruleId,
område: ruleClass.customProperties.område,
helpUrl: ruleId ? buildRuleHelpUrl(ruleId) : undefined,
...customProperties, // For more copy
...this.mapResultToCustom(result),
};
customResults.push(customResult);
break; // Break the loop once a match is found
}
}
}
}
return customResults;
}
private mapResultToCustom(result: ISpectralDiagnostic): RapLPCustomSpectralDiagnostic {
// Map properties from result ISpectralDiagnostic to CustomSpectralDiagnostic
const { message, code, severity, path, source, range, ...rest } = result;
// Map severity to corresponding string value for allvarlighetsgrad
let allvarlighetsgrad: string;
switch (severity) {
case 0:
allvarlighetsgrad = 'ERROR';
break;
case 1:
allvarlighetsgrad = 'WARNING';
break;
case 2:
allvarlighetsgrad = 'INFORMATION';
break;
case 3:
allvarlighetsgrad = 'HINT';
break;
default:
allvarlighetsgrad = ''; // Handle other cases if needed
}
return {
...rest,
krav: message,
allvarlighetsgrad,
sökväg: path,
omfattning: range,
};
}
}
export { RapLPCustomSpectral };
/**
* Own defined interface to extend ISpectralDiagnostic.
* The interface also extends the omit type in order to 'remove' some fields from the iSpectralDiagnostic
*/
interface EnabledRules {
rules: Record<string, any>;
}