Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sample/__tests__/reverse-identifiers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

31 changes: 22 additions & 9 deletions sample/reverse-identifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
55 changes: 55 additions & 0 deletions sample/transform-object-property.js
Original file line number Diff line number Diff line change
@@ -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 <file>
* jscodeshift -t transform-object-property.js --dry -p <file>
*/

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;
}