const Builder = require('@helpdotcom/build-ast')
const ast = Builder.ast
Creates a new Builder
object. The Builder
object will be used
to generate AST nodes. All of the methods on Builder.prototype
can be
chained together:
Builder()
.use('strict')
.module('Test')
The static methods on Builder
will return a <Object>
.
Takes a variable amount of arguments that should all be an <Object>
.
This joins two expressions using the &&
operator.
Creates an array of items.
items
<Array>
An array of nodes
Builder().array([
Builder.string('a')
, Builder.string('b')
])
// will be the ast for
['a', 'b']
Assign the object-path <String>
left to the right value.
left
<String>
A dot notated string that can represent a nested objectright
Any The valueoperator
<String>
The operator to use (=
,*=
, etc.)
Builder().assign('Event.VERSION', Builder.string('1.0.0'))
// will build the ast for
Event.VERSION = '1.0.0'
Creates a new BlockStatement
node and pushes it onto the current Builder
's
body.
body
<Array>
Returns the builder's body.
Calls a function with name with the args
name
<String>
The function name, a dot notated string that can represent a nested objectargs
<Array>
Array of AST Nodes
Declare a variable.
Example:
Builder().declare('const', 'http', Builder.require('http'))
// will build the ast for
const http = require('http')
Creates a <Function>
name
<String>
The function nameargs
<Array>
An array of arguments. These should be strings.body
<Array>
The function body. This will be wrapped in aBlockStatement
Builder().function('test', ['t'], [])
// will build the ast for
function test(t) {
}
Creates an IfStatement
test
AST Nodeblock
The consequent block. Should be wrapped in aBlockStatement
alt
The alternate block. Should be wrapped in aBlockStatement
Builder().if(
Builder.not(Builder.instanceOf(
Builder.this(), Builder.id('Event')
))
, Builder.block(
Builder.returns(Builder.new('Event', ['buffer']))
)
)
// will build the ast for
if (!(this instanceof Event)) {
return new Event(buffer)
}
Shortcut for the following:
Builder().if(Builder.not(test), consequent, alternate)
Creates an instanceof
check.
left
AST Noderight
AST Node
Adds a module.exports statement.
name
<String>
The function name to export
Calls new
on the name as a constructor.
name
<String>
The constructor name to callargs
<Array>
The arguments to pass to the constructor. In the event an item of args is a string, it will be returned as anIdentifier
.
Negates the expr by wrapping it in a UnaryExpression
with !
as the
operator.
expr
<Object>
AST Node
Note: uses strict equality (!==
)
Creates a raw number.
n
<Number>
The number. Will be coerced to a number.
The following will both return a node for the same value:
Builder().number(3)
Builder().number('3')
// => 3
Creates an array of properties.
properties
<Object>
An object mapping properties to values
Builder().object({
foo: Builder.number('42')
, bar: Builder.string('baz')
})
// will be the ast for
{ foo: 42, bar: 'baz' }
Takes a variable amount of arguments that should all be an <Object>
.
This joins two expressions using the ||
operator.
Creates a regular expression.
Must start with a slash (/
).
Adds a require statement.
str
<String>
The module to requireprop
<String>
A dot notated string that can represent a nested object
// To get require('path').join
Builder().require('path', 'join')
// To get require('path').posix.join
Builder().require('path', 'posix.join')
Clears the Builder
's body and resets the state.
Creates a ReturnStatement
.
arg
AST Node
Builder().returns(ast.literal(true))
// will create the ast for
return true
Creates a raw string.
str
<String>
The string. If not a string, it will be coerced.
Builder().string('Hi!')
Creates a ConditionalStatement
test
AST Nodeblock
The consequent block.alt
The alternate block.
Builder().ternary(
Builder.objectPath('isActive')
, Builder.string('User is active')
, Builder.string('User is offline')
)
// will build the ast for
isActive ? 'User is active' : 'User is offline'
Creates a ThrowStatement
throwing the given arg.
arg
<Object>
AST Node.
Adds a use
directive ('use strict'
or 'use asm'
).
str
<String>
Builder().use('strict')
Marks the builder as a program.
Returns a member expression for the given dot notated object path.
str
<String>
Dot notated string (ex. 'admin.id')
Note: To explicitly mark a segment as a computed property, prefix the
segment with a ^
. Ex: admin.^i
would represent admin[i]
.
Returns the representation of an <Error>
object.
If msg
is a string
, it will be properly wrapped. Otherwise, it will be
unmodified.
Returns the representation of a <TypeError>
object.
If msg
is a string
, it will be properly wrapped. Otherwise, it will be
unmodified.
Returns the representation of a <RangeError>
object.
If msg
is a string
, it will be properly wrapped. Otherwise, it will be
unmodified.
Returns a prototype to create a new class.
Forces the class to be a ClassDeclaration.
Returns this
Forces the class to be a ClassExpression.
Returns this
Define the constructor of the class.
Returns this
Define a method of the class.
name
<String>
The method nameargs
<Array>
The function argumentsbody
<Array>
The function body block
Returns this
Builds the class into either a ClassExpression
or a ClassDeclaration
.
Returns an <Object>