Skip to content

Commit

Permalink
Autofixing: add more rules to use assertion framework
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Feb 17, 2015
1 parent 9a99950 commit 712d53f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 46 deletions.
28 changes: 13 additions & 15 deletions lib/rules/disallow-padding-newlines-in-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,24 @@ module.exports.prototype = {
},

check: function(file, errors) {
var lines = file.getLines();

file.iterateNodesByType('BlockStatement', function(node) {
var openingBracket = file.getFirstNodeToken(node);
var startLine = openingBracket.loc.start.line;

var closingBracket = file.getLastNodeToken(node);
var closingLine = closingBracket.loc.start.line;

if (startLine === closingLine) {
return;
}
errors.assert.linesBetween({
token: openingBracket,
nextToken: file.getCommentAfterToken(openingBracket) || file.getNextToken(openingBracket),
atMost: 1,
message: 'Expected no padding newline after opening curly brace'
});

if (lines[startLine] === '') {
errors.add('Expected no padding newline after opening curly brace', openingBracket.loc.end);
}
var closingBracket = file.getLastNodeToken(node);

if (lines[closingLine - 2] === '') {
errors.add('Expected no padding newline before closing curly brace', closingBracket.loc.start);
}
errors.assert.linesBetween({
token: file.getCommentBeforeToken(closingBracket) || file.getPrevToken(closingBracket),
nextToken: closingBracket,
atMost: 1,
message: 'Expected no padding newline before closing curly brace'
});
});
}

Expand Down
3 changes: 1 addition & 2 deletions lib/rules/disallow-padding-newlines-in-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ module.exports.prototype = {
});

var closingBracket = file.getLastNodeToken(node);
var prevToken = file.getPrevToken(closingBracket);

errors.assert.sameLine({
token: prevToken,
token: file.getPrevToken(closingBracket),
nextToken: closingBracket,
message: 'Illegal newline before closing curly brace'
});
Expand Down
30 changes: 12 additions & 18 deletions lib/rules/disallow-spaces-inside-parentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,23 @@ module.exports.prototype = {
check: function(file, errors) {
var only = this._only;

function isCommentInRange(start, end) {
return file.getComments().some(function(comment) {
return start <= comment.range[0] && end >= comment.range[1];
});
}

file.iterateTokenByValue('(', function(token) {
var nextToken = file.getNextToken(token);
var nextToken = file.getCommentAfterToken(token) || file.getNextToken(token);
var value = nextToken.value;

if (only && !(value in only)) {
return;
}

if (token.range[1] !== nextToken.range[0] &&
token.loc.end.line === nextToken.loc.start.line &&
!isCommentInRange(token.range[1], nextToken.range[0])) {
errors.add('Illegal space after opening round bracket', token.loc.end);
}
errors.assert.noWhitespaceBetween({
token: token,
nextToken: nextToken,
message: 'Illegal space after opening round bracket'
});
});

file.iterateTokenByValue(')', function(token) {
var prevToken = file.getPrevToken(token);
var prevToken = file.getCommentBeforeToken(token) || file.getPrevToken(token);
var value = prevToken.value;

if (only) {
Expand All @@ -115,11 +109,11 @@ module.exports.prototype = {
}
}

if (prevToken.range[1] !== token.range[0] &&
prevToken.loc.end.line === token.loc.start.line &&
!isCommentInRange(prevToken.range[1], token.range[0])) {
errors.add('Illegal space before closing round bracket', prevToken.loc.end);
}
errors.assert.noWhitespaceBetween({
token: prevToken,
nextToken: token,
message: 'Illegal space before closing round bracket'
});
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/rules/require-padding-newlines-in-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,18 @@ module.exports.prototype = {
}

var openingBracket = file.getFirstNodeToken(node);
var nextToken = file.getCommentAfterToken(openingBracket) || file.getNextToken(openingBracket);

errors.assert.linesBetween({
token: openingBracket,
nextToken: nextToken,
nextToken: file.getCommentAfterToken(openingBracket) || file.getNextToken(openingBracket),
atLeast: 2,
message: 'Expected a padding newline after opening curly brace'
});

var closingBracket = file.getLastNodeToken(node);
var prevToken = file.getCommentBeforeToken(closingBracket) || file.getPrevToken(closingBracket);

errors.assert.linesBetween({
token: prevToken,
token: file.getCommentBeforeToken(closingBracket) || file.getPrevToken(closingBracket),
nextToken: closingBracket,
atLeast: 2,
message: 'Expected a padding newline before closing curly brace'
Expand Down
17 changes: 10 additions & 7 deletions lib/rules/require-padding-newlines-in-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ module.exports.prototype = {
return;
}

if (openingBracket.loc.end.line === nextToken.loc.start.line) {
errors.add('Missing newline after opening curly brace', nextToken.loc.start);
}
errors.assert.differentLine({
token: openingBracket,
nextToken: nextToken,
message: 'Missing newline after opening curly brace'
});

var closingBracket = file.getLastNodeToken(node);
var prevToken = file.getPrevToken(closingBracket);

if (closingBracket.loc.start.line === prevToken.loc.end.line) {
errors.add('Missing newline before closing curly brace', closingBracket.loc.start);
}
errors.assert.differentLine({
token: file.getPrevToken(closingBracket),
nextToken: closingBracket,
message: 'Missing newline before closing curly brace'
});
});
}

Expand Down

0 comments on commit 712d53f

Please sign in to comment.