Skip to content

Commit

Permalink
Handle special characters in use-safe-access rule correctly (#14593)
Browse files Browse the repository at this point in the history
I noticed that the safe access code fix is not suggesting the correct
replacement for property access containing special characters. This PR
fixes that.
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/14593)
  • Loading branch information
anthony-c-martin authored Jul 19, 2024
1 parent 57a44c0 commit cdb0fd7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ param target string
param foo object
param target string
var test = foo[?target] ?? 'baz'
""");

[TestMethod]
public void Codefix_fixes_syntax_which_can_be_simplified_named_array_access() => AssertCodeFix("""
param foo object
var test = contai|ns(foo, 'bar') ? foo['bar'] : 'baz'
""", """
param foo object
var test = foo.?bar ?? 'baz'
""");

[TestMethod]
public void Codefix_escapes_special_chars_correctly() => AssertCodeFix("""
param foo object
var test = contai|ns(foo, 'oh-dear') ? foo['oh-dear'] : 'baz'
""", """
param foo object
var test = foo[?'oh-dear'] ?? 'baz'
""");

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ functionCall.Arguments[2].Expression is StringSyntax fullString &&
SyntaxBase newSyntax = SyntaxFactory.CreateIdentifier(resource.Symbol.Name);
if (!isFull)
{
newSyntax = SyntaxFactory.CreatePropertyAccess(newSyntax, "properties");
newSyntax = SyntaxFactory.CreateAccessSyntax(newSyntax, "properties");
}

var codeReplacement = new CodeReplacement(functionCall.Span, newSyntax.ToString());
Expand Down
17 changes: 10 additions & 7 deletions src/Bicep.Core/Syntax/SyntaxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using Bicep.Core.Extensions;
using Bicep.Core.Parsing;
using Json.Pointer;

namespace Bicep.Core.Syntax
{
Expand Down Expand Up @@ -357,18 +358,20 @@ public static LambdaSyntax CreateLambdaSyntax(IReadOnlyList<string> parameterNam
_ => new NonNullAssertionSyntax(@base, ExclamationToken),
};

public static PropertyAccessSyntax CreatePropertyAccess(SyntaxBase @base, string propertyName)
=> CreatePropertyAccess(@base, false, propertyName);
public static AccessExpressionSyntax CreateAccessSyntax(SyntaxBase @base, string propertyName)
=> CreateAccessSyntax(@base, false, propertyName);

public static PropertyAccessSyntax CreatePropertyAccess(SyntaxBase @base, bool safe, string propertyName)
=> new(@base, DotToken, safe ? QuestionToken : null, CreateIdentifier(propertyName));
public static AccessExpressionSyntax CreateAccessSyntax(SyntaxBase @base, bool safe, string propertyName)
=> Lexer.IsValidIdentifier(propertyName) ?
new PropertyAccessSyntax(@base, DotToken, safe ? QuestionToken : null, CreateIdentifier(propertyName)) :
CreateArrayAccess(@base, safe, CreateStringLiteral(propertyName));

public static ArrayAccessSyntax CreateArrayAccess(SyntaxBase @base, bool safe, SyntaxBase accessExpression)
private static ArrayAccessSyntax CreateArrayAccess(SyntaxBase @base, bool safe, SyntaxBase accessExpression)
=> new(@base, LeftSquareToken, safe ? QuestionToken : null, accessExpression, RightSquareToken);

public static SyntaxBase CreateSafeAccess(SyntaxBase @base, SyntaxBase accessExpression)
public static AccessExpressionSyntax CreateSafeAccess(SyntaxBase @base, SyntaxBase accessExpression)
=> (accessExpression is StringSyntax stringAccess && stringAccess.TryGetLiteralValue() is { } stringValue) ?
CreatePropertyAccess(@base, true, stringValue) :
CreateAccessSyntax(@base, true, stringValue) :
CreateArrayAccess(@base, true, accessExpression);

public static ParameterAssignmentSyntax CreateParameterAssignmentSyntax(string name, SyntaxBase value)
Expand Down

0 comments on commit cdb0fd7

Please sign in to comment.