private static SyntaxNode MakeMultipleFieldsReadonly(SyntaxNode root, FieldDeclarationSyntax fieldDeclaration, VariableDeclaratorSyntax variableToMakeReadonly)
 {
     var newDeclaration = fieldDeclaration.Declaration.RemoveNode(variableToMakeReadonly, SyntaxRemoveOptions.KeepEndOfLine);
     var newFieldDeclaration = fieldDeclaration.WithDeclaration(newDeclaration);
     var newReadonlyFieldDeclaration = fieldDeclaration.WithDeclaration(SyntaxFactory.VariableDeclaration(fieldDeclaration.Declaration.Type, SyntaxFactory.SeparatedList(new[] { variableToMakeReadonly })))
         .WithoutLeadingTrivia()
         .WithTrailingTrivia(SyntaxFactory.ParseTrailingTrivia("\n"))
         .AddModifiers(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))
         .WithAdditionalAnnotations(Formatter.Annotation);
     var newRoot = root.ReplaceNode(fieldDeclaration, new[] { newFieldDeclaration, newReadonlyFieldDeclaration });
     return newRoot;
 }
            public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
            {
                bool isInstance;
                if (NeedsRewrite(node, out isInstance))
                {
                    var list = new List<VariableDeclaratorSyntax>(node.Declaration.Variables.Count);
                    foreach (var v in node.Declaration.Variables)
                    {
                        if (IsGoodPrivateFieldName(v.Identifier.Text, isInstance))
                        {
                            list.Add(v);
                        }
                        else
                        {
                            list.Add(v.WithAdditionalAnnotations(s_markerAnnotation));
                            _count++;
                        }
                    }

                    var declaration = node.Declaration.WithVariables(SyntaxFactory.SeparatedList(list));
                    node = node.WithDeclaration(declaration);

                    return node;
                }

                return node;
            }
        private static SyntaxNode HandleFieldDeclaration(FieldDeclarationSyntax node)
        {
            VariableDeclarationSyntax declaration = node.Declaration;
            if (declaration == null || declaration.IsMissing)
            {
                return null;
            }

            SyntaxTokenList modifiers = DeclarationModifiersHelper.AddModifier(node.Modifiers, ref declaration, SyntaxKind.PrivateKeyword);
            return node
                .WithDeclaration(declaration)
                .WithModifiers(modifiers)
                .WithoutFormatting();
        }
Example #4
0
        public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            node = node.WithDeclaration((VariableDeclarationSyntax)VisitVariableDeclaration(node.Declaration));
            if (node.Declaration.Variables.Count == 1)
            {
                string typeName = node.Declaration.Type.ToString();
                switch (typeName)
                {
                    case "typedef":
                    {
                        Debug.Assert(pending_ == null);
                        pending_ = new ResolveTypedef(node, ctx_);
                        return null;
                    }

                    case "property":
                    {
                        var variable    = node.Declaration.Variables[0];
                        var initializer = variable.Initializer;
                        if (initializer != null)
                        {
                            var propType = Compiler.ConstantType(initializer.Value);
                            var result   = Compiler.Property(propType, variable.Identifier);
                            return result;
                        }
                        return node;
                    }
                }

                string variableName = node.Declaration.Variables[0].Identifier.ToString();
                switch (variableName)
                {
                    case "function":
                    case "method":
                    {
                        //when functions are declared with types the parser generates
                        //a subsquent erroneous method declaration
                        Debug.Assert(pending_ == null);
                        pending_ = new ResolveTypedFunction(node, ctx_, variableName == "method");
                        return null;
                    }
                    case "property":
                    {
                        //when functions are declared with types the parser generates
                        //a subsquent erroneous method declaration
                        Debug.Assert(pending_ == null);

                        var result = Compiler.Property(node.Declaration.Type);
                        pending_ = new ResolveProperty(result);
                        return null;
                    }
                }

            }
            return node;
        }
 private static SyntaxNode RemoveField(SyntaxNode root, VariableDeclaratorSyntax variableDeclarator, FieldDeclarationSyntax fieldDeclaration)
 {
     var currentField = root.GetCurrentNode(fieldDeclaration);
     var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1;
     root = multipleVariableDeclaration
         ? root.ReplaceNode(currentField, fieldDeclaration
             .WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia)))
         : root.RemoveNode(currentField, SyntaxRemoveOptions.KeepNoTrivia);
     return root;
 }