Skip to content

feat: add cookbook section with practical recipes for common jscodeshift tasks#716

Open
dashitongzhi wants to merge 3 commits into
facebook:mainfrom
dashitongzhi:feat/docs-cookbook-20260524090259
Open

feat: add cookbook section with practical recipes for common jscodeshift tasks#716
dashitongzhi wants to merge 3 commits into
facebook:mainfrom
dashitongzhi:feat/docs-cookbook-20260524090259

Conversation

@dashitongzhi

Copy link
Copy Markdown

This addresses issue #193 by adding a comprehensive cookbook section with practical, copy-pasteable examples for new users.

Changes

Added a new 'Cookbook: Practical Recipes for Common Tasks' section to README.md containing 7 complete recipes:

  1. Changing literal values in object properties - The exact use case from issue Missing simple examples for new users #193 (changing foo: 3 to foo: 4)
  2. Adding new properties to objects - Using j.property to create new object properties
  3. Renaming identifiers throughout a file - Using j.find() and j.replaceWith() for simple replacements
  4. Converting CommonJS to ES modules - Replacing require() and module.exports
  5. Wrapping function calls with higher-order functions - Adding logging/error handling patterns
  6. Converting Array.indexOf() !== -1 to Array.includes() - Modern JS pattern upgrade
  7. Transforming for loops to forEach - Converting classic for loop patterns

Each recipe includes before/after code examples and complete transform code that can be copied directly into a transform.js file.

Motivation

Issue #193 described that new users find the documentation "very thin on examples" and lack simple examples for tasks like "changing the literal value of something." This cookbook directly addresses that gap by providing working, minimal examples for common codemod patterns.

- src/utils/intersection.js: Fix bug where result.length (undefined for Set)
  was used instead of result.size, causing resultSize to be NaN. Also add
  validation for empty/null arrays input.

- src/ignoreFiles.js: Add try/catch around fs.statSync to gracefully handle
  missing or inaccessible ignore config files, with a warning message.

- bin/jscodeshift.js: Add try/catch around JSON.parse for parser-config
  option to provide a helpful error message instead of crashing on
  invalid JSON files.

- src/Runner.js: Replace 'throw err' in async fs.readdir callback with
  proper error handling that calls done() and logs a warning, preventing
  uncaught errors in async callbacks.

- src/template.js: Add early return for empty nodes array in statements().
  Add clarifying comments explaining the reduce() index calculation.

- src/Worker.js: Add getDefaultExport() helper to safely access default
  exports from Babel presets/plugins that may use either
  module.exports.default or exports.default pattern.
…ift tasks

This addresses issue facebook#193 by adding a comprehensive cookbook section with
simple, copy-pasteable examples for new users. The cookbook includes:

- Changing literal values in object properties (exact issue example)
- Adding new properties to objects
- Renaming identifiers throughout a file
- Converting CommonJS to ES modules
- Wrapping function calls with higher-order functions
- Converting Array.indexOf() !== -1 to Array.includes()
- Transforming for loops to forEach

Each recipe includes before/after code examples and complete transform code.
Copilot AI review requested due to automatic review settings May 24, 2026 01:04
@changeset-bot

changeset-bot Bot commented May 24, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 7c4b90d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel

vercel Bot commented May 24, 2026

Copy link
Copy Markdown

@dashitongzhi is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed label May 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants