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 ¶
- func AdditionalHooksFromSettings(settings map[string]interface{}, key string) *regexp.Regexp
- func FindEnclosingFunction(node *ast.Node) *ast.Node
- func GetFunctionBody(fn *ast.Node) *ast.Node
- func GetFunctionName(fn *ast.Node) *ast.Node
- func HasAsyncModifier(fn *ast.Node) bool
- func IsClassMember(fn *ast.Node) bool
- func IsComponentNameStr(s string) bool
- func IsComponentOrHookFn(fn *ast.Node) bool
- func IsEffectStyleHookName(name string) bool
- func IsForwardRefOrMemoCallback(fn *ast.Node, name string) bool
- func IsFunctionLikeContainer(node *ast.Node) bool
- func IsHookCallee(node *ast.Node) bool
- func IsHookName(s string) bool
- func IsInsideComponentOrHook(node *ast.Node) bool
- func IsReactCalleeNamed(node *ast.Node, name string) bool
- func IsUseEffectEventCallee(node *ast.Node) bool
- func IsUseIdentifier(node *ast.Node) bool
- func StripReactNamespace(node *ast.Node) *ast.Node
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdditionalHooksFromSettings ¶
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 ¶
FindEnclosingFunction walks up from `node` and returns the nearest function-like ancestor, or nil when `node` is at top level.
func GetFunctionBody ¶
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 ¶
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 ¶
HasAsyncModifier reports whether the function-like node carries the `async` modifier.
func IsClassMember ¶
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 ¶
IsComponentNameStr reports whether `s` looks like a React component name — PascalCase. Upstream's `isComponentName` is identical.
func IsComponentOrHookFn ¶
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 ¶
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 ¶
IsForwardRefOrMemoCallback reports whether `fn` is the immediate argument of a CallExpression whose callee is `<name>` or `React.<name>`.
func IsFunctionLikeContainer ¶
IsFunctionLikeContainer reports whether `node` is one of the function-like kinds the upstream rules treat as a "code path" boundary.
func IsHookCallee ¶
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 ¶
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 ¶
IsInsideComponentOrHook walks up from `node` and returns true once any ancestor function-like classifies as a component or hook.
func IsReactCalleeNamed ¶
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 ¶
IsUseEffectEventCallee reports whether `node` is the bare `useEffectEvent` or `React.useEffectEvent` callee.
func IsUseIdentifier ¶
IsUseIdentifier reports whether `node` is the React `use(...)` callee — either bare `use` or `React.use`.
func StripReactNamespace ¶
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.