Skip to content

Commit bf383b5

Browse files
committed
Simplified check for decorators.
1 parent 1b8933c commit bf383b5

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

src/compiler/checker.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -8518,14 +8518,6 @@ module ts {
85188518
}
85198519
}
85208520

8521-
function checkDecoratorSignature(node: Decorator, exprType: Type, expectedErasedDecoratorType: Type, parentType?: Type, expectedGenericDecoratorType?: Type, message?: DiagnosticMessage) {
8522-
// first validate that we are using the correct decorator signature for the declaration
8523-
if (checkTypeAssignableTo(exprType, expectedErasedDecoratorType, node) && parentType && expectedGenericDecoratorType && message) {
8524-
// next validate that we are not changing the static type in the decorator to a type that is not assignable.
8525-
checkTypeAssignableTo(exprType, instantiateSingleCallFunctionType(expectedGenericDecoratorType, [parentType]), node, message);
8526-
}
8527-
}
8528-
85298521
/** Check a decorator */
85308522
function checkDecorator(node: Decorator): void {
85318523
let expression: Expression = node.expression;
@@ -8534,20 +8526,22 @@ module ts {
85348526
switch (node.parent.kind) {
85358527
case SyntaxKind.ClassDeclaration:
85368528
let classSymbol = getSymbolOfNode(node.parent);
8537-
let classType = getTypeOfSymbol(classSymbol);
8538-
checkDecoratorSignature(node, exprType, globalClassDecoratorErasedType, classType, globalClassDecoratorType, Diagnostics.A_decorator_may_not_change_the_type_of_a_class);
8529+
let classConstructorType = getTypeOfSymbol(classSymbol);
8530+
let classDecoratorType = instantiateSingleCallFunctionType(globalClassDecoratorType, [classConstructorType]);
8531+
checkTypeAssignableTo(exprType, classDecoratorType, node);
85398532
break;
85408533

85418534
case SyntaxKind.PropertyDeclaration:
85428535
case SyntaxKind.MethodDeclaration:
85438536
case SyntaxKind.GetAccessor:
85448537
case SyntaxKind.SetAccessor:
85458538
let propertyType = getTypeOfNode(node.parent);
8546-
checkDecoratorSignature(node, exprType, globalPropertyDecoratorErasedType, propertyType, globalPropertyDecoratorType, Diagnostics.A_decorator_may_not_change_the_type_of_a_member);
8539+
let propertyDecoratorType = instantiateSingleCallFunctionType(globalPropertyDecoratorType, [propertyType]);
8540+
checkTypeAssignableTo(exprType, propertyDecoratorType, node);
85478541
break;
85488542

85498543
case SyntaxKind.Parameter:
8550-
checkDecoratorSignature(node, exprType, globalParameterDecoratorType);
8544+
checkTypeAssignableTo(exprType, globalParameterDecoratorType, node);
85518545
break;
85528546
}
85538547
}
@@ -11430,12 +11424,12 @@ module ts {
1143011424
if (!node.decorators) {
1143111425
return false;
1143211426
}
11433-
if (languageVersion < ScriptTarget.ES5) {
11434-
return grammarErrorOnNode(node, Diagnostics.Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher);
11435-
}
11436-
else if (!isValidDecoratorTarget(node)) {
11427+
if (!isValidDecoratorTarget(node)) {
1143711428
return grammarErrorOnNode(node, Diagnostics.Decorators_are_not_valid_here);
1143811429
}
11430+
else if (languageVersion < ScriptTarget.ES5) {
11431+
return grammarErrorOnNode(node, Diagnostics.Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher);
11432+
}
1143911433
return false;
1144011434
}
1144111435

src/compiler/diagnosticInformationMap.generated.ts

-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ module ts {
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." },
165165
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." },
166166
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." },
169167
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
170168
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." },
171169
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

-8
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,6 @@
647647
"category": "Error",
648648
"code": 1206
649649
},
650-
"A decorator may not change the type of a member.": {
651-
"category": "Error",
652-
"code": 1208
653-
},
654-
"A decorator may not change the type of a class.": {
655-
"category": "Error",
656-
"code": 1209
657-
},
658650

659651
"Duplicate identifier '{0}'.": {
660652
"category": "Error",

0 commit comments

Comments
 (0)