jscodeshift cheatsheetEdit

Making an object literal

I had a use case where I wanted to replace the entire file with an object literal, based on a nested object props. The easy way:

const output = j(file.source)
  .replaceWith(expression(['(' + JSON.stringify(props) + ')']))

The hard way:

const output = j(file.source)
    function makeObject(fromJS) {
      if (Object.keys(fromJS).length) {
        return j.objectExpression(Object.keys(fromJS).map(key => (
          j.property("init", j.identifier(key), makeObject(fromJS[key]))
      } else {
        return j.identifier("4")