Skip to content

Commit 8350243

Browse files
committed
Merge branch 'master' of https://github.com/rollup/rollup into sync-bbc952c1
2 parents 9e52ae1 + bbc952c commit 8350243

File tree

7 files changed

+41
-0
lines changed

7 files changed

+41
-0
lines changed

src/ast/nodes/ArrayPattern.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type MagicString from 'magic-string';
2+
import type { RenderOptions } from '../../utils/renderHelpers';
13
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
24
import type { NodeInteractionAssigned } from '../NodeInteractions';
35
import { EMPTY_PATH, type ObjectPath, UnknownInteger, UnknownKey } from '../utils/PathTracker';
@@ -101,6 +103,20 @@ export default class ArrayPattern extends NodeBase implements DeclarationPattern
101103
return this.included;
102104
}
103105

106+
render(code: MagicString, options: RenderOptions): void {
107+
let removedStart = this.start + 1;
108+
for (const element of this.elements) {
109+
if (!element) continue;
110+
if (element.included) {
111+
element.render(code, options);
112+
removedStart = element.end;
113+
} else {
114+
code.remove(removedStart, this.end - 1);
115+
break;
116+
}
117+
}
118+
}
119+
104120
markDeclarationReached(): void {
105121
for (const element of this.elements) {
106122
(element as DeclarationPatternNode)?.markDeclarationReached();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = defineTest({
2+
description: 'tree-shake un-included elements in array pattern'
3+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function useState() {
2+
return ['foo', 'bar', 'qux'];
3+
}
4+
5+
const [foo$1] = useState();
6+
assert.ok(foo$1);
7+
8+
const [foo, , qux] = useState();
9+
assert.ok(foo);
10+
assert.ok(qux);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './module1.js';
2+
import './module2.js';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import useState from './module3.js';
2+
const [foo, , qux] = useState();
3+
assert.ok(foo);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import useState from './module3.js';
2+
const [foo, , qux] = useState();
3+
assert.ok(foo);
4+
assert.ok(qux);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function useState() {
2+
return ['foo', 'bar', 'qux'];
3+
}

0 commit comments

Comments
 (0)