Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(optimize-hook-destructuring): handle skipped items #23438

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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(
pieh marked this conversation as resolved.
Show resolved Hide resolved
(acc: BabelTypes.ObjectProperty[], element, i) => {
if (element) {
acc.push(t.objectProperty(t.numericLiteral(i), element))
}
return acc
},
[]
)
)
},
Expand Down