ast-to-literal

0.0.5 • Public • Published

ast-to-literal

Transform a Babel Expression into a JS object.


code example:

const t = require('@babel/types')
const equal = require('deep-equal')

const toJs = require('ast-to-literal')

const obj = { key: 'value' }
const astObject = t.objectExpression([
  t.objectProperty(t.identifier('key'), t.stringLiteral('value'))
])

console.log(equal(obj, toJs(astObject))) // true

Supported value types:

  • BooleanLiteral
  • StringLiteral
  • NumericLiteral
  • ObjectExpression
  • ArrayExpression
  • null (stripped from object)
  • undefined (stripped from object too)

It could support other value types as long as they don't need any sort of computation.

Feel free to file an issue if you use this package and need more ou of it ✌️

Readme

Keywords

none

Package Sidebar

Install

npm i ast-to-literal

Weekly Downloads

1,709

Version

0.0.5

License

MIT

Unpacked Size

3.48 kB

Total Files

6

Last publish

Collaborators

  • hypervillain