Skip to content

Commit e936e77

Browse files
authored
Merge pull request #12867 from Microsoft/mergeMaster1212-2
Merge master 12/12
2 parents 2f6ff9b + 5adea53 commit e936e77

23 files changed

+546
-59
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18100,7 +18100,7 @@ namespace ts {
1810018100

1810118101
const baseTypeNode = getClassExtendsHeritageClauseElement(node);
1810218102
if (baseTypeNode) {
18103-
if (languageVersion < ScriptTarget.ES2015) {
18103+
if (languageVersion < ScriptTarget.ES2015 && !isInAmbientContext(node)) {
1810418104
checkExternalEmitHelpers(baseTypeNode.parent, ExternalEmitHelpers.Extends);
1810518105
}
1810618106

src/compiler/parser.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -6337,7 +6337,7 @@ namespace ts {
63376337
break;
63386338
case SyntaxKind.AsteriskToken:
63396339
const asterisk = scanner.getTokenText();
6340-
if (state === JSDocState.SawAsterisk) {
6340+
if (state === JSDocState.SawAsterisk || state === JSDocState.SavingComments) {
63416341
// If we've already seen an asterisk, then we can no longer parse a tag on this line
63426342
state = JSDocState.SavingComments;
63436343
pushComment(asterisk);
@@ -6358,14 +6358,19 @@ namespace ts {
63586358
case SyntaxKind.WhitespaceTrivia:
63596359
// only collect whitespace if we're already saving comments or have just crossed the comment indent margin
63606360
const whitespace = scanner.getTokenText();
6361-
if (state === JSDocState.SavingComments || margin !== undefined && indent + whitespace.length > margin) {
6361+
if (state === JSDocState.SavingComments) {
6362+
comments.push(whitespace);
6363+
}
6364+
else if (margin !== undefined && indent + whitespace.length > margin) {
63626365
comments.push(whitespace.slice(margin - indent - 1));
63636366
}
63646367
indent += whitespace.length;
63656368
break;
63666369
case SyntaxKind.EndOfFileToken:
63676370
break;
63686371
default:
6372+
// anything other than whitespace or asterisk at the beginning of the line starts the comment text
6373+
state = JSDocState.SavingComments;
63696374
pushComment(scanner.getTokenText());
63706375
break;
63716376
}

src/compiler/transformers/ts.ts

+33-19
Original file line numberDiff line numberDiff line change
@@ -1277,15 +1277,15 @@ namespace ts {
12771277
* @param node The declaration node.
12781278
* @param allDecorators An object containing all of the decorators for the declaration.
12791279
*/
1280-
function transformAllDecoratorsOfDeclaration(node: Declaration, allDecorators: AllDecorators) {
1280+
function transformAllDecoratorsOfDeclaration(node: Declaration, container: ClassLikeDeclaration, allDecorators: AllDecorators) {
12811281
if (!allDecorators) {
12821282
return undefined;
12831283
}
12841284

12851285
const decoratorExpressions: Expression[] = [];
12861286
addRange(decoratorExpressions, map(allDecorators.decorators, transformDecorator));
12871287
addRange(decoratorExpressions, flatMap(allDecorators.parameters, transformDecoratorsOfParameter));
1288-
addTypeMetadata(node, decoratorExpressions);
1288+
addTypeMetadata(node, container, decoratorExpressions);
12891289
return decoratorExpressions;
12901290
}
12911291

@@ -1334,7 +1334,7 @@ namespace ts {
13341334
*/
13351335
function generateClassElementDecorationExpression(node: ClassExpression | ClassDeclaration, member: ClassElement) {
13361336
const allDecorators = getAllDecoratorsOfClassElement(node, member);
1337-
const decoratorExpressions = transformAllDecoratorsOfDeclaration(member, allDecorators);
1337+
const decoratorExpressions = transformAllDecoratorsOfDeclaration(member, node, allDecorators);
13381338
if (!decoratorExpressions) {
13391339
return undefined;
13401340
}
@@ -1415,7 +1415,7 @@ namespace ts {
14151415
*/
14161416
function generateConstructorDecorationExpression(node: ClassExpression | ClassDeclaration) {
14171417
const allDecorators = getAllDecoratorsOfConstructor(node);
1418-
const decoratorExpressions = transformAllDecoratorsOfDeclaration(node, allDecorators);
1418+
const decoratorExpressions = transformAllDecoratorsOfDeclaration(node, node, allDecorators);
14191419
if (!decoratorExpressions) {
14201420
return undefined;
14211421
}
@@ -1468,37 +1468,37 @@ namespace ts {
14681468
* @param node The declaration node.
14691469
* @param decoratorExpressions The destination array to which to add new decorator expressions.
14701470
*/
1471-
function addTypeMetadata(node: Declaration, decoratorExpressions: Expression[]) {
1471+
function addTypeMetadata(node: Declaration, container: ClassLikeDeclaration, decoratorExpressions: Expression[]) {
14721472
if (USE_NEW_TYPE_METADATA_FORMAT) {
1473-
addNewTypeMetadata(node, decoratorExpressions);
1473+
addNewTypeMetadata(node, container, decoratorExpressions);
14741474
}
14751475
else {
1476-
addOldTypeMetadata(node, decoratorExpressions);
1476+
addOldTypeMetadata(node, container, decoratorExpressions);
14771477
}
14781478
}
14791479

1480-
function addOldTypeMetadata(node: Declaration, decoratorExpressions: Expression[]) {
1480+
function addOldTypeMetadata(node: Declaration, container: ClassLikeDeclaration, decoratorExpressions: Expression[]) {
14811481
if (compilerOptions.emitDecoratorMetadata) {
14821482
if (shouldAddTypeMetadata(node)) {
14831483
decoratorExpressions.push(createMetadataHelper(context, "design:type", serializeTypeOfNode(node)));
14841484
}
14851485
if (shouldAddParamTypesMetadata(node)) {
1486-
decoratorExpressions.push(createMetadataHelper(context, "design:paramtypes", serializeParameterTypesOfNode(node)));
1486+
decoratorExpressions.push(createMetadataHelper(context, "design:paramtypes", serializeParameterTypesOfNode(node, container)));
14871487
}
14881488
if (shouldAddReturnTypeMetadata(node)) {
14891489
decoratorExpressions.push(createMetadataHelper(context, "design:returntype", serializeReturnTypeOfNode(node)));
14901490
}
14911491
}
14921492
}
14931493

1494-
function addNewTypeMetadata(node: Declaration, decoratorExpressions: Expression[]) {
1494+
function addNewTypeMetadata(node: Declaration, container: ClassLikeDeclaration, decoratorExpressions: Expression[]) {
14951495
if (compilerOptions.emitDecoratorMetadata) {
14961496
let properties: ObjectLiteralElementLike[];
14971497
if (shouldAddTypeMetadata(node)) {
14981498
(properties || (properties = [])).push(createPropertyAssignment("type", createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, createToken(SyntaxKind.EqualsGreaterThanToken), serializeTypeOfNode(node))));
14991499
}
15001500
if (shouldAddParamTypesMetadata(node)) {
1501-
(properties || (properties = [])).push(createPropertyAssignment("paramTypes", createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, createToken(SyntaxKind.EqualsGreaterThanToken), serializeParameterTypesOfNode(node))));
1501+
(properties || (properties = [])).push(createPropertyAssignment("paramTypes", createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, createToken(SyntaxKind.EqualsGreaterThanToken), serializeParameterTypesOfNode(node, container))));
15021502
}
15031503
if (shouldAddReturnTypeMetadata(node)) {
15041504
(properties || (properties = [])).push(createPropertyAssignment("returnType", createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, createToken(SyntaxKind.EqualsGreaterThanToken), serializeReturnTypeOfNode(node))));
@@ -1543,12 +1543,16 @@ namespace ts {
15431543
* @param node The node to test.
15441544
*/
15451545
function shouldAddParamTypesMetadata(node: Declaration): boolean {
1546-
const kind = node.kind;
1547-
return kind === SyntaxKind.ClassDeclaration
1548-
|| kind === SyntaxKind.ClassExpression
1549-
|| kind === SyntaxKind.MethodDeclaration
1550-
|| kind === SyntaxKind.GetAccessor
1551-
|| kind === SyntaxKind.SetAccessor;
1546+
switch (node.kind) {
1547+
case SyntaxKind.ClassDeclaration:
1548+
case SyntaxKind.ClassExpression:
1549+
return getFirstConstructorWithBody(<ClassLikeDeclaration>node) !== undefined;
1550+
case SyntaxKind.MethodDeclaration:
1551+
case SyntaxKind.GetAccessor:
1552+
case SyntaxKind.SetAccessor:
1553+
return true;
1554+
}
1555+
return false;
15521556
}
15531557

15541558
/**
@@ -1596,7 +1600,7 @@ namespace ts {
15961600
*
15971601
* @param node The node that should have its parameter types serialized.
15981602
*/
1599-
function serializeParameterTypesOfNode(node: Node): Expression {
1603+
function serializeParameterTypesOfNode(node: Node, container: ClassLikeDeclaration): Expression {
16001604
const valueDeclaration =
16011605
isClassLike(node)
16021606
? getFirstConstructorWithBody(node)
@@ -1606,7 +1610,7 @@ namespace ts {
16061610

16071611
const expressions: Expression[] = [];
16081612
if (valueDeclaration) {
1609-
const parameters = valueDeclaration.parameters;
1613+
const parameters = getParametersOfDecoratedDeclaration(valueDeclaration, container);
16101614
const numParameters = parameters.length;
16111615
for (let i = 0; i < numParameters; i++) {
16121616
const parameter = parameters[i];
@@ -1625,6 +1629,16 @@ namespace ts {
16251629
return createArrayLiteral(expressions);
16261630
}
16271631

1632+
function getParametersOfDecoratedDeclaration(node: FunctionLikeDeclaration, container: ClassLikeDeclaration) {
1633+
if (container && node.kind === SyntaxKind.GetAccessor) {
1634+
const { setAccessor } = getAllAccessorDeclarations(container.members, <AccessorDeclaration>node);
1635+
if (setAccessor) {
1636+
return setAccessor.parameters;
1637+
}
1638+
}
1639+
return node.parameters;
1640+
}
1641+
16281642
/**
16291643
* Serializes the return type of a node for use with decorator type metadata.
16301644
*

tests/baselines/reference/decoratedClassExportsCommonJS1.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
1414
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1515
return c > 3 && r && Object.defineProperty(target, key, r), r;
1616
};
17-
var __metadata = (this && this.__metadata) || function (k, v) {
18-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
19-
};
2017
let Testing123 = Testing123_1 = class Testing123 {
2118
};
2219
Testing123.prop1 = Testing123_1.prop0;
2320
Testing123 = Testing123_1 = __decorate([
24-
Something({ v: () => Testing123_1 }),
25-
__metadata("design:paramtypes", [])
21+
Something({ v: () => Testing123_1 })
2622
], Testing123);
2723
exports.Testing123 = Testing123;
2824
var Testing123_1;

tests/baselines/reference/decoratedClassExportsCommonJS2.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
1313
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1414
return c > 3 && r && Object.defineProperty(target, key, r), r;
1515
};
16-
var __metadata = (this && this.__metadata) || function (k, v) {
17-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
18-
};
1916
let Testing123 = Testing123_1 = class Testing123 {
2017
};
2118
Testing123 = Testing123_1 = __decorate([
22-
Something({ v: () => Testing123_1 }),
23-
__metadata("design:paramtypes", [])
19+
Something({ v: () => Testing123_1 })
2420
], Testing123);
2521
exports.Testing123 = Testing123;
2622
var Testing123_1;

tests/baselines/reference/decoratedClassExportsSystem1.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ System.register([], function (exports_1, context_1) {
1717
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1818
return c > 3 && r && Object.defineProperty(target, key, r), r;
1919
};
20-
var __metadata = (this && this.__metadata) || function (k, v) {
21-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
22-
};
2320
var __moduleName = context_1 && context_1.id;
2421
var Testing123, Testing123_1;
2522
return {
@@ -29,8 +26,7 @@ System.register([], function (exports_1, context_1) {
2926
};
3027
Testing123.prop1 = Testing123_1.prop0;
3128
Testing123 = Testing123_1 = __decorate([
32-
Something({ v: () => Testing123_1 }),
33-
__metadata("design:paramtypes", [])
29+
Something({ v: () => Testing123_1 })
3430
], Testing123);
3531
exports_1("Testing123", Testing123);
3632
}

tests/baselines/reference/decoratedClassExportsSystem2.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ System.register([], function (exports_1, context_1) {
1414
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1515
return c > 3 && r && Object.defineProperty(target, key, r), r;
1616
};
17-
var __metadata = (this && this.__metadata) || function (k, v) {
18-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
19-
};
2017
var __moduleName = context_1 && context_1.id;
2118
var Testing123, Testing123_1;
2219
return {
@@ -25,8 +22,7 @@ System.register([], function (exports_1, context_1) {
2522
Testing123 = Testing123_1 = class Testing123 {
2623
};
2724
Testing123 = Testing123_1 = __decorate([
28-
Something({ v: () => Testing123_1 }),
29-
__metadata("design:paramtypes", [])
25+
Something({ v: () => Testing123_1 })
3026
], Testing123);
3127
exports_1("Testing123", Testing123);
3228
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//// [decoratorOnClassAccessor8.ts]
2+
declare function dec<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
3+
4+
class A {
5+
@dec get x() { return 0; }
6+
set x(value: number) { }
7+
}
8+
9+
class B {
10+
get x() { return 0; }
11+
@dec set x(value: number) { }
12+
}
13+
14+
class C {
15+
@dec set x(value: number) { }
16+
get x() { return 0; }
17+
}
18+
19+
class D {
20+
set x(value: number) { }
21+
@dec get x() { return 0; }
22+
}
23+
24+
class E {
25+
@dec get x() { return 0; }
26+
}
27+
28+
class F {
29+
@dec set x(value: number) { }
30+
}
31+
32+
//// [decoratorOnClassAccessor8.js]
33+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
34+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
35+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
36+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
37+
return c > 3 && r && Object.defineProperty(target, key, r), r;
38+
};
39+
var __metadata = (this && this.__metadata) || function (k, v) {
40+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
41+
};
42+
var A = (function () {
43+
function A() {
44+
}
45+
Object.defineProperty(A.prototype, "x", {
46+
get: function () { return 0; },
47+
set: function (value) { },
48+
enumerable: true,
49+
configurable: true
50+
});
51+
return A;
52+
}());
53+
__decorate([
54+
dec,
55+
__metadata("design:type", Object),
56+
__metadata("design:paramtypes", [Number])
57+
], A.prototype, "x", null);
58+
var B = (function () {
59+
function B() {
60+
}
61+
Object.defineProperty(B.prototype, "x", {
62+
get: function () { return 0; },
63+
set: function (value) { },
64+
enumerable: true,
65+
configurable: true
66+
});
67+
return B;
68+
}());
69+
__decorate([
70+
dec,
71+
__metadata("design:type", Number),
72+
__metadata("design:paramtypes", [Number])
73+
], B.prototype, "x", null);
74+
var C = (function () {
75+
function C() {
76+
}
77+
Object.defineProperty(C.prototype, "x", {
78+
get: function () { return 0; },
79+
set: function (value) { },
80+
enumerable: true,
81+
configurable: true
82+
});
83+
return C;
84+
}());
85+
__decorate([
86+
dec,
87+
__metadata("design:type", Number),
88+
__metadata("design:paramtypes", [Number])
89+
], C.prototype, "x", null);
90+
var D = (function () {
91+
function D() {
92+
}
93+
Object.defineProperty(D.prototype, "x", {
94+
get: function () { return 0; },
95+
set: function (value) { },
96+
enumerable: true,
97+
configurable: true
98+
});
99+
return D;
100+
}());
101+
__decorate([
102+
dec,
103+
__metadata("design:type", Object),
104+
__metadata("design:paramtypes", [Number])
105+
], D.prototype, "x", null);
106+
var E = (function () {
107+
function E() {
108+
}
109+
Object.defineProperty(E.prototype, "x", {
110+
get: function () { return 0; },
111+
enumerable: true,
112+
configurable: true
113+
});
114+
return E;
115+
}());
116+
__decorate([
117+
dec,
118+
__metadata("design:type", Object),
119+
__metadata("design:paramtypes", [])
120+
], E.prototype, "x", null);
121+
var F = (function () {
122+
function F() {
123+
}
124+
Object.defineProperty(F.prototype, "x", {
125+
set: function (value) { },
126+
enumerable: true,
127+
configurable: true
128+
});
129+
return F;
130+
}());
131+
__decorate([
132+
dec,
133+
__metadata("design:type", Number),
134+
__metadata("design:paramtypes", [Number])
135+
], F.prototype, "x", null);

0 commit comments

Comments
 (0)