Ejemplo n.º 1
0
 public static FieldDeclarationSyntax AddObsoleteAttribute(this FieldDeclarationSyntax fieldDeclaration, string message = "", bool error = false)
 {
     return(fieldDeclaration.AddAttributeLists(
                AttributeList(
                    Attribute(
                        IdentifierName("Obsolete"),
                        AttributeArgumentList(
                            AttributeArgument(StringLiteralExpression(message)),
                            AttributeArgument(NameColon("error"), BooleanLiteralExpression(error)))))));
 }
Ejemplo n.º 2
0
 public MemberSyntax(FieldDeclarationSyntax field)
 {
     this.AttributeLists       = field.AttributeLists;
     this.GetLeadingTrivia     = () => field.GetLeadingTrivia();
     this.ReplaceNode          = (x, y) => field.ReplaceNode(x, y);
     this.WithoutLeadingTrivia = () => new MemberSyntax(field.WithoutLeadingTrivia());
     this.WithLeadingTrivia    = x => new MemberSyntax(field.WithLeadingTrivia(x));
     this.AddAttributeLists    = xs => new MemberSyntax(field.AddAttributeLists(xs));
     this.Self = field;
 }
Ejemplo n.º 3
0
            public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
            {
                string fullName = SyntaxUtils.GetFullName(node);

                this.GetRemapInfo(fullName, out var listAttributes, node.Declaration.Type.ToString(), out string newType, out string newName);

                // ClangSharp mistakenly emits string[] for WCHAR[] Foo = "Bar".
                // Change it to string
                if (newType == null && node.Declaration.Type.ToString() == "string[]")
                {
                    newType = "string";
                }

                // Turn public static readonly Guids into string constants with an attribute
                // to signal language projections to turn them into Guid constants. Guid constants
                // aren't allowed in metadata, requiring us to surface them this way
                if (node.Modifiers.ToString() == "public static readonly" && node.Declaration.Type.ToString() == "Guid")
                {
                    Guid guidVal        = Guid.Empty;
                    var  varInitializer = node.Declaration.Variables.First().Initializer;
                    if (varInitializer.Value is ObjectCreationExpressionSyntax objCreationSyntax)
                    {
                        var args = objCreationSyntax.ArgumentList.Arguments;
                        if (args.Count == 11)
                        {
                            uint   p0  = EncodeHelpers.ParseHex(args[0].ToString());
                            ushort p1  = (ushort)EncodeHelpers.ParseHex(args[1].ToString());
                            ushort p2  = (ushort)EncodeHelpers.ParseHex(args[2].ToString());
                            byte   p3  = (byte)EncodeHelpers.ParseHex(args[3].ToString());
                            byte   p4  = (byte)EncodeHelpers.ParseHex(args[4].ToString());
                            byte   p5  = (byte)EncodeHelpers.ParseHex(args[5].ToString());
                            byte   p6  = (byte)EncodeHelpers.ParseHex(args[6].ToString());
                            byte   p7  = (byte)EncodeHelpers.ParseHex(args[7].ToString());
                            byte   p8  = (byte)EncodeHelpers.ParseHex(args[8].ToString());
                            byte   p9  = (byte)EncodeHelpers.ParseHex(args[9].ToString());
                            byte   p10 = (byte)EncodeHelpers.ParseHex(args[10].ToString());

                            guidVal = new Guid(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
                        }
                        else if (objCreationSyntax.ArgumentList.Arguments.Count == 1)
                        {
                            // If this is an invalid format, remove the node
                            if (!Guid.TryParse(objCreationSyntax.ArgumentList.Arguments[0].ToString(), out guidVal))
                            {
                                return(null);
                            }
                        }
                    }

                    if (guidVal == Guid.Empty)
                    {
                        return(node);
                    }

                    node = node.RemoveNode(varInitializer, SyntaxRemoveOptions.KeepExteriorTrivia | SyntaxRemoveOptions.KeepEndOfLine);
                    node = node.AddAttributeLists(EncodeHelpers.ConvertGuidToAttributeList(guidVal).WithLeadingTrivia(node.GetLeadingTrivia()));

                    return(node);
                }

                node = (FieldDeclarationSyntax)base.VisitFieldDeclaration(node);
                if (listAttributes != null)
                {
                    foreach (var attrNode in listAttributes)
                    {
                        var attrListNode =
                            SyntaxFactory.AttributeList(
                                SyntaxFactory.SingletonSeparatedList <AttributeSyntax>(attrNode));
                        node = node.WithAttributeLists(node.AttributeLists.Add(attrListNode));
                    }
                }

                var firstVar = node.Declaration.Variables.First();

                if (newName != null)
                {
                    var newVar = SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(newName));
                    node = node.ReplaceNode(firstVar, newVar);
                }

                if (newType != null)
                {
                    node = node.WithDeclaration(node.Declaration.WithType(SyntaxFactory.ParseTypeName(newType).WithTrailingTrivia(SyntaxFactory.Space)));
                }

                return(node);
            }
 public static FieldDeclarationSyntax AddAttributes(this FieldDeclarationSyntax field, params string[] attributeNames)
 {
     return(field.AddAttributeLists(MakeAttributes(attributeNames)));
 }
        private static Task <Document> ApplyNonSerializedFixAsync(CodeFixContext context, SyntaxNode syntaxRoot, FieldDeclarationSyntax field)
        {
            var updated = field.AddAttributeLists(NonSerialized);

            return(Task.FromResult(context.Document.WithSyntaxRoot(syntaxRoot.ReplaceNode(field, updated))));
        }
