mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-23 17:30:04 -05:00
Merge pull request #15357 from Microsoft/es2015-transform-accessors-containing-captured-this
es2015 transform covers accessors that contain captured this
This commit is contained in:
@@ -3180,7 +3180,20 @@ namespace ts {
|
||||
const savedConvertedLoopState = convertedLoopState;
|
||||
convertedLoopState = undefined;
|
||||
const ancestorFacts = enterSubtree(HierarchyFacts.FunctionExcludes, HierarchyFacts.FunctionIncludes);
|
||||
const updated = visitEachChild(node, visitor, context);
|
||||
let updated: AccessorDeclaration;
|
||||
if (node.transformFlags & TransformFlags.ContainsCapturedLexicalThis) {
|
||||
const parameters = visitParameterList(node.parameters, visitor, context);
|
||||
const body = transformFunctionBody(node);
|
||||
if (node.kind === SyntaxKind.GetAccessor) {
|
||||
updated = updateGetAccessor(node, node.decorators, node.modifiers, node.name, parameters, node.type, body);
|
||||
}
|
||||
else {
|
||||
updated = updateSetAccessor(node, node.decorators, node.modifiers, node.name, parameters, body);
|
||||
}
|
||||
}
|
||||
else {
|
||||
updated = visitEachChild(node, visitor, context);
|
||||
}
|
||||
exitSubtree(ancestorFacts, HierarchyFacts.PropagateNewTargetMask, HierarchyFacts.None);
|
||||
convertedLoopState = savedConvertedLoopState;
|
||||
return updated;
|
||||
|
||||
15
tests/baselines/reference/emitThisInObjectLiteralGetter.js
Normal file
15
tests/baselines/reference/emitThisInObjectLiteralGetter.js
Normal file
@@ -0,0 +1,15 @@
|
||||
//// [emitThisInObjectLiteralGetter.ts]
|
||||
const example = {
|
||||
get foo() {
|
||||
return item => this.bar(item);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//// [emitThisInObjectLiteralGetter.js]
|
||||
var example = {
|
||||
get foo() {
|
||||
var _this = this;
|
||||
return function (item) { return _this.bar(item); };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/emitThisInObjectLiteralGetter.ts ===
|
||||
const example = {
|
||||
>example : Symbol(example, Decl(emitThisInObjectLiteralGetter.ts, 0, 5))
|
||||
|
||||
get foo() {
|
||||
>foo : Symbol(foo, Decl(emitThisInObjectLiteralGetter.ts, 0, 17))
|
||||
|
||||
return item => this.bar(item);
|
||||
>item : Symbol(item, Decl(emitThisInObjectLiteralGetter.ts, 2, 14))
|
||||
>item : Symbol(item, Decl(emitThisInObjectLiteralGetter.ts, 2, 14))
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
=== tests/cases/compiler/emitThisInObjectLiteralGetter.ts ===
|
||||
const example = {
|
||||
>example : { readonly foo: (item: any) => any; }
|
||||
>{ get foo() { return item => this.bar(item); }} : { readonly foo: (item: any) => any; }
|
||||
|
||||
get foo() {
|
||||
>foo : (item: any) => any
|
||||
|
||||
return item => this.bar(item);
|
||||
>item => this.bar(item) : (item: any) => any
|
||||
>item : any
|
||||
>this.bar(item) : any
|
||||
>this.bar : any
|
||||
>this : any
|
||||
>bar : any
|
||||
>item : any
|
||||
}
|
||||
};
|
||||
|
||||
6
tests/cases/compiler/emitThisInObjectLiteralGetter.ts
Normal file
6
tests/cases/compiler/emitThisInObjectLiteralGetter.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
// @target: es5
|
||||
const example = {
|
||||
get foo() {
|
||||
return item => this.bar(item);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user