Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
change getTokens 2nd/3rd arguments to count tokens, not characters
  • Loading branch information
michaelficarra committed Nov 29, 2013
1 parent 57bac48 commit 06c4372
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
37 changes: 27 additions & 10 deletions lib/eslint.js
Expand Up @@ -290,7 +290,7 @@ module.exports = (function() {
* improve traversal speed when wanting to find tokens for a given
* node
*/
currentTokens = {};
currentTokens = [];
ast.tokens.forEach(function(token) {
currentTokens[token.range[0]] = token;
});
Expand Down Expand Up @@ -397,18 +397,35 @@ module.exports = (function() {
*/
api.getTokens = function(node, beforeCount, afterCount) {
if (node) {
var startLocation = node.range[0] - (beforeCount || 0);
var endLocation = node.range[1] + (afterCount || 0) ;
var tokens = [];
while (startLocation < endLocation) {
if (currentTokens[startLocation]) {
tokens.push(currentTokens[startLocation]);
startLocation = currentTokens[startLocation].range[1];
var beforeTokens = [], tokens = [], afterTokens = [], cursor;
cursor = node.range[0] - 1;
while (beforeCount > 0 && cursor >= 0) {
if(currentTokens[cursor]) {
beforeTokens.unshift(currentTokens[cursor]);
--beforeCount;
}
--cursor;
}
cursor = node.range[0];
while (cursor < node.range[1]) {
if(currentTokens[cursor]) {
tokens.push(currentTokens[cursor]);
cursor = currentTokens[cursor].range[1];
} else {
++cursor;
}
}
cursor = node.range[1];
while (afterCount > 0 && cursor < currentTokens.length) {
if(currentTokens[cursor]) {
afterTokens.push(currentTokens[cursor]);
--afterCount;
cursor = currentTokens[cursor].range[1];
} else {
startLocation += 1;
++cursor;
}
}
return tokens;
return beforeTokens.concat(tokens, afterTokens);
} else {
return Object.keys(currentTokens).map(function(item) {
return item;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-div-regex.js
Expand Up @@ -19,7 +19,7 @@ module.exports = function(context) {
source;

if (nodeType === "RegularExpression") {
source = context.getTokens(node, 1, 1)[0].value;
source = context.getTokens(node)[0].value;

if (source[1] === "=") {
context.report(node, "A regular expression literal can be confused with '/='.");
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/eslint.js
Expand Up @@ -176,14 +176,14 @@ describe("eslint", function() {

eslint.reset();
eslint.on("BinaryExpression", function(node) {
var tokens = eslint.getTokens(node, 2);
var tokens = eslint.getTokens(node, 1);
assert.equal(tokens.length, 4);
});

eslint.verify(code, config, true);
});

it("should retrieve all tokens plus one character after for binary expression", function() {
it("should retrieve all tokens plus one after for binary expression", function() {
var config = { rules: {} };

eslint.reset();
Expand All @@ -195,13 +195,13 @@ describe("eslint", function() {
eslint.verify(code, config, true);
});

it("should retrieve all tokens plus two characters before and one character after for binary expression", function() {
it("should retrieve all tokens plus two before and one after for binary expression", function() {
var config = { rules: {} };

eslint.reset();
eslint.on("BinaryExpression", function(node) {
var tokens = eslint.getTokens(node, 2, 1);
assert.equal(tokens.length, 5);
assert.equal(tokens.length, 6);
});

eslint.verify(code, config, true);
Expand Down

0 comments on commit 06c4372

Please sign in to comment.