-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix how `formatter` is used in `cli.ts` * Fix #187
- Loading branch information
Showing
9 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @fileoverview The basic formatter, it just a table format with diferent colors | ||
* for errors and warnings. | ||
* | ||
* This formatter is based on [eslint stylish formatter](https://github.com/eslint/eslint/blob/master/lib/formatters/stylish.js) | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
import * as chalk from 'chalk'; | ||
import * as _ from 'lodash'; | ||
import * as table from 'text-table'; | ||
|
||
import { debug as d } from '../../utils/debug'; | ||
import { IFormatter, Severity } from '../../types'; // eslint-disable-line no-unused-vars | ||
import * as logger from '../../utils/logging'; | ||
|
||
const debug = d(__filename); | ||
|
||
const pluralize = (text, count) => { | ||
return `${text}${count !== 1 ? 's' : ''}`; | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Formatter | ||
// ------------------------------------------------------------------------------ | ||
|
||
const formatter: IFormatter = { | ||
/** Format the problems grouped by `resource` name and sorted by line and column number */ | ||
format(messages) { | ||
|
||
debug('Formatting results'); | ||
|
||
const resources = _.groupBy(messages, 'resource'); | ||
|
||
_.forEach(resources, (msgs, resource) => { | ||
let warnings = 0; | ||
let errors = 0; | ||
const sortedMessages = _.sortBy(msgs, ['line', 'column']); | ||
const tableData = []; | ||
|
||
logger.log(chalk.cyan(`${resource}`)); | ||
_.forEach(sortedMessages, (msg) => { | ||
const severity = Severity.error === msg.severity ? chalk.red('Error') : chalk.yellow('Warning'); | ||
|
||
if (Severity.error === msg.severity) { | ||
errors++; | ||
} else { | ||
warnings++; | ||
} | ||
tableData.push([severity, msg.message, msg.ruleId]); | ||
}); | ||
|
||
logger.log(table(tableData)); | ||
|
||
const color = errors > 0 ? chalk.red : chalk.yellow; | ||
|
||
logger.log(color.bold(`\u2716 Found ${errors} ${pluralize('error', errors)} and ${warnings} ${pluralize('warning', warnings)}`)); | ||
logger.log(''); | ||
}); | ||
} | ||
}; | ||
|
||
export default formatter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import test from 'ava'; | ||
import * as chalk from 'chalk'; | ||
import * as sinon from 'sinon'; | ||
import * as proxyquire from 'proxyquire'; | ||
import * as table from 'text-table'; | ||
|
||
const logging = { log() { } }; | ||
|
||
proxyquire('../../../src/lib/formatters/stylish/stylish', { '../../utils/logging': logging }); | ||
|
||
import stylish from '../../../src/lib/formatters/stylish/stylish'; | ||
import * as problems from './fixtures/list-of-problems'; | ||
|
||
test.beforeEach((t) => { | ||
sinon.spy(logging, 'log'); | ||
|
||
t.context.logger = logging; | ||
}); | ||
|
||
test.afterEach((t) => { | ||
t.context.logger.log.restore(); | ||
}); | ||
|
||
test(`Stylish formatter doesn't print anything if no values`, (t) => { | ||
stylish.format(problems.noproblems); | ||
|
||
t.is(t.context.logger.log.callCount, 0); | ||
}); | ||
|
||
test(`Stylish formatter prints a table and a summary for each resource`, (t) => { | ||
stylish.format(problems.multipleproblemsandresources); | ||
|
||
const log = t.context.logger.log; | ||
let problem = problems.multipleproblemsandresources[0]; | ||
let tableData = []; | ||
|
||
tableData.push([chalk.yellow('Warning'), problem.message, problem.ruleId]); | ||
problem = problems.multipleproblemsandresources[1]; | ||
tableData.push([chalk.yellow('Warning'), problem.message, problem.ruleId]); | ||
|
||
let tableString = table(tableData); | ||
|
||
t.is(log.args[0][0], chalk.cyan('http://myresource.com/')); | ||
t.is(log.args[1][0], tableString); | ||
t.is(log.args[2][0], chalk.yellow.bold(`\u2716 Found 0 errors and 2 warnings`)); | ||
t.is(log.args[3][0], ''); | ||
t.is(log.args[4][0], chalk.cyan('http://myresource2.com/')); | ||
|
||
tableData = []; | ||
problem = problems.multipleproblemsandresources[3]; | ||
tableData.push([chalk.yellow('Warning'), problem.message, problem.ruleId]); | ||
problem = problems.multipleproblemsandresources[2]; | ||
tableData.push([chalk.red('Error'), problem.message, problem.ruleId]); | ||
tableString = table(tableData); | ||
|
||
t.is(log.args[5][0], tableString); | ||
t.is(log.args[6][0], chalk.red.bold(`\u2716 Found 1 error and 1 warning`)); | ||
t.is(log.args[7][0], ''); | ||
}); |