Skip to content

Commit c4cb3e3

Browse files
committed
Merge pull request #2333 from Microsoft/emitClass
Class emit for ES6
2 parents 2240fbf + 9b3fccd commit c4cb3e3

File tree

204 files changed

+2621
-2081
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+2621
-2081
lines changed

src/compiler/emitter.ts

+207-97
Large diffs are not rendered by default.

src/compiler/parser.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ module ts {
614614
// change the token touching it, we actually need to look back *at least* one token so
615615
// that the prior token sees that change.
616616
let maxLookahead = 1;
617-
617+
618618
let start = changeRange.span.start;
619619

620620
// the first iteration aligns us with the change start. subsequent iteration move us to
@@ -832,7 +832,7 @@ module ts {
832832
// will immediately bail out of walking any subtrees when we can see that their parents
833833
// are already correct.
834834
let result = parseSourceFile(sourceFile.fileName, newText, sourceFile.languageVersion, syntaxCursor, /* setParentNode */ true)
835-
835+
836836
return result;
837837
}
838838

@@ -1987,7 +1987,7 @@ module ts {
19871987
//
19881988
// let v = new List < A, B >()
19891989
//
1990-
// then we have a problem. "v = new List<A" doesn't intersect the change range, so we
1990+
// then we have a problem. "v = new List<A" doesn't intersect the change range, so we
19911991
// start reparsing at "B" and we completely fail to handle this properly.
19921992
//
19931993
// In order to prevent this, we do not allow a variable declarator to be reused if it
@@ -3009,9 +3009,9 @@ module ts {
30093009
Debug.assert(token === SyntaxKind.EqualsGreaterThanToken, "parseSimpleArrowFunctionExpression should only have been called if we had a =>");
30103010

30113011
let node = <ArrowFunction>createNode(SyntaxKind.ArrowFunction, identifier.pos);
3012-
3012+
30133013
let parameter = <ParameterDeclaration>createNode(SyntaxKind.Parameter, identifier.pos);
3014-
parameter.name = identifier;
3014+
parameter.name = identifier;
30153015
finishNode(parameter);
30163016

30173017
node.parameters = <NodeArray<ParameterDeclaration>>[parameter];
@@ -3141,8 +3141,8 @@ module ts {
31413141

31423142
function parseParenthesizedArrowFunctionExpressionHead(allowAmbiguity: boolean): ArrowFunction {
31433143
let node = <ArrowFunction>createNode(SyntaxKind.ArrowFunction);
3144-
// Arrow functions are never generators.
3145-
//
3144+
// Arrow functions are never generators.
3145+
//
31463146
// If we're speculatively parsing a signature for a parenthesized arrow function, then
31473147
// we have to have a complete parameter list. Otherwise we might see something like
31483148
// a => (b => c)
@@ -3207,7 +3207,7 @@ module ts {
32073207
// Note: we explicitly 'allowIn' in the whenTrue part of the condition expression, and
32083208
// we do not that for the 'whenFalse' part.
32093209
let node = <ConditionalExpression>createNode(SyntaxKind.ConditionalExpression, leftOperand.pos);
3210-
node.condition = leftOperand;
3210+
node.condition = leftOperand;
32113211
node.questionToken = questionToken;
32123212
node.whenTrue = allowInAnd(parseAssignmentExpressionOrHigher);
32133213
node.colonToken = parseExpectedToken(SyntaxKind.ColonToken, /*reportAtCurrentPosition:*/ false,
@@ -4527,7 +4527,13 @@ module ts {
45274527
}
45284528

45294529
function parseClassDeclaration(fullStart: number, modifiers: ModifiersArray): ClassDeclaration {
4530-
let node = <ClassDeclaration>createNode(SyntaxKind.ClassDeclaration, fullStart);
4530+
// In ES6 specification, All parts of a ClassDeclaration or a ClassExpression are strict mode code
4531+
let savedStrictModeContext = inStrictModeContext();
4532+
if (languageVersion >= ScriptTarget.ES6) {
4533+
setStrictModeContext(true);
4534+
}
4535+
4536+
var node = <ClassDeclaration>createNode(SyntaxKind.ClassDeclaration, fullStart);
45314537
setModifiers(node, modifiers);
45324538
parseExpected(SyntaxKind.ClassKeyword);
45334539
node.name = node.flags & NodeFlags.Default ? parseOptionalIdentifier() : parseIdentifier();
@@ -4547,7 +4553,10 @@ module ts {
45474553
else {
45484554
node.members = createMissingList<ClassElement>();
45494555
}
4550-
return finishNode(node);
4556+
4557+
var finishedNode = finishNode(node);
4558+
setStrictModeContext(savedStrictModeContext);
4559+
return finishedNode;
45514560
}
45524561

45534562
function parseHeritageClauses(isClassHeritageClause: boolean): NodeArray<HeritageClause> {
@@ -4775,7 +4784,7 @@ module ts {
47754784
// walker.
47764785
let result = parseExpression();
47774786
// Ensure the string being required is in our 'identifier' table. This will ensure
4778-
// that features like 'find refs' will look inside this file when search for its name.
4787+
// that features like 'find refs' will look inside this file when search for its name.
47794788
if (result.kind === SyntaxKind.StringLiteral) {
47804789
internIdentifier((<LiteralExpression>result).text);
47814790
}

tests/baselines/reference/callWithSpreadES6.js

+14-23
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ var c = new C(1, 2, ...a);
5555

5656

5757
//// [callWithSpreadES6.js]
58-
var __extends = this.__extends || function (d, b) {
59-
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
60-
function __() { this.constructor = d; }
61-
__.prototype = b.prototype;
62-
d.prototype = new __();
63-
};
6458
function foo(x, y, ...z) {
6559
}
6660
var a;
@@ -84,26 +78,23 @@ xa[1].foo(...[
8478
2,
8579
"abc"
8680
]);
87-
var C = (function () {
88-
function C(x, y, ...z) {
81+
class C {
82+
constructor(x, y, ...z) {
8983
this.foo(x, y);
9084
this.foo(x, y, ...z);
9185
}
92-
C.prototype.foo = function (x, y, ...z) {
93-
};
94-
return C;
95-
})();
96-
var D = (function (_super) {
97-
__extends(D, _super);
98-
function D() {
99-
_super.call(this, 1, 2);
100-
_super.call(this, 1, 2, ...a);
86+
foo(x, y, ...z) {
10187
}
102-
D.prototype.foo = function () {
103-
_super.prototype.foo.call(this, 1, 2);
104-
_super.prototype.foo.call(this, 1, 2, ...a);
105-
};
106-
return D;
107-
})(C);
88+
}
89+
class D extends C {
90+
constructor() {
91+
super(1, 2);
92+
super(1, 2, ...a);
93+
}
94+
foo() {
95+
super.foo(1, 2);
96+
super.foo(1, 2, ...a);
97+
}
98+
}
10899
// Only supported in when target is ES6
109100
var c = new C(1, 2, ...a);

tests/baselines/reference/computedPropertyNames12_ES6.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ class C {
2020
var s;
2121
var n;
2222
var a;
23-
var C = (function () {
24-
function C() {
23+
class C {
24+
constructor() {
2525
this[n] = n;
2626
this[s + n] = 2;
2727
this[`hello bye`] = 0;
2828
}
29-
C[`hello ${a} bye`] = 0;
30-
return C;
31-
})();
29+
}
30+
C[`hello ${a} bye`] = 0;

tests/baselines/reference/computedPropertyNames13_ES6.js

+24-27
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,27 @@ class C {
2020
var s;
2121
var n;
2222
var a;
23-
var C = (function () {
24-
function C() {
25-
}
26-
C.prototype[s] = function () {
27-
};
28-
C.prototype[n] = function () {
29-
};
30-
C[s + s] = function () {
31-
};
32-
C.prototype[s + n] = function () {
33-
};
34-
C.prototype[+s] = function () {
35-
};
36-
C[""] = function () {
37-
};
38-
C.prototype[0] = function () {
39-
};
40-
C.prototype[a] = function () {
41-
};
42-
C[true] = function () {
43-
};
44-
C.prototype[`hello bye`] = function () {
45-
};
46-
C[`hello ${a} bye`] = function () {
47-
};
48-
return C;
49-
})();
23+
class C {
24+
[s]() {
25+
}
26+
[n]() {
27+
}
28+
static [s + s]() {
29+
}
30+
[s + n]() {
31+
}
32+
[+s]() {
33+
}
34+
static [""]() {
35+
}
36+
[0]() {
37+
}
38+
[a]() {
39+
}
40+
static [true]() {
41+
}
42+
[`hello bye`]() {
43+
}
44+
static [`hello ${a} bye`]() {
45+
}
46+
}

tests/baselines/reference/computedPropertyNames14_ES6.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,17 @@ class C {
1111

1212
//// [computedPropertyNames14_ES6.js]
1313
var b;
14-
var C = (function () {
15-
function C() {
14+
class C {
15+
[b]() {
1616
}
17-
C.prototype[b] = function () {
18-
};
19-
C[true] = function () {
20-
};
21-
C.prototype[[]] = function () {
22-
};
23-
C[{}] = function () {
24-
};
25-
C.prototype[undefined] = function () {
26-
};
27-
C[null] = function () {
28-
};
29-
return C;
30-
})();
17+
static [true]() {
18+
}
19+
[[]]() {
20+
}
21+
static [{}]() {
22+
}
23+
[undefined]() {
24+
}
25+
static [null]() {
26+
}
27+
}

tests/baselines/reference/computedPropertyNames15_ES6.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ class C {
1212
var p1;
1313
var p2;
1414
var p3;
15-
var C = (function () {
16-
function C() {
15+
class C {
16+
[p1]() {
1717
}
18-
C.prototype[p1] = function () {
19-
};
20-
C.prototype[p2] = function () {
21-
};
22-
C.prototype[p3] = function () {
23-
};
24-
return C;
25-
})();
18+
[p2]() {
19+
}
20+
[p3]() {
21+
}
22+
}

tests/baselines/reference/computedPropertyNames16_ES6.js

+29-76
Original file line numberDiff line numberDiff line change
@@ -20,80 +20,33 @@ class C {
2020
var s;
2121
var n;
2222
var a;
23-
var C = (function () {
24-
function C() {
23+
class C {
24+
get [s]() {
25+
return 0;
2526
}
26-
Object.defineProperty(C.prototype, s, {
27-
get: function () {
28-
return 0;
29-
},
30-
enumerable: true,
31-
configurable: true
32-
});
33-
Object.defineProperty(C.prototype, n, {
34-
set: function (v) {
35-
},
36-
enumerable: true,
37-
configurable: true
38-
});
39-
Object.defineProperty(C, s + s, {
40-
get: function () {
41-
return 0;
42-
},
43-
enumerable: true,
44-
configurable: true
45-
});
46-
Object.defineProperty(C.prototype, s + n, {
47-
set: function (v) {
48-
},
49-
enumerable: true,
50-
configurable: true
51-
});
52-
Object.defineProperty(C.prototype, +s, {
53-
get: function () {
54-
return 0;
55-
},
56-
enumerable: true,
57-
configurable: true
58-
});
59-
Object.defineProperty(C, "", {
60-
set: function (v) {
61-
},
62-
enumerable: true,
63-
configurable: true
64-
});
65-
Object.defineProperty(C.prototype, 0, {
66-
get: function () {
67-
return 0;
68-
},
69-
enumerable: true,
70-
configurable: true
71-
});
72-
Object.defineProperty(C.prototype, a, {
73-
set: function (v) {
74-
},
75-
enumerable: true,
76-
configurable: true
77-
});
78-
Object.defineProperty(C, true, {
79-
get: function () {
80-
return 0;
81-
},
82-
enumerable: true,
83-
configurable: true
84-
});
85-
Object.defineProperty(C.prototype, `hello bye`, {
86-
set: function (v) {
87-
},
88-
enumerable: true,
89-
configurable: true
90-
});
91-
Object.defineProperty(C.prototype, `hello ${a} bye`, {
92-
get: function () {
93-
return 0;
94-
},
95-
enumerable: true,
96-
configurable: true
97-
});
98-
return C;
99-
})();
27+
set [n](v) {
28+
}
29+
static get [s + s]() {
30+
return 0;
31+
}
32+
set [s + n](v) {
33+
}
34+
get [+s]() {
35+
return 0;
36+
}
37+
static set [""](v) {
38+
}
39+
get [0]() {
40+
return 0;
41+
}
42+
set [a](v) {
43+
}
44+
static get [true]() {
45+
return 0;
46+
}
47+
set [`hello bye`](v) {
48+
}
49+
get [`hello ${a} bye`]() {
50+
return 0;
51+
}
52+
}

0 commit comments

Comments
 (0)