reactutil

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT, MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultReactCreateClass = "createReactClass"

DefaultReactCreateClass is the fallback ES5 factory name when `settings.react.createClass` is not configured, matching eslint-plugin-react.

View Source
const DefaultReactFragment = "Fragment"

DefaultReactFragment is the fallback fragment name for JSX shorthand fragment diagnostics when `settings.react.fragment` is not configured, matching eslint-plugin-react.

View Source
const DefaultReactPragma = "React"

DefaultReactPragma is the fallback object name for createElement calls when `settings.react.pragma` is not configured, matching eslint-plugin-react.

Variables

This section is empty.

Functions

func ExtendsReactComponent added in v0.15.1

func ExtendsReactComponent(classNode *ast.Node, pragma string) bool

ExtendsReactComponent reports whether `classNode` (a ClassDeclaration or ClassExpression) has an `extends` clause referencing `Component` or `PureComponent` — either as a bare identifier or qualified by the configured pragma (e.g. `React.Component`). Parentheses are skipped. Pass the empty string for pragma to default to `DefaultReactPragma`.

NOTE: Matches the name regex used by eslint-plugin-react's `componentUtil.isES6Component` (`/^(Pure)?Component$/`). Aliased imports (e.g. `import { Component as C }`) are not resolved — same as the upstream rule.

func GetEnclosingReactComponent added in v0.15.1

func GetEnclosingReactComponent(node *ast.Node, pragma, createClass string) *ast.Node

GetEnclosingReactComponent is IsInsideReactComponent's sibling that returns the component node itself (the ClassDeclaration / ClassExpression, or the ObjectLiteralExpression passed to createReactClass) rather than a bool. Returns nil when `node` is not inside a React component. See IsInsideReactComponent for the detection rules.

func GetEnclosingReactComponentOrStateless added in v0.15.1

func GetEnclosingReactComponentOrStateless(node *ast.Node, pragma, createClass string) *ast.Node

GetEnclosingReactComponentOrStateless is GetEnclosingReactComponent extended with eslint-plugin-react's `getParentStatelessComponent` fallback: when no enclosing ES6 class / ES5 createReactClass component is found, the nearest FunctionLike ancestor that looks like a functional component (capital-cased name + returns JSX/null) is returned.

Priority matches upstream's `getParentComponent`:

getParentES6Component || getParentES5Component || getParentStatelessComponent

so when a mutation node is inside an inner function nested within an outer class component, the OUTER class component is returned (preventing the inner stateless candidate from masking the class boundary).

Only a restricted subset of upstream's heuristics is implemented — the patterns covering production React code: named FunctionDeclaration, FunctionExpression / ArrowFunction assigned to a capital-cased VariableDeclarator, PropertyAssignment, or ExportAssignment (default export), plus function expression in a CallExpression (e.g. React.memo wrapper — approximate match). This is intentionally conservative: missed detection causes a rule miss, over-detection would cause false-positive reports in non-component functions.

func GetJsxElementAttributes added in v0.15.1

func GetJsxElementAttributes(element *ast.Node) []*ast.Node

GetJsxElementAttributes returns the attribute nodes of a JsxOpeningElement or JsxSelfClosingElement, or nil for other kinds or when the element has no attributes. Each returned node is either a JsxAttribute or a JsxSpreadAttribute.

func GetJsxElementTypeString added in v0.15.1

func GetJsxElementTypeString(node *ast.Node) string

GetJsxElementTypeString returns the jsx-ast-utils `elementType(node)` equivalent — the dotted / namespaced display string of a JSX tag name as an ESTree-compatible source caller would see it. `node` may be either a JsxOpeningElement / JsxSelfClosingElement, or a raw tag-name node. Returns "" for shapes that don't correspond to a legal React/JSX element type (e.g. a computed member access), so callers can treat "" as "not a user component".

Supported tag shapes:

  • `<Foo>` / `<foo>` → "Foo" / "foo"
  • `<Foo.Bar.Baz>` → "Foo.Bar.Baz" (PropertyAccessExpression chain)
  • `<this.Foo>` → "this.Foo" (ThisKeyword base)
  • `<ns:Name>` → "ns:Name" (JsxNamespacedName)

This is AST-driven — interior whitespace or comments in unusual forms (e.g. `<Foo . Bar />`) are normalized away, matching jsx-ast-utils.

