Avoid replacing last statement of derived constructors if 'this' is referenced.#12893
Conversation
| // but only if the constructor itself doesn't use 'this' elsewhere. | ||
| if (superCallExpression | ||
| && statementOffset === ctorStatements.length - 1 | ||
| && !(ctor.transformFlags & (TransformFlags.ContainsLexicalThis | TransformFlags.ContainsCapturedLexicalThis))) { |
There was a problem hiding this comment.
No, it's only used during aggregation to determine whether to promote ContainsLexicalThis in a computed property name so that it doesn't get stopped at a method or accessor.
There was a problem hiding this comment.
The indentation of the if condition looks a bit odd here.
There was a problem hiding this comment.
So ContainsLexicalThis means merely that a this keyword was somewhere inside of the function. ContainsCapturedLexicalThis means that a this keyword was captured by an arrow function inside the function.
| __extends(Derived, _super); | ||
| function Derived() { | ||
| return _super.call(this, function () { return _this; }) || this; | ||
| var _this = _super.call(this, function () { return _this; }) || this; |
There was a problem hiding this comment.
These baselines don't quite seem correct. Surely, _this should be declared and set to this first before the super call. That way, if the super call invokes the function, it'll still function as expected if the super does not return a new value for this.
There was a problem hiding this comment.
This is intentional - in ES2015 it is an error for this to be used before a call to super(...) completes.
This means that it shouldn't be an error to reference _this, but it should be an error to evaluate _this in a callback before the super(...) call returns.
There was a problem hiding this comment.
ES2015 doesn't have a bunch of existing TypeScript code that evaluates this in a callback before the super() call returns.
| // but only if the constructor itself doesn't use 'this' elsewhere. | ||
| if (superCallExpression | ||
| && statementOffset === ctorStatements.length - 1 | ||
| && !(ctor.transformFlags & (TransformFlags.ContainsLexicalThis | TransformFlags.ContainsCapturedLexicalThis))) { |
There was a problem hiding this comment.
The indentation of the if condition looks a bit odd here.
| // but only if the constructor itself doesn't use 'this' elsewhere. | ||
| if (superCallExpression | ||
| && statementOffset === ctorStatements.length - 1 | ||
| && !(ctor.transformFlags & (TransformFlags.ContainsLexicalThis | TransformFlags.ContainsCapturedLexicalThis))) { |
There was a problem hiding this comment.
So ContainsLexicalThis means merely that a this keyword was somewhere inside of the function. ContainsCapturedLexicalThis means that a this keyword was captured by an arrow function inside the function.
Fixes #12764.