gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

question: how to remove property from object #136

Open bhainesva opened 3 years ago

bhainesva commented 3 years ago

I'd like to remove a property with a specific name from an object. For example given:

const x = {
  numprop: 3, // important comment about the numprop
  filter: {
     test: "hi",
  },
  objprop: {
     a: "a"
  },
}

I'd like to remove the filter property and end up with:

const x = {
  numprop: 3, // important comment about the numprop
  objprop: {
     a: "a"
  },
}

My first attempt was grasp 'obj.props[key=#filter]' -R '' but this leaves an extra comma where the property was removed.

I also tried grasp 'obj! > prop[key=#filter]' -R '{\n{{.props[key!=#filter] | join ",\n" }}\n}' which does work, but since it rewrites the whole object it also removes any comments on the parts of the object I want to keep. Is there an alternative to avoid this?