Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose valueFactory and teach it to create properties #28

Open
Madd0g opened this issue Jan 12, 2016 · 0 comments
Open

Expose valueFactory and teach it to create properties #28

Madd0g opened this issue Jan 12, 2016 · 0 comments

Comments

@Madd0g
Copy link

Madd0g commented Jan 12, 2016

I wanted to do some manipulation and I couldn't find a way to insert a property into an ObjectExpression. I dug around and found the value factory (/lib/factory/value.js) that allows to easily create AST elements.

However I couldn't find a way to create an object property, is that supported somehow?

Would be nice to have access to the value factory and be able to create object properties through it.

EDIT: this seems to work:

/**
 * Create a property node from a value string
 * @param  {String} valKey key string
 * @param  {String} valStr value string
 * @return {Object}        Property node
 */
exports.createProp = function (valKey, valStr) {
  var tree = esprima.parse('var astValFactory = { ' + valKey + ': ' + valStr + '};',esprimaOptions);
  tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
  return tree.body[0].declarations[0].init.properties[0];
};

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant