Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

disallowTrailingWhitespace: fix autofixing CRLF and CR files #1980

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 5 additions & 4 deletions lib/token-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ TokenAssert.prototype.noTokenBefore = function(options) {
TokenAssert.prototype.noTrailingSpaces = function(options) {
var ignoreEmptyLines = options.ignoreEmptyLines;
var lines = this._file.getLines();
var linebreak = this._file.getLineBreaks()[0] || '\n';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you cover this branch: || '\n'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is already a test without any line breaks in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you see the coverage saying that isn't tested can you point me at it.


for (var i = 0, l = lines.length; i < l; i++) {
if (lines[i].match(/\s$/) && !(ignoreEmptyLines && lines[i].match(/^\s*$/))) {
Expand Down Expand Up @@ -549,7 +550,7 @@ TokenAssert.prototype.noTrailingSpaces = function(options) {
if (precendingToken.type === 'Block') {

if (ignoreEmptyLines) {
var blockLines = precendingToken.value.split(/\n/);
var blockLines = precendingToken.value.split(/\r\n|\n|\r/);

for (var k = 0; k < blockLines.length; k++) {

Expand All @@ -558,9 +559,9 @@ TokenAssert.prototype.noTrailingSpaces = function(options) {
}
}

precendingToken.value = blockLines.join('\n');
precendingToken.value = blockLines.join(linebreak);
} else {
precendingToken.value = precendingToken.value.split(/\s\n/).join('\n');
precendingToken.value = precendingToken.value.split(/\s\r\n|\s\n|\s\r/).join(linebreak);
}
} else {
precendingToken.value = precendingToken.value.split(/\s$/).join('');
Expand All @@ -580,7 +581,7 @@ TokenAssert.prototype.noTrailingSpaces = function(options) {
targetIndent += whitespace;
}

this._file.setWhitespaceBefore(targetToken, new Array(eolCount).join('\n') + targetIndent);
this._file.setWhitespaceBefore(targetToken, new Array(eolCount).join(linebreak) + targetIndent);
fixed = true;
}

Expand Down
24 changes: 24 additions & 0 deletions test/specs/rules/disallow-trailing-whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ describe('rules/disallow-trailing-whitespace', function() {
output: '/*\nbla\n\t\n*/'
});

reportAndFix({
name: 'supports windows line breaks',
rules: rules,
errors: 2,
input: ' \r\nvar a; \r\nvar b;\r\n var c;\r\n',
output: '\r\nvar a;\r\nvar b;\r\n var c;\r\n'
});

reportAndFix({
name: 'supports windows line breaks in a comment',
rules: rules,
errors: 1,
input: '//hey \r\n',
output: '//hey\r\n'
});

reportAndFix({
name: 'supports mac line breaks',
rules: rules,
errors: 2,
input: ' \rvar a; \rvar b;\r var c;\r',
output: '\rvar a;\rvar b;\r var c;\r'
});

describe('option value true', function() {
beforeEach(function() {
checker.configure({ disallowTrailingWhitespace: true });
Expand Down