typescriptutil

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT, MIT Imports: 4 Imported by: 0

Documentation

Overview

Package typescriptutil holds helpers shared across typescript-eslint rules.

This file mirrors upstream's util/explicitReturnTypeUtils.ts. It is consumed by both `explicit-function-return-type` and `explicit-module-boundary-types`, which share semantic predicates for deciding whether a function expression is in a typed context (parent variable annotation, type assertion, JSX container, ...) and whether an ancestor already supplies the return type.

Package typescriptutil collects helpers shared across rules in the @typescript-eslint plugin. Anything specific to a single rule stays with the rule; whatever a second rule needs (or would naturally re-implement with the same semantics) lives here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AncestorHasReturnType

func AncestorHasReturnType(node *ast.Node) bool

AncestorHasReturnType reports whether any ancestor of `node` already supplies a return type, making `node`'s own return type unnecessary. Mirrors upstream's `ancestorHasReturnType`.

func DoesImmediatelyReturnFunctionExpression

func DoesImmediatelyReturnFunctionExpression(node *ast.Node, returns []*ast.Node) bool

DoesImmediatelyReturnFunctionExpression reports whether `node` is a function whose body (or every `return` statement) yields another function expression. Mirrors upstream's `doesImmediatelyReturnFunctionExpression`.

func FindTruthinessAssertedArgument

func FindTruthinessAssertedArgument(tc *checker.Checker, callExpr *ast.CallExpression) *ast.Node

FindTruthinessAssertedArgument returns the argument node corresponding to a truthiness-assertion call (`function f(x): asserts x`), or nil when the call is not such an assertion. Mirrors typescript-eslint's `findTruthinessAssertedArgument`:

  • the resolved signature must carry an `asserts` type predicate with no `Type()` (a truthiness assertion, not a type guard);
  • the asserted parameter index must map to an argument before any spread.

Extracted from no_unnecessary_condition to satisfy the plugin's duplicate-across-rules rule (strict_boolean_expressions consumes the same helper for its CallExpression listener).

func FirstSpreadIndex

func FirstSpreadIndex(callExpr *ast.CallExpression) int

FirstSpreadIndex returns the index of the first SpreadElement argument in a call expression, or -1 when there is none. Arguments before the first spread can still be mapped to parameters by index; arguments at or past it cannot.

Extracted from no_unnecessary_condition because strict_boolean_expressions needs the same predicate-parameter mapping; mirrors typescript-eslint's `firstSpreadIndex` helper.

func GetEffectiveParent

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

GetEffectiveParent returns the first meaningful parent, skipping ParenthesizedExpressions. tsgo preserves parens that ESLint strips, so this bridges the gap when walking from a function to its containing context.

func IsClassImplementsOrInterfaceExtends

func IsClassImplementsOrInterfaceExtends(node *ast.Node) bool

IsClassImplementsOrInterfaceExtends reports whether the given `ExpressionWithTypeArguments` sits in a heritage position the typescript-eslint rules listen to as `TSClassImplements` or `TSInterfaceHeritage`:

  • `class X implements ...`
  • `interface X extends ...`

`class X extends ...` is intentionally excluded: upstream does not register a listener for it. Non-heritage uses of `ExpressionWithTypeArguments` (e.g. JSDoc-style type contexts) are also rejected because upstream's listener set never matches them.

func IsConstructorArgument

func IsConstructorArgument(node *ast.Node) bool

IsConstructorArgument reports whether a node is a NewExpression. Used to detect functions passed directly as constructor arguments, e.g. `new Foo(() => {})`.

func IsDefaultFunctionParameterWithTypeAnnotation

func IsDefaultFunctionParameterWithTypeAnnotation(node *ast.Node) bool

IsDefaultFunctionParameterWithTypeAnnotation reports whether a node is a Parameter with both a type annotation and an initializer (default value), e.g. `(param: Type = () => {})`.

func IsFunction

func IsFunction(node *ast.Node) bool

IsFunction reports whether a node is FunctionDeclaration, FunctionExpression or ArrowFunction. Matches ESLint's ASTUtils.isFunction — excludes methods, getters, setters, and constructors.

func IsFunctionArgument

func IsFunctionArgument(parent *ast.Node, funcNode *ast.Node) bool

IsFunctionArgument reports whether `parent` is a CallExpression and `funcNode` is one of its arguments (not its callee — that would be an IIFE).

func IsPropertyDefinitionWithTypeAnnotation

func IsPropertyDefinitionWithTypeAnnotation(node *ast.Node) bool

