Skip to content

Commit

Permalink
Chore: Update formatters to support new severity values
Browse files Browse the repository at this point in the history
Fix #3182
Fix #3174
Close #3345
  • Loading branch information
sarvaje authored and molant committed Nov 15, 2019
1 parent 9c666d7 commit a112f4b
Show file tree
Hide file tree
Showing 30 changed files with 440 additions and 242 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/formatter-codeframe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 27 additions & 3 deletions packages/formatter-codeframe/src/_locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand All @@ -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."
Expand All @@ -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."
}
}
40 changes: 23 additions & 17 deletions packages/formatter-codeframe/src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand All @@ -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,
Expand All @@ -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}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>`
Expand Down
22 changes: 12 additions & 10 deletions packages/formatter-codeframe/tests/tests.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]}
^
Expand All @@ -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]}
^
Expand All @@ -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)}
Expand All @@ -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]}
Expand All @@ -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;
};
Expand Down Expand Up @@ -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]}
^
Expand All @@ -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)}
Expand All @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/formatter-stylish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 28 additions & 4 deletions packages/formatter-stylish/src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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": {
Expand Down
Loading

0 comments on commit a112f4b

Please sign in to comment.