To debug your codemod transforms using the VSCode IDE, follow these steps:
- Install jscodeshift locally: Ensure
jscodeshift is installed in your project via npm install --save jscodeshift. The debugger relies on the local node_modules binary. - Configure launch.json: Add the provided VSCode debugging configuration to your
.vscode/launch.json file. This configuration includes two modes: Debug Transform (for running a transform on a specific file) and Debug All JSCodeshift Jest Tests. - Select target file: In the VSCode file tree, click on the file you want to transform (e.g.,
foo.js). - Start Debugging:
- Open the Run and Debug menu and select
Debug Transform. - Click the Start Debugging button.
- When prompted, enter the path to your transform file (defaults to
transform.js). - Select the appropriate parser from the list (e.g.,
babel, ts, tsx).
- Review Results: The transform will run and stop at any set breakpoints. Because the configuration uses the
--dry flag, the original file will not be modified; instead, the transformed output will be printed to the VSCode Debug Console.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Debug Transform",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceRoot}/node_modules/.bin/jscodeshift",
"stopOnEntry": false,
"args": ["--dry", "--print", "-t", "${input:transformFile}", "--parser", "${input:parser}", "--run-in-band", "${file}"],
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"console": "internalConsole",
"sourceMaps": true,
"outFiles": []
},
{
"name": "Debug All JSCodeshift Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/jest/bin/jest.js",
"--runInBand",
"--testPathPattern=${fileBasenameNoExtension}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
],
"inputs": [
{
"type": "pickString",
"id": "parser",
"description": "jscodeshift parser",
"options": [
"babel",
"babylon",
"flow",
"ts",
"tsx",
],
"default": "babel"
},
{
"type": "promptString",
"id": "transformFile",
"description": "jscodeshift transform file",
"default": "transform.js"
}
]
}