From be461b0e588708d0f242280ad70dd4221ee1791d Mon Sep 17 00:00:00 2001 From: Joshua Clanton Date: Fri, 11 Sep 2015 12:40:18 -0500 Subject: [PATCH 1/3] :bug: Detect any line ending type regardless of OS. --- lib/helpers.js | 8 ++++++++ lib/rules/brace-style.js | 11 +++++------ lib/rules/empty-line-between-blocks.js | 5 ++--- lib/rules/final-newline.js | 7 +++---- lib/rules/single-line-per-selector.js | 5 ++--- lib/rules/space-after-comma.js | 5 ++--- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 2be0630f..b482c736 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|\r|\n|\f/.test(str); +}; + +helpers.isEmptyLine = function (str) { + return /(\r\n|\r|\n|\f){2}/.test(str); +}; + module.exports = helpers; diff --git a/lib/rules/brace-style.js b/lib/rules/brace-style.js index be2ff09d..296d0810 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 056c86f6..cbcf1253 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 9a3cf793..4eabb188 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 07212cfb..ed336499 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', @@ -14,7 +13,7 @@ module.exports = { var next = selector.content[i + 1].content[0]; if (next) { - if (next.type !== 'space' || next.content.indexOf(os.EOL) === -1) { + if (next.type !== '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 151f55c8..3143dafb 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, From 1ca2b7b7980785ad3f77e30a2a30f4de8f4b2e0b Mon Sep 17 00:00:00 2001 From: Joshua Clanton Date: Tue, 15 Sep 2015 14:39:26 -0500 Subject: [PATCH 2/3] Correct logic for single-line-per-selector EOL. --- lib/rules/single-line-per-selector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/single-line-per-selector.js b/lib/rules/single-line-per-selector.js index e05461a0..0f7a9ab6 100644 --- a/lib/rules/single-line-per-selector.js +++ b/lib/rules/single-line-per-selector.js @@ -17,7 +17,7 @@ module.exports = { next = next.content[0]; } - if (!(next.is('space') && !helpers.hasEOL(next.content))) { + if (!(next.is('space') && helpers.hasEOL(next.content))) { result = helpers.addUnique(result, { 'ruleId': parser.rule.name, 'line': next.start.line, From 8ed4ba409e2f828629431695fdc6c29d35f3eb8d Mon Sep 17 00:00:00 2001 From: Joshua Clanton Date: Tue, 15 Sep 2015 14:51:44 -0500 Subject: [PATCH 3/3] Add tests for newline helper functions. --- lib/helpers.js | 4 ++-- tests/helpers.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index b482c736..d09a95a5 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -150,11 +150,11 @@ helpers.loadConfigFile = function (configPath) { }; helpers.hasEOL = function (str) { - return /\r\n|\r|\n|\f/.test(str); + return /\r\n|\n/.test(str); }; helpers.isEmptyLine = function (str) { - return /(\r\n|\r|\n|\f){2}/.test(str); + return /(\r\n|\n){2}/.test(str); }; module.exports = helpers; diff --git a/tests/helpers.js b/tests/helpers.js index 2dad1b33..e25984cb 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(); + }); });