Skip to content

Instantly share code, notes, and snippets.

@jonathanperret
Last active May 22, 2024 12:55
Show Gist options
  • Save jonathanperret/88bd8d16b9bbd0ecad6d13365523440c to your computer and use it in GitHub Desktop.
Save jonathanperret/88bd8d16b9bbd0ecad6d13365523440c to your computer and use it in GitHub Desktop.
export default function transformer(file, { jscodeshift }) {
const j = jscodeshift;
return j(file.source)
// search for "path" properties with matching value
.find(j.Property, prop => {
return prop.key.name === "path" && /division/.test(prop.value.value)
})
// go to enclosing object literal
.closest(j.ObjectExpression)
.forEach((objPath) => {
// find a "tags" property
const tagsProp = j(objPath).find(j.Property, prop => prop.key.name === "tags");
// append element to its value
tagsProp.get("value", "elements").value.push(j.literal("new-tag"));
})
.toSource();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment