From bf383b5b54a44dc4bd3e3670f4d5f78e26c005d9 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 23 Mar 2015 12:08:15 -0700 Subject: [PATCH] Simplified check for decorators. --- src/compiler/checker.ts | 26 +++++++------------ .../diagnosticInformationMap.generated.ts | 2 -- src/compiler/diagnosticMessages.json | 8 ------ 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c6f8af968b1..9321ad41db2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8518,14 +8518,6 @@ module ts { } } - function checkDecoratorSignature(node: Decorator, exprType: Type, expectedErasedDecoratorType: Type, parentType?: Type, expectedGenericDecoratorType?: Type, message?: DiagnosticMessage) { - // first validate that we are using the correct decorator signature for the declaration - if (checkTypeAssignableTo(exprType, expectedErasedDecoratorType, node) && parentType && expectedGenericDecoratorType && message) { - // next validate that we are not changing the static type in the decorator to a type that is not assignable. - checkTypeAssignableTo(exprType, instantiateSingleCallFunctionType(expectedGenericDecoratorType, [parentType]), node, message); - } - } - /** Check a decorator */ function checkDecorator(node: Decorator): void { let expression: Expression = node.expression; @@ -8534,8 +8526,9 @@ module ts { switch (node.parent.kind) { case SyntaxKind.ClassDeclaration: let classSymbol = getSymbolOfNode(node.parent); - let classType = getTypeOfSymbol(classSymbol); - checkDecoratorSignature(node, exprType, globalClassDecoratorErasedType, classType, globalClassDecoratorType, Diagnostics.A_decorator_may_not_change_the_type_of_a_class); + let classConstructorType = getTypeOfSymbol(classSymbol); + let classDecoratorType = instantiateSingleCallFunctionType(globalClassDecoratorType, [classConstructorType]); + checkTypeAssignableTo(exprType, classDecoratorType, node); break; case SyntaxKind.PropertyDeclaration: @@ -8543,11 +8536,12 @@ module ts { case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: let propertyType = getTypeOfNode(node.parent); - checkDecoratorSignature(node, exprType, globalPropertyDecoratorErasedType, propertyType, globalPropertyDecoratorType, Diagnostics.A_decorator_may_not_change_the_type_of_a_member); + let propertyDecoratorType = instantiateSingleCallFunctionType(globalPropertyDecoratorType, [propertyType]); + checkTypeAssignableTo(exprType, propertyDecoratorType, node); break; case SyntaxKind.Parameter: - checkDecoratorSignature(node, exprType, globalParameterDecoratorType); + checkTypeAssignableTo(exprType, globalParameterDecoratorType, node); break; } } @@ -11430,12 +11424,12 @@ module ts { if (!node.decorators) { return false; } - if (languageVersion < ScriptTarget.ES5) { - return grammarErrorOnNode(node, Diagnostics.Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher); - } - else if (!isValidDecoratorTarget(node)) { + if (!isValidDecoratorTarget(node)) { return grammarErrorOnNode(node, Diagnostics.Decorators_are_not_valid_here); } + else if (languageVersion < ScriptTarget.ES5) { + return grammarErrorOnNode(node, Diagnostics.Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher); + } return false; } diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 839faf27b8d..883c5c4ab75 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -164,8 +164,6 @@ module ts { 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." }, 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." }, Decorators_are_not_valid_here: { code: 1206, category: DiagnosticCategory.Error, key: "Decorators are not valid here." }, - 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." }, - 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." }, Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, 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." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index c1c5152c7e4..2b40594c887 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -647,14 +647,6 @@ "category": "Error", "code": 1206 }, - "A decorator may not change the type of a member.": { - "category": "Error", - "code": 1208 - }, - "A decorator may not change the type of a class.": { - "category": "Error", - "code": 1209 - }, "Duplicate identifier '{0}'.": { "category": "Error",