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

no_spaces #556

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions src/coffeelint.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ coffeelint.getRules = ->
coffeelint.registerRule require './rules/arrow_spacing.coffee'
coffeelint.registerRule require './rules/braces_spacing.coffee'
coffeelint.registerRule require './rules/no_tabs.coffee'
coffeelint.registerRule require './rules/no_spaces.coffee'
coffeelint.registerRule require './rules/no_trailing_whitespace.coffee'
coffeelint.registerRule require './rules/max_line_length.coffee'
coffeelint.registerRule require './rules/line_endings.coffee'
Expand Down
2 changes: 1 addition & 1 deletion src/rules/indentation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class Indentation
level: 'error'
message: 'Line contains inconsistent indentation'
description: '''
This rule imposes a standard number of spaces to be used for
This rule imposes a standard number of spaces(tabs) to be used for
indentation. Since whitespace is significant in CoffeeScript, it's
critical that a project chooses a standard indentation format and
stays consistent. Other roads lead to darkness. <pre> <code>#
Expand Down
22 changes: 22 additions & 0 deletions src/rules/no_spaces.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
indentationRegex = /\S/

module.exports = class NoSpaces

rule:
name: 'no_spaces'
level: 'ignore'
message: 'Line contains space indentation'
description: '''
This rule forbids spaces in indentation. It is disabled by default.
'''

lintLine: (line, lineApi) ->
# Only check lines that have compiled tokens. This helps
# us ignore spaces in the middle of multi line strings, heredocs, etc.
# since they are all reduced to a single token whose line number
# is the start of the expression.
indentation = line.split(indentationRegex)[0]
if lineApi.lineHasToken() and '\ ' in indentation
true
else
null
111 changes: 111 additions & 0 deletions test/test_no_spaces.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
path = require 'path'
vows = require 'vows'
assert = require 'assert'
coffeelint = require path.join('..', 'lib', 'coffeelint')

RULE = 'no_spaces'

tabsConfig =
indentation: { level: 'error', value: 1 }
no_tabs: { level: 'ignore' }
no_spaces: { level: 'error' }

vows.describe(RULE).addBatch({

'Spaces':
topic:
'''
x = () ->
y = () ->
return 1234
'''

'can be forbidden': (source) ->
errors = coffeelint.lint(source, tabsConfig)
assert.equal(errors.length, 4)
error = errors[1]
assert.equal(error.lineNumber, 2)
assert.equal(error.message, 'Line contains space indentation')
assert.equal(error.rule, RULE)

'can be permitted': (source) ->
config =
no_tabs: { level: 'ignore' }
no_spaces: { level: 'ignore' }

errors = coffeelint.lint(source, config)
assert.equal(errors.length, 0)

'are permitted by default': (source) ->
errors = coffeelint.lint(source)
assert.equal(errors.length, 0)

'are allowed in strings': () ->
source = "x = () -> ' '"
errors = coffeelint.lint(source, tabsConfig)
assert.equal(errors.length, 0)

'Spaces in chains':
topic:
'''
startingChain()
.hello()
\t.world()
\t.today((x) -> x + 2)
'''

'can be forbidden': (source) ->
errors = coffeelint.lint(source, tabsConfig)
assert.equal(errors.length, 1)
error = errors[0]
assert.equal(error.lineNumber, 2)
assert.equal(error.message, 'Line contains space indentation')
assert.equal(error.rule, RULE)

'can be permitted': (source) ->
config =
no_tabs: { level: 'ignore' }
no_spaces: { level: 'ignore' }
indentation: { level: 'ignore' }

errors = coffeelint.lint(source, config)
assert.equal(errors.length, 0)

'Spaces in multi-line strings':
topic:
'''
x = 1234
y = """
asdf
"""
'''

'are ignored': (errors) ->
errors = coffeelint.lint(errors, tabsConfig)
assert.isEmpty(errors)

'Spaces in Heredocs':
topic:
'''
###
My Heredoc
###
'''

'are ignored': (errors) ->
errors = coffeelint.lint(errors, tabsConfig)
assert.isEmpty(errors)

'Spaces in multi line regular expressions':
topic:
'''
///
My Heredoc
///
'''

'are ignored': (errors) ->
errors = coffeelint.lint(errors, tabsConfig)
assert.isEmpty(errors)

}).export(module)