Commit 7c0ac822 authored by Sojin Park's avatar Sojin Park Committed by Joe Haddad

Fix bug in hook destructuring optimization when ignoring array elements (#8455)

* Fix bug in hook destructuring optimization when ignoring array elements

* Add test cases
parent 3896cbe7
...@@ -48,8 +48,17 @@ export default function({ ...@@ -48,8 +48,17 @@ export default function({
if (!(onlyBuiltIns ? isBuiltInHook : isHook).test(hookName)) return if (!(onlyBuiltIns ? isBuiltInHook : isHook).test(hookName)) return
path.parent.id = t.objectPattern( path.parent.id = t.objectPattern(
path.parent.id.elements.map((element, i) => path.parent.id.elements.reduce<Array<BabelTypes.ObjectProperty>>(
t.objectProperty(t.numericLiteral(i), element) (patterns, element, i) => {
if (element === null) {
return patterns
}
return patterns.concat(
t.objectProperty(t.numericLiteral(i), element)
)
},
[]
) )
) )
}, },
......
...@@ -116,5 +116,23 @@ describe('next/babel', () => { ...@@ -116,5 +116,23 @@ describe('next/babel', () => {
`"import{useState}from'react';var _useState=useState(0),count=_useState[0],setCount=_useState[1];"` `"import{useState}from'react';var _useState=useState(0),count=_useState[0],setCount=_useState[1];"`
) )
}) })
it('should be able to ignore some Array-destructured hook return values', () => {
const output = babel(
trim`
import { useState } from 'react';
const [, setCount] = useState(0);
`,
true
)
expect(output).toMatch(trim`
var _useState=useState(0),setCount=_useState[1];
`)
expect(output).toMatchInlineSnapshot(
`"import{useState}from'react';var _useState=useState(0),setCount=_useState[1];"`
)
})
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment