Skip to content

Expose valueFactory and teach it to create properties #28

@Madd0g

Description

@Madd0g

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions