Skip to content
Permalink
Browse files

Chore: Update formatters to support new severity values

Fix #3182
Fix #3174
Close #3345
  • Loading branch information
sarvaje authored and molant committed Nov 15, 2019
1 parent 9c666d7 commit a112f4b99b3b41abf8951c4557ec37d42cb4f866
Showing with 440 additions and 242 deletions.
  1. +1 βˆ’1 package.json
  2. +1 βˆ’1 packages/formatter-codeframe/package.json
  3. +27 βˆ’3 packages/formatter-codeframe/src/_locales/en/messages.json
  4. +23 βˆ’17 packages/formatter-codeframe/src/formatter.ts
  5. +2 βˆ’2 packages/formatter-codeframe/tests/fixtures/list-of-problems.ts
  6. +12 βˆ’10 packages/formatter-codeframe/tests/tests.ts
  7. +1 βˆ’1 packages/formatter-stylish/package.json
  8. +28 βˆ’4 packages/formatter-stylish/src/_locales/en/messages.json
  9. +40 βˆ’25 packages/formatter-stylish/src/formatter.ts
  10. +2 βˆ’2 packages/formatter-stylish/tests/fixtures/list-of-problems.ts
  11. +22 βˆ’19 packages/formatter-stylish/tests/tests.ts
  12. +7 βˆ’5 packages/formatter-summary/package.json
  13. +70 βˆ’0 packages/formatter-summary/src/_locales/en/messages.json
  14. +41 βˆ’20 packages/formatter-summary/src/formatter.ts
  15. +2 βˆ’2 packages/formatter-summary/tests/fixtures/list-of-problems.ts
  16. +14 βˆ’12 packages/formatter-summary/tests/tests.ts
  17. +1 βˆ’0 packages/formatter-summary/tsconfig.json
  18. +1 βˆ’1 packages/hint/package.json
  19. +1 βˆ’1 packages/hint/src/lib/cli.ts
  20. +4 βˆ’4 packages/hint/src/lib/cli/analyze.ts
  21. +1 βˆ’1 packages/hint/src/lib/engine.ts
  22. +1 βˆ’1 packages/hint/tests/lib/cli.ts
  23. +1 βˆ’0 packages/utils/package.json
  24. +2 βˆ’0 packages/utils/src/formatter/index.ts
  25. +17 βˆ’0 packages/utils/src/formatter/occurrences-to-color.ts
  26. +17 βˆ’0 packages/utils/src/formatter/severity-to-color.ts
  27. +1 βˆ’0 packages/utils/src/index.ts
  28. +54 βˆ’0 packages/utils/tests/formatter/occurences-to-color.ts
  29. +34 βˆ’0 packages/utils/tests/formatter/severity-to-color.ts
  30. +12 βˆ’110 yarn.lock
@@ -12,7 +12,7 @@
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.12.0",
"builtin-modules": "^3.1.0",
"chalk": "^2.4.2",
"chalk": "^3.0.0",
"cpx": "^1.5.0",
"eslint": "^6.5.1",
"eslint-plugin-import": "^2.18.2",
@@ -15,7 +15,7 @@
"@hint/utils-i18n": "^1.0.0",
"@hint/utils-string": "^1.0.0",
"@hint/utils-types": "^1.0.0",
"chalk": "^2.4.2",
"chalk": "^3.0.0",
"lodash": "^4.17.15",
"log-symbols": "^3.0.0",
"strip-ansi": "^6.0.0"
@@ -1,9 +1,17 @@
{
"capitalizedError": {
"capitalizederror": {
"description": "Show error text.",
"message": "Error"
},
"capitalizedWarning": {
"capitalizedhint": {
"description": "Show warning text.",
"message": "Hint"
},
"capitalizedinformation": {
"description": "Show information text.",
"message": "Information"
},
"capitalizedwarning": {
"description": "Show warning text.",
"message": "Warning"
},
@@ -15,10 +23,26 @@
"message": "errors",
"description": "Show errors text."
},
"hint": {
"description": "Show hint text.",
"message": "hint"
},
"hints": {
"description": "Show hints text.",
"message": "hints"
},
"hintInfo": {
"description": "Show the information for the hint",
"message": "$1: $2 ($3) at $4$5"
},
"information": {
"description": "Show information text.",
"message": "information"
},
"informations": {
"description": "Show informations text.",
"message": "informations"
},
"warning": {
"message": "warning",
"description": "Show warning text."
@@ -28,7 +52,7 @@
"description": "Show warnings text."
},
"totalFound": {
"message": "Found a total of $1 $2 and $3 $4",
"message": "Found a total of $1 $2, $3 $4, $5 $6 and $7 $8",
"description": "Show a summary of errors and warnings in a hint."
}
}
@@ -11,20 +11,20 @@
* ------------------------------------------------------------------------------
*/

import chalk from 'chalk';
import * as chalk from 'chalk';
import groupBy = require('lodash/groupBy');
import reduce = require('lodash/reduce');
import sortBy = require('lodash/sortBy');
import * as logSymbols from 'log-symbols';
const stripAnsi = require('strip-ansi');

import { logger } from '@hint/utils';
import { logger, severityToColor, occurencesToColor } from '@hint/utils';
import { cutString } from '@hint/utils-string';
import { writeFileAsync } from '@hint/utils-fs';
import { debug as d } from '@hint/utils-debug';
import { FormatterOptions, IFormatter } from 'hint';
import { Problem, ProblemLocation, Severity } from '@hint/utils-types';
import { getMessage } from './i18n.import';
import { getMessage, MessageName } from './i18n.import';

const _ = {
groupBy,
@@ -128,7 +128,7 @@ const codeFrame = (code: string, location: ProblemLocation): string => {
export default class CodeframeFormatter implements IFormatter {
/**
* Format the problems grouped by `resource` name and sorted by line and column number,
* indicating where in the element there is an error.
* indicating where in the element there is an error.
*/
public async format(messages: Problem[], options: FormatterOptions = {}) {
debug('Formatting results');
@@ -140,23 +140,24 @@ export default class CodeframeFormatter implements IFormatter {
}

const resources: _.Dictionary<Problem[]> = _.groupBy(messages, 'resource');
let totalErrors: number = 0;
let totalWarnings: number = 0;
const totals = {
[Severity.error.toString()]: 0,
[Severity.warning.toString()]: 0,
[Severity.information.toString()]: 0,
[Severity.hint.toString()]: 0
};

let result = _.reduce(resources, (total: string, msgs: Problem[], resource: string) => {
const sortedMessages: Problem[] = _.sortBy(msgs, ['location.line', 'location.column']);
const resourceString = chalk.cyan(`${cutString(resource, 80)}`);

const partialResult = _.reduce(sortedMessages, (subtotal: string, msg: Problem) => {
let partial: string;
const severity = Severity.error === msg.severity ? chalk.red(getMessage('capitalizedError', language)) : chalk.yellow(getMessage('capitalizedWarning', language));
const color = severityToColor(msg.severity);
const severity = color(getMessage(`capitalized${Severity[msg.severity].toString()}` as MessageName, language));
const location = msg.location;

if (Severity.error === msg.severity) {
totalErrors++;
} else {
totalWarnings++;
}
totals[msg.severity.toString()]++;

partial = `${getMessage('hintInfo', language, [
severity,
@@ -178,12 +179,17 @@ export default class CodeframeFormatter implements IFormatter {
return total + partialResult;
}, '');

const color: typeof chalk = totalErrors > 0 ? chalk.red : chalk.yellow;
const color = occurencesToColor(totals);

const foundTotalMessage = getMessage('totalFound', language, [
totalErrors.toString(),
totalErrors === 1 ? getMessage('error', language) : getMessage('errors', language),
totalWarnings.toString(),
totalWarnings === 1 ? getMessage('warning', language) : getMessage('warnings', language)
totals[Severity.error].toString(),
totals[Severity.error] === 1 ? getMessage('error', language) : getMessage('errors', language),
totals[Severity.warning].toString(),
totals[Severity.warning] === 1 ? getMessage('warning', language) : getMessage('warnings', language),
totals[Severity.hint].toString(),
totals[Severity.hint] === 1 ? getMessage('hint', language) : getMessage('hints', language),
totals[Severity.information].toString(),
totals[Severity.information] === 1 ? getMessage('information', language) : getMessage('informations', language)
]);

result += color.bold(`${logSymbols.error} ${foundTotalMessage}`);
@@ -67,7 +67,7 @@ const codeframeproblems: Problem[] = [{
},
message: `This is a problem in an element with wrong tabs`,
resource: 'http://myresource.com/',
severity: Severity.warning,
severity: Severity.hint,
sourceCode: `<html lang="en"><head>
<meta charset="utf-8">
<title></title>
@@ -87,7 +87,7 @@ const codeframeproblems: Problem[] = [{
},
message: 'This is a problem inside an element',
resource: 'http://myresource.com/',
severity: Severity.warning,
severity: Severity.information,
sourceCode: `<a href="//link.com">
<img src="//image.jpg"/>
</a>`
@@ -1,11 +1,13 @@
import anyTest, { TestInterface, ExecutionContext } from 'ava';
import chalk from 'chalk';
import * as chalk from 'chalk';
import * as sinon from 'sinon';
import * as proxyquire from 'proxyquire';
import * as logSymbols from 'log-symbols';
const stripAnsi = require('strip-ansi');

import * as problems from './fixtures/list-of-problems';
import { severityToColor } from '@hint/utils';
import { Severity } from '@hint/utils-types';

type Logging = {
log: () => void;
@@ -56,14 +58,14 @@ test(`Codeframe formatter doesn't print anything if no values`, (t) => {
const generateExpectedLogResult = () => {
let problem = problems.codeframeproblems[0];

let expectedLogResult = `${chalk.yellow('Warning')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}`;
let expectedLogResult = `${severityToColor(Severity.warning)('Warning')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}`;

problem = problems.codeframeproblems[1];
let sourceCode = problem.sourceCode.split('\n');

expectedLogResult += `
${chalk.yellow('Warning')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${severityToColor(Severity.warning)('Warning')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${sourceCode[0]}
^
@@ -76,7 +78,7 @@ ${sourceCode[2]}

expectedLogResult += `
${chalk.yellow('Warning')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${severityToColor(Severity.hint)('Hint')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${sourceCode[0]}
^
@@ -89,7 +91,7 @@ ${sourceCode[2]}

expectedLogResult += `
${chalk.yellow('Warning')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${severityToColor(Severity.information)('Information')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${sourceCode[0]}
${sourceCode[1].substr(8)}
@@ -101,7 +103,7 @@ ${sourceCode[2].substr(8)}`;

expectedLogResult += `
${chalk.red('Error')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${severityToColor(Severity.error)('Error')}: ${problem.message} (${problem.hintId}) at ${chalk.cyan(problem.resource)}:${problem.location.line}:${problem.location.column}
${sourceCode[0]}
${sourceCode[1]}
@@ -110,7 +112,7 @@ ${sourceCode[2].substr(8)}
${sourceCode[3]}
…
${chalk.red.bold(`${logSymbols.error} Found a total of 1 error and 4 warnings`)}`;
${severityToColor(Severity.error).bold(`${logSymbols.error} Found a total of 1 error, 2 warnings, 1 hint and 1 information`)}`;

return expectedLogResult;
};
@@ -138,7 +140,7 @@ ${sourceCode[2]}

expectedLogResult += `
Warning: ${problem.message} (${problem.hintId}) at ${problem.resource}:${problem.location.line}:${problem.location.column}
Hint: ${problem.message} (${problem.hintId}) at ${problem.resource}:${problem.location.line}:${problem.location.column}
${sourceCode[0]}
^
@@ -151,7 +153,7 @@ ${sourceCode[2]}

expectedLogResult += `
Warning: ${problem.message} (${problem.hintId}) at ${problem.resource}:${problem.location.line}:${problem.location.column}
Information: ${problem.message} (${problem.hintId}) at ${problem.resource}:${problem.location.line}:${problem.location.column}
${sourceCode[0]}
${sourceCode[1].substr(8)}
@@ -172,7 +174,7 @@ ${sourceCode[2].substr(8)}
${sourceCode[3]}
…
${stripAnsi(logSymbols.error)} Found a total of 1 error and 4 warnings`;
${stripAnsi(logSymbols.error)} Found a total of 1 error, 2 warnings, 1 hint and 1 information`;

return expectedLogResult;
};
@@ -15,7 +15,7 @@
"@hint/utils-i18n": "^1.0.0",
"@hint/utils-string": "^1.0.0",
"@hint/utils-types": "^1.0.0",
"chalk": "^2.4.2",
"chalk": "^3.0.0",
"lodash": "^4.17.15",
"log-symbols": "^3.0.0",
"strip-ansi": "^6.0.0",
@@ -3,11 +3,19 @@
"message": "line",
"description": "Indiates the line of an error."
},
"capitalizedError": {
"capitalizederror": {
"description": "Show error text.",
"message": "Error"
},
"capitalizedWarning": {
"capitalizedhint": {
"description": "Show warning text.",
"message": "Hint"
},
"capitalizedinformation": {
"description": "Show information text.",
"message": "Information"
},
"capitalizedwarning": {
"description": "Show warning text.",
"message": "Warning"
},
@@ -23,12 +31,28 @@
"message": "errors",
"description": "Show errors text."
},
"hint": {
"description": "Show hint text.",
"message": "hint"
},
"hints": {
"description": "Show hints text.",
"message": "hints"
},
"information": {
"description": "Show information text.",
"message": "information"
},
"informations": {
"description": "Show informations text.",
"message": "informations"
},
"partialFound": {
"message": "Found $1 $2 and $3 $4",
"message": "Found $1 $2, $3 $4, $5 $6 and $7 $8",
"description": "Show a summary of errors and warnings in a hint."
},
"totalFound": {
"message": "Found a total of $1 $2 and $3 $4",
"message": "Found a total of $1 $2, $3 $4, $5 $6 and $7 $8",
"description": "Show a summary of errors and warnings in a hint."
},
"warning": {

0 comments on commit a112f4b

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