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

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.

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.

Adds a predicate filter without changing the selected node.

Types

relation()

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

step()

@type step() :: {relation(), ExAST.Pattern.pattern()}

t()

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

Functions

ancestor(pattern)

Builds or applies a semantic ancestor predicate.

ancestor(selector, pattern)

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

child(selector, pattern)

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

Selects direct semantic children matching pattern.

descendant(selector, pattern)

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

Selects semantic descendants matching pattern.

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()

not predicate

Negates a predicate for use with where/2.

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()) :: t()

Starts a selector at pattern.

selector(pattern)

@spec selector(ExAST.Pattern.pattern()) :: t()

Alias for pattern/1.

where(selector, predicate)

@spec where(t(), ExAST.Selector.Predicate.t()) :: t()

Adds a predicate filter without changing the selected node.