Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some functions used in eslint rules #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/get-next-location.js
Original file line number Diff line number Diff line change
@@ -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
}
133 changes: 13 additions & 120 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,13 @@
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-next-location"
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"
57 changes: 47 additions & 10 deletions src/token-predicate.js
Original file line number Diff line number Diff line change
@@ -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)
}

/**
Expand Down Expand Up @@ -125,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)
Expand Down