Skip to content

Commit

Permalink
New: More severities and threshold in schema
Browse files Browse the repository at this point in the history
Fix #3065
  • Loading branch information
molant committed Nov 1, 2019
1 parent c982bb4 commit a7278f2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 77 deletions.
8 changes: 6 additions & 2 deletions packages/extension-vscode/src/utils/problems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { Problem, Severity } from 'hint';
// Translate a webhint severity into the VSCode DiagnosticSeverity format.
const webhintToDiagnosticServerity = (severity: Severity): DiagnosticSeverity => {
switch (severity) {
case 2:
case 4:
return DiagnosticSeverity.Error;
case 1:
case 3:
return DiagnosticSeverity.Warning;
case 2:
return DiagnosticSeverity.Information;
case 1:
return DiagnosticSeverity.Hint;
default:
return DiagnosticSeverity.Hint;
}
Expand Down
16 changes: 11 additions & 5 deletions packages/hint/src/lib/cli/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,16 @@ export default async (actions: CLIOptions): Promise<boolean> => {
}
};

const hasError = (reports: Problem[]): boolean => {
return reports.some((result: Problem) => {
return result.severity === Severity.error;
});
const hasIssues = (reports: Problem[]): boolean => {
const threshold = userConfig.severityThreshold || Severity.error;

for (const result of reports) {
if (result.severity >= threshold) {
return true;
}
}

return false;
};

const print = async (reports: Problem[], target?: string, scanTime?: number, date?: string): Promise<void> => {
Expand Down Expand Up @@ -447,7 +453,7 @@ export default async (actions: CLIOptions): Promise<boolean> => {
const scanEnd = Date.now();
const start = scanStart.get(end.url) || 0;

if (hasError(end.problems)) {
if (hasIssues(end.problems)) {
exitCode = 1;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hint/src/lib/config/config-hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getSeverity = (config: HintConfig | HintConfig[]): Severity | null
configuredSeverity = getSeverity(config[0]);
}

if (configuredSeverity !== null && configuredSeverity >= 0 && configuredSeverity <= 2) {
if (configuredSeverity !== null && configuredSeverity >= 0 && configuredSeverity <= 4) {
return configuredSeverity;
}

Expand Down
159 changes: 98 additions & 61 deletions packages/hint/src/lib/config/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,68 @@
"title": "Configuration",
"description": "A configuration file for hint",
"type": "object",
"properties": {
"definitions": {
"errorlevelstring": {
"type": "string",
"enum": [
"off",
"hint",
"information",
"warning",
"error"
]
},
"errorlevelnumber": {
"type": "number",
"enum": [
0,
1,
2,
3,
4
]
},
"hintconfig": {
"type": "array",
"items": [
{
"anyOf": [
{
"$ref": "#/definitions/errorlevelstring"
},
{
"$ref": "#/definitions/errorlevelnumber"
}
]
},
{
"type": "object"
}
],
"maxItems": 2,
"minItems": 1,
"uniqueItems": true
},
"hintobject": {
"type": "object",
"patternProperties": {
"^.+$": {
"anyOf": [
{
"$ref": "#/definitions/errorlevelstring"
},
{
"$ref": "#/definitions/errorlevelnumber"
},
{
"$ref": "#/definitions/hintconfig"
}
]
}
},
"minItems": 1,
"uniqueItems": true
},
"connector": {
"description": "The connector to use to gather information",
"anyOf": [
Expand Down Expand Up @@ -133,6 +194,41 @@
"language": {
"description": "Localization language",
"type": "string"
},
"severityThreshold": {
"$ref": "#/definitions/errorlevelstring"
}
},
"properties": {
"connector": {
"$ref": "#/definitions/connector"
},
"extends": {
"$ref": "#/definitions/extends"
},
"parsers": {
"$ref": "#/definitions/parsers"
},
"formatters": {
"$ref": "#/definitions/formatters"
},
"hintsTimeout": {
"$ref": "#/definitions/hintsTimeout"
},
"hints": {
"$ref": "#/definitions/hints"
},
"browserslist": {
"$ref": "#/definitions/browserslist"
},
"ignoredUrls": {
"$ref": "#/definitions/ignoredUrls"
},
"language": {
"$ref": "#/definitions/language"
},
"severityThreshold": {
"$ref": "#/definitions/severityThreshold"
}
},
"anyOf": [
Expand All @@ -148,64 +244,5 @@
]
}
],
"additionalProperties": false,
"definitions": {
"errorlevelstring": {
"type": "string",
"enum": [
"off",
"warning",
"error"
]
},
"errorlevelnumber": {
"type": "number",
"enum": [
0,
1,
2
]
},
"hintconfig": {
"type": "array",
"items": [
{
"anyOf": [
{
"$ref": "#/definitions/errorlevelstring"
},
{
"$ref": "#/definitions/errorlevelnumber"
}
]
},
{
"type": "object"
}
],
"maxItems": 2,
"minItems": 1,
"uniqueItems": true
},
"hintobject": {
"type": "object",
"patternProperties": {
"^.+$": {
"anyOf": [
{
"$ref": "#/definitions/errorlevelstring"
},
{
"$ref": "#/definitions/errorlevelnumber"
},
{
"$ref": "#/definitions/hintconfig"
}
]
}
},
"minItems": 1,
"uniqueItems": true
}
}
"additionalProperties": false
}
18 changes: 12 additions & 6 deletions packages/hint/tests/lib/config/config-hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ class HintWithSchema implements IHint {
test('getSeverity with an string should return the right value', (t) => {
const data = new Map([
['off', 0],
['warning', 1],
['error', 2],
['hint', 1],
['information', 2],
['warning', 3],
['error', 4],
['invalid', null],
['', null]
]);
Expand All @@ -79,7 +81,8 @@ test('getSeverity with a number should return the right value', (t) => {
[0, 0],
[1, 1],
[2, 2],
[3, null],
[3, 3],
[4, 4],
[-1, null]
]);

Expand All @@ -93,13 +96,16 @@ test('getSeverity with a number should return the right value', (t) => {
test('getSeverity with an array should return the right value', (t) => {
const data: Map<HintConfig, number | null> = new Map([
[(['off', {}] as HintConfig), 0],
[(['warning', {}] as HintConfig), 1],
[(['error', {}] as HintConfig), 2],
[(['hint', {}] as HintConfig), 1],
[(['information', {}] as HintConfig), 2],
[(['warning', {}] as HintConfig), 3],
[(['error', {}] as HintConfig), 4],
[(['invalid' as any, {}] as HintConfig), null],
[([0, {}] as HintConfig), 0],
[([1, {}] as HintConfig), 1],
[([2, {}] as HintConfig), 2],
[([3, {}] as HintConfig), null],
[([3, {}] as HintConfig), 3],
[([4, {}] as HintConfig), 4],
[([-1, {}] as HintConfig), null]
]);

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ export type UserConfig = {
ignoredUrls?: IgnoredUrl[];
language?: string;
parsers?: string[];
severityThreshold?: HintSeverity;
};
6 changes: 4 additions & 2 deletions packages/utils/src/types/problems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export { ProblemLocation };
/** The severity configuration of a hint */
export enum Severity {
off = 0,
warning = 1,
error = 2
hint = 1,
information = 2,
warning = 3,
error = 4
}

/** A problem found by a hint */
Expand Down

0 comments on commit a7278f2

Please sign in to comment.