Skip to content

Commit

Permalink
fix(optimize-hook-destructuring): handle skipped items (#23438)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Apr 24, 2020
1 parent d595fcb commit 2cf2dc5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,16 @@ describe(`optimize-hook-destructuring`, () => {
`"\\"use strict\\";var _react=require(\\"react\\");const{0:count,1:setCount}=(0,_react.useState)(0);"`
)
})

it(`should handle skipped items`, () => {
const input = trim`
import { useState } from 'react';
const [, setCount] = useState(0);
`

expect(() => babel(input)).not.toThrow()
expect(babel(input)).toMatchInlineSnapshot(
`"\\"use strict\\";var _react=require(\\"react\\");const{1:setCount}=(0,_react.useState)(0);"`
)
})
})
10 changes: 8 additions & 2 deletions packages/babel-preset-gatsby/src/optimize-hook-destructuring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ export default function ({
if (!(onlyBuiltIns ? isBuiltInHook : isHook).test(hookName)) return

path.parent.id = t.objectPattern(
path.parent.id.elements.map((element, i) =>
t.objectProperty(t.numericLiteral(i), element!)
path.parent.id.elements.reduce(
(acc: BabelTypes.ObjectProperty[], element, i) => {
if (element) {
acc.push(t.objectProperty(t.numericLiteral(i), element))
}
return acc
},
[]
)
)
},
Expand Down

0 comments on commit 2cf2dc5

Please sign in to comment.