Skip to content

Commit 0fb624a

Browse files
committed
PR feedback
1 parent 9761f4b commit 0fb624a

File tree

50 files changed

+422
-766
lines changed

Some content is hidden

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

50 files changed

+422
-766
lines changed

src/compiler/checker.ts

+47-30
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module ts {
103103
let globalTypedPropertyDescriptorType: ObjectType;
104104
let globalClassDecoratorType: ObjectType;
105105
let globalClassAnnotationType: ObjectType;
106-
let globalParameterAnnotationType: ObjectType;
106+
let globalParameterDecoratorType: ObjectType;
107107
let globalPropertyAnnotationType: ObjectType;
108108
let globalPropertyDecoratorType: ObjectType;
109109

@@ -417,22 +417,23 @@ module ts {
417417
}
418418
break;
419419
case SyntaxKind.Decorator:
420-
if (location.parent) {
420+
// Decorators are resolved at the class declaration as that point where they are evaluated in the emit:
421+
//
422+
// function y() {}
423+
// class C {
424+
// method(@y x, y) {} // <-- All references to decorators should occur at the class declaration
425+
// }
426+
//
427+
let parent = location.parent;
428+
if (parent && parent.kind === SyntaxKind.Parameter) {
429+
parent = parent.parent;
430+
}
431+
if (parent && isClassElement(parent)) {
432+
parent = parent.parent;
433+
}
434+
if (parent) {
421435
lastLocation = location;
422-
grandparent = location.parent.parent;
423-
if (location.parent.kind === SyntaxKind.Parameter) {
424-
// Parameter decorators are resolved in the context of their great-grandparent
425-
if (grandparent) {
426-
location = grandparent.parent;
427-
}
428-
else {
429-
break loop;
430-
}
431-
}
432-
else {
433-
// all other decorators are resolved in the context of their grandparent
434-
location = grandparent;
435-
}
436+
location = parent;
436437
continue;
437438
}
438439
break;
@@ -8121,8 +8122,8 @@ module ts {
81218122
checkFunctionLikeDeclaration(node);
81228123
}
81238124

8124-
function checkIncompleteDeclaration(node: Declaration) {
8125-
error(node, Diagnostics.Declaration_expected);
8125+
function checkMissingDeclaration(node: Node) {
8126+
checkDecorators(node);
81268127
}
81278128

81288129
function checkTypeReference(node: TypeReferenceNode) {
@@ -8538,21 +8539,21 @@ module ts {
85388539

85398540
switch (node.parent.kind) {
85408541
case SyntaxKind.ClassDeclaration:
8541-
let classSymbol = getSymbolOfNode(node.parent);
8542+
let classSymbol = getSymbolOfNode(node.parent);
85428543
let classType = getTypeOfSymbol(classSymbol);
8543-
checkDecoratorSignature(node, exprType, globalClassAnnotationType, classType, globalClassDecoratorType, Diagnostics.Decorators_may_not_change_the_type_of_a_class);
8544+
checkDecoratorSignature(node, exprType, globalClassAnnotationType, classType, globalClassDecoratorType, Diagnostics.A_decorator_may_not_change_the_type_of_a_class);
85448545
break;
85458546

85468547
case SyntaxKind.PropertyDeclaration:
85478548
case SyntaxKind.MethodDeclaration:
85488549
case SyntaxKind.GetAccessor:
85498550
case SyntaxKind.SetAccessor:
85508551
let propertyType = getTypeOfNode(node.parent);
8551-
checkDecoratorSignature(node, exprType, globalPropertyAnnotationType, propertyType, globalPropertyDecoratorType, Diagnostics.Decorators_may_not_change_the_type_of_a_member);
8552+
checkDecoratorSignature(node, exprType, globalPropertyAnnotationType, propertyType, globalPropertyDecoratorType, Diagnostics.A_decorator_may_not_change_the_type_of_a_member);
85528553
break;
85538554

85548555
case SyntaxKind.Parameter:
8555-
checkDecoratorSignature(node, exprType, globalParameterAnnotationType);
8556+
checkDecoratorSignature(node, exprType, globalParameterDecoratorType);
85568557
break;
85578558
}
85588559
}
@@ -8561,7 +8562,7 @@ module ts {
85618562
function checkDecorators(node: Node): void {
85628563
if (!node.decorators) {
85638564
return;
8564-
}
8565+
}
85658566

85668567
switch (node.kind) {
85678568
case SyntaxKind.ClassDeclaration:
@@ -10330,8 +10331,6 @@ module ts {
1033010331
case SyntaxKind.GetAccessor:
1033110332
case SyntaxKind.SetAccessor:
1033210333
return checkAccessorDeclaration(<AccessorDeclaration>node);
10333-
case SyntaxKind.IncompleteDeclaration:
10334-
return checkIncompleteDeclaration(<Declaration>node);
1033510334
case SyntaxKind.TypeReference:
1033610335
return checkTypeReference(<TypeReferenceNode>node);
1033710336
case SyntaxKind.TypeQuery:
@@ -10410,6 +10409,8 @@ module ts {
1041010409
case SyntaxKind.DebuggerStatement:
1041110410
checkGrammarStatementInAmbientContext(node);
1041210411
return;
10412+
case SyntaxKind.MissingDeclaration:
10413+
return checkMissingDeclaration(node);
1041310414
}
1041410415
}
1041510416

@@ -11315,6 +11316,20 @@ module ts {
1131511316
return undefined;
1131611317
}
1131711318

11319+
function getAnnotationTypeForDecoratorType(type: Type, typeArgument: Type): Type {
11320+
if (type === unknownType) {
11321+
return unknownType;
11322+
}
11323+
11324+
let signature = getSingleCallSignature(type);
11325+
if (!signature) {
11326+
return unknownType;
11327+
}
11328+
11329+
let instantiatedSignature = getSignatureInstantiation(signature, [typeArgument]);
11330+
return getOrCreateTypeFromSignature(instantiatedSignature);
11331+
}
11332+
1131811333
function createResolver(): EmitResolver {
1131911334
return {
1132011335
getGeneratedNameForNode,
@@ -11365,10 +11380,10 @@ module ts {
1136511380
globalRegExpType = getGlobalType("RegExp");
1136611381
globalTypedPropertyDescriptorType = getTypeOfGlobalSymbol(getGlobalTypeSymbol("TypedPropertyDescriptor"), 1);
1136711382
globalClassDecoratorType = getGlobalType("ClassDecorator");
11383+
globalClassAnnotationType = getAnnotationTypeForDecoratorType(globalClassDecoratorType, globalFunctionType);
1136811384
globalPropertyDecoratorType = getGlobalType("PropertyDecorator");
11369-
globalClassAnnotationType = getGlobalType("ClassAnnotation");
11370-
globalPropertyAnnotationType = getGlobalType("PropertyAnnotation");
11371-
globalParameterAnnotationType = getGlobalType("ParameterAnnotation");
11385+
globalPropertyAnnotationType = getAnnotationTypeForDecoratorType(globalPropertyDecoratorType, anyType);
11386+
globalParameterDecoratorType = getGlobalType("ParameterDecorator");
1137211387

1137311388
// If we're in ES6 mode, load the TemplateStringsArray.
1137411389
// Otherwise, default to 'unknown' for the purposes of type checking in LS scenarios.
@@ -11402,6 +11417,9 @@ module ts {
1140211417
while (target) {
1140311418
switch (target.kind) {
1140411419
case SyntaxKind.ClassDeclaration:
11420+
if (languageVersion < ScriptTarget.ES5) {
11421+
return grammarErrorOnNode(node, Diagnostics.Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher);
11422+
}
1140511423
return false;
1140611424

1140711425
case SyntaxKind.Constructor:
@@ -11414,7 +11432,6 @@ module ts {
1141411432
target = target.parent;
1141511433
continue;
1141611434

11417-
1141811435
case SyntaxKind.MethodDeclaration:
1141911436
case SyntaxKind.GetAccessor:
1142011437
case SyntaxKind.SetAccessor:
@@ -11426,7 +11443,7 @@ module ts {
1142611443
}
1142711444
break;
1142811445
}
11429-
return grammarErrorOnNode(node, Diagnostics.Decorators_are_not_valid_on_this_declaration_type);
11446+
return grammarErrorOnNode(node, Diagnostics.Decorators_are_not_valid_here);
1143011447
}
1143111448

1143211449
function checkGrammarModifiers(node: Node): boolean {

src/compiler/diagnosticInformationMap.generated.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,10 @@ module ts {
162162
Import_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_or_import_d_from_mod_instead: { code: 1202, category: DiagnosticCategory.Error, key: "Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from \"mod\"', 'import {a} from \"mod\"' or 'import d from \"mod\"' instead." },
163163
Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_export_default_instead: { code: 1203, category: DiagnosticCategory.Error, key: "Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead." },
164164
Cannot_compile_external_modules_into_amd_or_commonjs_when_targeting_es6_or_higher: { code: 1204, category: DiagnosticCategory.Error, key: "Cannot compile external modules into amd or commonjs when targeting es6 or higher." },
165-
Decorators_are_only_supported_on_class_members_when_targeting_ECMAScript_5_or_higher: { code: 1205, category: DiagnosticCategory.Error, key: "Decorators are only supported on class members when targeting ECMAScript 5 or higher." },
166-
Decorators_are_not_valid_on_this_declaration_type: { code: 1206, category: DiagnosticCategory.Error, key: "Decorators are not valid on this declaration type." },
167-
Argument_to_ambient_decorator_must_be_constant_expression: { code: 1207, category: DiagnosticCategory.Error, key: "Argument to ambient decorator must be constant expression." },
168-
Decorators_may_not_change_the_type_of_a_member: { code: 1208, category: DiagnosticCategory.Error, key: "Decorators may not change the type of a member." },
169-
Decorators_may_not_change_the_type_of_a_class: { code: 1209, category: DiagnosticCategory.Error, key: "Decorators may not change the type of a class." },
165+
Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher: { code: 1205, category: DiagnosticCategory.Error, key: "Decorators are only available when targeting ECMAScript 5 and higher." },
166+
Decorators_are_not_valid_here: { code: 1206, category: DiagnosticCategory.Error, key: "Decorators are not valid here." },
167+
A_decorator_may_not_change_the_type_of_a_member: { code: 1208, category: DiagnosticCategory.Error, key: "A decorator may not change the type of a member." },
168+
A_decorator_may_not_change_the_type_of_a_class: { code: 1209, category: DiagnosticCategory.Error, key: "A decorator may not change the type of a class." },
170169
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
171170
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
172171
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -639,23 +639,19 @@
639639
"category": "Error",
640640
"code": 1204
641641
},
642-
"Decorators are only supported on class members when targeting ECMAScript 5 or higher.": {
642+
"Decorators are only available when targeting ECMAScript 5 and higher.": {
643643
"category": "Error",
644644
"code": 1205
645645
},
646-
"Decorators are not valid on this declaration type.": {
646+
"Decorators are not valid here.": {
647647
"category": "Error",
648648
"code": 1206
649649
},
650-
"Argument to ambient decorator must be constant expression.": {
651-
"category": "Error",
652-
"code": 1207
653-
},
654-
"Decorators may not change the type of a member.": {
650+
"A decorator may not change the type of a member.": {
655651
"category": "Error",
656652
"code": 1208
657653
},
658-
"Decorators may not change the type of a class.": {
654+
"A decorator may not change the type of a class.": {
659655
"category": "Error",
660656
"code": 1209
661657
},

src/compiler/emitter.ts

+8
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ module ts {
277277
}
278278
}
279279

280+
function getFirstConstructorWithBody(node: ClassDeclaration): ConstructorDeclaration {
281+
return forEach(node.members, member => {
282+
if (member.kind === SyntaxKind.Constructor && nodeIsPresent((<ConstructorDeclaration>member).body)) {
283+
return <ConstructorDeclaration>member;
284+
}
285+
});
286+
}
287+
280288
function getAllAccessorDeclarations(declarations: NodeArray<Declaration>, accessor: AccessorDeclaration) {
281289
let firstAccessor: AccessorDeclaration;
282290
let lastAccessor: AccessorDeclaration;

0 commit comments

Comments
 (0)