Questions tagged [codemod]

codemod is a tool/library to assist you with large-scale codebase refactors that can be partially automated but still require human oversight and occasional intervention.

20 questions
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
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
2 answers

Codemod vs. eslint --fix

I want to write a couple of scripts to automatically detect missing imports and import them based upon a root directory. Is it better to write this script as a codemod script or as an eslint rule with the fix option?
Josh Unger
  • 6,717
  • 6
  • 33
  • 55
2
votes
1 answer

Replace all instances of a specific import via jscodeshift

okay so I have code that looks like this: import { wait } from "@testing-library/react"; describe("MyTest", () => { it("should wait", async () => { await wait(() => { console.log("Done"); }); }); }); I want to change that import…
corvid
  • 10,733
  • 11
  • 61
  • 130
2
votes
1 answer

How to change require statements inline using codemod (jscodeshift)?

Hi I'm trying to write a codemod which moves my require statement from top of the file to inside class constructor function. const moduleA = require('moduleA'); const moduleB = require('../moduleB'); class Example { constructor(context) { …
Sathish
  • 2,056
  • 3
  • 26
  • 40
2
votes
1 answer

jscodeshift throwing error: does not match type string

I am trying to transform this: function twist() { this.settings = null; delete this.settings; this.whatever = null; this.something['hello'] = null; this.hello = "test"; } into this: function twist() { delete this.settings; delete…
Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59
1
vote
0 answers

jscodeshift to convert all named imports to default import MUI V5

Can somebody help with a jscodeshift to convert all the named imports to default import for MUI V5 (React + Typescript) Current code import { Button, TextField } from '@mui/material'; Expected output import Button from…
1
vote
1 answer

Babel plugin addComment doesn't work when ran with codemod

I've created a babel plugin: module.exports = function (babel) { const { types: t } = babel; return { name: 'addComment', visitor: { Program(path, state) { …
konclave
  • 648
  • 1
  • 7
  • 19
1
vote
1 answer

jscodeshift TypeScript codemod - Remove Generic type but keep wrapped type

I'm currently trying to write a codemod that will remove all the $ReadOnly generic from a TypeScript codebase, keeping only T (T being an object/union) So far, this is what I came up with module.exports = (fileInfo, api) => { const j =…
gomes
  • 681
  • 1
  • 7
  • 20
1
vote
0 answers

Codemod - Reading a file in node that uses export syntax

Working on an npm package that runs a codemod on any given source repository. As part of my codemod I am looking to replace certain values in the source repository. Before I replace those values I need to check for values in an object being exported…
Ali Bhagat
  • 475
  • 1
  • 6
  • 15
1
vote
1 answer

Error running react-codemod for javascript with flow project

Error encountered running in javascript project using flow. npx react-codemod rename-unsafe-lifecycles How do I get this codemod to run properly. This is required to upgrade the project's version of React to 16.9+ ERR src\datatypes\Mammografie.js…
guus775
  • 11
  • 1
1
vote
2 answers

Codemod with jscodeshift - remove comma from import

I'm trying to write a small codemod to refactor some of the code. Consider I've somethihng like this: import { mod1, mod2, mod3 } from 'package1' import localMod from 'package2' and I wanted to change this to: import { mod1, mod3 } from…
Vimalraj Selvam
  • 2,155
  • 3
  • 23
  • 52
0
votes
0 answers

MUI TypeError: Cannot read properties of undefined (reading 'name')

I'm trying to migrate from jss to emotion and while running codemod on this code, I got an error message like this. Transformation error (Cannot read properties of undefined (TypeError: Cannot read properties of undefined (reading 'name') This code…
Smart
  • 1
0
votes
0 answers

Upgrading fluentui from version 7 to version 8 using codemods (npx @fluentui/codemod)No runnable mods were found in the config file?

I thought Codemods should just run without needing any configuration to take the heavy lifting away? Link to documentation: https://github.com/microsoft/fluentui/wiki/Version-8-release-notes Multiple Errors created of the following types: File …
Benjamin Roberts
  • 476
  • 4
  • 12
0
votes
1 answer

JSCodeShift: How to wrap object properties inside another property

I am new to jscodeshift and I'm using it to transform existing code, I have achieved almost everything but I am stuck at a part where I need to transform existing objects (there could be multiple objects in a file). I couldn't find a good example…
1
2