Last active
May 22, 2024 12:55
-
-
Save jonathanperret/88bd8d16b9bbd0ecad6d13365523440c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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