to-ast

WebJar for to-ast

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

to-ast
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

to-ast
WebJar for to-ast
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/devongovett/to-ast

Download to-ast

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/to-ast/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>to-ast</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/to-ast/
implementation 'org.webjars.npm:to-ast:1.0.0'
// https://jarcasting.com/artifacts/org.webjars.npm/to-ast/
implementation ("org.webjars.npm:to-ast:1.0.0")
'org.webjars.npm:to-ast:jar:1.0.0'
<dependency org="org.webjars.npm" name="to-ast" rev="1.0.0">
  <artifact name="to-ast" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='to-ast', version='1.0.0')
)
libraryDependencies += "org.webjars.npm" % "to-ast" % "1.0.0"
[org.webjars.npm/to-ast "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.webjars.npm : ast-types jar [0.7.2,0.8)
org.webjars.npm : esprima jar [2.1.0,3)

Project Modules

There are no modules declared in this project.

to-ast

This module converts JavaScript objects to an equivalent abstract syntax tree representation, compatible with the Mozilla Parser API. You can use it to generate JavaScript source code from objects, using escodegen.

Usage

Install with npm:

npm install to-ast

Here's a simple example:

var toAST = require('to-ast');
var escodegen = require('escodegen');

// get an AST from a number...
toAST(2) //=> { type: 'Literal', value: 2 }

// or if you want a source string...
escodegen.generate(toAST(2)) //=> '2'

Supported types

  • undefined
  • null
  • number, string, and boolean literals
  • functions
  • Node buffers
  • arrays
  • String, Number, and Boolean object wrappers
  • typed arrays and array buffers
  • dates
  • errors
  • regular expressions
  • object literals

Custom types

Most built-in JavaScript types as supported out of the box, but if you want to override the behavior for your particular object, you can provide a toAST method on your object:

var toAST = require('to-ast');
var escodegen = require('escodegen');

function Person(name) {
  this.name = name;
}

Person.prototype.toAST = function() {
  return {
    type: 'NewExpression',
    callee: { type: 'Identifier', name: 'Person' },
    arguments: [{ type: 'Literal', value: this.name }]
  };
};

escodegen.generate(toAST(new Person('Devon'))) //=> "new Person('Devon')"

License

MIT

Versions

Version
1.0.0