Building Nodes" class="reference-link">Building Nodes
The method name for a builder is simply the name of the node type you want to
build except with the first letter lowercased. For example if you wanted to
build a MemberExpression
you would use t.memberExpression(...)
.
The arguments of these builders are decided by the node definition. There’s some work that’s being done to generate easy-to-read documentation on the definitions, but for now they can all be found .
A node definition looks like the following:
Here you can see all the information about this particular node type, including how to build it, traverse it, and validate it.
builder: ["object", "property", "computed"],
// Example
// because the builder doesn't contain `async` as a property
var node = t.classMethod(
"constructor",
t.identifier("constructor"),
params,
)
// set it manually after creation
You can see the validation for the builder arguments with the fields
object.
You can see that object
needs to be an Expression
, property
either needs
to be an Expression
or an Identifier
depending on if the member expression
is computed
or not and computed
is simply a boolean that defaults to
false
.
So we can construct a MemberExpression
by doing the following:
t.memberExpression(
t.identifier('object'),
t.identifier('property')
// `computed` is optional
object.property
However, we said that needed to be an Expression
so why is
Identifier
valid?
Well if we look at the definition of Identifier
we can see that it has an
aliases
property which states that it is also an expression.
So since MemberExpression
is a type of Expression
, we could set it as the
object
of another MemberExpression
:
t.memberExpression(
t.memberExpression(
t.identifier('member'),
t.identifier('expression')
),
t.identifier('property')
Which will result in:
member.expression.property
You can find all of the actual definitions here and you can see them