facebook / jscodeshift

A JavaScript codemod toolkit.
https://jscodeshift.com
MIT License
9.32k stars 481 forks source link

Unexpected token export #344

Open kopax opened 5 years ago

kopax commented 5 years ago

Hello, I am back after a 3-month retreat and I am now trying to upgrade my react version within my libraries as suggested by react 16.9.0 using react-codemod that use under the hood jscodeshift.

This is the project I am currently trying to upgrade: https://github.com/bootstrap-styled/v4

I was using react 16.8.4 and now I'd like to upgrade to react 16.9.0.

I am running the code-mod with

npx react-codemod rename-unsafe-lifecycles src/**/*.js

Instead of adding UNSAFE_ before each method, it fails with:

dka@dev-11:[~/workspace/github.com/bootstrap-styled/v4 (react-16.9.0)]: npx react-codemod rename-unsafe-lifecycles src/**/*.js
? Which dialect of JavaScript do you use? JavaScript
Executing command: jscodeshift --verbose=2 --ignore-pattern=**/node_modules/** --parser babel --extensions=jsx,js --transform /home/dka/workspace/github.com/bootstrap-styled/v4/node_modules/react-codemod/transforms/rename-unsafe-lifecycles.js src/Abbr/index.js
Processing 1 files... 
Spawning 1 workers...
Sending 1 files to free worker...
/home/dka/workspace/github.com/bootstrap-styled/v4/node_modules/react-codemod/transforms/rename-unsafe-lifecycles.js:18
export default (file, api, options) => {
^^^^^^

SyntaxError: Unexpected token export
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:694:28)
    at Module._compile (/home/dka/workspace/github.com/bootstrap-styled/v4/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:745:10)
    at Object.newLoader [as .js] (/home/dka/workspace/github.com/bootstrap-styled/v4/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:626:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
    at Function.Module._load (internal/modules/cjs/loader.js:558:3)
All done. 
Results: 
0 errors
0 unmodified
0 skipped
0 ok
Time elapsed: 0.316seconds 

It seems that the ES6 files are not readen correctly, am I missing something ?

jedwards1211 commented 4 years ago

Since react-codemod is providing its own CLI to call jscodeshift, you could file an issue in react-codemod asking them to document how to pass a --parser option to jscodeshift, because the default parser is obviously not correct for your project.

The default parser selection isn't very intelligent, and to make matters worse, even if you tell it to use the babel parser, it doesn't use your local babel config when parsing.

ElonVolo commented 2 years ago

@gaearon, I know it's been two years, but does anyone still have this problem with react-codemod? (my apologies if you aren't one of the maintainers of the project, you were just one of the most visible names associated with it)