Questions tagged [jscodeshift]

jscodeshift is a toolkit created by Facebook for running codemods over multiple JS files

jscodeshift is a toolkit for running codemods over multiple JS files. It provides:

  • A runner, which executes the provided transform for each file passed to it. It also outputs a summary of how many files have (not) been transformed.
  • A wrapper around recast, providing a different API. Recast is an AST-to-AST transform tool, and also tries to preserve the style of original code as much as possible.

GitHub repo

61 questions
15
votes
2 answers

how to use jscodeshift to insert a line in the beginning of the file

https://astexplorer.net/#/gist/ad90272020dd0bfb15619d93cca81b66/28d3cf7178271f4f99b10bc9352daa873c2f2b20 // file var a = "a" // what if this is import statement? // jscodeshift export default (file, api) => { const j = api.jscodeshift; const…
user2167582
  • 5,986
  • 13
  • 64
  • 121
9
votes
2 answers

How can I parse, modify, and regenerate the AST of a TypeScript file (like jscodeshift)?

My use case: I'm building a Yeoman generator, that modifies TypeScript files; in ways similar to: Add import statements Import components into an AngularJS module Yeoman recommends using an AST parser for this task: The most reliable way to do so…
matthewsteele
  • 1,797
  • 1
  • 15
  • 31
7
votes
2 answers

This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom'

This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom' I am getting the above error while trying to move the entire application from react v15.6to v16.2, by using the migration tool from facebook like jscodeshift.
Praveen Kumar
  • 1,966
  • 1
  • 9
  • 16
7
votes
2 answers

How to replace a path in AST with just parsed javascript(string)?

https://astexplorer.net/#/gist/70df1bc56b9ee73d19fc949d2ef829ed/7e14217fd8510f0bf83f3372bf08454b7617bce1 I've found now I'm trying to replace an expression and I don't care whats in it. in this example I've found the this.state.showMenu &&…
user2167582
  • 5,986
  • 13
  • 64
  • 121
6
votes
0 answers

how to run jscodeshift from node api?

I've seen some developers wondering how to run jscodeshift without CLI, there are some issues but they're very simple, so If someone wants to effectively run it with options, here's an example. const path = require('path'); const Runner =…
aneurysm
  • 271
  • 3
  • 11
6
votes
1 answer

Cannot find module '@material-ui/core/es' error when running material-ui codemod

I'm trying to run the top-level-imports codemod found here: https://github.com/mui-org/material-ui/blob/master/packages/material-ui-codemod/README.md#top-level-imports. So I ran npm install -D @material-ui/codemod and then I ran the script find src…
ha1ogen
  • 13,375
  • 3
  • 19
  • 19
6
votes
2 answers

How to run jscodeshift transform on all files?

I am creating a transform that will replace all instances of: templateUrl: 'some/url/to/some.html' with template: require('some/url/to/some.html') I am doing this because I am changing the way that our AngularJS code brings in their templates. I…
frosty
  • 21,036
  • 7
  • 52
  • 74
6
votes
1 answer

jscodeshift change object literal value

Using jscodeshift, how can I transform // Some code ... const someObj = { x: { foo: 3 } }; // Some more code ... to // Some code ... const someObj = { x: { foo: 4, bar: '5' } }; // Some more code ... ? I have…
Steve
  • 8,066
  • 11
  • 70
  • 112
5
votes
1 answer

Transformation error ... parser plugin(s): 'decorators-legacy, decorators'

I get the following error when I run jscodeshift -t ./react-codemod/transforms/React-PropTypes-to-prop-types.js ./src Transformation error (This experimental syntax requires enabling one of the following parser plugin(s): 'decorators-legacy,…
The Third
  • 785
  • 2
  • 10
  • 30
4
votes
1 answer

How to wrap all toplevel statements and declarations except imports in jscodeshift?

I have this AST explorer snippet which almost lets me do what I want, which is to turn this: function bar() {return 42;} var baz = {} import 'get-outta-here'; into import 'get-outta-here'; function wrap() { function bar() {return 42;} var baz…
joao
  • 3,517
  • 1
  • 31
  • 43
4
votes
1 answer

Create a codemod to add a new element to array

I have been struggling to adding a new object to array of objects with jscodeshift. My problem is I can't figure out how I have to query an array after I got VariableDeclarator. I need to get a last element in the array after that I can insert a new…
rob111
  • 1,249
  • 1
  • 13
  • 18
4
votes
1 answer

Material-UI Migration Helper: codemod-script?

I'm updating from Material-UI 0.x to 1.0. The docs for migration-helper say to run: jscodeshift -t . I've never used jscodeshift before, and I've never seen the notation before, so I would like to get some advice on how…
VikR
  • 4,818
  • 8
  • 51
  • 96
3
votes
2 answers

jscodeshift - get count of all imports from a module in project

I'm playing around with jscodeshift. My goal is to get count of all imports from a particular module in a project import { Circle, Triangle, Rectangle } from 'geometry'; results in { Circle: 1, Triangle: 1, Rectangle: 1 } I have kind of achieved…
3
votes
3 answers

How to start jscodeshift in inspect mode?

This used to be runnable but recently I'm encounter the following issue. By running the following command: node --inspect-brk ./node_modules/.bin/jscodeshift mod.js file. I am encountering this problem Debugger listening on…
user2167582
  • 5,986
  • 13
  • 64
  • 121
3
votes
1 answer

JSCodeshift declare new variable

I want to write a template function to create new Variables in JsCodeShift. Anyone has an Idea how? Or some better documentation? I tried the code below down, according to this. const j = api.jscodeshift; let test = j.variableDeclaration('let', …
1
2 3 4 5