Support different parsers in the test utils #332
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
jscodeshift
now supports TypeScript (and other parsers) via the CLI. Thereact-codemod
package can now support different React codebases.Changes
In order to use the
defineTest
helper intestUtils.js
with different parsers thedefineTest
function needs to allow dynamic parsers and look at different extensions (for TypeScript:.ts
,.tsx
).The approach used in this PR was to add another parameter
testOptions
that allows passing in an object with aparser
key. The extension then can be inferred from the file being parsed.I didn't feel great about adding another parameter so I opted for an object so if there are additional config options they can be added to this object rather than adding more arguments and passing many
null
s to get to the proper position. I also considered foldingtestFilePrefix
into this object:However, this would be a breaking change for those using the
testUtils
. I'm happy to make that change but opted for avoiding the breaking change.I also originally experiment with reusing the second existing
options
parameter but that's what get's passed to the transformer and it felt like mixing two unrelated option configs. Open to feedback on this approach. See the proposed pull request inreact-codemod
for how this helper would be used and the overall motivation for this change: reactjs/react-codemod#228Testing
sample
directory)