Ejemplo n.º 6
0
        public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            if (node.Parent is not CompilationUnitSyntax)
            {
                return(node);
            }

            var variableDeclarator   = node.GetSingleDeclarator();
            var invocationExpression = variableDeclarator.Initializer?.Value as InvocationExpressionSyntax;

            if (invocationExpression?.GetIdentifierName() != "Argument")
            {
                return(node);
            }

            var originalNode    = node;
            var resolutionName  = invocationExpression.GetArgumentAt <LiteralExpressionSyntax>(0).GetConstantValue <string>();
            var defaultValue    = invocationExpression.GetArgumentAt <ExpressionSyntax>(1);
            var nonDefaultValue = defaultValue is LiteralExpressionSyntax literalExpression
                ? literalExpression.GetNonDefaultValue()
                : defaultValue;

            AttributeListSyntax CreateAttributeListWith(string name, string argument = null)
            {
                var attribute = Attribute(IdentifierName(name));

                if (argument != null)
                {
                    attribute = attribute.WithArgumentList(AttributeArgumentList(
                                                               SeparatedList(new[] { AttributeArgument(argument.ToLiteralExpression()) })));
                }

                return(AttributeList(SeparatedList(new[] { attribute })));
            }

            node = node.WithoutTrivia();
            node = node.AddAttributeLists(
                CreateAttributeListWith(
                    "Parameter",
                    !resolutionName.Replace("_", string.Empty).Replace("-", string.Empty)
                    .EqualsOrdinalIgnoreCase(variableDeclarator.Identifier.Text)
                        ? resolutionName
                        : null));
            node = node.WithDeclaration(
                node.Declaration.WithType(
                    variableDeclarator.Identifier.Text.ContainsAnyOrdinalIgnoreCase("path", "dir", "file")
                    ? ParseTypeName(nameof(AbsolutePath))
                    : invocationExpression.GetSingleGenericArgumentOrNull() ?? defaultValue.GetExpressionType()));
            node = node.WithDeclaration(
                node.Declaration
                .WithVariables(
                    SeparatedList(node.Declaration.Variables
                                  .Select(x => x.WithInitializer(nonDefaultValue != null
                                ? EqualsValueClause(nonDefaultValue)
                                : null)))));
            node = node.WithModifiers(TokenList(Token(SyntaxKind.ReadOnlyKeyword)));

            if (variableDeclarator.Identifier.Text.ContainsAnyOrdinalIgnoreCase("password", "key", "token", "secret", "credentials"))
            {
                node = node.AddAttributeLists(CreateAttributeListWith("Secret"));
            }

            return(node.WithTriviaFrom(originalNode));
        }
Ejemplo n.º 7
0
 public static FieldDeclarationSyntax WithAttributes(this FieldDeclarationSyntax node, params AttributeSyntax[] attributes)
 {
     return(node.AddAttributeLists(AttributeList(SeparatedList(attributes))));
 }
Ejemplo n.º 8
0
        public static FieldDeclarationSyntax AddAttribute(this FieldDeclarationSyntax fieldDeclarationSyntax, AttributeSyntax attributeSyntax)
        {
            var attributeList = SyntaxFactory.AttributeList().AddAttributes(attributeSyntax);

            return(fieldDeclarationSyntax.AddAttributeLists(attributeList));
        }
Ejemplo n.º 9
0
 public static FieldDeclarationSyntax WithGeneratedAttribute(this FieldDeclarationSyntax node)
 => node.AddAttributeLists(AttributeList(SingletonSeparatedList(CreateGeneratedCodeAttribute())));