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

colon_assignment_spacing: add optional properties min_left, min_right #594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/rules/colon_assignment_spacing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = class ColonAssignmentSpacing
spacing:
left: 0
right: 0
min_left: -1
min_right: -1
description: '''
<p>This rule checks to see that there is spacing before and
after the colon in a colon assignment (i.e., classes, objects).
Expand Down Expand Up @@ -37,27 +39,26 @@ module.exports = class ColonAssignmentSpacing
previousToken = tokenApi.peek -1
nextToken = tokenApi.peek 1

getSpaceFromToken = (direction) ->
switch direction
checkSpacing = (direction) ->
spacing = switch direction
when 'left'
token[2].first_column - previousToken[2].last_column - 1
when 'right'
else # when 'right'
nextToken[2].first_column - token[2].first_column - 1

checkSpacing = (direction) ->
spacing = getSpaceFromToken direction
# when spacing is negative, the neighboring token is a newline
isSpaced = if spacing < 0
if spacing < 0
true
else
spacing is parseInt spaceRules[direction]

[isSpaced, spacing]

[isLeftSpaced, leftSpacing] = checkSpacing 'left'
[isRightSpaced, rightSpacing] = checkSpacing 'right'
minDirection = parseInt spaceRules['min_' + direction]
# if a minimal spacing is specified, only check that
if minDirection >= 0
spacing >= minDirection
# otherwise check exact spacing
else
spacing is parseInt spaceRules[direction]

if isLeftSpaced and isRightSpaced
if (checkSpacing 'left') and (checkSpacing 'right')
null
else
context: "Incorrect spacing around column #{token[2].first_column}"
30 changes: 30 additions & 0 deletions test/test_colon_assignment_spacing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,34 @@ vows.describe(RULE).addBatch({
errors = coffeelint.lint(source, config)
assert.isEmpty(errors)

'Should ignore exact specification in case of minimum spacing':
topic:
'''
object = {spacing: true}
object =
spacing: true
object =
spacing : true
object =
spacing: true
object =
spacing : true
class Dog
barks: true
stringyObject =
'stringkey': 'ok'
'''

'will return an error': (source) ->
config =
colon_assignment_spacing:
level: 'error'
spacing:
left: 1
right: 1
min_left: 0
min_right: 1
errors = coffeelint.lint(source, config)
assert.isEmpty(errors)

}).export(module)