Skip to content

Commit 1c694f0

Browse files
committed
refactor: use @typescript-eslint/scope-manager instead of eslint-scope
1 parent 4537a01 commit 1c694f0

File tree

6 files changed

+66
-19
lines changed

6 files changed

+66
-19
lines changed

src/rules/@typescript-eslint/no-unnecessary-condition.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ export default createRule("@typescript-eslint/no-unnecessary-condition", {
182182
})
183183
}
184184

185-
const mutableVarReferenceIds: TSESTree.Identifier[] = []
185+
const mutableVarReferenceIds: (
186+
| TSESTree.Identifier
187+
| TSESTree.JSXIdentifier
188+
)[] = []
186189
const scriptElements: AST.SvelteScriptElement[] = []
187190
let inSvelteReactiveStatement = false
188191

src/rules/prefer-destructured-store-props.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ export default createRule("prefer-destructured-store-props", {
7878

7979
/** Checks whether the given node is reactive variable definition with member expression. */
8080
function isReactiveVariableDefinitionWithMemberExpression(
81-
node: TSESTree.Identifier,
81+
node: TSESTree.Identifier | TSESTree.JSXIdentifier,
8282
): node is TSESTree.Identifier & {
8383
parent: TSESTree.MemberExpression & {
8484
parent: TSESTree.AssignmentExpression & { left: TSESTree.Identifier }
8585
}
8686
} {
8787
return (
88+
node.type === "Identifier" &&
8889
node.parent?.type === "MemberExpression" &&
8990
node.parent.object === node &&
9091
getPropertyName(node.parent) === propName &&
@@ -101,13 +102,14 @@ export default createRule("prefer-destructured-store-props", {
101102

102103
/** Checks whether the given node is reactive variable definition with destructuring. */
103104
function isReactiveVariableDefinitionWithDestructuring(
104-
node: TSESTree.Identifier,
105+
node: TSESTree.Identifier | TSESTree.JSXIdentifier,
105106
): node is TSESTree.Identifier & {
106107
parent: TSESTree.AssignmentExpression & {
107108
left: TSESTree.ObjectPattern
108109
}
109110
} {
110111
return (
112+
node.type === "Identifier" &&
111113
node.parent?.type === "AssignmentExpression" &&
112114
node.parent.right === node &&
113115
node.parent.left.type === "ObjectPattern" &&

src/rules/reference-helpers/svelte-store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TSESTree } from "@typescript-eslint/types"
2+
import type { Variable } from "@typescript-eslint/scope-manager"
23
import { ReferenceTracker } from "eslint-utils"
3-
import type { Variable } from "eslint-scope"
44
import type { RuleContext } from "../../types"
55
import type { TS, TSTools } from "../../utils/ts-utils"
66
import { getTypeScriptTools } from "../../utils/ts-utils"

src/types.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { JSONSchema4 } from "json-schema"
2-
import type {
3-
Linter,
4-
Rule,
5-
Scope,
6-
SourceCode as ESLintSourceCode,
7-
} from "eslint"
2+
import type { Linter, Rule, SourceCode as ESLintSourceCode } from "eslint"
83
import type { AST } from "svelte-eslint-parser"
94
import type { TSESTree } from "@typescript-eslint/types"
5+
import type {
6+
ScopeManager,
7+
Scope,
8+
Variable,
9+
} from "@typescript-eslint/scope-manager"
1010
import type {
1111
ASTNode,
1212
ASTNodeWithParent,
@@ -140,11 +140,11 @@ export type RuleContext = {
140140

141141
getAncestors(): ASTNode[]
142142

143-
getDeclaredVariables(node: TSESTree.Node): Scope.Variable[]
143+
getDeclaredVariables(node: TSESTree.Node): Variable[]
144144

145145
getFilename(): string
146146

147-
getScope(): Scope.Scope
147+
getScope(): Scope
148148

149149
getSourceCode(): SourceCode
150150

@@ -215,7 +215,7 @@ export interface SourceCode {
215215
lines: string[]
216216
hasBOM: boolean
217217
parserServices: ESLintSourceCode.ParserServices
218-
scopeManager: Scope.ScopeManager
218+
scopeManager: ScopeManager
219219
visitorKeys: ESLintSourceCode.VisitorKeys
220220

221221
getText(node?: NodeOrToken, beforeCount?: number, afterCount?: number): string

src/utils/ast-utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ASTNode, RuleContext, SourceCode } from "../types"
22
import type { TSESTree } from "@typescript-eslint/types"
3+
import type { Scope, Variable } from "@typescript-eslint/scope-manager"
34
import type { AST as SvAST } from "svelte-eslint-parser"
45
import * as eslintUtils from "eslint-utils"
5-
import type { Scope } from "eslint"
66
import voidElements from "./void-elements"
77

88
/**
@@ -237,7 +237,7 @@ export function getLangValue(
237237
export function findVariable(
238238
context: RuleContext,
239239
node: TSESTree.Identifier,
240-
): Scope.Variable | null {
240+
): Variable | null {
241241
const initialScope = eslintUtils.getInnermostScope(
242242
getScope(context, node),
243243
node,
@@ -259,7 +259,7 @@ export function findVariable(
259259
export function getScope(
260260
context: RuleContext,
261261
currentNode: TSESTree.Node,
262-
): Scope.Scope {
262+
): Scope {
263263
const scopeManager = context.getSourceCode().scopeManager
264264

265265
let node: TSESTree.Node | null = currentNode

typings/eslint-utils/index.d.ts

+45-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AST } from "svelte-eslint-parser"
2-
import type { Scope } from "eslint"
32
import type { TSESTree } from "@typescript-eslint/types"
3+
import type { Scope } from "@typescript-eslint/scope-manager"
4+
import type { CALL, CONSTRUCT, ESM, READ } from "eslint-utils/referenceTracker"
45
export {
56
ReferenceTracker,
67
TrackedReferences,
@@ -32,7 +33,7 @@ export function isNotClosingBraceToken(token: Token): boolean
3233
export function isNotCommentToken(token: Token): boolean
3334

3435
export function findVariable(
35-
initialScope: Scope.Scope,
36+
initialScope: Scope,
3637
nameOrNode: TSESTree.Identifier | string,
3738
): Scope.Variable
3839

@@ -45,5 +46,46 @@ export function getPropertyName(
4546
| TSESTree.MethodDefinition
4647
| TSESTree.Property
4748
| TSESTree.PropertyDefinition,
48-
initialScope?: Scope.Scope,
49+
initialScope?: Scope,
4950
): string | null
51+
52+
/**
53+
* Get the innermost scope which contains a given location.
54+
*/
55+
export function getInnermostScope(
56+
initialScope: Scope,
57+
node: TSESTree.Node,
58+
): Scope
59+
60+
export class ReferenceTracker {
61+
public static readonly CALL: typeof CALL
62+
63+
public static readonly CONSTRUCT: typeof CONSTRUCT
64+
65+
public static readonly ESM: typeof ESM
66+
67+
public static readonly READ: typeof READ
68+
69+
public constructor(globalScope: Scope, options?: ReferenceTrackerOptions)
70+
71+
/**
72+
* Iterate the references of CommonJS modules.
73+
*/
74+
public iterateCjsReferences<T = unknown>(
75+
traceMap: TraceMap<T>,
76+
): IterableIterator<TrackedReferences<T>>
77+
78+
/**
79+
* Iterate the references of ES modules.
80+
*/
81+
public iterateEsmReferences<T = unknown>(
82+
traceMap: TraceMap<T>,
83+
): IterableIterator<TrackedReferences<T>>
84+
85+
/**
86+
* Iterate the references of global variables.
87+
*/
88+
public iterateGlobalReferences<T = unknown>(
89+
traceMap: TraceMap<T>,
90+
): IterableIterator<TrackedReferences<T>>
91+
}

0 commit comments

Comments
 (0)