jscodeshift icon indicating copy to clipboard operation
jscodeshift copied to clipboard

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

Open trivikr opened this issue 2 years ago • 2 comments

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
// transform.js
module.exports = function (fileInfo, api, options) {
  const j = api.jscodeshift;
  const source = j(fileInfo.source);
  return source.toSource();
};
example.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

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 avatar Mar 09 '22 04:03 trivikr