From 1ec63474b85227742e286071ff0e6f096bbced61 Mon Sep 17 00:00:00 2001 From: Julian Wielga Date: Thu, 7 Apr 2016 12:31:53 +0200 Subject: [PATCH 1/2] no_spaces --- src/coffeelint.coffee | 1 + src/rules/indentation.coffee | 2 +- src/rules/no_spaces.coffee | 22 ++++++++++ test/test_no_spaces.coffee | 85 ++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/rules/no_spaces.coffee create mode 100644 test/test_no_spaces.coffee diff --git a/src/coffeelint.coffee b/src/coffeelint.coffee index 89685f99..b9529399 100644 --- a/src/coffeelint.coffee +++ b/src/coffeelint.coffee @@ -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' diff --git a/src/rules/indentation.coffee b/src/rules/indentation.coffee index 0e033a06..dd26952f 100644 --- a/src/rules/indentation.coffee +++ b/src/rules/indentation.coffee @@ -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.
 #
diff --git a/src/rules/no_spaces.coffee b/src/rules/no_spaces.coffee
new file mode 100644
index 00000000..ad56fb66
--- /dev/null
+++ b/src/rules/no_spaces.coffee
@@ -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
diff --git a/test/test_no_spaces.coffee b/test/test_no_spaces.coffee
new file mode 100644
index 00000000..28e65da6
--- /dev/null
+++ b/test/test_no_spaces.coffee
@@ -0,0 +1,85 @@
+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 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)

From e5d9e59990e4e748ed4df4fbeda316921299ec57 Mon Sep 17 00:00:00 2001
From: Julian Wielga 
Date: Mon, 11 Apr 2016 13:34:09 +0200
Subject: [PATCH 2/2] added test for chains

---
 test/test_no_spaces.coffee | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/test/test_no_spaces.coffee b/test/test_no_spaces.coffee
index 28e65da6..da2f1fda 100644
--- a/test/test_no_spaces.coffee
+++ b/test/test_no_spaces.coffee
@@ -45,6 +45,32 @@ vows.describe(RULE).addBatch({
             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:
             '''