Code Monkey home page Code Monkey logo

to-ast's Introduction

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

to-ast's People

Contributors

devongovett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

to-ast's Issues

Doesn't work with bare objects...

If you attempt to use this with a bare object created via Object.create(null) it's unable to comprehend that. It's trick as well as there's no literal for that. Probably has to be Object.assign(Object.create(null), {stuff});

obj.hasOwnProperty is not a function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.