ExAST.Selector (ExAST v0.11.0)

Copy Markdown View Source

CSS-like AST selector builder.

Selectors are built from a starting pattern and relationship steps:

import ExAST.Selector

pattern("defmodule _ do ... end")
|> descendant("def _ do ... end")
|> child("IO.inspect(_)")

The final step is the selected node. Use where/2 with predicates such as has_child/1, has_descendant/1, parent/1, and ancestor/1 to filter the selected node without changing it.

pattern("def _ do ... end")
|> where(has_descendant("Repo.transaction(_)"))
|> where(not(has_descendant("IO.inspect(_)")))

where/2 also accepts quoted boolean predicate expressions, so you can use Kernel.not/1 without excluding it from imports:

pattern("def _ do ... end")
|> where(not has_descendant("IO.inspect(_)"))

where/2 supports capture guards using ^ to pin captured values, allowing runtime checks on matched nodes:

pattern("Enum.take(_, count)")
|> where(match?({:-, _, [_]}, ^count))

Summary

Functions

Matches when all nested predicates match.

Builds or applies a semantic ancestor predicate.

Matches when any nested predicate matches.

Selects direct semantic children matching pattern.

Matches comments associated with the selected node.

Matches comments immediately after the selected node.

Matches comments immediately before the selected node.

Matches inline comments on the selected node start line.

Matches comments inside the selected node range.

Selects semantic descendants matching pattern.

Builds an exact comment matcher.

Finds selector matches in source text, AST, or a Sourceror zipper.

Matches the first semantic child in its parent.

Matches when a previous sibling matches pattern.

SQL-like alias for pattern/1.

Builds or applies a direct semantic child predicate.

Builds or applies a semantic descendant predicate.

Matches when the immediately previous sibling matches pattern.

Matches when the immediately following sibling matches pattern.

SQL-like alias for ancestor/1 and ancestor/2.

Matches the last semantic child in its parent.

Returns true when the selector matches at least once.

Negates a predicate for use with where/2.

Matches the nth semantic child in its parent, using 1-based indexing.

Builds or applies a direct semantic parent predicate.

Starts a selector at pattern.

Matches when a following sibling matches pattern.

Builds a comment prefix matcher.

Returns true when matching this selector depends on comments.

Returns true when matching this selector requires source text.

Builds a comment suffix matcher.

Builds a substring comment matcher.

Adds a predicate filter without changing the selected node.

Types

relation()

@type relation() :: :self | :child | :descendant

step()

t()

@type t() :: %ExAST.Selector{filters: [ExAST.Selector.Predicate.t()], steps: [step()]}

Functions

all(predicates)

Matches when all nested predicates match.

ancestor(pattern)

Builds or applies a semantic ancestor predicate.

ancestor(selector, pattern)

@spec ancestor(t(), ExAST.Pattern.pattern()) :: t()

any(predicates)

Matches when any nested predicate matches.

child(selector, pattern)

@spec child(t(), ExAST.Pattern.pattern() | [ExAST.Pattern.pattern()]) :: t()

Selects direct semantic children matching pattern.

comment(matcher)

Matches comments associated with the selected node.

comment_after(matcher)

Matches comments immediately after the selected node.

comment_before(matcher)

Matches comments immediately before the selected node.

comment_inline(matcher)

Matches inline comments on the selected node start line.

comment_inside(matcher)

Matches comments inside the selected node range.

contains(pattern)

SQL-like alias for has_descendant/1 and has_descendant/2.

contains(selector, pattern)

@spec contains(t(), ExAST.Pattern.pattern()) :: t()

descendant(selector, pattern)

@spec descendant(t(), ExAST.Pattern.pattern() | [ExAST.Pattern.pattern()]) :: t()

Selects semantic descendants matching pattern.

exact(value, opts \\ [])

Builds an exact comment matcher.

find(selector, pattern)

@spec find(t(), ExAST.Pattern.pattern() | [ExAST.Pattern.pattern()]) :: t()

SQL-like alias for descendant/2.

find_all(input, selector, opts \\ [])

@spec find_all(String.t() | Macro.t() | Sourceror.Zipper.t(), t(), keyword()) :: [
  map()
]

Finds selector matches in source text, AST, or a Sourceror zipper.

find_child(selector, pattern)

@spec find_child(t(), ExAST.Pattern.pattern() | [ExAST.Pattern.pattern()]) :: t()

SQL-like alias for child/2.

first()

@spec first() :: ExAST.Selector.Predicate.t()

Matches the first semantic child in its parent.

follows(pattern)

Matches when a previous sibling matches pattern.

from(pattern)

@spec from(ExAST.Pattern.pattern() | [ExAST.Pattern.pattern()]) :: t()

SQL-like alias for pattern/1.

has(pattern)

Alias for has_descendant/1 and has_descendant/2.

has(selector, pattern)

@spec has(t(), ExAST.Pattern.pattern()) :: t()

has_child(pattern)

Builds or applies a direct semantic child predicate.

has_child(selector, pattern)

@spec has_child(t(), ExAST.Pattern.pattern()) :: t()

has_descendant(pattern)

@spec has_descendant(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()

Builds or applies a semantic descendant predicate.

has_descendant(selector, pattern)

@spec has_descendant(t(), ExAST.Pattern.pattern()) :: t()

immediately_follows(pattern)

@spec immediately_follows(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()

Matches when the immediately previous sibling matches pattern.

immediately_precedes(pattern)

@spec immediately_precedes(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()

Matches when the immediately following sibling matches pattern.

inside(pattern)

SQL-like alias for ancestor/1 and ancestor/2.

inside(selector, pattern)

@spec inside(t(), ExAST.Pattern.pattern()) :: t()

last()

@spec last() :: ExAST.Selector.Predicate.t()

Matches the last semantic child in its parent.

match?(input, selector, opts \\ [])

@spec match?(String.t() | Macro.t() | Sourceror.Zipper.t(), t(), keyword()) ::
  boolean()

Returns true when the selector matches at least once.

not predicate

Negates a predicate for use with where/2.

nth(index)

Matches the nth semantic child in its parent, using 1-based indexing.

parent(pattern)

Builds or applies a direct semantic parent predicate.

parent(selector, pattern)

@spec parent(t(), ExAST.Pattern.pattern()) :: t()

pattern(pattern)

@spec pattern(ExAST.Pattern.pattern() | [ExAST.Pattern.pattern()]) :: t()

Starts a selector at pattern.

precedes(pattern)

Matches when a following sibling matches pattern.

prefix(value, opts \\ [])

Builds a comment prefix matcher.

requires_comments?(selector)

@spec requires_comments?(t()) :: boolean()

Returns true when matching this selector depends on comments.

requires_source?(selector)

@spec requires_source?(t()) :: boolean()

Returns true when matching this selector requires source text.

selector(pattern)

@spec selector(ExAST.Pattern.pattern() | [ExAST.Pattern.pattern()]) :: t()

Alias for pattern/1.

suffix(value, opts \\ [])

Builds a comment suffix matcher.

text(value, opts \\ [])

Builds a substring comment matcher.

where(selector, expr)

(macro)

Adds a predicate filter without changing the selected node.