Skip to content

Minification: Create local variable for prototype during class declaration #16469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
32 changes: 28 additions & 4 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1578,21 +1578,28 @@ namespace ts {
* @param node The ClassExpression or ClassDeclaration node.
*/
function addClassMembers(statements: Statement[], node: ClassExpression | ClassDeclaration): void {
let prototypeName: Identifier;
for (const member of node.members) {
switch (member.kind) {
case SyntaxKind.SemicolonClassElement:
statements.push(transformSemicolonClassElementToStatement(<SemicolonClassElement>member));
break;

case SyntaxKind.MethodDeclaration:
statements.push(transformClassMethodDeclarationToStatement(getClassMemberPrefix(node, member), <MethodDeclaration>member, node));
if (!prototypeName && !hasModifier(member, ModifierFlags.Static)) {
prototypeName = addPrototypeDeclaration(statements, node);
}
statements.push(transformClassMethodDeclarationToStatement(getClassMemberPrefix(node, member, prototypeName), <MethodDeclaration>member, node));
break;

case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
if (!prototypeName && !hasModifier(member, ModifierFlags.Static)) {
prototypeName = addPrototypeDeclaration(statements, node);
}
const accessors = getAllAccessorDeclarations(node.members, <AccessorDeclaration>member);
if (member === accessors.firstAccessor) {
statements.push(transformAccessorsToStatement(getClassMemberPrefix(node, member), accessors, node));
statements.push(transformAccessorsToStatement(getClassMemberPrefix(node, member, prototypeName), accessors, node));
}

break;
Expand All @@ -1608,6 +1615,23 @@ namespace ts {
}
}

/**
* Adds a statement to the class body function declaring a local variable for the class prototype.
*
* @param statements The statements for the class body function.
* @param node The ClassExpression or ClassDeclaration node.
*/
function addPrototypeDeclaration(statements: Statement[], node: ClassExpression | ClassDeclaration): Identifier {
const prototypeName: Identifier = createUniqueName("proto");
statements.push(createVariableStatement(/* modifiers */ undefined, [
createVariableDeclaration(
prototypeName,
/* type */ undefined,
createPropertyAccess(getInternalName(node), "prototype"))
]));
return prototypeName;
}

/**
* Transforms a SemicolonClassElement into a statement for a class body function.
*
Expand Down Expand Up @@ -4040,10 +4064,10 @@ namespace ts {
return node;
}

function getClassMemberPrefix(node: ClassExpression | ClassDeclaration, member: ClassElement) {
function getClassMemberPrefix(node: ClassExpression | ClassDeclaration, member: ClassElement, prototypeAccessor: Identifier) {
return hasModifier(member, ModifierFlags.Static)
? getInternalName(node)
: createPropertyAccess(getInternalName(node), "prototype");
: prototypeAccessor;
}

function hasSynthesizedDefaultSuperCall(constructor: ConstructorDeclaration, hasExtendsClause: boolean) {
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/2dArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var Ship = (function () {
var Board = (function () {
function Board() {
}
Board.prototype.allShipsSunk = function () {
var proto_1 = Board.prototype;
proto_1.allShipsSunk = function () {
return this.ships.every(function (val) { return val.isSunk; });
};
return Board;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/ClassDeclaration11.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class C {
var C = (function () {
function C() {
}
C.prototype.foo = function () { };
var proto_1 = C.prototype;
proto_1.foo = function () { };
return C;
}());
3 changes: 2 additions & 1 deletion tests/baselines/reference/ClassDeclaration13.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class C {
var C = (function () {
function C() {
}
C.prototype.bar = function () { };
var proto_1 = C.prototype;
proto_1.bar = function () { };
return C;
}());
3 changes: 2 additions & 1 deletion tests/baselines/reference/ClassDeclaration21.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class C {
var C = (function () {
function C() {
}
C.prototype[1] = function () { };
var proto_1 = C.prototype;
proto_1[1] = function () { };
return C;
}());
3 changes: 2 additions & 1 deletion tests/baselines/reference/ClassDeclaration22.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class C {
var C = (function () {
function C() {
}
C.prototype["bar"] = function () { };
var proto_1 = C.prototype;
proto_1["bar"] = function () { };
return C;
}());
5 changes: 3 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck10.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ for (var v of new StringIterator) { }
var StringIterator = (function () {
function StringIterator() {
}
StringIterator.prototype.next = function () {
var proto_1 = StringIterator.prototype;
proto_1.next = function () {
return {
done: true,
value: ""
};
};
StringIterator.prototype[Symbol.iterator] = function () {
proto_1[Symbol.iterator] = function () {
return this;
};
return StringIterator;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/ES5SymbolProperty2.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var M;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () { };
var proto_1 = C.prototype;
proto_1[Symbol.iterator] = function () { };
return C;
}());
M.C = C;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/ES5SymbolProperty3.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () { };
var proto_1 = C.prototype;
proto_1[Symbol.iterator] = function () { };
return C;
}());
(new C)[Symbol.iterator];
3 changes: 2 additions & 1 deletion tests/baselines/reference/ES5SymbolProperty4.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () { };
var proto_1 = C.prototype;
proto_1[Symbol.iterator] = function () { };
return C;
}());
(new C)[Symbol.iterator];
3 changes: 2 additions & 1 deletion tests/baselines/reference/ES5SymbolProperty5.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () { };
var proto_1 = C.prototype;
proto_1[Symbol.iterator] = function () { };
return C;
}());
(new C)[Symbol.iterator](0); // Should error
3 changes: 2 additions & 1 deletion tests/baselines/reference/ES5SymbolProperty6.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class C {
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () { };
var proto_1 = C.prototype;
proto_1[Symbol.iterator] = function () { };
return C;
}());
(new C)[Symbol.iterator];
3 changes: 2 additions & 1 deletion tests/baselines/reference/ES5SymbolProperty7.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () { };
var proto_1 = C.prototype;
proto_1[Symbol.iterator] = function () { };
return C;
}());
(new C)[Symbol.iterator];
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var A;
this.x = x;
this.y = y;
}
Point2d.prototype.fromOrigin = function (p) {
var proto_1 = Point2d.prototype;
proto_1.fromOrigin = function (p) {
return 1;
};
return Point2d;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/MemberAccessorDeclaration15.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class C {
var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "Foo", {
var proto_1 = C.prototype;
Object.defineProperty(proto_1, "Foo", {
set: function (a) { },
enumerable: true,
configurable: true
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/Protected4.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class C {
var C = (function () {
function C() {
}
C.prototype.m = function () { };
var proto_1 = C.prototype;
proto_1.m = function () { };
return C;
}());
3 changes: 2 additions & 1 deletion tests/baselines/reference/Protected7.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class C {
var C = (function () {
function C() {
}
C.prototype.m = function () { };
var proto_1 = C.prototype;
proto_1.m = function () { };
return C;
}());
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ var A;
var Point = (function () {
function Point() {
}
Point.prototype.fromCarthesian = function (p) {
var proto_1 = Point.prototype;
proto_1.fromCarthesian = function (p) {
return { x: p.x, y: p.y };
};
return Point;
Expand Down
5 changes: 3 additions & 2 deletions tests/baselines/reference/abstractProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ var C = (function (_super) {
_this.ro = "readonly please";
return _this;
}
Object.defineProperty(C.prototype, "prop", {
var proto_1 = C.prototype;
Object.defineProperty(proto_1, "prop", {
get: function () { return "foo"; },
set: function (v) { },
enumerable: true,
configurable: true
});
C.prototype.m = function () { };
proto_1.m = function () { };
return C;
}(B));
11 changes: 7 additions & 4 deletions tests/baselines/reference/abstractPropertyNegative.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ var C = (function (_super) {
_this.ro = "readonly please";
return _this;
}
Object.defineProperty(C.prototype, "concreteWithNoBody", {
var proto_1 = C.prototype;
Object.defineProperty(proto_1, "concreteWithNoBody", {
get: function () { },
enumerable: true,
configurable: true
Expand Down Expand Up @@ -99,7 +100,8 @@ var WrongTypeAccessorImpl = (function (_super) {
function WrongTypeAccessorImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(WrongTypeAccessorImpl.prototype, "num", {
var proto_2 = WrongTypeAccessorImpl.prototype;
Object.defineProperty(proto_2, "num", {
get: function () { return "nope, wrong"; },
enumerable: true,
configurable: true
Expand All @@ -118,13 +120,14 @@ var WrongTypeAccessorImpl2 = (function (_super) {
var AbstractAccessorMismatch = (function () {
function AbstractAccessorMismatch() {
}
Object.defineProperty(AbstractAccessorMismatch.prototype, "p1", {
var proto_3 = AbstractAccessorMismatch.prototype;
Object.defineProperty(proto_3, "p1", {
set: function (val) { },
enumerable: true,
configurable: true
});
;
Object.defineProperty(AbstractAccessorMismatch.prototype, "p2", {
Object.defineProperty(proto_3, "p2", {
get: function () { return "should work"; },
enumerable: true,
configurable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class C {
var C = (function () {
function C() {
}
C.prototype.bar = function () {
var proto_1 = C.prototype;
proto_1.bar = function () {
var k = foo;
};
return C;
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/accessOverriddenBaseClassMember1.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var Point = (function () {
this.x = x;
this.y = y;
}
Point.prototype.toString = function () {
var proto_1 = Point.prototype;
proto_1.toString = function () {
return "x=" + this.x + " y=" + this.y;
};
return Point;
Expand All @@ -43,7 +44,8 @@ var ColoredPoint = (function (_super) {
_this.color = color;
return _this;
}
ColoredPoint.prototype.toString = function () {
var proto_2 = ColoredPoint.prototype;
proto_2.toString = function () {
return _super.prototype.toString.call(this) + " color=" + this.color;
};
return ColoredPoint;
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/accessibilityModifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ var D = (function () {
var E = (function () {
function E() {
}
E.prototype.method = function () { };
Object.defineProperty(E.prototype, "getter", {
var proto_1 = E.prototype;
proto_1.method = function () { };
Object.defineProperty(proto_1, "getter", {
get: function () { return 0; },
enumerable: true,
configurable: true
});
Object.defineProperty(E.prototype, "setter", {
Object.defineProperty(proto_1, "setter", {
set: function (a) { },
enumerable: true,
configurable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class C {
var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "X", {
var proto_1 = C.prototype;
Object.defineProperty(proto_1, "X", {
set: function (v) { },
enumerable: true,
configurable: true
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/accessorWithES3.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var y = {
var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "x", {
var proto_1 = C.prototype;
Object.defineProperty(proto_1, "x", {
get: function () {
return 1;
},
Expand All @@ -37,7 +38,8 @@ var C = (function () {
var D = (function () {
function D() {
}
Object.defineProperty(D.prototype, "x", {
var proto_2 = D.prototype;
Object.defineProperty(proto_2, "x", {
set: function (v) {
},
enumerable: true,
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/accessorWithES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var y = {
var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "x", {
var proto_1 = C.prototype;
Object.defineProperty(proto_1, "x", {
get: function () {
return 1;
},
Expand All @@ -34,7 +35,8 @@ var C = (function () {
var D = (function () {
function D() {
}
Object.defineProperty(D.prototype, "x", {
var proto_2 = D.prototype;
Object.defineProperty(proto_2, "x", {
set: function (v) {
},
enumerable: true,
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/accessorWithInitializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class C {
var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "X", {
var proto_1 = C.prototype;
Object.defineProperty(proto_1, "X", {
set: function (v) {
if (v === void 0) { v = 0; }
},
Expand Down
Loading