Skip to content

Commit

Permalink
Drop separate err and warnings on cache
Browse files Browse the repository at this point in the history
Fixes #357
  • Loading branch information
SimenB committed Oct 8, 2016
1 parent 0081cf4 commit b329507
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 122 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"columnify": "1.5.4",
"glob": "7.0.4",
"lodash.defaults": "4.0.1",
"lodash.groupby": "4.6.0",
"path-is-absolute": "1.0.0",
"stampit": "1.2.0",
"strip-json-comments": "2.0.1",
Expand Down
7 changes: 3 additions & 4 deletions src/core/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var cache = {
col: null, // column number for warning if applicable
comment: '', // the current line comment on the line, if there is one
customProperties: [], // an array of all declared mixins OR custom properties
errs: [], // array of errors detected so far
dir: path.dirname( require.main.filename ), // index.js directory
file: '', // curr filename we're testing
files: [], // all files as an arr
filesLen: 0, // # of files we're testing
Expand All @@ -19,10 +19,9 @@ var cache = {
prevFileNo: 0, // prev file no
prevLine: '', // the previous line
rule: '', // rule name for current check
dir: path.dirname( require.main.filename ), // index.js directory
sCache: { '0': [] }, // each key is an array of selectors in that context
sortOrderCache: [], // we keep a context based arr of selectors here to check sort orde
warnings: [], // array of the errors detected so far
sortOrderCache: [], // we keep a context based arr of selectors here to check sort order
violations: [], // array of all violations
zCache: [] // array of z-index uses
}

Expand Down
44 changes: 26 additions & 18 deletions src/core/done.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
'use strict'

var groupBy = require( 'lodash.groupby' )
var columnify = require( 'columnify' )

function getExitCode( errsLength, warningsLength, maxErrors, maxWarnings ) {
if ( errsLength > 0 ) {
if ( typeof maxErrors === 'number' ) {
if ( errsLength > maxErrors ) return 1
}
else return 1
}

if ( typeof maxWarnings === 'number' && warningsLength > maxWarnings ) return 1
function shouldExit1( warningsOrErrors ) {
var maxErrs = typeof this.config.maxErrors === 'number' ? this.config.maxErrors : false
var maxWarnings = typeof this.config.maxWarnings === 'number' ? this.config.maxWarnings : false

return 0
return maxErrs && warningsOrErrors.Error && warningsOrErrors.Error.length > this.config.maxErrors ||
maxWarnings && warningsOrErrors.Warning && warningsOrErrors.Warning.length > this.config.maxWarnings
}

/**
* @description outputs our messages, wipes errs/warnings if watching
* @returns {Object | Function} returns process exit if not watching, or obj otherwise
*/
var done = function() {
var warningsOrErrors = []
var msg = ''
var groupedByFile = {}
var msgGrouped
var group = this.config.groupOutputByFile
var opts = this.config.reporterOptions || {}

this.state.exitCode = getExitCode( this.cache.errs.length, this.cache.warnings.length, this.config.maxErrors, this.config.maxWarnings )
var violations = this.cache.violations
.filter( function( msg ) { return !!msg && !!msg.message } )

var shouldKill = shouldExit1.call( this, groupBy( violations, 'severity' ) )

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 ) {
warningsOrErrors = [].concat( this.cache.errs, this.cache.warnings )
.filter( function( str ) { return !!str } )
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 ) {
Expand All @@ -55,6 +56,14 @@ var done = function() {
} )
}

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

if ( warningsOrErrors.length ) {
if ( group ) {
msg += msgGrouped
Expand All @@ -65,6 +74,7 @@ var done = function() {
}

msg += this.cache.msg
msg = msg ? msg.trim() : msg

if ( msg ) {
console.log( msg )
Expand All @@ -79,15 +89,13 @@ var done = function() {
}

var returnValue = {
errs: this.cache.errs.slice( 0 ),
warnings: this.cache.warnings.slice( 0 ),
violations: this.cache.violations.slice( 0 ),
exitCode: this.state.exitCode,
msg: this.cache.msg
}

// if watching we reset the errors/warnings arrays
this.cache.errs = []
this.cache.warnings = []
this.cache.violations = []

return returnValue
}
Expand Down
13 changes: 1 addition & 12 deletions src/core/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
* @return {Function | undefined} undefined if just calling the method, function is linting over
*/
var lint = function() {
var method = ''
var method
var checks = Object.getPrototypeOf( this ).lintMethods
var maxErrs = typeof this.config.maxErrors === 'number' ? this.config.maxErrors : false
var maxWarnings = typeof this.config.maxWarnings === 'number' ? this.config.maxWarnings : false

for ( method in checks ) {
if ( checks.hasOwnProperty( method ) ) {
Expand All @@ -22,15 +20,6 @@ var lint = function() {
this.state.severity = this.config[method].error ? 'Error' : 'Warning'
// run the actual check against the line
checks[method].call( this, this.cache.line, this.cache.origLine )
// if check puts us over either limit, kill stylint
if ( maxErrs &&
this.cache.errs.length > this.config.maxErrors ) {
return this.reporter( '', 'done', 'kill' )
}
if ( maxWarnings &&
this.cache.warnings.length > this.config.maxWarnings ) {
return this.reporter( '', 'done', 'kill' )
}
}
}
}
Expand Down
49 changes: 27 additions & 22 deletions src/core/reporter.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,66 @@
'use strict'

var groupBy = require( 'lodash.groupby' )
var chalk = require( 'chalk' )


/**
* @description format output message for console (default)
* @param {string} 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
* @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()
*/
var reporter = function( msg, done, kill ) {
if ( done === 'done' ) {
// total errors
this.cache.msg = 'Stylint: ' + this.cache.errs.length + ' Errors.'
this.cache.msg = ''

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

var violations = groupBy( this.cache.violations, 'severity' )
var numOfErrors = violations.Error ? violations.Error.length : 0
var numOfWarnings = violations.Warning ? violations.Warning.length : 0

this.cache.msg = 'Stylint: ' + numOfErrors + ' Errors.'
this.cache.msg += this.config.maxErrors ? ' (Max Errors: ' + this.config.maxErrors + ')' : ''
// total warnings
this.cache.msg += '\nStylint: ' + this.cache.warnings.length + ' Warnings.'

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.'
}
else if ( this.cache.errs.length === 0 &&
this.cache.warnings.length === 0 ) {
this.cache.msg = ''
}

return this.done()
}

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

var severity = this.state.severity.toLowerCase()
var severity = msg.severity.toLowerCase()
severity = severity === 'warning' ?
chalk.yellow( severity ) :
chalk.red( severity )

var rule = chalk.grey( this.cache.rule )
var rule = chalk.grey( msg.rule )

// normal error or warning messages
var defaultMessage = file + '\n' + this.cache.lineNo + ' ' + rule + ' ' + severity + ' ' + msg
var defaultMessage = file + '\n' + msg.lineNo + ' ' + rule + ' ' + severity + ' ' + msg.message

// if column data available, output slightly different line
if ( typeof this.cache.col === 'number' && this.cache.col > -1 ) {
defaultMessage = file + '\n' + this.cache.lineNo + ':' + this.cache.col + ' ' + rule + ' ' + severity + ' ' + msg
if ( typeof msg.col === 'number' && msg.col > -1 ) {
defaultMessage = file + '\n' + msg.lineNo + ':' + msg.col + ' ' + rule + ' ' + severity + ' ' + msg.message
}

// weird syntax highlighting issue when this is inside a ternary
var linePlusCol = this.cache.lineNo + ':' + col
var linePlusCol = msg.lineNo + ':' + col
var messageObj = {
file: file,
lineData: col ? linePlusCol : this.cache.lineNo,
lineData: col ? linePlusCol : msg.lineNo,
severity: severity,
description: msg,
description: msg.message,
rule: rule
}

Expand Down
25 changes: 14 additions & 11 deletions src/utils/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@


/**
* @description basically just sets the severity and routes output to the reporter
* @param {string} [str] outputted string from one of the checks
* @description add violation to an array
* @param {string} [message] outputted string from one of the checks
* @param {number} [column] column number if applicable to the check
* @returns {Function} push formatted output to appropriate array
*/
var msg = function( str, column ) {
// determine which group the msg belongs to
var arr = this.state.severity === 'Warning' ? this.cache.warnings : this.cache.errs
this.cache.col = column

// push the final output
return arr.push( this.reporter( str ) )
* @returns {void}
*/
var msg = function( message, column ) {
this.cache.violations.push( {
message: message,
severity: this.state.severity,
file: this.cache.file,
lineNo: this.cache.lineNo,
col: column,
origLine: this.cache.origLine,
rule: this.cache.rule
} )
}

module.exports = msg
3 changes: 1 addition & 2 deletions src/utils/resetOnChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*/
var resetOnChange = function( newPath ) {
this.state.path = newPath ? newPath : this.state.path
this.cache.errs = []
this.cache.warnings = []
this.cache.violations = []
this.cache.alphaCache = []
this.cache.rootCache = []
this.cache.zCache = []
Expand Down
Loading

0 comments on commit b329507

Please sign in to comment.