react_hooksutil

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package react_hooksutil holds the AST helpers shared by every rule in the `eslint-plugin-react-hooks` port.

Both `rules-of-hooks` and `exhaustive-deps` need the same set of queries — "is this a hook callee?", "is this enclosing function a component?", "what's the display name of this function-like?", "is this a `React.<x>` member access?", etc. Putting them here keeps semantics consistent across rules and removes the second (and third) copy of every predicate.

Naming convention follows the rest of the codebase: predicates start with `Is*`, getters with `Get*`. The package never imports `internal/utils/` — the rules consume `internal/utils/` separately and pass values through.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdditionalHooksFromSettings

func AdditionalHooksFromSettings(settings map[string]interface{}, key string) *regexp.Regexp

AdditionalHooksFromSettings reads `settings['react-hooks'].<key>` (typically `additionalHooks` for exhaustive-deps, or `additionalEffectHooks` for rules-of-hooks) and compiles it as a regex. Returns nil when the setting is absent or the pattern fails to compile — mirroring upstream's lenient behavior of silently ignoring malformed regex strings.

func FindEnclosingFunction

func FindEnclosingFunction(node *ast.Node) *ast.Node

FindEnclosingFunction walks up from `node` and returns the nearest function-like ancestor, or nil when `node` is at top level.

func GetFunctionBody

func GetFunctionBody(fn *ast.Node) *ast.Node

GetFunctionBody returns the body of a function-like node — Block for normal functions, the BlockOrExpression body for arrow functions, or nil for abstract/declared signatures.

func GetFunctionName

func GetFunctionName(fn *ast.Node) *ast.Node

GetFunctionName mirrors upstream's `getFunctionName(node)`. Returns:

  • For named FunctionDeclaration / FunctionExpression: the `id` Identifier.
  • For MethodDeclaration / GetAccessor / SetAccessor inside an ObjectLiteralExpression: the method's own name.
  • For ArrowFunction / anonymous FunctionExpression: the assignment target — VariableDeclaration name, BinaryExpression LHS, PropertyAssignment name, BindingElement name, or ShorthandPropertyAssignment name.
  • nil otherwise.

MethodDeclaration / GetAccessor / SetAccessor inside a class body intentionally returns nil so the class-member branch can take over.

func HasAsyncModifier

func HasAsyncModifier(fn *ast.Node) bool

HasAsyncModifier reports whether the function-like node carries the `async` modifier.

func IsClassMember

func IsClassMember(fn *ast.Node) bool

IsClassMember reports whether `fn` is a member of a class — either a MethodDeclaration / GetAccessor / SetAccessor / Constructor whose direct parent is a class, or an ArrowFunction / FunctionExpression that initializes a class PropertyDeclaration (class-field arrow).

func IsComponentNameStr

func IsComponentNameStr(s string) bool

IsComponentNameStr reports whether `s` looks like a React component name — PascalCase. Upstream's `isComponentName` is identical.

func IsComponentOrHookFn

func IsComponentOrHookFn(fn *ast.Node) bool

IsComponentOrHookFn reports whether the function-like itself is a React component or hook (named appropriately, or an anonymous arg to forwardRef / memo). Mirrors upstream's `isDirectlyInsideComponentOrHook` predicate at the function level.

func IsEffectStyleHookName

func IsEffectStyleHookName(name string) bool

IsEffectStyleHookName reports whether the bare hook name (no `React.` prefix) parses as an effect-style hook by the `Effect($|[^a-z])` rule. Used by exhaustive-deps to gate the "extra over-specified deps are okay" exception that effects get but memo / callback don't.

func IsForwardRefOrMemoCallback

func IsForwardRefOrMemoCallback(fn *ast.Node, name string) bool

IsForwardRefOrMemoCallback reports whether `fn` is the immediate argument of a CallExpression whose callee is `<name>` or `React.<name>`.

func IsFunctionLikeContainer

func IsFunctionLikeContainer(node *ast.Node) bool

IsFunctionLikeContainer reports whether `node` is one of the function-like kinds the upstream rules treat as a "code path" boundary.

func IsHookCallee

func IsHookCallee(node *ast.Node) bool

IsHookCallee mirrors upstream's `isHook(node)`: the callee is either an Identifier whose name is a hook, or a non-computed member expression `Namespace.useFoo` whose object is a PascalCase identifier and whose property is itself a hook name.

func IsHookName

func IsHookName(s string) bool

IsHookName reports whether `s` follows the React hook naming convention: either the bare `use` (the React `use(...)` hook) or `useFoo` / `use1` (`use` followed by uppercase letter or digit).

func IsInsideComponentOrHook

func IsInsideComponentOrHook(node *ast.Node) bool

IsInsideComponentOrHook walks up from `node` and returns true once any ancestor function-like classifies as a component or hook.

func IsReactCalleeNamed

func IsReactCalleeNamed(node *ast.Node, name string) bool

IsReactCalleeNamed reports whether `node` is `<name>` (bare identifier) or `React.<name>` (PropertyAccessExpression with object `React` and property `<name>`). Mirrors upstream's `isReactFunction(node, functionName)`.

func IsUseEffectEventCallee

func IsUseEffectEventCallee(node *ast.Node) bool

IsUseEffectEventCallee reports whether `node` is the bare `useEffectEvent` or `React.useEffectEvent` callee.

func IsUseIdentifier

func IsUseIdentifier(node *ast.Node) bool

IsUseIdentifier reports whether `node` is the React `use(...)` callee — either bare `use` or `React.use`.

func StripReactNamespace

func StripReactNamespace(node *ast.Node) *ast.Node

StripReactNamespace returns the property identifier of a `React.foo` member expression, or the original node otherwise. Mirrors upstream's `getNodeWithoutReactNamespace`. Paren wrappers are peeled transparently — tsgo preserves them where ESTree flattens.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL