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:
Nathan Shively-Sanders
2017-04-25 08:36:53 -07:00
committed by GitHub
5 changed files with 67 additions and 1 deletions

View File

@@ -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;

View 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); };
}
};

View File

@@ -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))
}
};

View File

@@ -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
}
};

View File

@@ -0,0 +1,6 @@
// @target: es5
const example = {
get foo() {
return item => this.bar(item);
}
};