Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
Compatibility for eslint plugin, (mainly adding column numbers in out…
Browse files Browse the repository at this point in the history
…put)
  • Loading branch information
aminland committed Feb 23, 2018
1 parent 0f32767 commit e370247
Show file tree
Hide file tree
Showing 37 changed files with 71 additions and 37 deletions.
6 changes: 6 additions & 0 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ task 'compile:commandline', 'Compiles commandline.js', ->
coffeeSync 'src/cache.coffee', 'lib/cache.js'
coffeeSync 'src/ruleLoader.coffee', 'lib/ruleLoader.js'
fs.mkdirSync 'lib/reporters' unless fs.existsSync 'lib/reporters'
fs.mkdirSync 'lib/rules' unless fs.existsSync 'lib/rules'
for src in glob.sync('reporters/*.coffee', { cwd: 'src' })
# Slice the "coffee" extension of the end and replace with js
dest = src[...-6] + 'js'
coffeeSync "src/#{src}", "lib/#{dest}"

for src in glob.sync('rules/*.coffee', { cwd: 'src' })
# Slice the "coffee" extension of the end and replace with js
dest = src[...-6] + 'js'
coffeeSync "src/#{src}", "lib/#{dest}"

task 'compile:browserify', 'Uses browserify to compile coffeelint', ->
opts =
standalone: 'coffeelint'
Expand Down
3 changes: 3 additions & 0 deletions src/ast_linter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ module.exports = class ASTLinter extends BaseLinter
lineNumber = -1
if coffeeError.location?
lineNumber = coffeeError.location.first_line + 1
columnNumber = coffeeError.location.first_column + 1
else
match = /line (\d+)/.exec message
lineNumber = parseInt match[1], 10 if match?.length > 1
columnNumber = 1
attrs = {
message: message
level: rule.level
lineNumber: lineNumber
columnNumber: columnNumber
}
return @createError 'coffeescript_error', attrs
9 changes: 9 additions & 0 deletions src/lexical_linter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ module.exports = class LexicalLinter extends BaseLinter
attrs.lineNumber ?= @lineNumber
attrs.lineNumber += 1
attrs.line = @tokenApi.lines[attrs.lineNumber - 1]
if attrs.token
token = attrs.token
attrs.lineNumber = token[2].first_line + 1
attrs.columnNumber = token[2].first_column + 1
if token[2].last_line
attrs.lineNumberEnd = token[2].last_line + 1
if token[2].last_column
attrs.columnNumberEnd = token[2].last_column + 1

super ruleName, attrs
2 changes: 1 addition & 1 deletion src/line_linter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ module.exports = class LineLinter extends BaseLinter


createError: (rule, attrs = {}) ->
attrs.lineNumber = @lineNumber + 1 # Lines are indexed by zero.
attrs.lineNumber ?= @lineNumber + 1 # Lines are indexed by zero.
attrs.level = @config[rule]?.level
super rule, attrs
2 changes: 1 addition & 1 deletion src/rules/arrow_spacing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ module.exports = class ArrowSpacing
pp.generated? or #2
pp[0] is 'INDENT' or #3
(pp[1] is '(' and not pp.generated?)) #4
true
{ token }
else
null
2 changes: 1 addition & 1 deletion src/rules/braces_spacing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ module.exports = class BracesSpacing
msg = "There should be #{expected} space"
msg += 's' unless expected is 1
msg += " inside \"#{token[0]}\""
context: msg
{ token, context: msg }
2 changes: 1 addition & 1 deletion src/rules/camel_case_classes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ module.exports = class CamelCaseClasses

# Now check for the error.
if not regexes.camelCase.test(className)
return { context: "class name: #{className}" }
return { token, context: "class name: #{className}" }
1 change: 1 addition & 0 deletions src/rules/colon_assignment_spacing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ module.exports = class ColonAssignmentSpacing
if token.csxColon or isLeftSpaced and isRightSpaced
null
else
token: token
context: "Incorrect spacing around column #{token[2].first_column}"
2 changes: 2 additions & 0 deletions src/rules/cyclomatic_complexity.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ module.exports = class CyclomaticComplexity
context: complexity + 1
lineNumber: node.locationData.first_line + 1
lineNumberEnd: node.locationData.last_line + 1
columnNumber: node.locationData.first_column + 1
columnNumberEnd: node.locationData.last_column + 1
}
@errors.push error if error

