facebook / jscodeshift

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

jscodeshift throws `SyntaxError: Unexpected token, expected ","` for typescript file with just types #488

Open trivikr opened 2 years ago

trivikr commented 2 years ago

Describe the bug

jscodeshift throws SyntaxError: Unexpected token, expected "," for typescript file with just types

jscodeshift: 0.13.1
 - babel: 7.17.5
 - babylon: 7.17.3
 - flow: 0.173.0
 - recast: 0.20.5

Steps to reproduce

Stackblitz: https://stackblitz.com/edit/node-u3ttzg Run npx jscodeshift example.ts in console.

To reproduce in your workspace, add the following files:

transform.js ```js // transform.js module.exports = function (fileInfo, api, options) { const j = api.jscodeshift; const source = j(fileInfo.source); return source.toSource(); }; ```
example.ts ```ts // example.ts import AWS from 'aws-sdk'; export const listTables = (client: AWS.DynamoDB) => client.listTables().promise(); ```

Observed behavior

Throws error:

Processing 1 files... 
Spawning 1 workers...
Sending 1 files to free worker...
 ERR example.ts Transformation error (Unexpected token, expected "," (3:33))
SyntaxError: Unexpected token, expected "," (3:33)
    at Parser._raise (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/error.js:150:45)
    at Parser.raiseWithData (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/error.js:145:17)
    at Parser.raise (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/error.js:89:17)
    at Parser.unexpected (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/util.js:174:16)
    at Parser.expect (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/util.js:150:28)
    at Parser.parseParenAndDistinguishExpression (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/expression.js:1702:14)
    at Parser.parseExprAtom (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/expression.js:1122:21)
    at Parser.parseExprAtom (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/plugins/jsx/index.js:571:22)
    at Parser.parseExprSubscripts (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/expression.js:682:23)
    at Parser.parseUpdate (/home/.turbo/npx/sd7KuR/node_modules/@babel/parser/src/parser/expression.js:662:21)
All done. 
Results: 
1 errors
0 unmodified
0 skipped
0 ok
Time elapsed: 1.119seconds

Expected behavior

The jscodeshift run is complete with no transformation

Processing 1 files... 
Spawning 1 workers...
Sending 1 files to free worker...
All done. 
Results: 
0 errors
1 unmodified
0 skipped
0 ok
Time elapsed: 0.587seconds

Screenshots

Screenshot ![unexpected-token-error](https://user-images.githubusercontent.com/16024985/157371026-f9e47b67-814c-4fb4-8d76-a0e8f365b986.png)

Additional context

We noticed this issue in aws-sdk-js-codemod and we tried to fix it by passing parser=ts in the tests in https://github.com/trivikr/aws-sdk-js-codemod/pull/83

But explicitly setting the parser breaks other tests. We can pass parser for the specific test, but it will affect our consumers which may run transformation on or multiple files.

trivikr commented 2 years ago

The @babel/parser-7.16.12 on https://astexplorer.net/ does not throw any error.

Screenshot ![babel-parser-astexplorer](https://user-images.githubusercontent.com/16024985/157478673-a9d8f0c4-3e4a-4108-b486-5ace49de5788.png)
ElonVolo commented 2 years ago

If you don't supply jscodeshift a parser parameter the default that jscodeshift uses something called babel5compat, which is has a significantly reduced feature set from ts, babylon, etc. So you could think of it as behaving like a much older version of babel.

Babylon should probably be made the default parser at some point, but at the moment having babylon become the default parser breaks a ton of unit tests.