Skip to content

refactor: use @typescript-eslint/scope-manager instead of eslint-scope #301

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

Merged
merged 1 commit into from
Nov 9, 2022
Merged
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
5 changes: 4 additions & 1 deletion src/rules/@typescript-eslint/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ export default createRule("@typescript-eslint/no-unnecessary-condition", {
})
}

const mutableVarReferenceIds: TSESTree.Identifier[] = []
const mutableVarReferenceIds: (
| TSESTree.Identifier
| TSESTree.JSXIdentifier
)[] = []
const scriptElements: AST.SvelteScriptElement[] = []
let inSvelteReactiveStatement = false

Expand Down
6 changes: 4 additions & 2 deletions src/rules/prefer-destructured-store-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ export default createRule("prefer-destructured-store-props", {

/** Checks whether the given node is reactive variable definition with member expression. */
function isReactiveVariableDefinitionWithMemberExpression(
node: TSESTree.Identifier,
node: TSESTree.Identifier | TSESTree.JSXIdentifier,
): node is TSESTree.Identifier & {
parent: TSESTree.MemberExpression & {
parent: TSESTree.AssignmentExpression & { left: TSESTree.Identifier }
}
} {
return (
node.type === "Identifier" &&
node.parent?.type === "MemberExpression" &&
node.parent.object === node &&
getPropertyName(node.parent) === propName &&
Expand All @@ -101,13 +102,14 @@ export default createRule("prefer-destructured-store-props", {

/** Checks whether the given node is reactive variable definition with destructuring. */
function isReactiveVariableDefinitionWithDestructuring(
node: TSESTree.Identifier,
node: TSESTree.Identifier | TSESTree.JSXIdentifier,
): node is TSESTree.Identifier & {
parent: TSESTree.AssignmentExpression & {
left: TSESTree.ObjectPattern
}
} {
return (
node.type === "Identifier" &&
node.parent?.type === "AssignmentExpression" &&
node.parent.right === node &&
node.parent.left.type === "ObjectPattern" &&
Expand Down
2 changes: 1 addition & 1 deletion src/rules/reference-helpers/svelte-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TSESTree } from "@typescript-eslint/types"
import type { Variable } from "@typescript-eslint/scope-manager"
import { ReferenceTracker } from "eslint-utils"
import type { Variable } from "eslint-scope"
import type { RuleContext } from "../../types"
import type { TS, TSTools } from "../../utils/ts-utils"
import { getTypeScriptTools } from "../../utils/ts-utils"
Expand Down
18 changes: 9 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { JSONSchema4 } from "json-schema"
import type {
Linter,
Rule,
Scope,
SourceCode as ESLintSourceCode,
} from "eslint"
import type { Linter, Rule, SourceCode as ESLintSourceCode } from "eslint"
import type { AST } from "svelte-eslint-parser"
import type { TSESTree } from "@typescript-eslint/types"
import type {
ScopeManager,
Scope,
Variable,
} from "@typescript-eslint/scope-manager"
import type {
ASTNode,
ASTNodeWithParent,
Expand Down Expand Up @@ -140,11 +140,11 @@ export type RuleContext = {

getAncestors(): ASTNode[]

getDeclaredVariables(node: TSESTree.Node): Scope.Variable[]
getDeclaredVariables(node: TSESTree.Node): Variable[]

getFilename(): string

getScope(): Scope.Scope
getScope(): Scope

getSourceCode(): SourceCode

Expand Down Expand Up @@ -215,7 +215,7 @@ export interface SourceCode {
lines: string[]
hasBOM: boolean
parserServices: ESLintSourceCode.ParserServices
scopeManager: Scope.ScopeManager
scopeManager: ScopeManager
visitorKeys: ESLintSourceCode.VisitorKeys

getText(node?: NodeOrToken, beforeCount?: number, afterCount?: number): string
Expand Down
6 changes: 3 additions & 3 deletions src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ASTNode, RuleContext, SourceCode } from "../types"
import type { TSESTree } from "@typescript-eslint/types"
import type { Scope, Variable } from "@typescript-eslint/scope-manager"
import type { AST as SvAST } from "svelte-eslint-parser"
import * as eslintUtils from "eslint-utils"
import type { Scope } from "eslint"
import voidElements from "./void-elements"

/**
Expand Down Expand Up @@ -237,7 +237,7 @@ export function getLangValue(
export function findVariable(
context: RuleContext,
node: TSESTree.Identifier,
): Scope.Variable | null {
): Variable | null {
const initialScope = eslintUtils.getInnermostScope(
getScope(context, node),
node,
Expand All @@ -259,7 +259,7 @@ export function findVariable(
export function getScope(
context: RuleContext,
currentNode: TSESTree.Node,
): Scope.Scope {
): Scope {
const scopeManager = context.getSourceCode().scopeManager

let node: TSESTree.Node | null = currentNode
Expand Down
48 changes: 45 additions & 3 deletions typings/eslint-utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST } from "svelte-eslint-parser"
import type { Scope } from "eslint"
import type { TSESTree } from "@typescript-eslint/types"
import type { Scope } from "@typescript-eslint/scope-manager"
import type { CALL, CONSTRUCT, ESM, READ } from "eslint-utils/referenceTracker"
export {
ReferenceTracker,
TrackedReferences,
Expand Down Expand Up @@ -32,7 +33,7 @@ export function isNotClosingBraceToken(token: Token): boolean
export function isNotCommentToken(token: Token): boolean

export function findVariable(
initialScope: Scope.Scope,
initialScope: Scope,
nameOrNode: TSESTree.Identifier | string,
): Scope.Variable

Expand All @@ -45,5 +46,46 @@ export function getPropertyName(
| TSESTree.MethodDefinition
| TSESTree.Property
| TSESTree.PropertyDefinition,
initialScope?: Scope.Scope,
initialScope?: Scope,
): string | null

/**
* Get the innermost scope which contains a given location.
*/
export function getInnermostScope(
initialScope: Scope,
node: TSESTree.Node,
): Scope

export class ReferenceTracker {
public static readonly CALL: typeof CALL

public static readonly CONSTRUCT: typeof CONSTRUCT

public static readonly ESM: typeof ESM

public static readonly READ: typeof READ

public constructor(globalScope: Scope, options?: ReferenceTrackerOptions)

/**
* Iterate the references of CommonJS modules.
*/
public iterateCjsReferences<T = unknown>(
traceMap: TraceMap<T>,
): IterableIterator<TrackedReferences<T>>

/**
* Iterate the references of ES modules.
*/
public iterateEsmReferences<T = unknown>(
traceMap: TraceMap<T>,
): IterableIterator<TrackedReferences<T>>

/**
* Iterate the references of global variables.
*/
public iterateGlobalReferences<T = unknown>(
traceMap: TraceMap<T>,
): IterableIterator<TrackedReferences<T>>
}