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

Commit

Permalink
disallowTrailingWhitespace: use CST for fix method
Browse files Browse the repository at this point in the history
  • Loading branch information
qfox committed Mar 24, 2016
1 parent 9dd4dc8 commit 3a16cf0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
63 changes: 34 additions & 29 deletions lib/rules/disallow-trailing-whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,20 @@ module.exports.prototype = {
var linebreak = file.getLineBreakStyle();
var lines = file.getLines();

var stripTrailingWs = ignoreEmptyLines ?
function(line) {
return line.match(/^\s*$/) ? line : line.trimRight();
}
: Function.prototype.call.bind(String.prototype.trimRight);

var fixed = false;
var eolCount;
var startLineNumber;
var Token;
var precedingToken;
var targetToken;
var newValue;
var replacementToken;

while (!precedingToken && currentLineNumber > 0) {
precedingToken = file.getLastTokenOnLine(currentLineNumber, {
Expand All @@ -111,44 +120,40 @@ module.exports.prototype = {
precedingToken.loc.start.line <= error.line &&
precedingToken.loc.end.line >= error.line) {

if (precedingToken.type === 'Block') {

if (ignoreEmptyLines) {
var blockLines = precedingToken.value.split(/\r\n|\n|\r/);
var sourceCode = precedingToken.sourceCodeLines
.map(stripTrailingWs)
.join(linebreak);

for (var k = 0; k < blockLines.length; k++) {
// Remove `/*`, `*/` and `//` parts
var isBlock = precedingToken.type === 'CommentBlock';
newValue = sourceCode.slice(2, (-2 * isBlock) || undefined);

if (!blockLines[k].match(/^\s*$/) || k === 0) {
blockLines[k] = blockLines[k].split(/\s+$/).join('');
}
}
// Make a replacement for existing token
Token = precedingToken.constructor;
replacementToken = new Token(precedingToken.type, newValue);

newValue = blockLines.join(linebreak);
} else {
newValue = precedingToken.value
.split(/[^\S\r\n]+\r\n|[^\S\n]+\n|[^\S\r]+\r/)
.join(linebreak);
}
} else {
newValue = precedingToken.value.split(/\s+$/).join('');
}

file.setWhitespaceBefore(precedingToken.nextToken, newValue);
precedingToken.parentElement.replaceChild(replacementToken, precedingToken);
fixed = true;
}
}

if (targetToken !== null && targetToken.isWhitespace && !fixed) {
eolCount = targetToken.loc.end.line - targetToken.loc.start.line + 1;
newValue = new Array(eolCount).join(linebreak);

Token = targetToken.constructor;
replacementToken = new Token(targetToken.type, newValue);

targetToken.parentElement.replaceChild(replacementToken, targetToken);
fixed = true;
}

if (targetToken !== null && !fixed) {
var eolCount = targetToken.loc.start.line - startLineNumber + 1;
var targetIndent = '';
var targetLine = lines[targetToken.loc.start.line - 1];
for (var j = 0, whitespace = targetLine.charAt(j);
whitespace.match(/\s/);
j++, whitespace = targetLine.charAt(j)) {
targetIndent += whitespace;
}
eolCount = targetToken.loc.start.line - startLineNumber + 1;
newValue = new Array(eolCount).join(linebreak);
newValue += lines[targetToken.loc.start.line - 1].replace(/^(\s*).*?$/, '$1');

file.setWhitespaceBefore(targetToken, new Array(eolCount).join(linebreak) + targetIndent);
file.setWhitespaceBefore(targetToken, newValue);
fixed = true;
}

Expand Down
2 changes: 1 addition & 1 deletion test/specs/rules/disallow-trailing-whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Checker = require('../../../lib/checker');
var expect = require('chai').expect;
var reportAndFix = require('../../lib/assertHelpers').reportAndFix;

describe.skip('rules/disallow-trailing-whitespace', function() {
describe('rules/disallow-trailing-whitespace', function() {
var rules = { disallowTrailingWhitespace: true };
var checker;

Expand Down

0 comments on commit 3a16cf0

Please sign in to comment.