Skip to content

Commit

Permalink
Chore: add equalTokens in ast-utils. (#9500)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Nov 6, 2017
1 parent b7c5b19 commit 9064b9c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 50 deletions.
26 changes: 26 additions & 0 deletions lib/ast-utils.js
Expand Up @@ -406,6 +406,31 @@ function createGlobalLinebreakMatcher() {
return new RegExp(LINEBREAK_MATCHER.source, "g");
}

/**
* Checks whether or not the tokens of two given nodes are same.
* @param {ASTNode} left - A node 1 to compare.
* @param {ASTNode} right - A node 2 to compare.
* @param {SourceCode} sourceCode - The ESLint source code object.
* @returns {boolean} the source code for the given node.
*/
function equalTokens(left, right, sourceCode) {
const tokensL = sourceCode.getTokens(left);
const tokensR = sourceCode.getTokens(right);

if (tokensL.length !== tokensR.length) {
return false;
}
for (let i = 0; i < tokensL.length; ++i) {
if (tokensL[i].type !== tokensR[i].type ||
tokensL[i].value !== tokensR[i].value
) {
return false;
}
}

return true;
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -438,6 +463,7 @@ module.exports = {
isArrayFromMethod,
isParenthesised,
createGlobalLinebreakMatcher,
equalTokens,

isArrowToken,
isClosingBraceToken,
Expand Down
26 changes: 1 addition & 25 deletions lib/rules/no-useless-call.js
Expand Up @@ -28,30 +28,6 @@ function isCallOrNonVariadicApply(node) {
);
}

/**
* Checks whether or not the tokens of two given nodes are same.
* @param {ASTNode} left - A node 1 to compare.
* @param {ASTNode} right - A node 2 to compare.
* @param {SourceCode} sourceCode - The ESLint source code object.
* @returns {boolean} the source code for the given node.
*/
function equalTokens(left, right, sourceCode) {
const tokensL = sourceCode.getTokens(left);
const tokensR = sourceCode.getTokens(right);

if (tokensL.length !== tokensR.length) {
return false;
}
for (let i = 0; i < tokensL.length; ++i) {
if (tokensL[i].type !== tokensR[i].type ||
tokensL[i].value !== tokensR[i].value
) {
return false;
}
}

return true;
}

/**
* Checks whether or not `thisArg` is not changed by `.call()`/`.apply()`.
Expand All @@ -64,7 +40,7 @@ function isValidThisArg(expectedThis, thisArg, sourceCode) {
if (!expectedThis) {
return astUtils.isNullOrUndefined(thisArg);
}
return equalTokens(expectedThis, thisArg, sourceCode);
return astUtils.equalTokens(expectedThis, thisArg, sourceCode);
}

//------------------------------------------------------------------------------
Expand Down
26 changes: 1 addition & 25 deletions lib/rules/prefer-spread.js
Expand Up @@ -28,30 +28,6 @@ function isVariadicApplyCalling(node) {
);
}

/**
* Checks whether or not the tokens of two given nodes are same.
* @param {ASTNode} left - A node 1 to compare.
* @param {ASTNode} right - A node 2 to compare.
* @param {SourceCode} sourceCode - The ESLint source code object.
* @returns {boolean} the source code for the given node.
*/
function equalTokens(left, right, sourceCode) {
const tokensL = sourceCode.getTokens(left);
const tokensR = sourceCode.getTokens(right);

if (tokensL.length !== tokensR.length) {
return false;
}
for (let i = 0; i < tokensL.length; ++i) {
if (tokensL[i].type !== tokensR[i].type ||
tokensL[i].value !== tokensR[i].value
) {
return false;
}
}

return true;
}

/**
* Checks whether or not `thisArg` is not changed by `.apply()`.
Expand All @@ -64,7 +40,7 @@ function isValidThisArg(expectedThis, thisArg, context) {
if (!expectedThis) {
return astUtils.isNullOrUndefined(thisArg);
}
return equalTokens(expectedThis, thisArg, context);
return astUtils.equalTokens(expectedThis, thisArg, context);
}

//------------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/ast-utils.js
Expand Up @@ -1198,4 +1198,22 @@ describe("ast-utils", () => {
});
});
});

describe("equalTokens", () => {
it("should return true if tokens are equal", () => {
const code = "a=0;a=0;";
const ast = espree.parse(code, ESPREE_CONFIG);
const sourceCode = new SourceCode(code, ast);

assert.strictEqual(astUtils.equalTokens(ast.body[0], ast.body[1], sourceCode), true);
});

it("should return false if tokens are not equal", () => {
const code = "a=0;a=1;";
const ast = espree.parse(code, ESPREE_CONFIG);
const sourceCode = new SourceCode(code, ast);

assert.strictEqual(astUtils.equalTokens(ast.body[0], ast.body[1], sourceCode), false);
});
});
});

0 comments on commit 9064b9c

Please sign in to comment.