Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle special characters in use-safe-access rule correctly #14593

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading