From 8aa54fa5712c782bee2f6b0bc66c25d286500151 Mon Sep 17 00:00:00 2001 From: Mathias Rasmussen Date: Fri, 12 Nov 2021 23:05:37 +0100 Subject: [PATCH 1/4] Simplify negate function --- src/token-predicate.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/token-predicate.js b/src/token-predicate.js index 9814c1f..ed6f0f4 100644 --- a/src/token-predicate.js +++ b/src/token-predicate.js @@ -1,19 +1,10 @@ -/** - * Negate the result of `this` calling. - * @param {Token} token The token to check. - * @returns {boolean} `true` if the result of `this(token)` is `false`. - */ -function negate0(token) { - return !this(token) //eslint-disable-line no-invalid-this -} - /** * Creates the negate function of the given function. * @param {function(Token):boolean} f - The function to negate. * @returns {function(Token):boolean} Negated function. */ function negate(f) { - return negate0.bind(f) + return token => !f(token); } /** From ae67da32c9d3218db0ee6eef9f65a1e8be74d587 Mon Sep 17 00:00:00 2001 From: Mathias Rasmussen Date: Sat, 13 Nov 2021 00:09:03 +0100 Subject: [PATCH 2/4] Simplify exports --- src/index.js | 132 +++++---------------------------------------------- 1 file changed, 12 insertions(+), 120 deletions(-) diff --git a/src/index.js b/src/index.js index 4641431..08c2cbc 100644 --- a/src/index.js +++ b/src/index.js @@ -1,120 +1,12 @@ -import { findVariable } from "./find-variable" -import { getFunctionHeadLocation } from "./get-function-head-location" -import { getFunctionNameWithKind } from "./get-function-name-with-kind" -import { getInnermostScope } from "./get-innermost-scope" -import { getPropertyName } from "./get-property-name" -import { getStaticValue } from "./get-static-value" -import { getStringIfConstant } from "./get-string-if-constant" -import { hasSideEffect } from "./has-side-effect" -import { isParenthesized } from "./is-parenthesized" -import { PatternMatcher } from "./pattern-matcher" -import { - CALL, - CONSTRUCT, - ESM, - READ, - ReferenceTracker, -} from "./reference-tracker" -import { - isArrowToken, - isClosingBraceToken, - isClosingBracketToken, - isClosingParenToken, - isColonToken, - isCommaToken, - isCommentToken, - isNotArrowToken, - isNotClosingBraceToken, - isNotClosingBracketToken, - isNotClosingParenToken, - isNotColonToken, - isNotCommaToken, - isNotCommentToken, - isNotOpeningBraceToken, - isNotOpeningBracketToken, - isNotOpeningParenToken, - isNotSemicolonToken, - isOpeningBraceToken, - isOpeningBracketToken, - isOpeningParenToken, - isSemicolonToken, -} from "./token-predicate" - -export default { - CALL, - CONSTRUCT, - ESM, - findVariable, - getFunctionHeadLocation, - getFunctionNameWithKind, - getInnermostScope, - getPropertyName, - getStaticValue, - getStringIfConstant, - hasSideEffect, - isArrowToken, - isClosingBraceToken, - isClosingBracketToken, - isClosingParenToken, - isColonToken, - isCommaToken, - isCommentToken, - isNotArrowToken, - isNotClosingBraceToken, - isNotClosingBracketToken, - isNotClosingParenToken, - isNotColonToken, - isNotCommaToken, - isNotCommentToken, - isNotOpeningBraceToken, - isNotOpeningBracketToken, - isNotOpeningParenToken, - isNotSemicolonToken, - isOpeningBraceToken, - isOpeningBracketToken, - isOpeningParenToken, - isParenthesized, - isSemicolonToken, - PatternMatcher, - READ, - ReferenceTracker, -} -export { - CALL, - CONSTRUCT, - ESM, - findVariable, - getFunctionHeadLocation, - getFunctionNameWithKind, - getInnermostScope, - getPropertyName, - getStaticValue, - getStringIfConstant, - hasSideEffect, - isArrowToken, - isClosingBraceToken, - isClosingBracketToken, - isClosingParenToken, - isColonToken, - isCommaToken, - isCommentToken, - isNotArrowToken, - isNotClosingBraceToken, - isNotClosingBracketToken, - isNotClosingParenToken, - isNotColonToken, - isNotCommaToken, - isNotCommentToken, - isNotOpeningBraceToken, - isNotOpeningBracketToken, - isNotOpeningParenToken, - isNotSemicolonToken, - isOpeningBraceToken, - isOpeningBracketToken, - isOpeningParenToken, - isParenthesized, - isSemicolonToken, - PatternMatcher, - READ, - ReferenceTracker, -} +export * from "./find-variable" +export * from "./get-function-head-location" +export * from "./get-function-name-with-kind" +export * from "./get-innermost-scope" +export * from "./get-property-name" +export * from "./get-static-value" +export * from "./get-string-if-constant" +export * from "./has-side-effect" +export * from "./is-parenthesized" +export * from "./pattern-matcher" +export * from "./reference-tracker" +export * from "./token-predicate" From 3cfd20cf9d427d61f4015676ffceca0462ece958 Mon Sep 17 00:00:00 2001 From: Mathias Rasmussen Date: Fri, 12 Nov 2021 23:17:47 +0100 Subject: [PATCH 3/4] Add missing token predicates from eslint --- src/token-predicate.js | 48 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/token-predicate.js b/src/token-predicate.js index ed6f0f4..a19446e 100644 --- a/src/token-predicate.js +++ b/src/token-predicate.js @@ -4,7 +4,7 @@ * @returns {function(Token):boolean} Negated function. */ function negate(f) { - return token => !f(token); + return (token) => !f(token) } /** @@ -116,6 +116,52 @@ export function isCommentToken(token) { return ["Block", "Line", "Shebang"].includes(token.type) } +/** + * Checks if the given token is a `=` token or not. + * @param {Token} token The token to check. + * @returns {boolean} `true` if the token is a `=` token. + */ +export function isEqToken(token) { + return isPunctuatorTokenWithValue(token, "=") +} + +/** + * Checks if the given token is a dot token or not. + * @param {Token} token The token to check. + * @returns {boolean} `true` if the token is a dot token. + */ +export function isDotToken(token) { + return isPunctuatorTokenWithValue(token, ".") +} + +/** + * Checks if the given token is a `?.` token or not. + * @param {Token} token The token to check. + * @returns {boolean} `true` if the token is a `?.` token. + */ +export function isQuestionDotToken(token) { + return isPunctuatorTokenWithValue(token, "?.") +} + +/** + * Checks if the given token is a keyword token or not. + * @param {Token} token The token to check. + * @returns {boolean} `true` if the token is a keyword token. + */ +export function isKeywordToken(token) { + return token.type === "Keyword" +} + +/** + * Determines whether two adjacent tokens are on the same line. + * @param {Object} left The left token object. + * @param {Object} right The right token object. + * @returns {boolean} Whether or not the tokens are on the same line. + */ +export function isTokenOnSameLine(left, right) { + return left.loc.end.line === right.loc.start.line +} + export const isNotArrowToken = negate(isArrowToken) export const isNotCommaToken = negate(isCommaToken) export const isNotSemicolonToken = negate(isSemicolonToken) From 2ab1f57dc794ed15e00ccc7c45d0d0df2974d714 Mon Sep 17 00:00:00 2001 From: Mathias Rasmussen Date: Sat, 13 Nov 2021 00:36:26 +0100 Subject: [PATCH 4/4] Add getNextLocation --- src/get-next-location.js | 17 +++++++++++++++++ src/index.js | 1 + 2 files changed, 18 insertions(+) create mode 100644 src/get-next-location.js diff --git a/src/get-next-location.js b/src/get-next-location.js new file mode 100644 index 0000000..6b8e82b --- /dev/null +++ b/src/get-next-location.js @@ -0,0 +1,17 @@ +/** + * Gets next location when the result is not out of bound, otherwise returns null. + * @param {SourceCode} sourceCode The sourceCode + * @param {{line: number, column: number}} location The location + * @returns {{line: number, column: number} | null} Next location + */ +export function getNextLocation(sourceCode, { line, column }) { + if (column < sourceCode.lines[line - 1].length) { + return { line, column: column + 1 } + } + + if (line < sourceCode.lines.length) { + return { line: line + 1, column: 0 } + } + + return null +} diff --git a/src/index.js b/src/index.js index 08c2cbc..02ac0a2 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ export * from "./find-variable" export * from "./get-function-head-location" export * from "./get-function-name-with-kind" export * from "./get-innermost-scope" +export * from "./get-next-location" export * from "./get-property-name" export * from "./get-static-value" export * from "./get-string-if-constant"