Skip to content

Commit

Permalink
CSVReporter: Properly double quote field
Browse files Browse the repository at this point in the history
Follows this http://tools.ietf.org/html/rfc4180 in regards to using
double quotes around fields that can have commas or spaces, as well as
using two quotes ("") to escape inside a field's value

Closes clutchski#298
  • Loading branch information
swang committed Oct 7, 2015
1 parent 6d6b9f4 commit 3dad060
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/reporters/csv.coffee
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
module.exports = class CSVReporter

constructor : (@errorReport, options = {}) ->
constructor: (@errorReport, options = {}) ->

print : (message) ->
print: (message) ->
# coffeelint: disable=no_debugger
console.log message
# coffeelint: enable=no_debugger

publish : () ->
header = ["path","lineNumber", "lineNumberEnd", "level", "message"]
publish: () ->
header = ["path", "lineNumber", "lineNumberEnd", "level", "message"]
@print header.join(",")
for path, errors of @errorReport.paths
for e in errors
# Having the context is useful for the cyclomatic_complexity
# rule and critical for the undefined_variables rule.
e.message += " #{e.context}." if e.context
e.message += " #{e.context}" if e.context
f = [
path
e.lineNumber
e.lineNumberEnd ? e.lineNumberEnd
e.level
e.message
'"' + e.message.replace(/\"/g, "") + '"'
]
@print f.join(",")
6 changes: 3 additions & 3 deletions src/reporters/raw.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports = class RawReporter

constructor : (@errorReport, options = {}) ->
constructor: (@errorReport, options = {}) ->

print : (message) ->
print: (message) ->
# coffeelint: disable=no_debugger
console.log message
# coffeelint: enable=no_debugger

publish : () ->
publish: () ->
@print JSON.stringify(@errorReport.paths, undefined, 2)
57 changes: 52 additions & 5 deletions test/test_reporters.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,33 @@ assert = require 'assert'
###
coffeelint = require path.join('..', 'lib', 'coffeelint')
RawReporter = require path.join('..', 'lib', 'reporters', 'raw')
CSVReporter = require path.join('..', 'lib', 'reporters', 'csv')

class TestCSVReporter extends CSVReporter
output = ''

print: (input) ->
output += input + '\r\n'

publish: () ->
super()
output

class PassThroughReporter extends RawReporter
print: (input) ->
return JSON.parse(input)

vows.describe('reporters').addBatch({

'Can be used by 3rd party projects' :
'Can be used by 3rd party projects':

topic : """
topic:
'''
if true
undefined
"""
'''

'(example)' : (code) ->
'(example)': (code) ->

# Grab your own ErrorReport
errorReport = coffeelint.getErrorReport()
Expand All @@ -40,5 +52,40 @@ vows.describe('reporters').addBatch({
error = result.stdin[0]
assert.equal(error.name, 'indentation')

}).export(module)
'Make sure CSV is properly escaped':
topic:
'''
class X
y: ->
'''

'Make sure CSV columns are quoted, and newlines are escaped': (code) ->
config =
colon_assignment_spacing:
level: 'error'
spacing:
left: 0
right: 0

errorReport = coffeelint.getErrorReport()
errorReport.lint 'stdin', code, config

# Construct a new reporter and publish the results. You can use the
# built in reporters, or make your own.
reporter = new TestCSVReporter errorReport
result = reporter.publish().split(/\r?\n/)
output = result[1].split(',')

assert.equal(output[0], 'stdin')
assert.equal(output[1], 2)
assert.equal(output[2], '')
assert.equal(output[3], 'error')
context =
'"Colon assignment without proper spacing Incorrect spacing ' +
'around column 3.'

assert.equal(output[4], context)
assert.equal(result[2], 'Expected left: 0, right: 0.')
assert.equal(result[3], 'Got left: 0, right: 1."')

}).export(module)

0 comments on commit 3dad060

Please sign in to comment.