From 9064b9c1849feb87b05a22f751bc2b45317417ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Mon, 6 Nov 2017 11:13:21 +0800 Subject: [PATCH] Chore: add equalTokens in ast-utils. (#9500) --- lib/ast-utils.js | 26 ++++++++++++++++++++++++++ lib/rules/no-useless-call.js | 26 +------------------------- lib/rules/prefer-spread.js | 26 +------------------------- tests/lib/ast-utils.js | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 50 deletions(-) diff --git a/lib/ast-utils.js b/lib/ast-utils.js index 23d2a7dcbc4..a186bdee54d 100644 --- a/lib/ast-utils.js +++ b/lib/ast-utils.js @@ -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 //------------------------------------------------------------------------------ @@ -438,6 +463,7 @@ module.exports = { isArrayFromMethod, isParenthesised, createGlobalLinebreakMatcher, + equalTokens, isArrowToken, isClosingBraceToken, diff --git a/lib/rules/no-useless-call.js b/lib/rules/no-useless-call.js index eb67bcb3b25..e4820ac248e 100644 --- a/lib/rules/no-useless-call.js +++ b/lib/rules/no-useless-call.js @@ -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()`. @@ -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); } //------------------------------------------------------------------------------ diff --git a/lib/rules/prefer-spread.js b/lib/rules/prefer-spread.js index 1f578582843..c111d5f98e8 100644 --- a/lib/rules/prefer-spread.js +++ b/lib/rules/prefer-spread.js @@ -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()`. @@ -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); } //------------------------------------------------------------------------------ diff --git a/tests/lib/ast-utils.js b/tests/lib/ast-utils.js index 65600f3532a..538b941cd03 100644 --- a/tests/lib/ast-utils.js +++ b/tests/lib/ast-utils.js @@ -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); + }); + }); });