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

Commit

Permalink
[Perf] Remove getLoc from linesBetween() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Apr 12, 2016
1 parent c2c4d5d commit 0e9f4fa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
20 changes: 20 additions & 0 deletions lib/js-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,26 @@ JsFile.prototype = {
return distance;
},

getLineCountBetween(tokenBefore, tokenAfter) {
if (tokenBefore === tokenAfter) {
return 0;
}
tokenBefore = tokenBefore instanceof Token ? tokenBefore : tokenBefore.getLastToken();
tokenAfter = tokenAfter instanceof Token ? tokenAfter : tokenAfter.getFirstToken();

var currentToken = tokenBefore.getNextToken();
var lineCount = 0;
while (currentToken) {
if (currentToken === tokenAfter) {
break;
}

lineCount += currentToken.getNewlineCount();
currentToken = currentToken.getNextToken();
}
return lineCount;
},

/**
* Returns array of source lines for the file with comments removed.
*
Expand Down
25 changes: 20 additions & 5 deletions lib/rules/require-newline-before-single-statements-in-if.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ var assert = require('assert');

module.exports = function() {};

function getFirstStatement(node) {
if (node && node.type === 'BlockStatement') {
return node.body[0];
}
return node;
}

module.exports.prototype = {

configure: function(value) {
Expand Down Expand Up @@ -80,13 +87,21 @@ module.exports.prototype = {
}

file.iterateNodesByType('IfStatement', function(node) {
var consequentNode = node.consequent;
var alternateNode = node.alternate;
var consequentNode = getFirstStatement(node.consequent);
var alternateNode = getFirstStatement(node.alternate);

assertDifferentLine(consequentNode, getToken(consequentNode, 'Keyword', 'previousSibling'));
if (consequentNode) {
assertDifferentLine(
getToken(consequentNode, 'Keyword', 'previousSibling'),
consequentNode
);
}

if (alternateNode) {
assertDifferentLine(alternateNode, getToken(alternateNode, 'Keyword', 'previousSibling'));
if (alternateNode && alternateNode.type !== 'IfStatement' && alternateNode.type !== 'BlockStatement') {
assertDifferentLine(
getToken(alternateNode, 'Keyword', 'previousSibling'),
alternateNode
);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/token-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ TokenAssert.prototype.linesBetween = function(options) {
// as this prevents accidentally moving a valid token onto a line comment ed line
var fixed = !options.token.getNextNonWhitespaceToken().isComment;

var linesBetween = Math.abs(token.getLoc().end.line - nextToken.getLoc().start.line);
var linesBetween = this._file.getLineCountBetween(token, nextToken);

var emitError = function(countPrefix, lineCount) {
var msgPrefix = token.value + ' and ' + nextToken.value;
Expand Down

0 comments on commit 0e9f4fa

Please sign in to comment.