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

Commit b49097c

Browse files
committed
[Perf] Remove JsFile.getFirstTokenOnLine
1 parent 7363537 commit b49097c

File tree

3 files changed

+11
-120
lines changed

3 files changed

+11
-120
lines changed

lib/js-file.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -373,49 +373,6 @@ JsFile.prototype = {
373373
});
374374
},
375375

376-
/**
377-
* Returns first token for the specified line.
378-
* Line numbers start with 1.
379-
*
380-
* @param {Number} lineNumber
381-
* @param {Object} [options]
382-
* @param {Boolean} [options.includeComments = false]
383-
* @param {Boolean} [options.includeWhitespace = false]
384-
* @returns {Object|null}
385-
*/
386-
getFirstTokenOnLine: function(lineNumber, options) {
387-
options = options || {};
388-
389-
var loc;
390-
var token = this._program.getFirstToken();
391-
var currentToken;
392-
393-
while (token) {
394-
loc = token.getLoc();
395-
currentToken = token;
396-
token = token.getNextToken();
397-
398-
if (loc.start.line <= lineNumber && loc.end.line >= lineNumber) {
399-
400-
// Since whitespace tokens can contain newlines we need to check
401-
// if position is in the range, not exact match
402-
if (currentToken.isWhitespace && !options.includeWhitespace) {
403-
continue;
404-
}
405-
}
406-
407-
if (loc.start.line === lineNumber || loc.end.line === lineNumber) {
408-
if (currentToken.isComment && !options.includeComments) {
409-
continue;
410-
}
411-
412-
return currentToken;
413-
}
414-
}
415-
416-
return null;
417-
},
418-
419376
getFirstTokenOnLineWith: function(element, options) {
420377
options = options || {};
421378
var firstToken = element;

test/specs/js-file.js

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -377,74 +377,6 @@ describe('js-file', function() {
377377
});
378378
});
379379

