diff --git a/sample/__tests__/reverse-identifiers-test.js b/sample/__tests__/reverse-identifiers-test.js index 9d43da29..3a85168b 100644 --- a/sample/__tests__/reverse-identifiers-test.js +++ b/sample/__tests__/reverse-identifiers-test.js @@ -41,5 +41,4 @@ var egassem = droWtsrif + droWdnoces; ); }); -// the snapshot output of this file should be the same as reverse-identifiers.output.js -defineSnapshotTestFromFixture(__dirname, transform, {}, 'reverse-identifiers'); +// inline tests for reference purposes diff --git a/sample/reverse-identifiers.js b/sample/reverse-identifiers.js index e7e4be36..e91a5435 100644 --- a/sample/reverse-identifiers.js +++ b/sample/reverse-identifiers.js @@ -5,19 +5,32 @@ * LICENSE file in the root directory of this source tree. */ +import type { + ASTPath, + Collection, + JSCodeshift, + Transform, +} from 'jscodeshift'; + /** * Example jscodeshift transformer. Simply reverses the names of all * identifiers. */ -function transformer(file, api) { +const transform: Transform = (file, api: JSCodeshift) => { const j = api.jscodeshift; - return j(file.source) - .find(j.Identifier) - .replaceWith( - p => j.identifier(p.node.name.split('').reverse().join('')) - ) - .toSource(); -} + const buildTransformer = () => { + return (path: ASTPath) => { + const reversed = path.node.name.split('').reverse().join(''); + path.node.name = reversed; + }; + }; + + const root: Collection = j(file.source); + + root.find(j.Identifier).forEach(buildTransformer()); + + return root.toSource(); +}; -module.exports = transformer; +export default transform; diff --git a/sample/transform-object-property.js b/sample/transform-object-property.js new file mode 100644 index 00000000..c7e1c241 --- /dev/null +++ b/sample/transform-object-property.js @@ -0,0 +1,55 @@ +/** + * This is an example transform demonstrating how to modify object properties. + * It shows how to: + * - Change the value of an existing property + * - Add a new property to an object + * + * To run this transform: + * jscodeshift -t transform-object-property.js + * jscodeshift -t transform-object-property.js --dry -p + */ + +import { parse } from 'recast'; +import { objectExpression, property, identifier, numericLiteral, stringLiteral } from 'babel-types'; + +// The main transform function receives the source code as a string +// and returns the transformed source code +function transformObjectProperty(fileInfo, api) { + const j = api.jscodeshift; + const source = fileInfo.source; + + // Parse the source into an AST + const ast = j.withParser('babylon')(source); + + // Find all object expressions (object literals like { foo: 3 }) + ast.find(j.ObjectExpression) + .forEach(path => { + // Check if the object has a 'foo' property + const fooProperty = path.node.properties.find( + prop => prop.key && prop.key.name === 'foo' + ); + + // If 'foo' property exists, change its value from 3 to 4 + if (fooProperty) { + fooProperty.value = numericLiteral(4); + } + + // Add a new 'bar' property with value '5' + const newProperty = j.property( + 'init', + identifier('bar'), + stringLiteral('5') + ); + path.node.properties.push(newProperty); + }); + + // Return the transformed source code + return ast.toSource(); +} + +export default transformObjectProperty; + +// Also support CommonJS for older Node versions +if (typeof module !== 'undefined' && module.exports) { + module.exports = transformObjectProperty; +}