Expand Down
2 changes: 1 addition & 1 deletion src/rules/duplicate_key.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = class DuplicateKey
# Added a prefix to not interfere with things like "constructor".
key = "identifier-#{key}"
if @currentScope[key]
return true
return { token }
else
@currentScope[key] = token
null
Expand Down
6 changes: 3 additions & 3 deletions src/rules/empty_constructor_needs_parens.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module.exports = class EmptyConstructorNeedsParens
# line with implicit parens, and if your parameters start on the
# next line, but is missing if there are no params and no parens.
if isIdent and nextToken?
return @handleExpectedCallStart(nextToken)
return @handleExpectedCallStart(nextToken, tokenApi)

handleExpectedCallStart: (isCallStart) ->
handleExpectedCallStart: (isCallStart, tokenApi) ->
if isCallStart[0] isnt 'CALL_START'
return true
return { token: tokenApi.peek(isCallStart, 1) }
4 changes: 2 additions & 2 deletions src/rules/ensure_comprehensions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = class EnsureComprehensions
break

if prevToken[0] is '=' and numParenEnds is numParenStarts
atEqual = true
atEqual = { token }

peeker--

Expand All @@ -62,7 +62,7 @@ module.exports = class EnsureComprehensions
# amount of CALL_START/CALL_END tokens. An unequal number means the list
# comprehension is inside of a function call
if atEqual and numCallStarts is numCallEnds
return { context: '' }
return { token, context: '' }

findIdents: (tokenApi) ->
peeker = 1
Expand Down
3 changes: 2 additions & 1 deletion src/rules/indentation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports = class Indentation

if dotIndent % expected isnt 0
return {
token,
context: "Expected #{expected} got #{got}"
}

Expand Down Expand Up @@ -97,7 +98,7 @@ module.exports = class Indentation

# Now check the indentation.
if not ignoreIndent and not (expected in numIndents)
return { context: "Expected #{expected} got #{numIndents[0]}" }
return { token, context: "Expected #{expected} got #{numIndents[0]}" }

# Return true if the current token is inside of an array.
inArray: () ->
Expand Down
2 changes: 1 addition & 1 deletion src/rules/line_endings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ module.exports = class LineEndings
else
throw new Error("unknown line ending type: #{ending}")
if not valid
return { context: "Expected #{ending}" }
return { columnNumber: line.length, context: "Expected #{ending}" }
else
return null
1 change: 1 addition & 0 deletions src/rules/max_line_length.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ module.exports = class MaxLineLength
return

return {
columnNumber: max
context: "Length is #{lineLength}, max is #{max}"
}
1 change: 1 addition & 0 deletions src/rules/missing_fat_arrows.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports = class MissingFatArrows
(@needsFatArrow node)
error = @astApi.createError
lineNumber: node.locationData.first_line + 1
columnNumber: node.locationData.first_column + 1
@errors.push error

node.eachChild (child) => @lintNode child,
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no_backticks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ module.exports = class NoBackticks
tokens: ['JS']

lintToken: (token, tokenApi) ->
return not token.comments?
if not token.comments?
{ token }
4 changes: 2 additions & 2 deletions src/rules/no_debugger.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ module.exports = class NoDebugger

lintToken: (token, tokenApi) ->
if token[0] in ['DEBUGGER', 'STATEMENT'] and token[1] is 'debugger'
return { context: "found '#{token[0]}'" }
return { token, context: "found '#{token[0]}'" }

