Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 24, 2025
1 parent 70d4b06 commit 5b97867
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 42 deletions.
29 changes: 4 additions & 25 deletions lib/utils/fix-sort-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@ import type { Rule, AST as ESLintAST, SourceCode } from "eslint";
import type { AST } from "jsonc-eslint-parser";
import type * as ESTree from "estree";

/**
* Check if the token is a comma.
*/
function isComma(token: ESLintAST.Token): boolean {
return isCommaToken(token);
}

/**
* Check if the token is a closing brace.
*/
function isClosingBrace(token: ESLintAST.Token): boolean {
return isClosingBraceToken(token);
}

/**
* Check if the token is a closing bracket.
*/
function isClosingBracket(token: ESLintAST.Token): boolean {
return isClosingBracketToken(token);
}

export type AroundTarget =
| {
before: ESLintAST.Token;
Expand Down Expand Up @@ -201,7 +180,7 @@ function getElementEndInfo(
}
const comma = afterToken;
const nextElement = sourceCode.getTokenAfter(afterToken)!;
if (isComma(nextElement)) {
if (isCommaToken(nextElement)) {
// If the next element is empty,
// the position of the comma is the end of the element's range.
return {
Expand All @@ -210,7 +189,7 @@ function getElementEndInfo(
last: comma,
};
}
if (isClosingBrace(nextElement) || isClosingBracket(nextElement)) {
if (isClosingBraceToken(nextElement) || isClosingBracketToken(nextElement)) {
// If the next token is a closing brace or bracket,
// the position of the comma is the end of the element's range.
return {
Expand Down Expand Up @@ -274,7 +253,7 @@ function getLastTokenWithTrailingComments(
(after = sourceCode.getTokenAfter(last, {
includeComments: true,
})) &&
(isCommentToken(after) || isComma(after)) &&
(isCommentToken(after) || isCommaToken(after)) &&
node.loc.end.line === after.loc!.end.line
) {
last = after;
Expand Down Expand Up @@ -310,7 +289,7 @@ function getPrevElementInfo(
const comma = beforeToken;
const prevElement = sourceCode.getTokenBefore(beforeToken)!;

if (isComma(prevElement)) {
if (isCommaToken(prevElement)) {
// If the previous element is empty,
// the position of the comma is the end of the previous element's range.
return {
Expand Down
35 changes: 18 additions & 17 deletions typings/@eslint-community/eslint-utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import type { Comment } from "estree";
import type { AST, SourceCode } from "eslint";

type IsToken = (token: AST.Token | Comment) => token is AST.Token;
type IsToken<V extends string> = (
token: AST.Token | Comment,
) => token is AST.Token & { value: V };
type IsNotToken = (token: AST.Token | Comment) => boolean;

declare module "@eslint-community/eslint-utils" {
export const findVariable: unknown;
export const getFunctionHeadLocation: IsToken;
export const getFunctionNameWithKind: IsToken;
export const getInnermostScope: IsToken;
export const getPropertyName: IsToken;
export const getStaticValue: IsToken;
export const getStringIfConstant: IsToken;
export const hasSideEffect: IsToken;
export const isArrowToken: IsToken;
export const isClosingBraceToken: IsToken;
export const isClosingBracketToken: IsToken;
export const isClosingParenToken: IsToken;
export const isColonToken: IsToken;
export const isCommaToken: IsToken;
export const getFunctionHeadLocation: unknown;
export const getFunctionNameWithKind: unknown;
export const getInnermostScope: unknown;
export const getPropertyName: unknown;
export const getStaticValue: unknown;
export const getStringIfConstant: unknown;
export const hasSideEffect: unknown;
export const isClosingBraceToken: IsToken<"}">;
export const isClosingBracketToken: IsToken<"]">;
export const isClosingParenToken: IsToken<")">;
export const isColonToken: IsToken<";">;
export const isCommaToken: IsToken<",">;
export const isCommentToken: (token: AST.Token | Comment) => token is Comment;
export const isNotArrowToken: IsNotToken;
export const isNotClosingBraceToken: IsNotToken;
Expand All @@ -31,9 +32,9 @@ declare module "@eslint-community/eslint-utils" {
export const isNotOpeningBracketToken: IsNotToken;
export const isNotOpeningParenToken: IsNotToken;
export const isNotSemicolonToken: IsNotToken;
export const isOpeningBraceToken: IsToken;
export const isOpeningBracketToken: IsToken;
export const isOpeningParenToken: IsToken;
export const isOpeningBraceToken: IsToken<"{">;
export const isOpeningBracketToken: IsToken<"[">;
export const isOpeningParenToken: IsToken<"(">;
export function isParenthesized(node: any, sourceCode: SourceCode): boolean;
export function isParenthesized(
times: number,
Expand Down

0 comments on commit 5b97867

Please sign in to comment.