func GetJsxParentElement added in v0.15.1

func GetJsxParentElement(attr *ast.Node) *ast.Node

GetJsxParentElement returns the JsxOpeningElement or JsxSelfClosingElement that owns the given JsxAttribute (or JsxSpreadAttribute), or nil if not applicable.

func GetJsxPropName

func GetJsxPropName(node *ast.Node) string

GetJsxPropName returns the display name of a JSX node. For JsxAttribute: returns the attribute name (including namespaced names like "foo:bar"). For JsxSpreadAttribute: returns "spread". For Identifier nodes (e.g. tag names): returns the identifier text. For unknown nodes: returns "".

func GetJsxTagBaseIdentifier added in v0.15.1

func GetJsxTagBaseIdentifier(tagName *ast.Node) *ast.Node

GetJsxTagBaseIdentifier returns the leftmost Identifier of a JSX tag-name node — i.e. the symbol a rule must resolve to classify the tag. Pass the tag-name node obtained from `GetJsxTagName` (or directly from `JsxOpeningElement.TagName` / `JsxSelfClosingElement.TagName`). Returns nil when the tag does not terminate in an Identifier (ThisKeyword base, JsxNamespacedName, unknown shape).

Shapes handled:

  • `<Foo />` → Identifier("Foo")
  • `<Foo.Bar />` → Identifier("Foo")
  • `<Foo.Bar.Baz />` → Identifier("Foo")
  • `<this />` / `<this.X />` → nil (ThisKeyword base)
  • `<a:b />` → nil (JsxNamespacedName — not an identifier reference in any scope)
  • `<foo-bar />` → Identifier("foo-bar") (tsgo preserves the hyphenated text verbatim; callers decide whether that's DOM).

func GetJsxTagName added in v0.15.1

func GetJsxTagName(element *ast.Node) *ast.Node

GetJsxTagName returns the tag-name node of a JsxOpeningElement or JsxSelfClosingElement, or nil for other kinds.

func GetReactCreateClass added in v0.15.1

func GetReactCreateClass(settings map[string]interface{}) string

GetReactCreateClass reads `settings.react.createClass` from the config settings map. Returns DefaultReactCreateClass when the setting is absent, not a string, or empty.

func GetReactFragmentPragma added in v0.15.1

func GetReactFragmentPragma(settings map[string]interface{}) string

GetReactFragmentPragma reads `settings.react.fragment` from the config settings map. Returns DefaultReactFragment when the setting is absent, not a string, or empty.

func GetReactPragma added in v0.15.1

func GetReactPragma(settings map[string]interface{}) string

GetReactPragma reads `settings.react.pragma` from the config settings map. Returns DefaultReactPragma when the setting is absent, not a string, or empty.

func IsCreateClassCall added in v0.15.1

func IsCreateClassCall(call *ast.CallExpression, pragma, createClass string) bool

IsCreateClassCall reports whether the given CallExpression's callee is `<createClass>(...)` or `<pragma>.<createClass>(...)`. Parentheses are skipped on both the callee and the pragma identifier. Pass the empty string for pragma/createClass to fall back to `DefaultReactPragma` / `DefaultReactCreateClass`.

func IsCreateElementCall

func IsCreateElementCall(callee *ast.Node, pragma string) bool

IsCreateElementCall reports whether the callee is `<pragma>.createElement`. Pass an empty pragma to default to "React"; pass GetReactPragma(ctx.Settings) to honor the user's `settings.react.pragma` configuration.

Parentheses are transparently skipped on both the callee itself and the pragma identifier (e.g. `(React).createElement` / `(React.createElement)()`), matching ESTree's flattened shape.

func IsCreateReactClassObjectArg added in v0.15.1

func IsCreateReactClassObjectArg(obj *ast.Node, pragma, createClass string) bool

IsCreateReactClassObjectArg reports whether `obj` (an ObjectLiteralExpression) is the FIRST argument of a `<createClass>(...)` / `<pragma>.<createClass>(...)` call. Parens wrapping `obj` before it reaches the call argument position are transparent — tsgo preserves them while ESTree flattens — so `createReactClass(({...}))` still matches.

Pass the empty string for pragma / createClass to fall back to `DefaultReactPragma` / `DefaultReactCreateClass`. Returns false for any non-ObjectLiteralExpression input, for objects in non-argument positions, and for calls whose callee is not the configured createClass name.

func IsDOMComponent added in v0.15.1

func IsDOMComponent(element *ast.Node) bool

IsDOMComponent reports whether a JSX opening/self-closing element refers to an intrinsic (DOM) element like <div> or <svg:path>, rather than a user component like <Foo> or <Foo.Bar>.

Mirrors ESLint-plugin-react's `jsxUtil.isDOMComponent`: a tag name is intrinsic iff `elementType(node)` starts with a lowercase letter (regex `/^[a-z]/`). For member-expression tags (`<foo.bar>`, `<this.Foo>`) this means the classification is decided by the leftmost base identifier's first character — so `<foo.bar>` is DOM (matches ESLint, even though the runtime React behavior is "always user component"), while `<Foo.Bar>` is a user component.

func IsInsideReactComponent added in v0.15.1

func IsInsideReactComponent(node *ast.Node, pragma, createClass string) bool

IsInsideReactComponent reports whether `node` is lexically contained within a React component — either an ES5 component (object literal passed as an argument to `<createClass>(...)` / `<pragma>.<createClass>(...)`) or an ES6 component (ClassDeclaration / ClassExpression extending Component or PureComponent, optionally qualified by pragma).

Pass the empty string for pragma/createClass to fall back to `DefaultReactPragma` / `DefaultReactCreateClass`.

Mirrors eslint-plugin-react's componentUtil:

  • ES6 path: only the nearest enclosing class decides component status (matching `getParentES6Component`'s `while scope.type !== 'class'`). A non-component class does not "pass through" to let an outer component class match — this prevents false positives like a non-React inner class nested inside a React class.

  • ES5 path: `this` / `this.refs` must occur inside some function, whose ObjectExpression parent is the argument to `<createClass>(...)`. We approximate this by requiring that an enclosing function has been passed on the walk up before an ObjectExpression is accepted — which rules out pathological cases like `createReactClass({ x: this.refs.y })` where `this` is not inside any function (ESLint's scope walk returns null for that too).

func IsStatelessReactComponent added in v0.15.1

func IsStatelessReactComponent(fn *ast.Node, pragma string) bool

IsStatelessReactComponent reports whether `fn` (a FunctionLike) looks like a React functional component. Mirrors eslint-plugin-react's `getStatelessComponent` decision tree:

  • FunctionDeclaration — component iff returns JSX/null AND either: (a) its own Identifier is capitalized, OR (b) it is anonymous AND carries the `export default` modifier (ESLint's `!node.id || capitalized(node.id.name)` condition).

  • FunctionExpression / ArrowFunction — component iff returns JSX/null AND either wrapped in a pragma component call OR in an "allowed position" AND the position-specific capitalization check passes:

  • Wrapped in `<pragma>.memo(...)` / `<pragma>.forwardRef(...)` / bare `memo(...)` / bare `forwardRef(...)` — always a component.

  • Allowed positions (VariableDeclarator, AssignmentExpression, PropertyAssignment, ReturnStatement, ExportAssignment, outer ArrowFunction body) gate everything else. A bare IIFE or any other CallExpression argument position is NOT allowed, matching upstream's `isInAllowedPositionForComponent` default-false branch.

  • Within an allowed position, specific capitalization rules apply per upstream: VariableDeclarator/PropertyAssignment use the binding name; `Id = fn` assignments use the LHS Identifier; MemberExpression LHS uses the rightmost property name (with `module.exports = ...` as a special blanket-true case); a named FunctionExpression defers to its own Identifier.

Pass the empty string for `pragma` to default to `DefaultReactPragma`.

func ParseReactVersion added in v0.15.1

func ParseReactVersion(settings map[string]interface{}) (int, int, int)

ParseReactVersion returns the (major, minor, patch) triple of `settings.react.version`. When the setting is missing, not a string, empty, or not recognizable as a version, it defaults to (999, 999, 999) — matching eslint-plugin-react's `getReactVersionFromContext`, which treats an absent version as "latest".

func ReactVersionLessThan added in v0.15.1

func ReactVersionLessThan(settings map[string]interface{}, major, minor, patch int) bool

ReactVersionLessThan reports whether `settings.react.version` is strictly less than the given major.minor.patch. See ParseReactVersion for the default when the setting is missing.

Types

This section is empty.

Jump to

Keyboard shortcuts

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