if tokenApi.config[@rule.name]?.console
if token[1] is 'console' and tokenApi.peek(1)?[0] is '.'
method = tokenApi.peek(2)
return { context: "found 'console.#{method[1]}'" }
return { token, context: "found 'console.#{method[1]}'" }
1 change: 1 addition & 0 deletions src/rules/no_empty_functions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ module.exports = class NoEmptyFunctions
if isEmptyCode node, astApi
error = astApi.createError
lineNumber: node.locationData.first_line + 1
columnNumber: node.locationData.first_column + 1
@errors.push error
node.eachChild (child) => @lintNode child, astApi
3 changes: 2 additions & 1 deletion src/rules/no_empty_param_list.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ module.exports = class NoEmptyParamList

lintToken: (token, tokenApi) ->
nextType = tokenApi.peek()[0]
return nextType is 'PARAM_END'
if nextType is 'PARAM_END'
return { token }
2 changes: 1 addition & 1 deletion src/rules/no_implicit_braces.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = class NoImplicitBraces
if peekIdent is @className
return

return true
return { token: tokenApi.peek(c + 1) }

trackClass: (token, tokenApi) ->

Expand Down
4 changes: 2 additions & 2 deletions src/rules/no_implicit_parens.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = class NoImplicitParens
lintToken: (token, tokenApi) ->
if token.generated
unless tokenApi.config[@rule.name].strict is false
return true
return { token }
else
# If strict mode is turned off it allows implicit parens when
# the expression is spread over multiple lines.
Expand All @@ -36,7 +36,7 @@ module.exports = class NoImplicitParens
genCallStart = t[0] is 'CALL_START' and t.generated

if not t? or genCallStart and sameLine
return true
return { token: t or token }

