Skip to content
Permalink
Browse files

New: More severities and threshold in schema

Fix #3065
  • Loading branch information
molant committed Oct 3, 2019
1 parent c982bb4 commit a7278f2f78edb07df5e610c3b207c3b779ec1f2b
@@ -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;
}
@@ -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> => {
@@ -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;
}

@@ -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;
}

@@ -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": [
@@ -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": [
@@ -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
}
@@ -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]
]);
@@ -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]
]);

@@ -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]
]);

@@ -57,4 +57,5 @@ export type UserConfig = {
ignoredUrls?: IgnoredUrl[];
language?: string;
parsers?: string[];
severityThreshold?: HintSeverity;
};
@@ -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 */

0 comments on commit a7278f2

Please sign in to comment.
You can’t perform that action at this time.