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.

    1. builder: ["object", "property", "computed"],
    1. // Example
    2. // because the builder doesn't contain `async` as a property
    3. var node = t.classMethod(
    4. "constructor",
    5. t.identifier("constructor"),
    6. params,
    7. )
    8. // 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:

    1. t.memberExpression(
    2. t.identifier('object'),
    3. t.identifier('property')
    4. // `computed` is optional
    1. 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:

    1. t.memberExpression(
    2. t.memberExpression(
    3. t.identifier('member'),
    4. t.identifier('expression')
    5. ),
    6. t.identifier('property')

    Which will result in:

    1. member.expression.property

    You can find all of the actual definitions here and you can see them