IsPropertyDefinitionWithTypeAnnotation reports whether a node is a PropertyDeclaration with a type annotation, e.g. `public x: Foo = ...`.

func IsPropertyOfObjectWithType

func IsPropertyOfObjectWithType(property *ast.Node, funcNode *ast.Node) bool

IsPropertyOfObjectWithType reports whether a node is a property (or nested property) of a typed object expression. Mirrors upstream's `isPropertyOfObjectWithType` walk.

func IsReferenceToGlobalIdentifier

func IsReferenceToGlobalIdentifier(ctx rule.RuleContext, ident *ast.Node) bool

IsReferenceToGlobalIdentifier mirrors typescript-eslint's `isReferenceToGlobalFunction` helper: an identifier counts as referring to a lib.d.ts-provided global only when no user-source declaration in the *current file* provides a binding for it.

The check is layered to match the upstream ESLint scope-manager behavior across the cases tsgo's TypeChecker handles inconsistently:

  1. The TypeChecker side — if `GetSymbolAtLocation` resolves the identifier to a symbol whose declarations include any from the current source file, treat the reference as locally bound. This covers declaration merging (e.g. a same-file `interface Function {...}` that augments the lib type) and value-side bindings (`class`, `let`, `function`).

  2. The lexical scope side — if any enclosing block, function body, module declaration, or SourceFile statement list directly declares a `type` alias, `interface`, `class`, `enum`, `namespace`, `var`, `let`, `const`, `function`, or `import` with the same identifier name, treat the reference as locally bound. This catches the script-scope cases where a `type Number = 0 | 1` collides with the lib `interface Number` and tsgo's checker resolves to the lib symbol anyway — upstream's ESLint scope manager sees the local def and stays silent.

Returns true only when neither path finds a local binding (or the TypeChecker is unavailable AND the lexical walk also fails), matching upstream's "unresolved means global" fallback.

func IsTypeAssertion

func IsTypeAssertion(node *ast.Node) bool

IsTypeAssertion reports whether a node is `x as T` or `<T>x`.

func IsTypedFunctionExpression

func IsTypedFunctionExpression(node *ast.Node, allowTypedFunctionExpressions bool) bool

IsTypedFunctionExpression reports whether a function expression is in a typed context, gated by allowTypedFunctionExpressions.

func IsTypedJSX

func IsTypedJSX(node *ast.Node) bool

IsTypedJSX reports whether a node is JsxExpression or JsxSpreadAttribute — the two JSX containers ESTree models as `JSXExpressionContainer` / `JSXSpreadAttribute`.

func IsTypedParent

func IsTypedParent(parent *ast.Node, funcNode *ast.Node) bool

IsTypedParent reports whether the parent of a function expression provides type context: type assertion, typed variable declarator, default parameter with type, typed property declaration, function-call argument, or JSX container.

func IsValidFunctionExpressionReturnType

func IsValidFunctionExpressionReturnType(
	node *ast.Node,
	allowTypedFunctionExpressions bool,
	allowExpressions bool,
	allowDirectConstAssertionInArrowFunctions bool,
) bool

IsValidFunctionExpressionReturnType reports whether a function expression's return type is typed (per IsTypedFunctionExpression), is wrapped in a permitted expression context (allowExpressions), or is an arrow function returning `as const` (allowDirectConstAssertionInArrowFunctions).

func IsVariableDeclaratorWithTypeAnnotation

func IsVariableDeclaratorWithTypeAnnotation(node *ast.Node) bool

IsVariableDeclaratorWithTypeAnnotation reports whether a node is a VariableDeclaration (tsgo's equivalent of ESTree's VariableDeclarator) with an explicit type annotation, e.g. `const x: Foo = ...`.

func IsWeakPrecedenceParent

func IsWeakPrecedenceParent(node *ast.Node) bool

IsWeakPrecedenceParent mirrors typescript-eslint's `isWeakPrecedenceParent` inside `getWrappingFixer`: it asks whether `node`'s parent might silently rebind operator precedence after `node` is rewrapped, so the caller can decide whether to wrap the replacement in parens. The list is kept in sync with upstream's set:

  • any unary/binary/conditional/await/typeof/void/delete parent
  • a property access whose object IS `node`
  • an element access whose expression IS `node`
  • a call/new whose callee IS `node`
  • a tagged template whose tag IS `node`

Pairs with utils.IsStrongPrecedenceNode (which decides the inverse: whether the INNER expression already binds tight enough to skip its inner paren).

Types

This section is empty.

Jump to

Keyboard shortcuts

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