Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect any line ending type regardless of OS. #154

Merged
merged 7 commits into from
Sep 16, 2015
Merged
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
8 changes: 8 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
11 changes: 5 additions & 6 deletions lib/rules/brace-style.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/empty-line-between-blocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var helpers = require('../helpers'),
os = require('os');
var helpers = require('../helpers');

var findNearestReturn = function (parent, i) {
var previous,
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions lib/rules/final-newline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var helpers = require('../helpers'),
os = require('os');
var helpers = require('../helpers');

module.exports = {
'name': 'final-newline',
Expand All @@ -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';
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/single-line-per-selector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var helpers = require('../helpers'),
os = require('os');
var helpers = require('../helpers');

module.exports = {
'name': 'single-line-per-selector',
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/space-after-comma.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var helpers = require('../helpers'),
os = require('os');
var helpers = require('../helpers');

module.exports = {
'name': 'space-after-comma',
Expand All @@ -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,
Expand Down
56 changes: 56 additions & 0 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});