Skip to content

Commit 1db769d

Browse files
authored
Transform setter bodies if they contain es2015 (#22931)
1 parent 6ef4d77 commit 1db769d

5 files changed

+68
-11
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ namespace ts {
19221922
return block;
19231923
}
19241924

1925-
function visitFunctionBodyDownLevel(node: FunctionDeclaration | FunctionExpression) {
1925+
function visitFunctionBodyDownLevel(node: FunctionDeclaration | FunctionExpression | AccessorDeclaration) {
19261926
const updated = visitFunctionBody(node.body, functionBodyVisitor, context);
19271927
return updateBlock(
19281928
updated,
@@ -3196,18 +3196,15 @@ namespace ts {
31963196
convertedLoopState = undefined;
31973197
const ancestorFacts = enterSubtree(HierarchyFacts.FunctionExcludes, HierarchyFacts.FunctionIncludes);
31983198
let updated: AccessorDeclaration;
3199-
if (node.transformFlags & TransformFlags.ContainsCapturedLexicalThis) {
3200-
const parameters = visitParameterList(node.parameters, visitor, context);
3201-
const body = transformFunctionBody(node);
3202-
if (node.kind === SyntaxKind.GetAccessor) {
3203-
updated = updateGetAccessor(node, node.decorators, node.modifiers, node.name, parameters, node.type, body);
3204-
}
3205-
else {
3206-
updated = updateSetAccessor(node, node.decorators, node.modifiers, node.name, parameters, body);
3207-
}
3199+
const parameters = visitParameterList(node.parameters, visitor, context);
3200+
const body = node.transformFlags & (TransformFlags.ContainsCapturedLexicalThis | TransformFlags.ContainsES2015)
3201+
? transformFunctionBody(node)
3202+
: visitFunctionBodyDownLevel(node);
3203+
if (node.kind === SyntaxKind.GetAccessor) {
3204+
updated = updateGetAccessor(node, node.decorators, node.modifiers, node.name, parameters, node.type, body);
32083205
}
32093206
else {
3210-
updated = visitEachChild(node, visitor, context);
3207+
updated = updateSetAccessor(node, node.decorators, node.modifiers, node.name, parameters, body);
32113208
}
32123209
exitSubtree(ancestorFacts, HierarchyFacts.PropagateNewTargetMask, HierarchyFacts.None);
32133210
convertedLoopState = savedConvertedLoopState;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [es5SetterparameterDestructuringNotElided.ts]
2+
const foo = {
3+
set foo([start, end]: [any, any]) {
4+
void start;
5+
void end;
6+
},
7+
};
8+
9+
//// [es5SetterparameterDestructuringNotElided.js]
10+
var foo = {
11+
set foo(_a) {
12+
var start = _a[0], end = _a[1];
13+
void start;
14+
void end;
15+
},
16+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/es5SetterparameterDestructuringNotElided.ts ===
2+
const foo = {
3+
>foo : Symbol(foo, Decl(es5SetterparameterDestructuringNotElided.ts, 0, 5))
4+
5+
set foo([start, end]: [any, any]) {
6+
>foo : Symbol(foo, Decl(es5SetterparameterDestructuringNotElided.ts, 0, 13))
7+
>start : Symbol(start, Decl(es5SetterparameterDestructuringNotElided.ts, 1, 13))
8+
>end : Symbol(end, Decl(es5SetterparameterDestructuringNotElided.ts, 1, 19))
9+
10+
void start;
11+
>start : Symbol(start, Decl(es5SetterparameterDestructuringNotElided.ts, 1, 13))
12+
13+
void end;
14+
>end : Symbol(end, Decl(es5SetterparameterDestructuringNotElided.ts, 1, 19))
15+
16+
},
17+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/es5SetterparameterDestructuringNotElided.ts ===
2+
const foo = {
3+
>foo : { foo: [any, any]; }
4+
>{ set foo([start, end]: [any, any]) { void start; void end; },} : { foo: [any, any]; }
5+
6+
set foo([start, end]: [any, any]) {
7+
>foo : [any, any]
8+
>start : any
9+
>end : any
10+
11+
void start;
12+
>void start : undefined
13+
>start : any
14+
15+
void end;
16+
>void end : undefined
17+
>end : any
18+
19+
},
20+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @target: es5
2+
const foo = {
3+
set foo([start, end]: [any, any]) {
4+
void start;
5+
void end;
6+
},
7+
};

0 commit comments

Comments
 (0)