private static PropertyDeclarationSyntax GetSimpleProperty(PropertyDeclarationSyntax property, VariableDeclaratorSyntax variableDeclarator) { var simpleGetSetPropetie = property.WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)), SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)) }))); return variableDeclarator.Initializer == null ? simpleGetSetPropetie : simpleGetSetPropetie.WithInitializer(variableDeclarator.Initializer) .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)); }
private static bool IsPropertyCandidate(PropertyDeclarationSyntax propertySyntax, SemanticModel semanticModel) { if (HasDocumentationComment(propertySyntax)) { return false; } var propertySymbol = semanticModel.GetDeclaredSymbol(propertySyntax); if (propertySymbol == null || !propertySymbol.IsOverride || propertySymbol.IsSealed || propertySymbol.OverriddenProperty == null) { return false; } if (propertySymbol.GetMethod != null && propertySymbol.OverriddenProperty.GetMethod == null) { return false; } if (propertySymbol.SetMethod != null && propertySymbol.OverriddenProperty.SetMethod == null) { return false; } return CheckGetAccessorIfAny(propertySyntax, propertySymbol, semanticModel) && CheckSetAccessorIfAny(propertySyntax, propertySymbol, semanticModel); }
public Property(SemanticModel model, PropertyDeclarationSyntax syntax, AttributeSyntax attribute = null, AttributeSyntax classAttribute = null) { this.model = model; Syntax = syntax; propertyAttribute = attribute; ClassAttribute = classAttribute; }
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node) { if (RequiresNullChecks(node.AttributeLists)) { AddMessage(node, $"property {node.Identifier} needs null checks"); } }
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node) { return base.VisitPropertyDeclaration( node.WithType(UpdateType(node.Type)) .WithModifiers(SyntaxFactory.TokenList()) .WithAccessorList(UpdateAccessorList(node.AccessorList, node.Type))); }
private Diagnostic HandleProperty(PropertyDeclarationSyntax propertyDeclaration) { if (propertyDeclaration.ExpressionBody != null) { return null; } if (propertyDeclaration.DescendantNodesAndTokensAndSelf().Any(x => x.GetLeadingTrivia().Concat(x.GetTrailingTrivia()).Any(y => !y.IsWhitespaceTrivia()))) { return null; } var getter = propertyDeclaration.AccessorList.Accessors.FirstOrDefault(x => x.Keyword.ValueText == "get"); if (getter == null) { return null; } if (getter.AttributeLists.Any(x => x.Attributes.Any())) { return null; } if (getter.Body?.Statements.Count != 1) { return null; } var statement = getter.Body.Statements.First(); return Diagnostic.Create(Rule, statement.GetLocation(), "Property", propertyDeclaration.Identifier); }
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node) { _cancellationToken.ThrowIfCancellationRequested(); var propertySymbol = _semanticModel.GetDeclaredSymbol(node, _cancellationToken); var propertyAssumptions = _propertyAssumptions.Where(pa => pa.Property == propertySymbol).ToList(); if (propertyAssumptions.Count == 0) { return base.VisitPropertyDeclaration(node); } var newProperty = node; foreach (var propertyAssumption in propertyAssumptions) { foreach (string attributeToDelete in propertyAssumption.GetAttributesToDelete(_objectTheoremResult)) { newProperty = RemoveAttribute(newProperty, attributeToDelete); } foreach (string attributeToAdd in propertyAssumption.GetAttributesToAdd(_objectTheoremResult)) { newProperty = EnsureAttribute(newProperty, attributeToAdd); } foreach (AttributeSyntax attributeToAdd in propertyAssumption.GetAttributeSyntaxexToAdd(_objectTheoremResult)) { newProperty = EnsureAttribute(newProperty, attributeToAdd); } } return base.VisitPropertyDeclaration(newProperty); }
public async static Task<Solution> MakeAutoPropertyAsync(Document document, SyntaxNode root, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var getterReturn = (ReturnStatementSyntax)property.AccessorList.Accessors.First(a => a.Keyword.ValueText == "get").Body.Statements.First(); var returnIdentifier = (IdentifierNameSyntax)(getterReturn.Expression is MemberAccessExpressionSyntax ? ((MemberAccessExpressionSyntax)getterReturn.Expression).Name : getterReturn.Expression); var returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol; var variableDeclarator = (VariableDeclaratorSyntax)returnIdentifierSymbol.DeclaringSyntaxReferences.First().GetSyntax(); var fieldDeclaration = variableDeclarator.FirstAncestorOfType<FieldDeclarationSyntax>(); root = root.TrackNodes(returnIdentifier, fieldDeclaration, property); document = document.WithSyntaxRoot(root); root = await document.GetSyntaxRootAsync(cancellationToken); semanticModel = await document.GetSemanticModelAsync(cancellationToken); returnIdentifier = root.GetCurrentNode(returnIdentifier); returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol; var newProperty = GetSimpleProperty(property, variableDeclarator) .WithTriviaFrom(property) .WithAdditionalAnnotations(Formatter.Annotation); var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, returnIdentifierSymbol, property.Identifier.ValueText, document.Project.Solution.Workspace.Options, cancellationToken); document = newSolution.GetDocument(document.Id); root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); root = root.InsertNodesAfter(root.GetCurrentNode(property), new[] { newProperty }); var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1; if (multipleVariableDeclaration) { var newfieldDeclaration = fieldDeclaration.WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia)); root = root.RemoveNode(root.GetCurrentNode<SyntaxNode>(property), SyntaxRemoveOptions.KeepNoTrivia); root = root.ReplaceNode(root.GetCurrentNode(fieldDeclaration), newfieldDeclaration); } else { root = root.RemoveNodes(root.GetCurrentNodes<SyntaxNode>(new SyntaxNode[] { fieldDeclaration, property }), SyntaxRemoveOptions.KeepNoTrivia); } document = document.WithSyntaxRoot(root); return document.Project.Solution; }
public async static Task<Solution> MakeAutoPropertyAsync(Document document, SyntaxNode root, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var getterReturn = (ReturnStatementSyntax)property.AccessorList.Accessors.First(a => a.Keyword.ValueText == "get").Body.Statements.First(); var returnIdentifier = (IdentifierNameSyntax)(getterReturn.Expression is MemberAccessExpressionSyntax ? ((MemberAccessExpressionSyntax)getterReturn.Expression).Name : getterReturn.Expression); var fieldSymbol = (IFieldSymbol)semanticModel.GetSymbolInfo(returnIdentifier).Symbol; var variableDeclarator = (VariableDeclaratorSyntax)fieldSymbol.DeclaringSyntaxReferences.First().GetSyntax(); var fieldDeclaration = variableDeclarator.FirstAncestorOfType<FieldDeclarationSyntax>(); var propertySymbol = semanticModel.GetDeclaredSymbol(property); var newRoot = root.TrackNodes(returnIdentifier, fieldDeclaration, property, variableDeclarator); //cycle var newDocument = document.WithSyntaxRoot(newRoot); newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken); var newProperty = CreateAutoProperty(property, variableDeclarator, fieldSymbol, propertySymbol); Solution newSolution; if (IsExplicityImplementation(propertySymbol)) { semanticModel = await newDocument.GetSemanticModelAsync(cancellationToken); returnIdentifier = newRoot.GetCurrentNode(returnIdentifier); fieldSymbol = (IFieldSymbol)semanticModel.GetSymbolInfo(returnIdentifier).Symbol; newSolution = await RenameSymbolAndKeepExplicitPropertiesBoundAsync(newDocument.Project.Solution, property.Identifier.ValueText, fieldSymbol, propertySymbol, cancellationToken); newDocument = newSolution.GetDocument(newDocument.Id); newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(property), newProperty); newSolution = newSolution.WithDocumentSyntaxRoot(newDocument.Id, newRoot); } else { var currentProperty = newRoot.GetCurrentNode(property); var type = (TypeDeclarationSyntax)currentProperty.Parent; var propertyIndex = type.Members.IndexOf(currentProperty); //Remove the property: this is needed otherwise the rename that happens bellow will not be able to //correctly redirect the references to the field, as the property will conflict with the name. //The conflict is specially troublesome for circular references, such as the one that caused the bug #702. newRoot = newRoot.ReplaceNode(type, type.RemoveNode(currentProperty, SyntaxRemoveOptions.KeepNoTrivia)); //cycle newDocument = newDocument.WithSyntaxRoot(newRoot); newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken); semanticModel = await newDocument.GetSemanticModelAsync(cancellationToken); fieldSymbol = (IFieldSymbol)semanticModel.GetDeclaredSymbol(newRoot.GetCurrentNode(variableDeclarator)); //rename the field: newSolution = await Renamer.RenameSymbolAsync(newDocument.Project.Solution, fieldSymbol, property.Identifier.ValueText, newDocument.Project.Solution.Workspace.Options, cancellationToken).ConfigureAwait(false); //cycle newDocument = newSolution.GetDocument(newDocument.Id); newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); //add the property back: var currentType = (TypeDeclarationSyntax)newRoot.GetCurrentNode(fieldDeclaration).Parent; var newMembers = currentType.Members.Insert(propertyIndex, newProperty); var newType = WithMembers(currentType, newMembers); newRoot = newRoot.ReplaceNode(currentType, newType); newSolution = newSolution.WithDocumentSyntaxRoot(newDocument.Id, newRoot); } newDocument = newSolution.GetDocument(newDocument.Id); newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); newRoot = RemoveField(newRoot, variableDeclarator, fieldDeclaration); return newSolution.WithDocumentSyntaxRoot(newDocument.Id, newRoot); }
/// <summary> /// Return true if the supplied property is a boolean property. /// </summary> /// <param name="propertyNode">the property node.</param> /// <returns>true if the property is a boolean property.</returns> public bool IsPropertyBoolean(PropertyDeclarationSyntax propertyNode) { if (propertyNode.Type.Kind() != SyntaxKind.PredefinedType) return false; var predefined = propertyNode.Type as PredefinedTypeSyntax; return predefined?.Keyword.Kind() == SyntaxKind.BoolKeyword; }
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax propertySyntax) { var leadingTrivia = propertySyntax.Identifier.LeadingTrivia; var trailingTriva = propertySyntax.Identifier.TrailingTrivia; return propertySyntax.ReplaceToken(propertySyntax.Identifier, SyntaxFactory.Identifier(leadingTrivia, ToCamelCase(propertySyntax.Identifier.ValueText), trailingTriva) ); }
private bool IsAutoImplementedProperty(PropertyDeclarationSyntax propertyDecl) { SyntaxList<AccessorDeclarationSyntax> accessors = propertyDecl.AccessorList.Accessors; AccessorDeclarationSyntax getter = accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.GetAccessorDeclaration); AccessorDeclarationSyntax setter = accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.SetAccessorDeclaration); if (getter == null || setter == null) return false; return getter.Body == null && setter.Body == null; }
private async Task<Document> ChangeToFullPropertyAsync(Document document, PropertyDeclarationSyntax propertyDecl, CancellationToken cancellationToken) { SemanticModel model = await document.GetSemanticModelAsync(cancellationToken); var root = await document.GetSyntaxRootAsync(cancellationToken) as CompilationUnitSyntax; document = document.WithSyntaxRoot(CodeGeneration.ImplementFullProperty(root, model, propertyDecl, document.Project.Solution.Workspace)); return document; }
public static bool IsGetOnlyAutoProperty(this IPropertySymbol property, PropertyDeclarationSyntax propertyDeclaration) { Contract.Requires(property != null); Contract.Requires(propertyDeclaration != null); var getter = propertyDeclaration.Getter(); if (getter == null) return false; return property.IsReadOnly && getter.SemicolonToken.IsKind(SyntaxKind.SemicolonToken); }
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node) { // TODO: Parse properties correctly and load their nodes. PropertyWalker walker = this.CreateSyntaxWalker<PropertyWalker>(node); walker.Visit(node); this.Variables.Add(new Property() { Name = node.Identifier.ToString(), Type = node.Type.ToString() }); base.VisitPropertyDeclaration(node); }
private static PropertyDeclarationSyntax MakeInterfaceProperty(PropertyDeclarationSyntax propertySyntax) { var accessors = propertySyntax.AccessorList.Accessors.Select(f => MakeInterfaceAccessor(f)); var syntaxList = new SyntaxList<AccessorDeclarationSyntax>(); syntaxList = syntaxList.AddRange(accessors); var accessorList = propertySyntax.AccessorList.WithAccessors(syntaxList); return propertySyntax.WithModifiers(new SyntaxTokenList()).WithAccessorList(accessorList); }
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax propertyDeclaration) { if (propertyDeclaration == property) { // Add an annotation to format the new property. return ConvertToAutoProperty(propertyDeclaration).WithAdditionalAnnotations(Formatter.Annotation); } return base.VisitPropertyDeclaration(propertyDeclaration); }
public static PropertyDeclarationSyntax[] GetTrackableProperties(PropertyDeclarationSyntax[] properties) { // NOTE: it's naive approach because we don't know semantic type information here. return properties.Where(p => { var parts = p.Type.ToString().Split('.'); var typeName = parts[parts.Length - 1]; return typeName.StartsWith("Trackable"); }).ToArray(); }
/// <summary> /// Retrieves the get and set accessor declarations of the specified property. /// Returns true if both get and set accessors exist; otherwise false. /// </summary> internal static bool TryGetAccessors( PropertyDeclarationSyntax property, out AccessorDeclarationSyntax getter, out AccessorDeclarationSyntax setter) { var accessors = property.AccessorList.Accessors; getter = accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.GetAccessorDeclaration); setter = accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.SetAccessorDeclaration); return accessors.Count == 2 && getter != null && setter != null; }
private static string GetDefaultValue(PropertyDeclarationSyntax property) { var childSyntaxList = property.Initializer.Value; if (property.Initializer.Value is LiteralExpressionSyntax) { var literalExpression = property.Initializer.Value as LiteralExpressionSyntax; return literalExpression.Token.ValueText; } return childSyntaxList.DescendantNodesAndSelf().OfType<ObjectCreationExpressionSyntax>().First().ToFullString(); }
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node) { base.VisitPropertyDeclaration(node); if (node.AccessorList != null) { foreach (var accessor in node.AccessorList.Accessors) { _members.Add(accessor); } } }
private static void AnalyzePropertyGetter(PropertyDeclarationSyntax propertyNode, SyntaxNodeAnalysisContext context) { if (propertyNode.ExpressionBody == null) { EvaluatePropertiesForSimplicityAnalyzer.AnalyzePropertyGetterWithGet(propertyNode, context); } else { EvaluatePropertiesForSimplicityAnalyzer.AnalyzePropertyGetterWithExpressionBody(propertyNode, context); } }
internal static BoundStatement AddSequencePoint(PropertyDeclarationSyntax declarationSyntax, BoundStatement rewrittenStatement) { Debug.Assert(declarationSyntax.Initializer != null); int start = declarationSyntax.Initializer.Value.SpanStart; int end = declarationSyntax.Initializer.Span.End; TextSpan part = TextSpan.FromBounds(start, end); var result = BoundSequencePoint.Create(declarationSyntax, part, rewrittenStatement); result.WasCompilerGenerated = rewrittenStatement.WasCompilerGenerated; return result; }
private async Task<Document> ExplodePropertyAsync(SyntaxNode oldRoot, Document document, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); // Call the Property Exploder. The code refactoring happens here. var expander = new PropertyExploder(property.Parent, property); var newRoot = expander.Visit(oldRoot); // Update code with the refactored Root return document.WithSyntaxRoot(newRoot); }
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node) { if (HasBothAccessors(node)) { IFieldSymbol backingField = GetBackingFieldFromGetter(node.AccessorList.Accessors.Single(ad => ad.Kind() == SyntaxKind.GetAccessorDeclaration)); SyntaxNode fieldDeclaration = backingField.DeclaringSyntaxReferences.First().GetSyntax().Ancestors().Where(a => a is FieldDeclarationSyntax).FirstOrDefault(); _fieldsToRemove.Add((fieldDeclaration as FieldDeclarationSyntax)?.GetText().ToString()); PropertyDeclarationSyntax property = ConvertToAutoProperty(node).WithAdditionalAnnotations(Formatter.Annotation); return property; } return node; }
private PropertyDeclarationSyntax ConvertToAutoProperty(PropertyDeclarationSyntax propertyDeclaration) { var newProperty = propertyDeclaration .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.List(new[] { SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)).WithAdditionalAnnotations(Formatter.Annotation), SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)) }))); return newProperty; }
public static async Task<Solution> FixProperty(Document document, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var classParent = (ClassDeclarationSyntax)property.Parent; var field = AutoPropertyAnalyzer.PreviosField(classParent, property); var fieldVariable = field.Declaration.Variables.Single(); var semanticModel = await document.GetSemanticModelAsync(); var symbol = semanticModel.GetDeclaredSymbol(fieldVariable); var solution = document.Project.Solution; solution = await Renamer.RenameSymbolAsync(solution, symbol, property.Identifier.ToString(), solution.Workspace.Options, cancellationToken); document = solution.GetDocument(document.Id); var root = await document.GetSyntaxRootAsync(); classParent = root.DescendantNodes().OfType<ClassDeclarationSyntax>().SingleOrDefault(c => c.Identifier.IsEquivalentTo(classParent.Identifier)); field = classParent.Members.OfType<FieldDeclarationSyntax>().SingleOrDefault(f => f.Declaration.Variables.Any(v => v.Identifier.ToString() == property.Identifier.ToString())); fieldVariable = field.Declaration.Variables.Single(); var oldProperty = classParent.Members.OfType<PropertyDeclarationSyntax>().SingleOrDefault(a => a.Identifier.ToString() == property.Identifier.ToString()); var newProperty = SyntaxFactory.PropertyDeclaration( new SyntaxList<AttributeListSyntax>().AddRange(field.AttributeLists).AddRange(oldProperty.AttributeLists), oldProperty.Modifiers, oldProperty.Type, null, oldProperty.Identifier, SyntaxFactory.AccessorList(SyntaxFactory.List( property.AccessorList.Accessors.Select(a => a.WithBody(null).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))) ))); var leading = field.DescendantTokens().First().LeadingTrivia; var first = newProperty.DescendantTokens().First(); newProperty = newProperty.ReplaceToken(first, first.WithLeadingTrivia(leading)); if (fieldVariable.Initializer != null) newProperty = newProperty.WithInitializer(fieldVariable.Initializer).WithSemicolonToken(field.SemicolonToken); var members = classParent.Members.Replace(oldProperty, newProperty); members = members.Remove(members.Single(m => m.IsEquivalentTo(field))); var newClass = classParent.WithMembers(members); var docNode = await document.GetSyntaxRootAsync(cancellationToken); docNode = docNode.ReplaceNode(classParent, newClass); docNode = Formatter.Format(docNode, solution.Workspace); var resultSolution = solution.WithDocumentSyntaxRoot(document.Id, docNode); return resultSolution; }
private async Task<Document> ApplyAttribute(Document document, PropertyDeclarationSyntax propertyDeclaration, string attributeName, CancellationToken cancellationToken) { var attribute = SyntaxFactory.Attribute(SyntaxFactory.IdentifierName(attributeName)); var syntaxes = new[] { attribute }; var attributeList = propertyDeclaration.AttributeLists.Add(SyntaxFactory.AttributeList().WithAttributes(SyntaxFactory.SeparatedList(syntaxes)).WithTrailingTrivia(SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, Environment.NewLine))); var syntaxNode = (await document.GetSyntaxRootAsync(cancellationToken)).ReplaceNode(propertyDeclaration, propertyDeclaration.WithAttributeLists(attributeList)); return document.WithSyntaxRoot(syntaxNode); }
private async Task<Document> MakePrivateAsync(Document document, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var newPropertyDeclaration = SyntaxFactory.PropertyDeclaration(property.Type, property.Identifier) .AddModifiers(SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.PrivateKeyword, SyntaxTriviaList.Create(SyntaxFactory.Space))) .WithLeadingTrivia(property.GetLeadingTrivia()) .WithAccessorList(property.AccessorList) ; var root = await document.GetSyntaxRootAsync(); var newRoot = root.ReplaceNode(property, newPropertyDeclaration); return document.WithSyntaxRoot(newRoot); }
public override VisualBasicSyntaxNode VisitPropertyDeclaration(CSS.PropertyDeclarationSyntax node) { var id = SyntaxFactory.Identifier(node.Identifier.ValueText, SyntaxFacts.IsKeywordKind(node.Identifier.Kind()), node.Identifier.GetIdentifierText(), TypeCharacter.None); SyntaxList <AttributeListSyntax> attributes, returnAttributes; ConvertAndSplitAttributes(node.AttributeLists, out attributes, out returnAttributes); var stmt = SyntaxFactory.PropertyStatement( attributes, ConvertModifiers(node.Modifiers, TokenContext.Member), id, null, SyntaxFactory.SimpleAsClause(returnAttributes, (TypeSyntax)node.Type.Accept(this)), null, null ); if (node.AccessorList.Accessors.All(a => a.Body == null)) { return(stmt); } var accessors = node.AccessorList?.Accessors.Select(a => (AccessorBlockSyntax)a.Accept(this)).ToArray(); return(SyntaxFactory.PropertyBlock(stmt, SyntaxFactory.List(accessors))); }
private VariableDeclaratorSyntax ConvertToVariableDeclarator(CSS.PropertyDeclarationSyntax des) { var id = GetVbPropertyBackingFieldName(des); var ids = SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(id)); TypeSyntax typeSyntax; if (des.Type.IsVar) { var typeSymbol = ModelExtensions.GetSymbolInfo(_semanticModel, des.Type).ExtractBestMatch <ITypeSymbol>(); typeSyntax = typeSymbol?.ToVbSyntax(_semanticModel, des.Type); } else { typeSyntax = (TypeSyntax)des.Type.Accept(_nodesVisitor); } var simpleAsClauseSyntax = typeSyntax != null?SyntaxFactory.SimpleAsClause(typeSyntax) : null; //Gracefully degrade when no type information available EqualsValueSyntax equalsValueSyntax = null; return(SyntaxFactory.VariableDeclarator(ids, simpleAsClauseSyntax, equalsValueSyntax)); }
public override void VisitPropertyDeclaration(Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax node) { base.VisitPropertyDeclaration(node); Colorize(node.Identifier, propertyDeclarationColor); }
/// <summary> /// A position is inside a property body only if it is inside an expression body. /// All block bodies for properties are part of the accessor declaration (a type /// of BaseMethodDeclaration), not the property declaration. /// </summary> internal static bool IsInBody(int position, PropertyDeclarationSyntax property) => IsInBody(position, blockOpt: null, property.GetExpressionBodySyntax(), property.SemicolonToken);
public static bool IsAutoProperty(this PropertyDeclarationSyntax property) { return(property?.AccessorList?.Accessors.All(x => x.Body == null) ?? false); }
/// <summary> /// A position is inside a property body only if it is inside an expression body. /// All block bodies for properties are part of the accessor declaration (a type /// of BaseMethodDeclaration), not the property declaration. /// </summary> internal static bool IsInBody(int position, PropertyDeclarationSyntax property) => IsInBody(position, default(BlockSyntax), property.GetExpressionBodySyntax(), property.SemicolonToken);
/// <summary> /// Normalizes the <paramref name="declaration" />. /// </summary> public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax declaration) { var propertySymbol = declaration.GetPropertySymbol(SemanticModel); if (!propertySymbol.ContainingType.IsComponent(SemanticModel) || !propertySymbol.IsExtern) return declaration; var originalDeclaration = declaration; var index = declaration.Modifiers.IndexOf(SyntaxKind.ExternKeyword); declaration = declaration.WithModifiers(declaration.Modifiers.RemoveAt(index)).WithSemicolonToken(default(SyntaxToken)); var accessors = SyntaxFactory.List(NormalizerAccessors(originalDeclaration.AccessorList.Accessors)); return declaration.WithAccessorList(declaration.AccessorList.WithAccessors(accessors)).EnsureLineCount(originalDeclaration); }