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(_)")))not/1 has the same name as Kernel.not/1, so import with
import Kernel, except: [not: 1] when you want bare not(...) calls.
Summary
Functions
Builds or applies a semantic ancestor predicate.
Selects direct semantic children matching pattern.
Selects semantic descendants matching pattern.
Alias for has_descendant/1 and has_descendant/2.
Builds or applies a direct semantic child predicate.
Builds or applies a semantic descendant predicate.
Negates a predicate for use with where/2.
Builds or applies a direct semantic parent predicate.
Starts a selector at pattern.
Alias for pattern/1.
Adds a predicate filter without changing the selected node.
Types
@type relation() :: :self | :child | :descendant
@type step() :: {relation(), ExAST.Pattern.pattern()}
@type t() :: %ExAST.Selector{filters: [ExAST.Selector.Predicate.t()], steps: [step()]}
Functions
@spec ancestor(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()
Builds or applies a semantic ancestor predicate.
@spec ancestor(t(), ExAST.Pattern.pattern()) :: t()
@spec child(t(), ExAST.Pattern.pattern()) :: t()
Selects direct semantic children matching pattern.
@spec descendant(t(), ExAST.Pattern.pattern()) :: t()
Selects semantic descendants matching pattern.
@spec has(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()
Alias for has_descendant/1 and has_descendant/2.
@spec has(t(), ExAST.Pattern.pattern()) :: t()
@spec has_child(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()
Builds or applies a direct semantic child predicate.
@spec has_child(t(), ExAST.Pattern.pattern()) :: t()
@spec has_descendant(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()
Builds or applies a semantic descendant predicate.
@spec has_descendant(t(), ExAST.Pattern.pattern()) :: t()
@spec not ExAST.Selector.Predicate.t() :: ExAST.Selector.Predicate.t()
Negates a predicate for use with where/2.
@spec parent(ExAST.Pattern.pattern()) :: ExAST.Selector.Predicate.t()
Builds or applies a direct semantic parent predicate.
@spec parent(t(), ExAST.Pattern.pattern()) :: t()
@spec pattern(ExAST.Pattern.pattern()) :: t()
Starts a selector at pattern.
@spec selector(ExAST.Pattern.pattern()) :: t()
Alias for pattern/1.
@spec where(t(), ExAST.Selector.Predicate.t()) :: t()
Adds a predicate filter without changing the selected node.