Skip to content

Commit

Permalink
[WIP] Don't use this in reporter
Browse files Browse the repository at this point in the history
Fixes #358
  • Loading branch information
SimenB committed Oct 8, 2016
1 parent 64e350e commit 30ba3ff
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 112 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
"yargs": "4.7.1"
},
"scripts": {
"cover": "istanbul cover node_modules/.bin/_mocha -- -u exports -R spec test/test.js",
"cover": "istanbul cover node_modules/.bin/_mocha -- -u exports -R spec --recursive",
"lint": "eslint .",
"precover": "npm run lint",
"pretest": "npm run lint",
"start": "node --harmony index.js",
"test": "mocha"
"test": "mocha --recursive"
}
}
71 changes: 13 additions & 58 deletions src/core/done.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
var groupBy = require( 'lodash.groupby' )
var columnify = require( 'columnify' )

function shouldExit1( warningsOrErrors ) {
var maxErrs = typeof this.config.maxErrors === 'number' ? this.config.maxErrors : null
var maxWarnings = typeof this.config.maxWarnings === 'number' ? this.config.maxWarnings : null
function shouldExit1( warningsOrErrors, maxErrors, maxWarnings ) {
var numberofErrors = warningsOrErrors.Error && warningsOrErrors.Error.length || 0
var numberofWarnings = warningsOrErrors.Warning && warningsOrErrors.Warning.length || 0

if ( maxErrs == null && numberofErrors > 0 ) {
if ( maxErrors < 0 && numberofErrors > 0 ) {
return true
}

return maxErrs != null && numberofErrors > this.config.maxErrors ||
return maxErrors != null && numberofErrors > this.config.maxErrors ||
maxWarnings != null && numberofWarnings > this.config.maxWarnings
}

Expand All @@ -22,68 +20,25 @@ function shouldExit1( warningsOrErrors ) {
* @returns {Object | Function} returns process exit if not watching, or obj otherwise
*/
var done = function() {
var msg = ''
var groupedByFile = {}
var msgGrouped
var group = this.config.groupOutputByFile
var opts = this.config.reporterOptions || {}

var violations = this.cache.violations
// TODO: is this needed?
var messages = this.cache.messages
.filter( function( msg ) { return !!msg && !!msg.message } )

var shouldKill = shouldExit1.call( this, groupBy( violations, 'severity' ) )
var maxErrors = typeof this.config.maxErrors === 'number' ? this.config.maxErrors : -1
var maxWarnings = typeof this.config.maxWarnings === 'number' ? this.config.maxWarnings : -1

var shouldKill = shouldExit1.call( this, groupBy( messages, 'severity' ), maxErrors, maxWarnings )

this.state.exitCode = shouldKill ? 1 : 0

// when testing we want to silence the console a bit, so we have the quiet option
if ( !this.state.quiet ) {
violations.forEach( function( msg ) { this.reporter( msg ) }.bind( this ) )

this.reporter( null, 'done', shouldKill ? 'kill' : null )

// by default group warnings and errs by file
if ( group && this.cache.messages ) {
this.cache.messages.forEach( function( output ) {
var file = output.file

if ( groupedByFile.hasOwnProperty( file ) ) {
groupedByFile[file].push( output )
}
else {
groupedByFile[file] = [output]
}
} )

// iterate over arrays of message objects
// each array consists of all the errors and warnings for a file
// columnify the errors/warnings and prefix them with the file name
msgGrouped = Object.keys( groupedByFile ).map( function( key ) {
return key + '\n' + columnify( groupedByFile[key], opts ) + '\n\n'
} )
}

var warningsOrErrors = violations
.map( function( violation ) {
return violation.message
} )
.filter( function( message ) {
return !!message
} )

if ( warningsOrErrors.length ) {
if ( group ) {
msg += msgGrouped
}
else {
msg = warningsOrErrors.join( '\n\n' ) + '\n\n'
}
}
messages.forEach( function( msg ) { this.reporter( msg ) }.bind( this ) )

msg += this.cache.msg
msg = msg ? msg.trim() : msg
var message = this.reporter( messages, shouldKill, maxErrors, maxWarnings )

if ( msg ) {
console.log( msg )
if ( message ) {
console.log( message )
}
}

Expand Down
85 changes: 34 additions & 51 deletions src/core/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,52 @@ var chalk = require( 'chalk' )

/**
* @description format output message for console (default)
* @param {object} msg error msg from one of the checks
* @param {string} done whether or not this is the last message to output
* @param {string} kill whether or not we're over one of our limits
* @return {string | Function} either the formatted msg or done()
* @param {Object[]} messages error msg from one of the checks
* @param {boolean} kill whether or not we're over one of our limits
* @param {Number} maxErrors maximum number of errors
* @param {Number} maxWarnings maximum number of warnings
* @return {string} the formatted message
*/
var reporter = function( msg, done, kill ) {
if ( done === 'done' ) {
this.cache.msg = ''
// TODO: Structure input to this method better
var reporter = function( messages, kill, maxErrors, maxWarnings ) {
var formattedMessages = messages.map( function( msg ) {
var file = chalk.underline( msg.file )

if ( this.cache.violations.length === 0 && kill !== 'kill' ) {
return
}

var violations = groupBy( this.cache.violations, 'severity' )
var numOfErrors = violations.Error ? violations.Error.length : 0
var numOfWarnings = violations.Warning ? violations.Warning.length : 0
var severity = msg.severity.toLowerCase()
severity = severity === 'warning' ?
chalk.yellow( severity ) :
chalk.red( severity )

this.cache.msg = 'Stylint: ' + numOfErrors + ' Errors.'
this.cache.msg += this.config.maxErrors ? ' (Max Errors: ' + this.config.maxErrors + ')' : ''
var rule = chalk.grey( msg.rule )

this.cache.msg += '\nStylint: ' + numOfWarnings + ' Warnings.'
this.cache.msg += this.config.maxWarnings ? ' (Max Warnings: ' + this.config.maxWarnings + ')' : ''

// if you set a max it kills the linter
if ( kill === 'kill' ) {
this.cache.msg += '\nStylint: Over Error or Warning Limit.'
// if column data available, output slightly different line
if ( typeof msg.col === 'number' && msg.col > -1 ) {
return file + '\n' + msg.lineNo + ':' + msg.col + ' ' + rule + ' ' + severity + ' ' + msg.message
}

return
}

var file = chalk.underline( msg.file )
var col = typeof msg.col === 'number' && msg.col > 0 ? msg.col : null

var severity = msg.severity.toLowerCase()
severity = severity === 'warning' ?
chalk.yellow( severity ) :
chalk.red( severity )
return file + '\n' + msg.lineNo + ' ' + rule + ' ' + severity + ' ' + msg.message
} )
.reduce( function( memo, msg ) {
return memo + msg
}, '' )
.trim()

var rule = chalk.grey( msg.rule )
var groupedMessages = groupBy( messages, 'severity' )
var numOfErrors = groupedMessages.Error ? groupedMessages.Error.length : 0
var numOfWarnings = groupedMessages.Warning ? groupedMessages.Warning.length : 0

// normal error or warning messages
var defaultMessage = file + '\n' + msg.lineNo + ' ' + rule + ' ' + severity + ' ' + msg.message
var formattedMessage = 'Stylint: ' + numOfErrors + ' Errors.'
formattedMessage += maxErrors ? ' (Max Errors: ' + maxErrors + ')' : ''

// if column data available, output slightly different line
if ( typeof msg.col === 'number' && msg.col > -1 ) {
defaultMessage = file + '\n' + msg.lineNo + ':' + msg.col + ' ' + rule + ' ' + severity + ' ' + msg.message
}
formattedMessage += '\nStylint: ' + numOfWarnings + ' Warnings.'
formattedMessage += maxWarnings ? ' (Max Warnings: ' + maxWarnings + ')' : ''

// weird syntax highlighting issue when this is inside a ternary
var linePlusCol = msg.lineNo + ':' + col
var messageObj = {
file: file,
lineData: col ? linePlusCol : msg.lineNo,
severity: severity,
description: msg.message,
rule: rule
// if you set a max it kills the linter
if ( kill ) {
formattedMessage += '\nStylint: Over Error or Warning Limit.'
}

messageObj[file] = true
this.cache.messages.push( messageObj )

return defaultMessage
return ( formattedMessages + '\n\n' + formattedMessage ).trim()
}

module.exports = reporter
2 changes: 1 addition & 1 deletion src/utils/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @returns {void}
*/
var msg = function( message, column ) {
this.cache.violations.push( {
this.cache.messages.push( {
message: message,
severity: this.state.severity,
file: this.cache.file,
Expand Down
7 changes: 7 additions & 0 deletions test/core/reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var assert = require( 'assert' )

describe( 'nested test', function() {
it( 'should be executed', function() {
assert( true )
} )
} )

0 comments on commit 30ba3ff

Please sign in to comment.