Repository: skovhus/jest-codemods
Pull request #586 (2024-07-12 22:19:52)

fix(`jest-globals`): set array accessor type for parser compatibility
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]: ## [Codecov](https://app.codecov.io/gh/skovhus/jest-codemods/pull/586?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 92.63%. Comparing base [(`a94e2b5`)](https://app.codecov.io/gh/skovhus/jest-codemods/commit/a94e2b561fed619cbebf0b9c28e0a919c44aa4e3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) to head [(`965cf47`)](https://app.codecov.io/gh/skovhus/jest-codemods/commit/965cf4718bd82f3d44bf061caa5b73183c7b42e7?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).
> Report is 56 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #586      +/-   ##
==========================================
+ Coverage   92.38%   92.63%   +0.24%     
==========================================
  Files          26       27       +1     
  Lines        1944     2010      +66     
  Branches      405      416      +11     
==========================================
+ Hits         1796     1862      +66     
  Misses        102      102              
  Partials       46       46              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/skovhus/jest-codemods/pull/586?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).

skovhus: Thanks!

Patch:
diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts
index d3ec1af..528f29e 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -371,8 +371,16 @@ export default function jasmineGlobals(fileInfo, api, options) {
         j.identifier('calls')
       )
 
+      const expressionIndex =
+        path.node.arguments[0].type === 'Identifier'
+          ? j.memberExpression(expressionMockCalls, path.node.arguments[0], true)
+          : j.memberExpression(
+              expressionMockCalls,
+              j.literal(path.node.arguments[0].value)
+            )
+
       // make it `*.mock.calls[index]`
-      j(path).replaceWith(j.memberExpression(expressionMockCalls, path.node.arguments[0]))
+      j(path).replaceWith(expressionIndex)
     })
 
   root


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 03980f0..e757b7b 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -162,16 +162,21 @@ test('*.argsForCall', () => {
   )
 })
 
-test('*.calls.argsFor()', () => {
+test.each(['babel', 'flow', 'ts'])('*.calls.argsFor() with parser `%s`', (parser) => {
   expectTransformation(
     `
     oklahoma.calls.argsFor(0)
     idaho.calls.argsFor(0)[1]
+    mySpy.calls.argsFor(myCounter)[0]
     `,
     `
     oklahoma.mock.calls[0]
     idaho.mock.calls[0][1]
-    `
+    mySpy.mock.calls[myCounter][0]
+    `,
+    {
+      parser,
+    }
   )
 })
 
@@ -252,7 +257,7 @@ test('return value', () => {
   )
 })
 
-function expectTransformation(source, expectedOutput) {
-  const result = wrappedPlugin(source)
+function expectTransformation(source, expectedOutput, options = {}) {
+  const result = wrappedPlugin(source, options)
   expect(result).toBe(expectedOutput)
 }