Пример #1
0
        public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
        => AssignType(syntax, () => {
            var errors = new List <ErrorDiagnostic>();
            var types  = new List <TypeSymbol>();

            if (syntax.Key is StringSyntax stringSyntax && stringSyntax.IsInterpolated())
            {
 public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
 {
     // Checked for short circuiting: We only show one violation at a time per variable
     if (this.invalidReferencedBodyObj != null)
     {
         return;
     }
     base.VisitObjectPropertySyntax(syntax);
 }
Пример #3
0
 public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
 {
     base.VisitObjectPropertySyntax(syntax);
     // if an error is found at the end we emit it and move on to the next key of this object declaration.
     // The last error of this property access will be emitted here.
     if (this.errorSyntax != null)
     {
         this.AppendError();
     }
 }
Пример #4
0
        public static bool IsAddingPropertyLoopAllowed(SemanticModel semanticModel, ObjectPropertySyntax property)
        {
            SyntaxBase?current           = property;
            int        propertyLoopCount = 0;

            while (current is not null)
            {
                var parent = semanticModel.SourceFile.Hierarchy.GetParent(current);
                if (current is ForSyntax @for && IsPropertyLoop(parent, @for))
                {
                    ++propertyLoopCount;
                }

                current = parent;
            }

            // adding a new property loop is only allowed if we're under the limit
            return(propertyLoopCount < MaximumNestedPropertyLoopCount);
        }
Пример #5
0
        private DeclaredTypeAssignment?GetObjectPropertyType(ObjectPropertySyntax syntax)
        {
            var propertyName = syntax.TryGetKeyText();
            var parent       = this.hierarchy.GetParent(syntax);

            if (propertyName == null || !(parent is ObjectSyntax parentObject))
            {
                // the property name is an interpolated string (expression) OR the parent is missing OR the parent is not ObjectSyntax
                // cannot establish declared type
                // TODO: Improve this when we have constant folding
                return(null);
            }

            var assignment = GetDeclaredTypeAssignment(parent);

            // we are in the process of establishing the declared type for the syntax nodes,
            // so we must set useSyntax to false to avoid a stack overflow
            return(GetObjectPropertyType(assignment?.Reference.Type, parentObject, propertyName, useSyntax: false));
        }
Пример #6
0
        public override Task <DocumentHighlightContainer?> Handle(DocumentHighlightParams request, CancellationToken cancellationToken)
        {
            var result = this.symbolResolver.ResolveSymbol(request.TextDocument.Uri, request.Position);

            if (result == null)
            {
                return(Task.FromResult <DocumentHighlightContainer?>(null));
            }

            var highlights = result.Context.Compilation.GetEntrypointSemanticModel()
                             .FindReferences(result.Symbol)
                             .Select(referenceSyntax => new DocumentHighlight
            {
                Range = PositionHelper.GetNameRange(result.Context.LineStarts, referenceSyntax),
                Kind  = referenceSyntax switch {
                    INamedDeclarationSyntax _ => DocumentHighlightKind.Write,
                    ObjectPropertySyntax _ => DocumentHighlightKind.Write,
                    _ => DocumentHighlightKind.Read,
                },
            });
Пример #7
0
            public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
            {
                string?propertyName = syntax.TryGetKeyText();

                // Check all properties with the name 'adminUsername', regardless of casing
                if (propertyName != null && StringComparer.OrdinalIgnoreCase.Equals(propertyName, adminUsernamePropertyName))
                {
                    var type = model.GetTypeInfo(syntax.Value);
                    if (type is StringLiteralType stringType)
                    {
                        diagnostics.Add(parent.CreateDiagnosticForSpan(syntax.Value.Span, stringType.RawStringValue));
                    }

                    // No need to traverse deeper
                }
                else
                {
                    base.VisitObjectPropertySyntax(syntax);
                }
            }
Пример #8
0
        public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
        {
            if (syntax.TryGetKeyText() == null && syntax.Value is ForSyntax)
            {
                // block loop usage with properties whose names are expressions
                this.diagnosticWriter.Write(DiagnosticBuilder.ForPosition(syntax.Key).ExpressionedPropertiesNotAllowedWithLoops());
            }

            bool insideDependsOnInThisScope = ReferenceEquals(this.currentDependsOnProperty, syntax);

            // set this to true if the current property is the top-level dependsOn property
            // leave it true if already set to true
            this.insideTopLevelDependsOn = this.insideTopLevelDependsOn || insideDependsOnInThisScope;

            // visit children
            base.VisitObjectPropertySyntax(syntax);

            // clear the flag after we leave the dependsOn property
            if (insideDependsOnInThisScope)
            {
                this.insideTopLevelDependsOn = false;
            }
        }
Пример #9
0
        public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
        => AssignTypeWithCaching(syntax, () => {
            Visit(syntax.Value);

            return(assignedTypes[syntax.Value]);
        });
        protected override SyntaxBase ReplaceResourceDeclarationSyntax(ResourceDeclarationSyntax syntax)
        {
            if (syntax.TryGetBody() is not ObjectSyntax resourceBody ||
                resourceBody.SafeGetPropertyByName("name") is not ObjectPropertySyntax resourceNameProp ||
                resourceNameProp.Value is not StringSyntax resourceName)
            {
                return(syntax);
            }

            if (semanticModel.GetSymbolInfo(syntax) is not ResourceSymbol resourceSymbol ||
                resourceSymbol.Type is not ResourceType resourceType)
            {
                return(syntax);
            }

            if (resourceType.TypeReference.Types.Length < 2)
            {
                // we're only looking for child resources here
                return(syntax);
            }

            foreach (var otherResource in semanticModel.AllResources)
            {
                var otherResourceSymbol = otherResource.Symbol;

                if (otherResourceSymbol.Type is not ResourceType otherResourceType ||
                    otherResourceType.TypeReference.Types.Length != resourceType.TypeReference.Types.Length - 1 ||
                    !resourceType.TypeReference.TypesString.StartsWith($"{otherResourceType.TypeReference.TypesString}/", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                // The other resource is a parent type to this one. check if we can refactor the name.
                if (otherResourceSymbol.DeclaringResource.TryGetBody() is not ObjectSyntax otherResourceBody ||
                    otherResourceBody.SafeGetPropertyByName("name") is not ObjectPropertySyntax otherResourceNameProp)
                {
                    continue;
                }

                if (TryGetReplacementChildName(resourceName, otherResourceNameProp.Value, otherResourceSymbol) is not {
                } newName)
                {
                    continue;
                }

                var replacementNameProp = new ObjectPropertySyntax(resourceNameProp.Key, resourceNameProp.Colon, newName);
                var parentProp          = new ObjectPropertySyntax(
                    SyntaxFactory.CreateIdentifier(LanguageConstants.ResourceParentPropertyName),
                    SyntaxFactory.ColonToken,
                    SyntaxFactory.CreateVariableAccess(otherResourceSymbol.Name));

                var replacementBody = new ObjectSyntax(
                    resourceBody.OpenBrace,
                    // parent prop comes first!
                    parentProp.AsEnumerable().Concat(resourceBody.Children.Replace(resourceNameProp, replacementNameProp)),
                    resourceBody.CloseBrace);

                // at the top we just checked if there is a legitimate body
                // but to do the replacement correctly we may need to wrap it inside an IfConditionSyntax
                SyntaxBase replacementValue = syntax.Value switch
                {
                    ObjectSyntax => replacementBody,
                    IfConditionSyntax ifCondition => new IfConditionSyntax(ifCondition.Keyword, ifCondition.ConditionExpression, replacementBody),

                    // should not be possible
                    _ => throw new NotImplementedException($"Unexpected resource value type '{syntax.Value.GetType().Name}'.")
                };

                return(new ResourceDeclarationSyntax(
                           syntax.LeadingNodes,
                           syntax.Keyword,
                           syntax.Name,
                           syntax.Type,
                           syntax.ExistingKeyword,
                           syntax.Assignment,
                           replacementValue));
            }

            return(syntax);
        }
Пример #11
0
 public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
 {
     AddCodeFixIfSingleInterpolatedString(syntax.Value);
     base.VisitObjectPropertySyntax(syntax);
 }