Skip to content

Commit c6bf6a2

Browse files
authored
Merge pull request #15357 from Microsoft/es2015-transform-accessors-containing-captured-this
es2015 transform covers accessors that contain captured this
2 parents da5fd93 + 00848fc commit c6bf6a2

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3180,7 +3180,20 @@ namespace ts {
31803180
const savedConvertedLoopState = convertedLoopState;
31813181
convertedLoopState = undefined;
31823182
const ancestorFacts = enterSubtree(HierarchyFacts.FunctionExcludes, HierarchyFacts.FunctionIncludes);
3183-
const updated = visitEachChild(node, visitor, context);
3183+
let updated: AccessorDeclaration;
3184+
if (node.transformFlags & TransformFlags.ContainsCapturedLexicalThis) {
3185+
const parameters = visitParameterList(node.parameters, visitor, context);
3186+
const body = transformFunctionBody(node);
3187+
if (node.kind === SyntaxKind.GetAccessor) {
3188+
updated = updateGetAccessor(node, node.decorators, node.modifiers, node.name, parameters, node.type, body);
3189+
}
3190+
else {
3191+
updated = updateSetAccessor(node, node.decorators, node.modifiers, node.name, parameters, body);
3192+
}
3193+
}
3194+
else {
3195+
updated = visitEachChild(node, visitor, context);
3196+
}
31843197
exitSubtree(ancestorFacts, HierarchyFacts.PropagateNewTargetMask, HierarchyFacts.None);
31853198
convertedLoopState = savedConvertedLoopState;
31863199
return updated;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [emitThisInObjectLiteralGetter.ts]
2+
const example = {
3+
get foo() {
4+
return item => this.bar(item);
5+
}
6+
};
7+
8+
9+
//// [emitThisInObjectLiteralGetter.js]
10+
var example = {
11+
get foo() {
12+
var _this = this;
13+
return function (item) { return _this.bar(item); };
14+
}
15+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/emitThisInObjectLiteralGetter.ts ===
2+
const example = {
3+
>example : Symbol(example, Decl(emitThisInObjectLiteralGetter.ts, 0, 5))
4+
5+
get foo() {
6+
>foo : Symbol(foo, Decl(emitThisInObjectLiteralGetter.ts, 0, 17))
7+
8+
return item => this.bar(item);
9+
>item : Symbol(item, Decl(emitThisInObjectLiteralGetter.ts, 2, 14))
10+
>item : Symbol(item, Decl(emitThisInObjectLiteralGetter.ts, 2, 14))
11+
}
12+
};
13+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/emitThisInObjectLiteralGetter.ts ===
2+
const example = {
3+
>example : { readonly foo: (item: any) => any; }
4+
>{ get foo() { return item => this.bar(item); }} : { readonly foo: (item: any) => any; }
5+
6+
get foo() {
7+
>foo : (item: any) => any
8+
9+
return item => this.bar(item);
10+
>item => this.bar(item) : (item: any) => any
11+
>item : any
12+
>this.bar(item) : any
13+
>this.bar : any
14+
>this : any
15+
>bar : any
16+
>item : any
17+
}
18+
};
19+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: es5
2+
const example = {
3+
get foo() {
4+
return item => this.bar(item);
5+
}
6+
};

0 commit comments

Comments
 (0)