Repository: sasstools/sass-lint
Pull request #154 (2015-09-11 17:46:04)

Detect any line ending type regardless of OS.
Mostly opening this PR to see what Travis has to say about it. I'm still getting some failing tests locally that seem unrelated to the newline issue.

But in case this is acceptable...

DCO 1.1 Signed-off-by: Joshua Clanton <joshua.clanton@gmail.com>


Patch:
diff --git a/lib/helpers.js b/lib/helpers.js
index 2be0630..d09a95a 100644
--- a/lib/helpers.js
+++ b/lib/helpers.js
@@ -149,4 +149,12 @@ helpers.loadConfigFile = function (configPath) {
   return file;
 };
 
+helpers.hasEOL = function (str) {
+  return /\r\n|\n/.test(str);
+};
+
+helpers.isEmptyLine = function (str) {
+  return /(\r\n|\n){2}/.test(str);
+};
+
 module.exports = helpers;
diff --git a/lib/rules/brace-style.js b/lib/rules/brace-style.js
index be2ff09..296d081 100644
--- a/lib/rules/brace-style.js
+++ b/lib/rules/brace-style.js
@@ -1,7 +1,6 @@
 'use strict';
 
-var helpers = require('../helpers'),
-    os = require('os');
+var helpers = require('../helpers');
 
 var isSingleLineStatement = function (node) {
   return node.start.line === node.end.line;
@@ -67,7 +66,7 @@ module.exports = {
       else {
         if (next) {
           if (next.is('space')) {
-            if (next.content.indexOf(os.EOL) === -1) {
+            if (!helpers.hasEOL(next.content)) {
               if (parser.options.style === 'allman') {
                 // Report if it's not single line statement
                 if (!((parser.options['allow-single-line'] === true) && isSingleLine)) {
@@ -82,7 +81,7 @@ module.exports = {
               }
             }
 
-            if (next.content.indexOf(os.EOL) !== -1) {
+            if (helpers.hasEOL(next.content)) {
               if (parser.options.style === '1tbs' || parser.options.style === 'stroustrup') {
                 result = helpers.addUnique(result, {
                   'ruleId': parser.rule.name,
@@ -98,7 +97,7 @@ module.exports = {
 
         if (previous) {
           if (previous.is('space')) {
-            if (previous.content.indexOf(os.EOL) === -1) {
+            if (!helpers.hasEOL(previous.content)) {
               if (parser.options.style === 'allman' || parser.options.style === 'stroustrup') {
                 result = helpers.addUnique(result, {
                   'ruleId': parser.rule.name,
@@ -110,7 +109,7 @@ module.exports = {
               }
             }
 
-            if (previous.content.indexOf(os.EOL) !== -1) {
+            if (helpers.hasEOL(previous.content)) {
               if (parser.options.style === '1tbs') {
                 result = helpers.addUnique(result, {
                   'ruleId': parser.rule.name,
diff --git a/lib/rules/empty-line-between-blocks.js b/lib/rules/empty-line-between-blocks.js
index 056c86f..cbcf125 100644
--- a/lib/rules/empty-line-between-blocks.js
+++ b/lib/rules/empty-line-between-blocks.js
@@ -1,7 +1,6 @@
 'use strict';
 
-var helpers = require('../helpers'),
-    os = require('os');
+var helpers = require('../helpers');
 
 var findNearestReturn = function (parent, i) {
   var previous,
@@ -24,7 +23,7 @@ var findNearestReturn = function (parent, i) {
     }
 
     if (previous.type === 'space') {
-      space = previous.content.indexOf(os.EOL + '' + os.EOL) >= 0;
+      space = helpers.isEmptyLine(previous.content);
 
       return {
         'space': space,
diff --git a/lib/rules/final-newline.js b/lib/rules/final-newline.js
index 9a3cf79..4eabb18 100644
--- a/lib/rules/final-newline.js
+++ b/lib/rules/final-newline.js
@@ -1,7 +1,6 @@
 'use strict';
 
-var helpers = require('../helpers'),
-    os = require('os');
+var helpers = require('../helpers');
 
 module.exports = {
   'name': 'final-newline',
@@ -25,13 +24,13 @@ module.exports = {
       }
     }
     else {
-      if (last.content !== os.EOL && parser.options.include) {
+      if (!helpers.hasEOL(last.content) && parser.options.include) {
         error.line = last.start.line;
         error.column = last.start.column;
         error.message = 'Files must end with a new line';
         result = helpers.addUnique(result, error);
       }
-      else if (last.content === os.EOL && !parser.options.include) {
+      else if (helpers.hasEOL(last.content) && !parser.options.include) {
         error.line = last.start.line;
         error.column = last.start.column;
         error.message = 'Files must not end with a new line';
diff --git a/lib/rules/single-line-per-selector.js b/lib/rules/single-line-per-selector.js
index 524693a..0f7a9ab 100644
--- a/lib/rules/single-line-per-selector.js
+++ b/lib/rules/single-line-per-selector.js
@@ -1,7 +1,6 @@
 'use strict';
 
-var helpers = require('../helpers'),
-    os = require('os');
+var helpers = require('../helpers');
 
 module.exports = {
   'name': 'single-line-per-selector',
@@ -18,7 +17,7 @@ module.exports = {
             next = next.content[0];
           }
 
-          if (!(next.is('space') && next.content.indexOf(os.EOL) !== -1)) {
+          if (!(next.is('space') && helpers.hasEOL(next.content))) {
             result = helpers.addUnique(result, {
               'ruleId': parser.rule.name,
               'line': next.start.line,
diff --git a/lib/rules/space-after-comma.js b/lib/rules/space-after-comma.js
index 151f55c..3143daf 100644
--- a/lib/rules/space-after-comma.js
+++ b/lib/rules/space-after-comma.js
@@ -1,7 +1,6 @@
 'use strict';
 
-var helpers = require('../helpers'),
-    os = require('os');
+var helpers = require('../helpers');
 
 module.exports = {
   'name': 'space-after-comma',
@@ -23,7 +22,7 @@ module.exports = {
               next = next.content[0];
             }
           }
-          if ((next.is('space') && next.content.indexOf(os.EOL) === -1) && !parser.options.include) {
+          if ((next.is('space') && !helpers.hasEOL(next.content)) && !parser.options.include) {
             result = helpers.addUnique(result, {
               'ruleId': parser.rule.name,
               'line': next.start.line,


Test patch:
diff --git a/tests/helpers.js b/tests/helpers.js
index 2dad1b3..e25984c 100644
--- a/tests/helpers.js
+++ b/tests/helpers.js
@@ -533,4 +533,60 @@ describe('helpers', function () {
     assert.equal(expect, result);
     done();
   });
+
+  //////////////////////////////
+  // hasEOL
+  //////////////////////////////
+
+  it('hasEOL - [\'\\n\' - true]', function (done) {
+
+    var result = helpers.hasEOL('\n');
+
+    assert.equal(true, result);
+    done();
+  });
+
+  it('hasEOL - [\'\\r\\n\' - true]', function (done) {
+
+    var result = helpers.hasEOL('\r\n');
+
+    assert.equal(true, result);
+    done();
+  });
+
+  //////////////////////////////
+  // isEmptyLine
+  //////////////////////////////
+
+  it('isEmptyLine - [\'\\n\\n\' - true]', function (done) {
+
+    var result = helpers.isEmptyLine('\n\n');
+
+    assert.equal(true, result);
+    done();
+  });
+
+  it('isEmptyLine - [\'\\r\\n\\r\\n\' - true]', function (done) {
+
+    var result = helpers.isEmptyLine('\r\n\r\n');
+
+    assert.equal(true, result);
+    done();
+  });
+
+  it('isEmptyLine - [\'\\n \\n\' - false]', function (done) {
+
+    var result = helpers.isEmptyLine('\n \n');
+
+    assert.equal(false, result);
+    done();
+  });
+
+  it('isEmptyLine - [\'\\r\\nabc\\r\\n\' - false]', function (done) {
+
+    var result = helpers.isEmptyLine('\r\nabc\r\n');
+
+    assert.equal(false, result);
+    done();
+  });
 });