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

Commit 0e9f4fa

Browse files
committed
[Perf] Remove getLoc from linesBetween() method
1 parent c2c4d5d commit 0e9f4fa

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

lib/js-file.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,26 @@ JsFile.prototype = {
647647
return distance;
648648
},
649649

650+
getLineCountBetween(tokenBefore, tokenAfter) {
651+
if (tokenBefore === tokenAfter) {
652+
return 0;
653+
}
654+
tokenBefore = tokenBefore instanceof Token ? tokenBefore : tokenBefore.getLastToken();
655+
tokenAfter = tokenAfter instanceof Token ? tokenAfter : tokenAfter.getFirstToken();
656+
657+
var currentToken = tokenBefore.getNextToken();
658+
var lineCount = 0;
659+
while (currentToken) {
660+
if (currentToken === tokenAfter) {
661+
break;
662+
}
663+
664+
lineCount += currentToken.getNewlineCount();
665+
currentToken = currentToken.getNextToken();
666+
}
667+
return lineCount;
668+
},
669+
650670
/**
651671
* Returns array of source lines for the file with comments removed.
652672
*

lib/rules/require-newline-before-single-statements-in-if.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ var assert = require('assert');
4848

4949
module.exports = function() {};
5050

51+
function getFirstStatement(node) {
52+
if (node && node.type === 'BlockStatement') {
53+
return node.body[0];
54+
}
55+
return node;
56+
}
57+
5158
module.exports.prototype = {
5259

5360
configure: function(value) {
@@ -80,13 +87,21 @@ module.exports.prototype = {
8087
}
8188

8289
file.iterateNodesByType('IfStatement', function(node) {
83-
var consequentNode = node.consequent;
84-
var alternateNode = node.alternate;
90+
var consequentNode = getFirstStatement(node.consequent);
91+
var alternateNode = getFirstStatement(node.alternate);
8592

86-
assertDifferentLine(consequentNode, getToken(consequentNode, 'Keyword', 'previousSibling'));
93+
if (consequentNode) {
94+
assertDifferentLine(
95+
getToken(consequentNode, 'Keyword', 'previousSibling'),
96+
consequentNode
97+
);
98+
}
8799

88-
if (alternateNode) {
89-
assertDifferentLine(alternateNode, getToken(alternateNode, 'Keyword', 'previousSibling'));
100+
if (alternateNode && alternateNode.type !== 'IfStatement' && alternateNode.type !== 'BlockStatement') {
101+
assertDifferentLine(
102+
getToken(alternateNode, 'Keyword', 'previousSibling'),
103+
alternateNode
104+
);
90105
}
91106
});
92107
}

lib/token-assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ TokenAssert.prototype.linesBetween = function(options) {
297297
// as this prevents accidentally moving a valid token onto a line comment ed line
298298
var fixed = !options.token.getNextNonWhitespaceToken().isComment;
299299

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

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

0 commit comments

Comments
 (0)