Skip to content

Commit 5b97867

Browse files
committed
minor refactor
1 parent 70d4b06 commit 5b97867

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

lib/utils/fix-sort-elements.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,6 @@ import type { Rule, AST as ESLintAST, SourceCode } from "eslint";
1111
import type { AST } from "jsonc-eslint-parser";
1212
import type * as ESTree from "estree";
1313

14-
/**
15-
* Check if the token is a comma.
16-
*/
17-
function isComma(token: ESLintAST.Token): boolean {
18-
return isCommaToken(token);
19-
}
20-
21-
/**
22-
* Check if the token is a closing brace.
23-
*/
24-
function isClosingBrace(token: ESLintAST.Token): boolean {
25-
return isClosingBraceToken(token);
26-
}
27-
28-
/**
29-
* Check if the token is a closing bracket.
30-
*/
31-
function isClosingBracket(token: ESLintAST.Token): boolean {
32-
return isClosingBracketToken(token);
33-
}
34-
3514
export type AroundTarget =
3615
| {
3716
before: ESLintAST.Token;
@@ -201,7 +180,7 @@ function getElementEndInfo(
201180
}
202181
const comma = afterToken;
203182
const nextElement = sourceCode.getTokenAfter(afterToken)!;
204-
if (isComma(nextElement)) {
183+
if (isCommaToken(nextElement)) {
205184
// If the next element is empty,
206185
// the position of the comma is the end of the element's range.
207186
return {
@@ -210,7 +189,7 @@ function getElementEndInfo(
210189
last: comma,
211190
};
212191
}
213-
if (isClosingBrace(nextElement) || isClosingBracket(nextElement)) {
192+
if (isClosingBraceToken(nextElement) || isClosingBracketToken(nextElement)) {
214193
// If the next token is a closing brace or bracket,
215194
// the position of the comma is the end of the element's range.
216195
return {
@@ -274,7 +253,7 @@ function getLastTokenWithTrailingComments(
274253
(after = sourceCode.getTokenAfter(last, {
275254
includeComments: true,
276255
})) &&
277-
(isCommentToken(after) || isComma(after)) &&
256+
(isCommentToken(after) || isCommaToken(after)) &&
278257
node.loc.end.line === after.loc!.end.line
279258
) {
280259
last = after;
@@ -310,7 +289,7 @@ function getPrevElementInfo(
310289
const comma = beforeToken;
311290
const prevElement = sourceCode.getTokenBefore(beforeToken)!;
312291

313-
if (isComma(prevElement)) {
292+
if (isCommaToken(prevElement)) {
314293
// If the previous element is empty,
315294
// the position of the comma is the end of the previous element's range.
316295
return {

typings/@eslint-community/eslint-utils/index.d.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import type { Comment } from "estree";
22
import type { AST, SourceCode } from "eslint";
33

4-
type IsToken = (token: AST.Token | Comment) => token is AST.Token;
4+
type IsToken<V extends string> = (
5+
token: AST.Token | Comment,
6+
) => token is AST.Token & { value: V };
57
type IsNotToken = (token: AST.Token | Comment) => boolean;
68

79
declare module "@eslint-community/eslint-utils" {
810
export const findVariable: unknown;
9-
export const getFunctionHeadLocation: IsToken;
10-
export const getFunctionNameWithKind: IsToken;
11-
export const getInnermostScope: IsToken;
12-
export const getPropertyName: IsToken;
13-
export const getStaticValue: IsToken;
14-
export const getStringIfConstant: IsToken;
15-
export const hasSideEffect: IsToken;
16-
export const isArrowToken: IsToken;
17-
export const isClosingBraceToken: IsToken;
18-
export const isClosingBracketToken: IsToken;
19-
export const isClosingParenToken: IsToken;
20-
export const isColonToken: IsToken;
21-
export const isCommaToken: IsToken;
11+
export const getFunctionHeadLocation: unknown;
12+
export const getFunctionNameWithKind: unknown;
13+
export const getInnermostScope: unknown;
14+
export const getPropertyName: unknown;
15+
export const getStaticValue: unknown;
16+
export const getStringIfConstant: unknown;
17+
export const hasSideEffect: unknown;
18+
export const isClosingBraceToken: IsToken<"}">;
19+
export const isClosingBracketToken: IsToken<"]">;
20+
export const isClosingParenToken: IsToken<")">;
21+
export const isColonToken: IsToken<";">;
22+
export const isCommaToken: IsToken<",">;
2223
export const isCommentToken: (token: AST.Token | Comment) => token is Comment;
2324
export const isNotArrowToken: IsNotToken;
2425
export const isNotClosingBraceToken: IsNotToken;
@@ -31,9 +32,9 @@ declare module "@eslint-community/eslint-utils" {
3132
export const isNotOpeningBracketToken: IsNotToken;
3233
export const isNotOpeningParenToken: IsNotToken;
3334
export const isNotSemicolonToken: IsNotToken;
34-
export const isOpeningBraceToken: IsToken;
35-
export const isOpeningBracketToken: IsToken;
36-
export const isOpeningParenToken: IsToken;
35+
export const isOpeningBraceToken: IsToken<"{">;
36+
export const isOpeningBracketToken: IsToken<"[">;
37+
export const isOpeningParenToken: IsToken<"(">;
3738
export function isParenthesized(node: any, sourceCode: SourceCode): boolean;
3839
export function isParenthesized(
3940
times: number,

0 commit comments

Comments
 (0)