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

Commit

Permalink
[Perf] Remove JsFile.getFirstTokenOnLine
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Apr 12, 2016
1 parent 7363537 commit b49097c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 120 deletions.
43 changes: 0 additions & 43 deletions lib/js-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,49 +373,6 @@ JsFile.prototype = {
});
},

/**
* Returns first token for the specified line.
* Line numbers start with 1.
*
* @param {Number} lineNumber
* @param {Object} [options]
* @param {Boolean} [options.includeComments = false]
* @param {Boolean} [options.includeWhitespace = false]
* @returns {Object|null}
*/
getFirstTokenOnLine: function(lineNumber, options) {
options = options || {};

var loc;
var token = this._program.getFirstToken();
var currentToken;

while (token) {
loc = token.getLoc();
currentToken = token;
token = token.getNextToken();

if (loc.start.line <= lineNumber && loc.end.line >= lineNumber) {

// Since whitespace tokens can contain newlines we need to check
// if position is in the range, not exact match
if (currentToken.isWhitespace && !options.includeWhitespace) {
continue;
}
}

if (loc.start.line === lineNumber || loc.end.line === lineNumber) {
if (currentToken.isComment && !options.includeComments) {
continue;
}

return currentToken;
}
}

return null;
},

getFirstTokenOnLineWith: function(element, options) {
options = options || {};
var firstToken = element;
Expand Down
68 changes: 0 additions & 68 deletions test/specs/js-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,74 +377,6 @@ describe('js-file', function() {
});
});

describe('getFirstTokenOnLine', function() {
it('should return first line token', function() {
var file = createJsFile('x += 1;\ny += 4;');
var xToken = file.getFirstTokenOnLine(1);
expect(xToken.type).to.equal('Identifier');
expect(xToken.value).to.equal('x');
var yToken = file.getFirstTokenOnLine(2);
expect(yToken.type).to.equal('Identifier');
expect(yToken.value).to.equal('y');
});

it('should return whitespace token', function() {
expect(createJsFile('\n\n ').getFirstTokenOnLine(1, {
includeWhitespace: true
}).type).to.equal('Whitespace');
});

it('should return first line token if token is indented', function() {
var file = createJsFile('\t\tx += 1;\n\t\ty += 4;');
var xToken = file.getFirstTokenOnLine(1);
expect(xToken.type).to.equal('Identifier');
expect(xToken.value).to.equal('x');
var yToken = file.getFirstTokenOnLine(2);
expect(yToken.type).to.equal('Identifier');
expect(yToken.value).to.equal('y');
});

it('should return null if no token was found', function() {
var file = createJsFile('\t\tx += 1;\n\n');
var yToken = file.getFirstTokenOnLine(2);
expect(yToken).to.equal(null);
});

it('should return null if only comment was found', function() {
var file = createJsFile('\t\tx += 1;\n/*123*/\n');
var yToken = file.getFirstTokenOnLine(2);
expect(yToken).to.equal(null);
});

it('should return first line token ignoring comments', function() {
var file = createJsFile('\t/* 123 */\tx += 1;\n\t/* 321 */\ty += 4;');
var xToken = file.getFirstTokenOnLine(1);
expect(xToken.type).to.equal('Identifier');
expect(xToken.value).to.equal('x');
var yToken = file.getFirstTokenOnLine(2);
expect(yToken.type).to.equal('Identifier');
expect(yToken.value).to.equal('y');
});

it('should return first line token including comments', function() {
var file = createJsFile('\t/*123*/\tx += 1;\n\t/*321*/\ty += 4;');
var commentToken1 = file.getFirstTokenOnLine(1, {includeComments: true});
expect(!!commentToken1.isComment).to.equal(true);
expect(commentToken1.type).to.equal('CommentBlock');
expect(commentToken1.value).to.equal('123');
var commentToken2 = file.getFirstTokenOnLine(2, {includeComments: true});
expect(!!commentToken2.isComment).to.equal(true);
expect(commentToken2.type).to.equal('CommentBlock');
expect(commentToken2.value).to.equal('321');
});

it('should return null if no token was found including comments', function() {
var file = createJsFile('\t\tx += 1;\n\n');
var yToken = file.getFirstTokenOnLine(2, {includeComments: true});
expect(yToken).to.equal(null);
});
});

describe('getLastTokenOnLine', function() {
it('should return last line token', function() {
var file = createJsFile('x = 1;\nif (x) {}\n');
Expand Down
20 changes: 11 additions & 9 deletions test/specs/token-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ describe('token-assert', function() {
});
});

describe.only('indentation', function() {
describe('indentation', function() {
it('should not trigger on correct indentation', function() {
var file = createJsFile('x=y;');

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

var comment = file.getProgram().getFirstToken().getNextNonWhitespaceToken();
tokenAssert.indentation({
token: file.getProgram().getFirstToken().getNextNonWhitespaceToken(),
token: comment,
actual: 2,
expected: 0,
indentChar: ' '
});

var newToken = file.getFirstTokenOnLine(1, {includeComments: true});
expect(file.getWhitespaceBefore(newToken)).to.equal('');
expect(newToken.value).to.equal('\n *\n ');
comment = file.getProgram().getFirstToken();
expect(file.getWhitespaceBefore(comment)).to.equal('');
expect(comment.value).to.equal('\n *\n ');
});

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

var comment = file.getProgram().getFirstToken().getNextNonWhitespaceToken();
tokenAssert.indentation({
token: file.getProgram().getFirstToken().getNextNonWhitespaceToken(),
token: comment,
actual: 2,
expected: 4,
indentChar: ' '
});

var newToken = file.getFirstTokenOnLine(1, {includeComments: true});
expect(file.getWhitespaceBefore(newToken)).to.equal(' ');
expect(newToken.value).to.equal('\n *\n ');
comment = file.getProgram().getFirstToken().getNextNonWhitespaceToken();
expect(file.getWhitespaceBefore(comment)).to.equal(' ');
expect(comment.value).to.equal('\n *\n ');
});
});
});

0 comments on commit b49097c

Please sign in to comment.