Skip to content

Commit

Permalink
More reliable mechanism for _this/_super simplification (#56130)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton authored Oct 24, 2023
1 parent e5e7cbb commit 8e37e4b
Show file tree
Hide file tree
Showing 38 changed files with 769 additions and 349 deletions.
22 changes: 22 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
__String,
AccessorDeclaration,
addRange,
append,
appendIfUnique,
Expand Down Expand Up @@ -1180,6 +1181,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
mergeLexicalEnvironment,
replaceModifiers,
replaceDecoratorsAndModifiers,
replacePropertyName,
};

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

function replacePropertyName<T extends AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment>(node: T, name: T["name"]): T;
function replacePropertyName(node: AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment, name: PropertyName) {
switch (node.kind) {
case SyntaxKind.GetAccessor:
return updateGetAccessorDeclaration(node, node.modifiers, name, node.parameters, node.type, node.body);
case SyntaxKind.SetAccessor:
return updateSetAccessorDeclaration(node, node.modifiers, name, node.parameters, node.body);
case SyntaxKind.MethodDeclaration:
return updateMethodDeclaration(node, node.modifiers, node.asteriskToken, name, node.questionToken, node.typeParameters, node.parameters, node.type, node.body);
case SyntaxKind.MethodSignature:
return updateMethodSignature(node, node.modifiers, name, node.questionToken, node.typeParameters, node.parameters, node.type);
case SyntaxKind.PropertyDeclaration:
return updatePropertyDeclaration(node, node.modifiers, name, node.questionToken ?? node.exclamationToken, node.type, node.initializer);
case SyntaxKind.PropertySignature:
return updatePropertySignature(node, node.modifiers, name, node.questionToken, node.type);
case SyntaxKind.PropertyAssignment:
return updatePropertyAssignment(node, name, node.initializer);
}
}

function asNodeArray<T extends Node>(array: readonly T[]): NodeArray<T>;
function asNodeArray<T extends Node>(array: readonly T[] | undefined): NodeArray<T> | undefined;
function asNodeArray<T extends Node>(array: readonly T[] | undefined): NodeArray<T> | undefined {
Expand Down
800 changes: 554 additions & 246 deletions src/compiler/transformers/es2015.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9080,6 +9080,10 @@ export interface NodeFactory {
* Updates a node that may contain decorators or modifiers, replacing only the decorators and modifiers of the node.
*/
replaceDecoratorsAndModifiers<T extends HasModifiers & HasDecorators>(node: T, modifiers: readonly ModifierLike[] | undefined): T;
/**
* Updates a node that contains a property name, replacing only the name of the node.
*/
replacePropertyName<T extends AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment>(node: T, name: T["name"]): T;
}

/** @internal */
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8394,6 +8394,10 @@ declare namespace ts {
* Updates a node that may contain decorators or modifiers, replacing only the decorators and modifiers of the node.
*/
replaceDecoratorsAndModifiers<T extends HasModifiers & HasDecorators>(node: T, modifiers: readonly ModifierLike[] | undefined): T;
/**
* Updates a node that contains a property name, replacing only the name of the node.
*/
replacePropertyName<T extends AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment>(node: T, name: T["name"]): T;
}
interface CoreTransformationContext {
readonly factory: NodeFactory;
Expand Down
3 changes: 1 addition & 2 deletions tests/baselines/reference/callWithSpread.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ var D = /** @class */ (function (_super) {
__extends(D, _super);
function D() {
var _this = _super.call(this, 1, 2) || this;
_this = _super.apply(this, __spreadArray([1, 2], a, false)) || this;
return _this;
return _super.apply(this, __spreadArray([1, 2], a, false)) || this;
}
D.prototype.foo = function () {
_super.prototype.foo.call(this, 1, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ var Super = /** @class */ (function (_super) {
function Super() {
var _this = this;
(function () { return _this; }); // No Error
_this = _super.call(this) || this;
return _this;
return _this = _super.call(this) || this;
}
return Super;
}(Base));
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var Base = /** @class */ (function () {
var Super = /** @class */ (function (_super) {
__extends(Super, _super);
function Super() {
var _this = _super.call(this, (function () { return _this; })) || this;
var _this = _super.call(this, (function () { return _this; })) || this; // No error
return _this;
}
return Super;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ var Super = /** @class */ (function (_super) {
function Super() {
var _this = this;
var that = _this;
_this = _super.call(this) || this;
return _this;
return _this = _super.call(this) || this;
}
return Super;
}(Base));
4 changes: 2 additions & 2 deletions tests/baselines/reference/classUpdateTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var I = /** @class */ (function (_super) {
var J = /** @class */ (function (_super) {
__extends(J, _super);
function J(p1) {
var _this = _super.call(this) || this;
var _this = _super.call(this) || this; // NO ERROR
_this.p1 = p1;
return _this;
}
Expand All @@ -228,7 +228,7 @@ var K = /** @class */ (function (_super) {
var L = /** @class */ (function (_super) {
__extends(L, _super);
function L(p1) {
var _this = _super.call(this) || this;
var _this = _super.call(this) || this; // NO ERROR
_this.p1 = p1;
return _this;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -270,36 +270,27 @@ sourceFile:derivedClassConstructorWithExplicitReturns01.ts
>>> var _this = _super.call(this, a) || this;
1->^^^^^^^^
2 > ^^^^^^^^^^^^
3 > ^^^^^^^^^^^^^^^^^^
4 > ^
5 > ^
6 > ^^^^^^^^^
7 > ^^^^->
1->
2 > constructor(a = 100) {
>
3 > super(
4 > a
5 > )
6 > ;
>
> if (Math.random() < 0.5) {
> "You win!"
> return {
> cProp: 1,
> dProp: () => this,
> foo() { return "You win!!!!!" }
> };
> }
> else
> return null;
> }
1->Emitted(33, 9) Source(19, 5) + SourceIndex(0)
3 > ^^^^^^
4 > ^^^^^^^^^^^^
5 > ^
6 > ^
7 > ^^^^^^^^^
8 > ^^^^->
1->) {
>
2 >
3 > super
4 > (
5 > a
6 > )
7 > ;
1->Emitted(33, 9) Source(20, 9) + SourceIndex(0)
2 >Emitted(33, 21) Source(20, 9) + SourceIndex(0)
3 >Emitted(33, 39) Source(20, 15) + SourceIndex(0)
4 >Emitted(33, 40) Source(20, 16) + SourceIndex(0)
5 >Emitted(33, 41) Source(20, 17) + SourceIndex(0)
6 >Emitted(33, 50) Source(32, 6) + SourceIndex(0)
3 >Emitted(33, 27) Source(20, 14) + SourceIndex(0)
4 >Emitted(33, 39) Source(20, 15) + SourceIndex(0)
5 >Emitted(33, 40) Source(20, 16) + SourceIndex(0)
6 >Emitted(33, 41) Source(20, 17) + SourceIndex(0)
7 >Emitted(33, 50) Source(20, 18) + SourceIndex(0)
---
>>> _this.dProp = function () { return _this; };
1->^^^^^^^^
Expand Down
3 changes: 1 addition & 2 deletions tests/baselines/reference/derivedClassParameterProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ var Derived6 = /** @class */ (function (_super) {
var _this = this;
_this.a = 1;
var b = 2;
_this = _super.call(this) || this;
return _this;
return _this = _super.call(this) || this;
}
return Derived6;
}(Base));
Expand Down
Loading

0 comments on commit 8e37e4b

Please sign in to comment.