1. ast-to-literal
Transform a Babel Expression into a JS value
ast-to-literal
Package: ast-to-literal
Created by: hypervillain
Last modified: Mon, 11 Apr 2022 15:27:08 GMT
Version: 0.0.5
License: MIT
Downloads: 6,905
Repository: https://github.com/hypervillain/ast-to-literal

Install

npm install ast-to-literal
yarn add ast-to-literal

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 ✌️

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin