ExAST.Selector (ExAST v0.7.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(_)"))

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.

Selects semantic descendants matching pattern.

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.

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.

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.

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.

find(selector, pattern)

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

SQL-like alias for descendant/2.

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.

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.

selector(pattern)

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

Alias for pattern/1.

where(selector, expr)

(macro)

Adds a predicate filter without changing the selected node.