skovhus / jest-codemods

Codemods for migrating to Jest https://github.com/facebook/jest 👾
MIT License
877 stars 81 forks source link

fix(`jest-globals`): set array accessor type for parser compatibility #586

Closed puglyfe closed 1 month ago

puglyfe commented 1 month ago

Fixes https://github.com/skovhus/jest-codemods/issues/585

Given an expression statement like someSpy.calls.argsFor(0)[1], different parsers assign different type values to the argsFor arg node.

babel and flow parse it as type: "Literal", where as babylon, ts and tsx parse it as type: "NumericLiteral". When parsed as NumericLiteral, it results in invalid syntax when used as an array accessor.

This change updates the transform to explicitly set it as type: "Literal" regardless of the parser used. I also updated the test for this scenario to use multiple parsers. It's maybe a pattern that would be useful elsewhere, but I'll leave that as a separate effort 😅

I also added support for argsFor args that are type: "Identifier" (e.g. argsFor(someVariable)).

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 92.64%. Comparing base (a94e2b5) to head (87677dc). Report is 56 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #586 +/- ## ========================================== + Coverage 92.38% 92.64% +0.25% ========================================== Files 26 27 +1 Lines 1944 2012 +68 Branches 405 417 +12 ========================================== + Hits 1796 1864 +68 Misses 102 102 Partials 46 46 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

skovhus commented 1 month ago

Thanks!