Skip to content

Commit 5c0bc4b

Browse files
committed
feat(core): lint issues
1 parent 35b9256 commit 5c0bc4b

File tree

7 files changed

+77
-68
lines changed

7 files changed

+77
-68
lines changed

docs/guides/2-cli.md

+52-50
Original file line numberDiff line numberDiff line change
@@ -78,58 +78,60 @@ Usage:
7878

7979
```bash
8080
spectral lint ./reference/**/*.oas*.{json,yml,yaml} --ruleset mycustomruleset.js --scoring-config ./scoringFile.json
81-
```
82-
or
83-
```bash
84-
spectral lint ./reference/**/*.oas*.{json,yml,yaml} -r mycustomruleset.js -s ./scoringFile.json
85-
```
86-
87-
88-
Heres an example of this scoringFile config file:
89-
90-
```
91-
{
92-
"scoringSubtract":
93-
{
94-
"error":
95-
{
96-
1:55,
97-
2:65,
98-
3:75,
99-
6:85,
100-
10:95
101-
}
102-
"warn":
103-
{
104-
1:3,
105-
2:7,
106-
3:10,
107-
6:15,
108-
10:18
109-
}
110-
},
111-
"scoringLetter":
112-
{
113-
"A":75,
114-
"B":65,
115-
"C":55,
116-
"D":45,
117-
"E":0
118-
},
119-
"threshold":50,
120-
"warningsSubtract": true,
121-
"uniqueErrors": false
122-
}
123-
```
81+
```
82+
83+
or
84+
85+
```bash
86+
spectral lint ./reference/**/*.oas*.{json,yml,yaml} -r mycustomruleset.js -s ./scoringFile.json
87+
```
88+
89+
Heres an example of this scoringFile config file:
90+
91+
```
92+
{
93+
"scoringSubtract":
94+
{
95+
"error":
96+
{
97+
1:55,
98+
2:65,
99+
3:75,
100+
6:85,
101+
10:95
102+
}
103+
"warn":
104+
{
105+
1:3,
106+
2:7,
107+
3:10,
108+
6:15,
109+
10:18
110+
}
111+
},
112+
"scoringLetter":
113+
{
114+
"A":75,
115+
"B":65,
116+
"C":55,
117+
"D":45,
118+
"E":0
119+
},
120+
"threshold":50,
121+
"warningsSubtract": true,
122+
"uniqueErrors": false
123+
}
124+
```
124125

125126
Where:
126-
- scoringSubtract : An object with a key/value pair objects for every result level we want to subtract percentage, with the percentage to subtract from number of results on every result type
127-
- scoringLetter : An object with key/value pairs with scoring letter and scoring percentage, that the result must be greater , for this letter
128-
- threshold : A number with minimum percentage value to provide valid the file we are checking
129-
- warningsSubtract : A boolean to setup if accumulate the result types to less the scoring percentage or stop counting on most critical result types
130-
- uniqueErrors : A boolean to setup a count with unique errors or with all of them
131127

132-
Example:
128+
- scoringSubtract : An object with a key/value pair objects for every result level we want to subtract percentage, with the percentage to subtract from number of results on every result type
129+
- scoringLetter : An object with key/value pairs with scoring letter and scoring percentage, that the result must be greater , for this letter
130+
- threshold : A number with minimum percentage value to provide valid the file we are checking
131+
- warningsSubtract : A boolean to setup if accumulate the result types to less the scoring percentage or stop counting on most critical result types
132+
- uniqueErrors : A boolean to setup a count with unique errors or with all of them
133+
134+
Example:
133135

134136
With previous scoring config file, if we have:
135137

@@ -139,7 +141,7 @@ Where:
139141
4 errors, the scoring is 25% and E
140142
and so on
141143

142-
Output:
144+
Output:
143145

144146
Below your output log you can see the scoring, like:
145147

packages/cli/src/commands/lint.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import { formatOutput, writeOutput } from '../services/output';
1414
import { FailSeverity, ILintConfig, OutputFormat } from '../services/config';
1515

1616
import { CLIError } from '../errors';
17-
import { ScoringConfig } from "./../formatters/types";
17+
import { ScoringConfig } from './../formatters/types';
1818
import {
1919
getScoringConfig,
2020
getScoringLevel,
2121
groupBySource,
2222
getCountsBySeverity,
23-
uniqueErrors
23+
uniqueErrors,
2424
} from '../formatters//utils';
2525

2626
const formatOptions = Object.values(OutputFormat);
@@ -222,7 +222,7 @@ const lintCommand: CommandModule = {
222222
return writeOutput(formattedOutput, output?.[f] ?? '<stdout>');
223223
}),
224224
);
225-
//process.stdout.write(`${scoringThresholdNotEnough(results, scoringConfigData)}\n`);
225+
226226
if (results.length > 0) {
227227
process.exit(
228228
scoringThresholdNotEnough(results, scoringConfigData) ? 1 : severeEnoughToFail(results, failSeverity) ? 1 : 0,
@@ -232,6 +232,9 @@ const lintCommand: CommandModule = {
232232
process.stdout.write(
233233
`No results with a severity of '${failSeverity}' ${isErrorSeverity ? '' : 'or higher '}found!\n`,
234234
);
235+
if (scoringConfig !== void 0) {
236+
process.stdout.write(`SCORING: (100%)\nPASSED!`);
237+
}
235238
}
236239
} catch (ex) {
237240
fail(isError(ex) ? ex : new Error(String(ex)), config.verbose === true);
@@ -302,12 +305,12 @@ const scoringThresholdNotEnough = (results: IRuleResult[], scoringConfig: Scorin
302305
groupedUniqueResults = { ...groupBySource(uniqueErrors(results)) };
303306
}
304307
return (
305-
scoringConfig.threshold >
306-
getScoringLevel(
307-
getCountsBySeverity(groupedUniqueResults),
308-
scoringConfig.scoringSubtract,
309-
scoringConfig.warningsSubtract,
310-
)
308+
scoringConfig.threshold >
309+
getScoringLevel(
310+
getCountsBySeverity(groupedUniqueResults),
311+
scoringConfig.scoringSubtract,
312+
scoringConfig.warningsSubtract,
313+
)
311314
);
312315
}
313316
return false;

packages/cli/src/formatters/json.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Formatter, FormatterOptions } from './types';
33

44
import { groupBySource, uniqueErrors, getCountsBySeverity, getScoringText } from './utils';
55

6-
const { version } = require('../../package.json');
6+
const version = process.env.npm_package_version;
77

88
export const json: Formatter = (results: ISpectralDiagnostic[], options: FormatterOptions) => {
99
let spectralVersion = '';

packages/cli/src/formatters/pretty.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
groupBySource,
3939
getScoringText,
4040
getCountsBySeverity,
41-
uniqueErrors
41+
uniqueErrors,
4242
} from './utils';
4343

4444
const { version } = require('../../package.json');

packages/cli/src/formatters/stylish.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import {
4242
groupBySource,
4343
getScoringText,
4444
getCountsBySeverity,
45-
uniqueErrors
45+
uniqueErrors,
4646
} from './utils';
4747

48-
const { version } = require('../../package.json');
48+
const version = process.env.npm_package_version;
4949

5050
// -----------------------------------------------------------------------------
5151
// Helpers

packages/cli/src/formatters/utils/getScoring.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SEVERITY_MAP } from '@stoplight/spectral-core';
22
import { DiagnosticSeverity } from '@stoplight/types';
3-
import { ScoringConfig, ScoringTable } from '../types';
3+
import { ScoringConfig, ScoringTable, ScoringSubtract } from '../types';
44
import * as path from '@stoplight/path';
55
import fs from 'fs';
66

@@ -32,10 +32,13 @@ export const getScoringLevel = (
3232
if (scoringSubtract[scoringKey] !== void 0) {
3333
if (scoring < 100 && !warningsSubtract) return;
3434
let subtractValue = 0;
35-
Object.keys(scoringSubtract[scoringKey]).forEach(
36-
subtractKey =>
37-
(subtractValue = issuesCount[key] >= subtractKey ? scoringSubtract[scoringKey][subtractKey] : subtractValue),
38-
);
35+
Object.keys(scoringSubtract[scoringKey] as ScoringSubtract[]).forEach((subtractKey: string): void => {
36+
subtractValue = (
37+
issuesCount[key] >= subtractKey
38+
? (scoringSubtract[scoringKey] as ScoringSubtract[])[subtractKey]
39+
: subtractValue
40+
) as number;
41+
});
3942
scoring -= subtractValue;
4043
}
4144
});

test-harness/scenarios/valid-no-errors.oas2-scoring.scenario

+1
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ module.exports = oas;
5454
{bin} lint {document} --ruleset "{asset:ruleset}" --scoring-config "{asset:scoring-config.json}"
5555
====stdout====
5656
No results with a severity of 'error' found!
57+
SCORING: (100%)PASSED!

0 commit comments

Comments
 (0)