Exemple #1
0
        public static async Task <Document> EnumToClass(Document document, EnumDeclarationSyntax enumNode, CancellationToken cancellationToken)
        {
            var funicularGeneratorsReferenced = document.FunicularGeneratorsReferenced();

            var enumName      = enumNode.QualifiedName();
            var caseTypeNames = enumNode.Members.Select(m => m.Identifier.Text);

            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var unionType = enumName.Name == WrapEnumToClass.DefaultNestedEnumTypeName
                ? (ClassDeclarationSyntax)enumNode.Parent
                : Option.None <ClassDeclarationSyntax>();
            var unionTypeName = unionType.Match(u => u.QualifiedName(), () => enumNode.QualifiedName());

            root = root.GenerateEnumClass(enumName, unionType, funicularGeneratorsReferenced);

            var extensionClassName = $"{unionTypeName.QualifiedName("")}Extension";

            if (!funicularGeneratorsReferenced)
            {
                root = AddMatchExtensions(enumNode, root, extensionClassName, unionType, unionTypeName, caseTypeNames);
            }

            document = document.WithSyntaxRoot(root);
            document = await Formatter.FormatAsync(document, cancellationToken : cancellationToken);

            return(document);
        }
Exemple #2
0
            public override SyntaxNode VisitEnumDeclaration(EnumDeclarationSyntax node)
            {
                var nodeName = node.QualifiedName();

                if (nodeName != m_EnumTypeName)
                {
                    return(base.VisitEnumDeclaration(node));
                }

                var classDeclaration = ClassDeclaration(m_EnumTypeName.Name)
                                       .WithModifiers(node.Modifiers)
                                       .Abstract()
                                       .AddMembers(node
                                                   .WithIdentifier(ParseToken(m_NestedEnumTypeName))
                                                   .Internal())
                                       .AddProperty(m_NestedEnumTypeName, m_EnumPropertyName, p => p.Internal())
                                       .AddMembers(ConstructorDeclaration(node.Identifier.WithoutTrivia())
                                                   .AddParameterListParameters(Parameter(ParseToken(m_EnumPropertyName.ToParameterName()))
                                                                               .WithType(ParseTypeName(m_NestedEnumTypeName)))
                                                   .WithExpressionBody($"{m_EnumPropertyName} = {m_EnumPropertyName.ToParameterName()}")
                                                   )
                                       .AddMembers(MethodDeclaration(ParseTypeName("string"), "ToString")
                                                   .Public()
                                                   .Override()
                                                   .WithExpressionBody($"Enum.GetName(typeof({m_NestedEnumTypeName}), {m_EnumPropertyName}) ?? {DefaultEnumPropertyName}.ToString()")
                                                   )
                                       .AddMembers(MethodDeclaration(ParseTypeName("bool"), "Equals")
                                                   .AddParameterListParameters(Parameter(ParseToken("other")).WithType(ParseTypeName(m_EnumTypeName.Name)))
                                                   .WithExpressionBody($"{m_EnumPropertyName} == other.{m_EnumPropertyName}"))
                                       .AddMembers(MethodDeclaration(ParseTypeName("bool"), "Equals")
                                                   .Public()
                                                   .Override()
                                                   .AddParameterListParameters(Parameter(ParseToken("obj")).WithType(CurrentCompilationOptions.Nullability ? ParseTypeName("object?") : ParseTypeName("object")))
                                                   .WithBody(Block(
                                                                 ParseStatement("if (ReferenceEquals(null, obj)) return false;").WithTrailingTrivia(CarriageReturnLineFeed),
                                                                 ParseStatement("if (ReferenceEquals(this, obj)) return true;").WithTrailingTrivia(CarriageReturnLineFeed),
                                                                 ParseStatement("if (obj.GetType() != GetType()) return false;").WithTrailingTrivia(CarriageReturnLineFeed),
                                                                 ParseStatement($"return Equals(({m_EnumTypeName.Name}) obj);")
                                                                 )))
                                       .AddMembers(MethodDeclaration(ParseTypeName("int"), "GetHashCode")
                                                   .Public()
                                                   .Override()
                                                   .WithParameterList(ParameterList())
                                                   .WithExpressionBody($"(int) {m_EnumPropertyName}")
                                                   );

                return(classDeclaration);
            }