Beispiel #1
0
        private static void CheckAllowedTypeLiteralMember(ITypeElement typeElement, ISemanticModel semanticModel, DiagnosticContext context)
        {
            var propertySignature = typeElement.As <IPropertySignature>();

            // The propery signature name should always be an identifier
            if (propertySignature?.Name.Kind != TypeScript.Net.Types.SyntaxKind.Identifier)
            {
                context.Logger.QualifierLiteralMemberShouldBeAnIdentifier(
                    context.LoggingContext,
                    propertySignature.LocationForLogging(context.SourceFile));
                return;
            }

            // TODO: This requirements should be relaxed when we fully implement default qualifier keys
            if (propertySignature.QuestionToken.HasValue)
            {
                context.Logger.QualifierOptionalMembersAreNotAllowed(
                    context.LoggingContext,
                    propertySignature.LocationForLogging(context.SourceFile));
                return;
            }

            // If the property signature is string, this represents a value wildcard
            // TODO: Uncomment when we start supporting this at runtime
            // if (propertySignature.Type.Kind == TypeScript.Net.Types.SyntaxKind.StringKeyword)
            // {
            //    return true;
            // }

            // A union type is allowed, as long as it is a StringLiteral type
            var type = semanticModel.GetTypeAtLocation(propertySignature.Type);

            if ((type.Flags & TypeFlags.StringLiteral) == TypeFlags.None)
            {
                var unionType = type.As <IUnionType>();
                if (unionType == null || unionType.Types.Any(t => (t.Flags & TypeFlags.StringLiteral) == TypeFlags.None))
                {
                    context.Logger.QualifierLiteralTypeMemberShouldHaveStringLiteralType(
                        context.LoggingContext,
                        propertySignature.LocationForLogging(context.SourceFile));
                    return;
                }
            }
        }