astflat

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package astflat provides flat-AST navigation primitives that are general across rule and analyzer packages: call/navigation/argument decomposition, scope-walk helpers, condition-operand iteration. It depends only on the scanner package.

This is the foundation tier between scanner (raw flat AST) and the higher-level analyzers (nullflow, future control/dataflow packages, etc.). Helpers that operate purely on AST shape — without semantic interpretation — belong here.

Helpers that need rule-package context (config, metadata, registries) do NOT belong here. Helpers that interpret AST shape semantically (e.g. "is this a logging call?", "is this a Compose modifier?") belong in their own analyzer packages, not in astflat.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllConditionOperands

func AllConditionOperands(file *scanner.File, idx uint32, predicate func(uint32) bool) bool

AllConditionOperands returns true if at least one named child exists and every named child satisfies the predicate.

func AnyConditionOperand

func AnyConditionOperand(file *scanner.File, idx uint32, predicate func(uint32) bool) bool

AnyConditionOperand returns true if any named child of idx satisfies the predicate.

func CallExpressionName

func CallExpressionName(file *scanner.File, idx uint32) string

CallExpressionName returns the rightmost identifier name of a call_expression's callee — either the simple_identifier or, for navigation callees, the last identifier in the chain.

func CallExpressionParts

func CallExpressionParts(file *scanner.File, idx uint32) (navExpr uint32, args uint32)

CallExpressionParts splits a call_expression into its navigation_expression (the callee) and value_arguments (the args). Either return value may be 0 when absent or when the call uses a trailing-lambda only.

func CallNameAny

func CallNameAny(file *scanner.File, idx uint32) string

CallNameAny is like CallExpressionName but also handles the trailing-lambda idiom where tree-sitter wraps `name(args) { body }` in an outer call_expression whose first child is the inner `name(args)` call.

func CallSuffixValueArgs

func CallSuffixValueArgs(file *scanner.File, suffix uint32) uint32

CallSuffixValueArgs returns the value_arguments child of a call_suffix node, or 0.

func EnclosingAncestor

func EnclosingAncestor(file *scanner.File, idx uint32, types ...string) (uint32, bool)

EnclosingAncestor returns the closest ancestor of idx whose type matches any of the given types, or (0, false) if none exists.

func ExtractIdentifier

func ExtractIdentifier(file *scanner.File, idx uint32) string

ExtractIdentifier returns the identifier of a declaration node — function, class, parameter, property, or variable_declaration — by finding its first named simple_identifier / type_identifier child. Returns "" when no such child exists.

func FinalSimpleIdentifier

func FinalSimpleIdentifier(file *scanner.File, idx uint32) string

FinalSimpleIdentifier returns the identifier text at the end of a navigation chain or directly_assignable_expression — the rightmost simple_identifier reachable by walking named children. For `w.settings.javaScriptEnabled` this returns `javaScriptEnabled`.

func HasValueArgumentLabel

func HasValueArgumentLabel(file *scanner.File, arg uint32) bool

HasValueArgumentLabel reports whether a value_argument has a named label (either via value_argument_label child or `name = ...` shape).

func IfConditionThenElseBodies

func IfConditionThenElseBodies(file *scanner.File, node uint32) (condition, thenBody, elseBody uint32)

IfConditionThenElseBodies decomposes an if_expression into its condition, then-body, and (optional) else-body nodes. Tolerates the various brace / keyword shapes the tree-sitter Kotlin grammar emits.

func IsNullLiteral

func IsNullLiteral(file *scanner.File, idx uint32) bool

IsNullLiteral reports whether the (parenthesis-unwrapped) idx is the `null` literal.

func LastNamedChild

func LastNamedChild(file *scanner.File, idx uint32) uint32

LastNamedChild returns the last named child of idx, or 0.

func NavigationExpressionLastIdentifier(file *scanner.File, idx uint32) string

NavigationExpressionLastIdentifier returns the rightmost simple_identifier reachable inside a navigation_expression. For `a.b.c` this returns "c". Skips over value-arguments / lambda-literal / string-literal subtrees so argument identifiers don't shadow the actual callee name.

func NavigationExpressionReceiver(file *scanner.File, nav uint32) uint32

NavigationExpressionReceiver returns the receiver (first named child) of a navigation_expression, or 0 if the node is malformed.

func NavigationLastSuffixHasSafeAccess(file *scanner.File, nav uint32) bool

NavigationLastSuffixHasSafeAccess reports whether the trailing navigation_suffix uses safe-call (`?.`) rather than `.`.

func NodeWithin

func NodeWithin(file *scanner.File, container, node uint32) bool

NodeWithin reports whether `node` is a descendant of `container` (or the same node).

func PositionalValueArgument

func PositionalValueArgument(file *scanner.File, args uint32, index int) uint32

PositionalValueArgument returns the index-th positional (unlabeled) argument from a value_arguments list, or 0 if absent.

func PrefixExpressionIsNot

func PrefixExpressionIsNot(file *scanner.File, idx uint32) bool

PrefixExpressionIsNot reports whether a prefix_expression has the `!` operator (logical not).

func SingleValueArgumentExpression

func SingleValueArgumentExpression(file *scanner.File, args uint32) (uint32, bool)

SingleValueArgumentExpression returns the lone value_argument expression in args, or (0, false) if the args list contains zero or more than one argument.

func UnwrapParenExpr

func UnwrapParenExpr(file *scanner.File, idx uint32) uint32

UnwrapParenExpr peels parenthesized_expression layers off idx, returning the innermost wrapped expression (or idx itself when not parenthesized).

func ValueArgumentExpression

func ValueArgumentExpression(file *scanner.File, arg uint32) uint32

ValueArgumentExpression returns the expression node carried by a value_argument, skipping any leading label.

Types

This section is empty.

Jump to

Keyboard shortcuts

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