# If we have not found a CALL_START token that is generated,
# and we've moved into a new line, this is fine and should
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no_interpolation_in_single_quotes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ module.exports = class NoInterpolationInSingleQuotes
lintToken: (token, tokenApi) ->
tokenValue = token[1]
hasInterpolation = tokenValue.match(/^\'.*#\{[^}]+\}.*\'$/)
return hasInterpolation
if hasInterpolation
return { token }
5 changes: 3 additions & 2 deletions src/rules/no_nested_string_interpolation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = class NoNestedStringInterpolation
constructor: ->
@blocks = []

lintToken: ([tag], tokenApi) ->
lintToken: (token, tokenApi) ->
[tag] = token
@blocks.push [] unless @blocks.length

block = @blocks[@blocks.length - 1]
Expand All @@ -49,5 +50,5 @@ module.exports = class NoNestedStringInterpolation
# Don't make multiple errors for deeply nested interpolation
if block.strCount > 1 and not block.error
block.error = true
return true
return { token }
return
2 changes: 1 addition & 1 deletion src/rules/no_plusplus.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ module.exports = class NoPlusPlus
tokens: ['++', '--']

lintToken: (token, tokenApi) ->
return { context: "found '#{token[0]}'" }
return { token, context: "found '#{token[0]}'" }
1 change: 1 addition & 0 deletions src/rules/no_private_function_fat_arrows.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = class NoPrivateFunctionFatArrows
if @isFatArrowCode(node) and node in functions
error = @astApi.createError
lineNumber: node.locationData.first_line + 1
columnNumber: node.locationData.first_column + 1
@errors.push error

node.eachChild (child) => @lintNode child,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no_stand_alone_at.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ module.exports = class NoStandAloneAt
# is an property, the start of an index '[' or is an property after
# the '::'
unless (isDot or (noSpace and (isProp or isAStart or isProtoProp)))
return true
return { token }
2 changes: 1 addition & 1 deletion src/rules/no_tabs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ module.exports = class NoTabs
# is the start of the expression.
indentation = line.split(indentationRegex)[0]
if lineApi.lineHasToken() and '\t' in indentation
true
{ columnNumber: indentation.indexOf('\t') }
else
null
2 changes: 1 addition & 1 deletion src/rules/no_this.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ module.exports = class NoThis
{ config: { no_stand_alone_at: { level } } } = tokenApi
nextToken = tokenApi.peek(1)?[0]

true unless level isnt 'ignore' and nextToken isnt '.'
{ token } unless level isnt 'ignore' and nextToken isnt '.'
3 changes: 2 additions & 1 deletion src/rules/no_throwing_strings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ module.exports = class NoThrowingStrings

nextIsString = n1 is 'STRING' or n1 is 'STRING_START'

return nextIsString
if nextIsString
{ token }
7 changes: 4 additions & 3 deletions src/rules/no_trailing_whitespace.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
regexes =
trailingWhitespace: /[^\s]+[\t ]+\r?$/
onlySpaces: /^[\t ]+\r?$/
spacesStart: /[\t ]+\r?$/
lineHasComment: /^\s*[^\#]*\#/

module.exports = class NoTrailingWhitespace
Expand All @@ -19,12 +20,12 @@ module.exports = class NoTrailingWhitespace
lintLine: (line, lineApi) ->
unless lineApi.config['no_trailing_whitespace']?.allowed_in_empty_lines
if regexes.onlySpaces.test(line)
return true
return { columnNumber: line.match(regexes.spacesStart).length + 1 }

if regexes.trailingWhitespace.test(line)
# By default only the regex above is needed.
unless lineApi.config['no_trailing_whitespace']?.allowed_in_comments
return true
return { columnNumber: line.match(regexes.spacesStart).index + 1 }

line = line
tokens = lineApi.tokensByLine[lineApi.lineNumber]
Expand All @@ -40,4 +41,4 @@ module.exports = class NoTrailingWhitespace
line = line.replace(str, 'STRING')

if !regexes.lineHasComment.test(line)
return true
return { columnNumber: line.match(regexes.spacesStart).index + 1 }
3 changes: 2 additions & 1 deletion src/rules/no_unnecessary_double_quotes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ module.exports = class NoUnnecessaryDoubleQuotes
return false

hasLegalConstructs = @isInInterpolation() or @hasSingleQuote(tokenValue)
return not hasLegalConstructs
if not hasLegalConstructs
{ token }

isInInterpolation: () ->
@interpolationLevel > 0
Expand Down
1 change: 1 addition & 0 deletions src/rules/no_unnecessary_fat_arrows.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = class NoUnnecessaryFatArrows
if (@isFatArrowCode node) and (not @needsFatArrow node)
error = @astApi.createError
lineNumber: node.locationData.first_line + 1
columnNumber: node.locationData.first_column + 1
@errors.push error
node.eachChild (child) => @lintNode child

Expand Down
4 changes: 2 additions & 2 deletions src/rules/non_empty_constructor_needs_parens.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ module.exports = class NonEmptyConstructorNeedsParens extends ParentClass
Requires constructors with parameters to include the parens
'''

handleExpectedCallStart: (isCallStart) ->
handleExpectedCallStart: (isCallStart, tokenApi) ->
if isCallStart[0] is 'CALL_START' and isCallStart.generated
return true
return { token: tokenApi.peek(isCallStart, 1) }
2 changes: 1 addition & 1 deletion src/rules/prefer_english_operator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ module.exports = class PreferEnglishOperator
else undefined

if context?
{ level, context }
{ token, level, context }
4 changes: 2 additions & 2 deletions src/rules/space_operators.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ module.exports = class SpaceOperators
if notFirstToken and ((isUnary and token.spaced?) or
(not isUnary and not token.newLine and
(not token.spaced or (p and not p.spaced))))
return { context: token[1] }
return { token, context: token[1] }
else
null

lintMath: (token, tokenApi) ->
p = tokenApi.peek(-1)
if not token.newLine and (not token.spaced or (p and not p.spaced))
return { context: token[1] }
return { token, context: token[1] }
else
null

Expand Down
2 changes: 1 addition & 1 deletion src/rules/spacing_after_comma.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = class SpacingAfterComma

unless token.spaced or token.newLine or @isGenerated(token, tokenApi) or
@isRegexFlag(token, tokenApi)
return true
return { token }

# Coffeescript does some code generation when using CSX syntax, and it adds
# brackets & commas that are not marked as generated. The only way to check
Expand Down

0 comments on commit e370247

Please sign in to comment.