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

Correctly eliminate destructuring assignment #13144

Merged
merged 2 commits into from
May 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions packages/next/build/babel/plugins/next-ssg-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,33 @@ export default function nextTransformSsg({
path.traverse(
{
VariableDeclarator(path, state) {
if (path.node.id.type !== 'Identifier') {
return
}

const local = path.get('id') as NodePath<BabelTypes.Identifier>
if (isIdentifierReferenced(local)) {
state.refs.add(local)
if (path.node.id.type === 'Identifier') {
const local = path.get('id') as NodePath<
BabelTypes.Identifier
>
if (isIdentifierReferenced(local)) {
state.refs.add(local)
}
} else if (path.node.id.type === 'ObjectPattern') {
const pattern = path.get('id') as NodePath<
BabelTypes.ObjectPattern
>

const properties = pattern.get('properties')
properties.forEach((p) => {
const local = p.get(
p.node.type === 'ObjectProperty'
? 'value'
: p.node.type === 'RestElement'
? 'argument'
: (function () {
throw new Error('invariant')
})()
) as NodePath<BabelTypes.Identifier>
if (isIdentifierReferenced(local)) {
state.refs.add(local)
}
})
}
},
FunctionDeclaration: markFunction,
Expand Down Expand Up @@ -319,14 +339,44 @@ export default function nextTransformSsg({
path.traverse({
// eslint-disable-next-line no-loop-func
VariableDeclarator(path) {
if (path.node.id.type !== 'Identifier') {
return
}
if (path.node.id.type === 'Identifier') {
const local = path.get('id') as NodePath<
BabelTypes.Identifier
>
if (refs.has(local) && !isIdentifierReferenced(local)) {
++count
path.remove()
}
} else if (path.node.id.type === 'ObjectPattern') {
const pattern = path.get('id') as NodePath<
BabelTypes.ObjectPattern
>

const beforeCount = count
const properties = pattern.get('properties')
properties.forEach((p) => {
const local = p.get(
p.node.type === 'ObjectProperty'
? 'value'
: p.node.type === 'RestElement'
? 'argument'
: (function () {
throw new Error('invariant')
})()
) as NodePath<BabelTypes.Identifier>

if (refs.has(local) && !isIdentifierReferenced(local)) {
++count
p.remove()
}
})

const local = path.get('id') as NodePath<BabelTypes.Identifier>
if (refs.has(local) && !isIdentifierReferenced(local)) {
++count
path.remove()
if (
beforeCount !== count &&
pattern.get('properties').length < 1
) {
path.remove()
}
}
},
FunctionDeclaration: sweepFunction,
Expand Down
24 changes: 24 additions & 0 deletions test/unit/babel-plugin-next-ssg-transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,5 +452,29 @@ describe('babel plugin (next-ssg-transform)', () => {
`"export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
)
})

it('destructuring assignment (object)', () => {
const output = babel(trim`
import fs from 'fs';
import other from 'other';
const {readFile, readdir, access: foo} = fs.promises;
const {a,b, cat: bar,...rem} = other;
export async function getStaticProps() {
readFile;
readdir;
foo;
b;
cat;
rem;
}
export default function Home() { return <div />; }
`)

expect(output).toMatchInlineSnapshot(
`"import other from'other';const{a,cat:bar}=other;export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
)
})
})
})