0.57.0 • Published 6 years ago

an-expression v0.57.0

Weekly downloads
176
License
-
Repository
github
Last release
6 years ago

anExpression lets you programmatically build JavaScript expressions, modify them, and then turn them into source code.

var anExpression = require("an-expression")
var javascriptToEzjs = require("javascript-to-ezjs")

var tree = anExpression.tree()

javascriptToEzjs("blah({\n  a: 1,})", tree)

Once we have built a tree, we can query the data. Here we'll iterate through the key value pairs on that object expression, and expect to see the number 1:

var objectExpressionId = tree.getListItem("arguments", tree.rootId(), 0)

tree.eachListItem(
  "pairIds",
  objectExpressionId,
  function(pairId) {
    var key = tree.getAttribute(tree, "key", pairId)
    var parentId = tree.getAttribute("key", pairId)
    var valueId = tree.getAttribute("valueId", pairId)
    var number = tree.getAttribute("number", valueId)

    if (number != 1) {
      throw new Error("Bad number") }

    if (parentId != tree.root().id) {
      throw new Error("Bad root")}})

We can also modify the tree:

tree.addKeyValuePair(
  objectExpressionId,
  "b",
  tree.numberLiteral(2))

And then dump the modified code:

// tree.toJavaScript() returns:
blah({
  a: 1,
  b: 2})

Methods

// Adds an empty function literal at the next available position
tree.addExpressionAt(
  tree.reservePosition(),
  anExpression.functionLiteral())

// Add a string literal inside that function literal
var functionId = tree.rootId()
var stringLiteral = anExpression.stringLiteral("alert")
tree.addToParent(functionId, stringLiteral)

Expression generators

var hello = anExpression.stringLiteral("hello, world")

// "hello, world"

anExpression.functionLiteral({
  functionName: "add",
  argumentNames: ["a", "b"],
  body: hello})

// function add(a, b) {
//   "hello, world"
// }

var a = anExpression.numberLiteral(42)

// 42

var b = anExpression.numberLiteral(1)

// 1

anExpression.functionCall({
  functionName: "add",
  arguments: [a, b]})

// add(42, 1)

anExpression.variableReference("a")

// a

anExpression.variableAssignment({
  variableName: "a",
  expression: "b"})

// a = b

anExpression.variableAssignment({
  variableName: "c",
  expression: "b",
  isDeclaration: true})

// var c = b

anExpression.objectLiteral({
  name: a.id
})

// {name: a}

anExpression.arrayLiteral([a.id, b.id])

// [a, b]

anExpression.boolean(true)
anExpression.true()

// true

anExpression.false()

// false

anExpression.returnStatement({
  expression: a})

// return a
0.57.0

6 years ago

0.56.0

6 years ago

0.55.0

6 years ago

0.54.0

6 years ago

0.53.0

6 years ago

0.52.0

6 years ago

0.51.0

6 years ago

0.50.0

6 years ago

0.49.0

6 years ago

0.48.0

6 years ago

0.47.0

6 years ago

0.46.0

7 years ago

0.45.0

7 years ago

0.44.0

7 years ago

0.43.0

7 years ago

0.42.0

7 years ago

0.41.0

7 years ago

0.40.0

7 years ago

0.39.0

7 years ago

0.38.0

7 years ago

0.37.0

7 years ago

0.36.0

7 years ago

0.35.0

7 years ago

0.34.0

7 years ago

0.33.0

7 years ago

0.32.0

7 years ago

0.31.0

7 years ago

0.30.0

7 years ago

0.29.0

7 years ago

0.28.0

7 years ago

0.27.0

7 years ago

0.26.0

7 years ago

0.25.0

7 years ago

0.24.0

7 years ago

0.23.0

7 years ago

0.22.0

7 years ago

0.21.0

7 years ago

0.20.0

7 years ago

0.19.0

7 years ago

0.18.0

7 years ago

0.17.0

7 years ago

0.16.0

7 years ago

0.15.0

7 years ago

0.14.0

7 years ago

0.13.0

7 years ago

0.12.0

7 years ago

0.11.0

7 years ago

0.10.0

7 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago