Unverified Commit 74d7e358 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Eliminate array destructuring assignment (#13151)

While we're at it, we should fix this too.

x-ref: #13144
parent b325f15e
...@@ -212,6 +212,28 @@ export default function nextTransformSsg({ ...@@ -212,6 +212,28 @@ export default function nextTransformSsg({
throw new Error('invariant') throw new Error('invariant')
})() })()
) as NodePath<BabelTypes.Identifier> ) as NodePath<BabelTypes.Identifier>
if (isIdentifierReferenced(local)) {
state.refs.add(local)
}
})
} else if (path.node.id.type === 'ArrayPattern') {
const pattern = path.get('id') as NodePath<
BabelTypes.ArrayPattern
>
const elements = pattern.get('elements')
elements.forEach((e) => {
let local: NodePath<BabelTypes.Identifier>
if (e.node?.type === 'Identifier') {
local = e as NodePath<BabelTypes.Identifier>
} else if (e.node?.type === 'RestElement') {
local = e.get('argument') as NodePath<
BabelTypes.Identifier
>
} else {
return
}
if (isIdentifierReferenced(local)) { if (isIdentifierReferenced(local)) {
state.refs.add(local) state.refs.add(local)
} }
...@@ -377,6 +399,37 @@ export default function nextTransformSsg({ ...@@ -377,6 +399,37 @@ export default function nextTransformSsg({
) { ) {
path.remove() path.remove()
} }
} else if (path.node.id.type === 'ArrayPattern') {
const pattern = path.get('id') as NodePath<
BabelTypes.ArrayPattern
>
const beforeCount = count
const elements = pattern.get('elements')
elements.forEach((e) => {
let local: NodePath<BabelTypes.Identifier>
if (e.node?.type === 'Identifier') {
local = e as NodePath<BabelTypes.Identifier>
} else if (e.node?.type === 'RestElement') {
local = e.get('argument') as NodePath<
BabelTypes.Identifier
>
} else {
return
}
if (refs.has(local) && !isIdentifierReferenced(local)) {
++count
e.remove()
}
})
if (
beforeCount !== count &&
pattern.get('elements').length < 1
) {
path.remove()
}
} }
}, },
FunctionDeclaration: sweepFunction, FunctionDeclaration: sweepFunction,
......
...@@ -476,5 +476,27 @@ describe('babel plugin (next-ssg-transform)', () => { ...@@ -476,5 +476,27 @@ describe('babel plugin (next-ssg-transform)', () => {
`"import other from'other';const{a,cat:bar}=other;export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"` `"import other from'other';const{a,cat:bar}=other;export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
) )
}) })
it('destructuring assignment (array)', () => {
const output = babel(trim`
import fs from 'fs';
import other from 'other';
const [a, b, ...rest]= fs.promises;
const [foo, bar] = other;
export async function getStaticProps() {
a;
b;
rest;
bar;
}
export default function Home() { return <div />; }
`)
expect(output).toMatchInlineSnapshot(
`"import other from'other';const[foo]=other;export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
)
})
}) })
}) })
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