Skip to content

Commit 8e37e4b

Browse files
authored
More reliable mechanism for _this/_super simplification (#56130)
1 parent e5e7cbb commit 8e37e4b

38 files changed

+769
-349
lines changed

src/compiler/factory/nodeFactory.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
__String,
3+
AccessorDeclaration,
34
addRange,
45
append,
56
appendIfUnique,
@@ -1180,6 +1181,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
11801181
mergeLexicalEnvironment,
11811182
replaceModifiers,
11821183
replaceDecoratorsAndModifiers,
1184+
replacePropertyName,
11831185
};
11841186

11851187
forEach(nodeFactoryPatchers, fn => fn(factory));
@@ -7149,6 +7151,26 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
71497151
Debug.assertNever(node);
71507152
}
71517153

7154+
function replacePropertyName<T extends AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment>(node: T, name: T["name"]): T;
7155+
function replacePropertyName(node: AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment, name: PropertyName) {
7156+
switch (node.kind) {
7157+
case SyntaxKind.GetAccessor:
7158+
return updateGetAccessorDeclaration(node, node.modifiers, name, node.parameters, node.type, node.body);
7159+
case SyntaxKind.SetAccessor:
7160+
return updateSetAccessorDeclaration(node, node.modifiers, name, node.parameters, node.body);
7161+
case SyntaxKind.MethodDeclaration:
7162+
return updateMethodDeclaration(node, node.modifiers, node.asteriskToken, name, node.questionToken, node.typeParameters, node.parameters, node.type, node.body);
7163+
case SyntaxKind.MethodSignature:
7164+
return updateMethodSignature(node, node.modifiers, name, node.questionToken, node.typeParameters, node.parameters, node.type);
7165+
case SyntaxKind.PropertyDeclaration:
7166+
return updatePropertyDeclaration(node, node.modifiers, name, node.questionToken ?? node.exclamationToken, node.type, node.initializer);
7167+
case SyntaxKind.PropertySignature:
7168+
return updatePropertySignature(node, node.modifiers, name, node.questionToken, node.type);
7169+
case SyntaxKind.PropertyAssignment:
7170+
return updatePropertyAssignment(node, name, node.initializer);
7171+
}
7172+
}
7173+
71527174
function asNodeArray<T extends Node>(array: readonly T[]): NodeArray<T>;
71537175
function asNodeArray<T extends Node>(array: readonly T[] | undefined): NodeArray<T> | undefined;
71547176
function asNodeArray<T extends Node>(array: readonly T[] | undefined): NodeArray<T> | undefined {

src/compiler/transformers/es2015.ts

Lines changed: 554 additions & 246 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9080,6 +9080,10 @@ export interface NodeFactory {
90809080
* Updates a node that may contain decorators or modifiers, replacing only the decorators and modifiers of the node.
90819081
*/
90829082
replaceDecoratorsAndModifiers<T extends HasModifiers & HasDecorators>(node: T, modifiers: readonly ModifierLike[] | undefined): T;
9083+
/**
9084+
* Updates a node that contains a property name, replacing only the name of the node.
9085+
*/
9086+
replacePropertyName<T extends AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment>(node: T, name: T["name"]): T;
90839087
}
90849088

90859089
/** @internal */

tests/baselines/reference/api/typescript.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8394,6 +8394,10 @@ declare namespace ts {
83948394
* Updates a node that may contain decorators or modifiers, replacing only the decorators and modifiers of the node.
83958395
*/
83968396
replaceDecoratorsAndModifiers<T extends HasModifiers & HasDecorators>(node: T, modifiers: readonly ModifierLike[] | undefined): T;
8397+
/**
8398+
* Updates a node that contains a property name, replacing only the name of the node.
8399+
*/
8400+
replacePropertyName<T extends AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment>(node: T, name: T["name"]): T;
83978401
}
83988402
interface CoreTransformationContext {
83998403
readonly factory: NodeFactory;

tests/baselines/reference/callWithSpread.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ var D = /** @class */ (function (_super) {
136136
__extends(D, _super);
137137
function D() {
138138
var _this = _super.call(this, 1, 2) || this;
139-
_this = _super.apply(this, __spreadArray([1, 2], a, false)) || this;
140-
return _this;
139+
return _super.apply(this, __spreadArray([1, 2], a, false)) || this;
141140
}
142141
D.prototype.foo = function () {
143142
_super.prototype.foo.call(this, 1, 2);

tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ var Super = /** @class */ (function (_super) {
4242
function Super() {
4343
var _this = this;
4444
(function () { return _this; }); // No Error
45-
_this = _super.call(this) || this;
46-
return _this;
45+
return _this = _super.call(this) || this;
4746
}
4847
return Super;
4948
}(Base));

tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var Base = /** @class */ (function () {
3535
var Super = /** @class */ (function (_super) {
3636
__extends(Super, _super);
3737
function Super() {
38-
var _this = _super.call(this, (function () { return _this; })) || this;
38+
var _this = _super.call(this, (function () { return _this; })) || this; // No error
3939
return _this;
4040
}
4141
return Super;

tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ var Super = /** @class */ (function (_super) {
4242
function Super() {
4343
var _this = this;
4444
var that = _this;
45-
_this = _super.call(this) || this;
46-
return _this;
45+
return _this = _super.call(this) || this;
4746
}
4847
return Super;
4948
}(Base));

tests/baselines/reference/classUpdateTests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ var I = /** @class */ (function (_super) {
208208
var J = /** @class */ (function (_super) {
209209
__extends(J, _super);
210210
function J(p1) {
211-
var _this = _super.call(this) || this;
211+
var _this = _super.call(this) || this; // NO ERROR
212212
_this.p1 = p1;
213213
return _this;
214214
}
@@ -228,7 +228,7 @@ var K = /** @class */ (function (_super) {
228228
var L = /** @class */ (function (_super) {
229229
__extends(L, _super);
230230
function L(p1) {
231-
var _this = _super.call(this) || this;
231+
var _this = _super.call(this) || this; // NO ERROR
232232
_this.p1 = p1;
233233
return _this;
234234
}

tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)