380-
describe('getFirstTokenOnLine', function() {
381-
it('should return first line token', function() {
382-
var file = createJsFile('x += 1;\ny += 4;');
383-
var xToken = file.getFirstTokenOnLine(1);
384-
expect(xToken.type).to.equal('Identifier');
385-
expect(xToken.value).to.equal('x');
386-
var yToken = file.getFirstTokenOnLine(2);
387-
expect(yToken.type).to.equal('Identifier');
388-
expect(yToken.value).to.equal('y');
389-
});
390-
391-
it('should return whitespace token', function() {
392-
expect(createJsFile('\n\n ').getFirstTokenOnLine(1, {
393-
includeWhitespace: true
394-
}).type).to.equal('Whitespace');
395-
});
396-
397-
it('should return first line token if token is indented', function() {
398-
var file = createJsFile('\t\tx += 1;\n\t\ty += 4;');
399-
var xToken = file.getFirstTokenOnLine(1);
400-
expect(xToken.type).to.equal('Identifier');
401-
expect(xToken.value).to.equal('x');
402-
var yToken = file.getFirstTokenOnLine(2);
403-
expect(yToken.type).to.equal('Identifier');
404-
expect(yToken.value).to.equal('y');
405-
});
406-
407-
it('should return null if no token was found', function() {
408-
var file = createJsFile('\t\tx += 1;\n\n');
409-
var yToken = file.getFirstTokenOnLine(2);
410-
expect(yToken).to.equal(null);
411-
});
412-
413-
it('should return null if only comment was found', function() {
414-
var file = createJsFile('\t\tx += 1;\n/*123*/\n');
415-
var yToken = file.getFirstTokenOnLine(2);
416-
expect(yToken).to.equal(null);
417-
});
418-
419-
it('should return first line token ignoring comments', function() {
420-
var file = createJsFile('\t/* 123 */\tx += 1;\n\t/* 321 */\ty += 4;');
421-
var xToken = file.getFirstTokenOnLine(1);
422-
expect(xToken.type).to.equal('Identifier');
423-
expect(xToken.value).to.equal('x');
424-
var yToken = file.getFirstTokenOnLine(2);
425-
expect(yToken.type).to.equal('Identifier');
426-
expect(yToken.value).to.equal('y');
427-
});
428-
429-
it('should return first line token including comments', function() {
430-
var file = createJsFile('\t/*123*/\tx += 1;\n\t/*321*/\ty += 4;');
431-
var commentToken1 = file.getFirstTokenOnLine(1, {includeComments: true});
432-
expect(!!commentToken1.isComment).to.equal(true);
433-
expect(commentToken1.type).to.equal('CommentBlock');
434-
expect(commentToken1.value).to.equal('123');
435-
var commentToken2 = file.getFirstTokenOnLine(2, {includeComments: true});
436-
expect(!!commentToken2.isComment).to.equal(true);
437-
expect(commentToken2.type).to.equal('CommentBlock');
438-
expect(commentToken2.value).to.equal('321');
439-
});
440-
441-
it('should return null if no token was found including comments', function() {
442-
var file = createJsFile('\t\tx += 1;\n\n');
443-
var yToken = file.getFirstTokenOnLine(2, {includeComments: true});
444-
expect(yToken).to.equal(null);
445-
});
446-
});
447-
448380
describe('getLastTokenOnLine', function() {
449381
it('should return last line token', function() {
450382
var file = createJsFile('x = 1;\nif (x) {}\n');

test/specs/token-assert.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ describe('token-assert', function() {
12161216
});
12171217
});
12181218

1219-
describe.only('indentation', function() {
1219+
describe('indentation', function() {
12201220
it('should not trigger on correct indentation', function() {
12211221
var file = createJsFile('x=y;');
12221222

@@ -1293,16 +1293,17 @@ describe('token-assert', function() {
12931293
var onError = sinon.spy();
12941294
tokenAssert.on('error', onError);
12951295

1296+
var comment = file.getProgram().getFirstToken().getNextNonWhitespaceToken();
12961297
tokenAssert.indentation({
1297-
token: file.getProgram().getFirstToken().getNextNonWhitespaceToken(),
1298+
token: comment,
12981299
actual: 2,
12991300
expected: 0,
13001301
indentChar: ' '
13011302
});
13021303

1303-
var newToken = file.getFirstTokenOnLine(1, {includeComments: true});
1304-
expect(file.getWhitespaceBefore(newToken)).to.equal('');
1305-
expect(newToken.value).to.equal('\n *\n ');
1304+
comment = file.getProgram().getFirstToken();
1305+
expect(file.getWhitespaceBefore(comment)).to.equal('');
1306+
expect(comment.value).to.equal('\n *\n ');
13061307
});
13071308

13081309
it('should fix docblock on incorrect underindentation', function() {
@@ -1312,16 +1313,17 @@ describe('token-assert', function() {
13121313
var onError = sinon.spy();
13131314
tokenAssert.on('error', onError);
13141315

1316+
var comment = file.getProgram().getFirstToken().getNextNonWhitespaceToken();
13151317
tokenAssert.indentation({
1316-
token: file.getProgram().getFirstToken().getNextNonWhitespaceToken(),
1318+
token: comment,
13171319
actual: 2,
13181320
expected: 4,
13191321
indentChar: ' '
13201322
});
13211323

1322-
var newToken = file.getFirstTokenOnLine(1, {includeComments: true});
1323-
expect(file.getWhitespaceBefore(newToken)).to.equal(' ');
1324-
expect(newToken.value).to.equal('\n *\n ');
1324+
comment = file.getProgram().getFirstToken().getNextNonWhitespaceToken();
1325+
expect(file.getWhitespaceBefore(comment)).to.equal(' ');
1326+
expect(comment.value).to.equal('\n *\n ');
13251327
});
13261328
});
13271329
});

0 commit comments

Comments
 (0)