facebook / jscodeshift

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

extensions cli option not respected when passing individual file paths #561

Closed robcmills closed 1 year ago

robcmills commented 1 year ago

Description

If you run jscodeshift on the cli, with --extensions=ts provided, and you pass individual file paths (not a directory) like this:

jscodeshift -t transform.ts --extensions=ts readme.md

Then the extensions are not respected and files that do not match those extensions are sent to the workers and processed through your transform file. Clearly, if the file is not the expected extension, then when your transform function tries to parse it, you will get an error:

Screenshot 2023-06-28 at 10 45 40 AM

I think this is because of this code right here in jscodeshift/src/Runner.js:

Screenshot 2023-06-28 at 10 50 02 AM

In this function, filter is passed in from this methods only usage:

Screenshot 2023-06-28 at 10 55 43 AM

and you can see that it takes into account the extensions, filtering out any files that do not match. And in getAllFiles, you can see that if the file is a directory, then it filters the files, but the else if condition does not. The else if clause only accounts for ignore patterns.

To Reproduce

  1. Run jscodeshift from the cli with --extensions=ts and pass it a filepath with a different extension

Note that it will not ignore that file and try to process it

Processing 1 files...

Expected Behavior

--extensions option should filter which files are processed, ignoring non-matching files

System Information:

Additional Context

My use case, which I believe is quite common, is running jscodeshift as a part of a Github workflow automation, where we run a git command to get a list of files that are changed in a PR as compared to a development (or main) branch, and then pass those files to the codemod. This results in many individual filepaths (instead of a directory) that can include non-typescript files.

Our current workaround is to just filter that list ourselves before passing it to jscodeshift, but it would be nice to not have that restriction and for jscodeshift to work as expected, so we can be confident in the stability of our generic codemod script.

Also, I opened a PR to fix: https://github.com/facebook/jscodeshift/pull/562

Error Logs

npx jscodeshift -t transform.ts --extensions=ts readme.md
Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
 ERR readme.md Transformation error (Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option. (1:0))
SyntaxError: Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option. (1:0)
    at instantiate (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parse-error/credentials.ts:62:21)
    at toParseError (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parse-error.ts:60:12)
    at TypeScriptParserMixin.raise (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/tokenizer/index.ts:1487:19)
    at TypeScriptParserMixin.testTopicReferenceConfiguration (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parser/expression.ts:1505:20)
    at TypeScriptParserMixin.finishTopicReference (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parser/expression.ts:1437:12)
    at TypeScriptParserMixin.parseTopicReference (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parser/expression.ts:1414:17)
    at TypeScriptParserMixin.parseExprAtom (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parser/expression.ts:1275:23)
    at TypeScriptParserMixin.parseExprAtom (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/plugins/jsx/index.ts:574:22)
    at TypeScriptParserMixin.parseExprSubscripts (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parser/expression.ts:718:23)
    at TypeScriptParserMixin.parseUpdate (/Users/robcmills/src/jscodeshift-fix-extensions/node_modules/@babel/parser/src/parser/expression.ts:695:21)
All done.
Results:
1 errors
0 unmodified
0 skipped
0 ok
Time elapsed: 0.369seconds