-
-
Notifications
You must be signed in to change notification settings - Fork 25
Open
Description
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!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels