facebook / jscodeshift

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

[Bug] jscodeshift@0.15.1 ignores the files not starting with `.js` extension by default #582

Closed trivikr closed 4 months ago

trivikr commented 5 months ago

Describe the bug

The jscodeshift ignores the file if it's extension does not match the default one

Steps to reproduce

$ echo 'export default function transformer(file, api) {
  return api.jscodeshift(file.source).toSource();
}' > transform.js

$ echo '' > example.ts

$ npx jscodeshift@0.15.1 example.ts

Observed behavior

The file example.ts is not processed, even though CLI explicitly asked for it.

$ npx jscodeshift@0.15.1 example.ts

No files selected, nothing to do. 
All done. 
Results: 
0 errors
0 unmodified
0 skipped
0 ok
Time elapsed: 0.001seconds

This happens as extensions is set to js by default, and the fix from https://github.com/facebook/jscodeshift/pull/562 caused regression.

Expected behavior

The file example.ts is processed, like it happens in 0.15.0

$ npx jscodeshift@0.15.0 example.ts

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.251seconds

If extensions is not explicitly specified by user, it should default to all extensions.

Additional context

This issue was noticed in downstream utility https://github.com/aws/aws-sdk-js-codemod/issues/766#issuecomment-1954975237

trivikr commented 5 months ago

cc participants from previous discussion for awareness @robcmills @Daniel15 @lg-kialo

trivikr commented 5 months ago

There are two solutions I can think of for updated default value of extensions:

trivikr commented 5 months ago

Example fix which allows all extensions by default https://github.com/facebook/jscodeshift/pull/583

Although this fixes the regression, it will send all files for processing by default and will have a performance impact if the folder has lot of files.

danieldelcore commented 5 months ago

I find folks trip over this quite often. My thinking with @hypermod/cli was to turn on all extensions by default and parse them with the TSX parser since it's a large superset of js/jsx/ts (there are some minor caveats here in the form of generic syntax between ts and TSX, perhaps TS would have been a better way to go 😬).

https://github.com/hypermod-io/hypermod-community/blob/3b4722329bed3f02ac56c23c89925e6b906b9fa7/packages/cli/src/index.ts#L37-L49

trivikr commented 5 months ago

I posted a safer fix at https://github.com/facebook/jscodeshift/pull/584, which explicitly passes supported JavaScript and TypeScript files.