Skip to content
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
3 changes: 1 addition & 2 deletions src/SharpSyntaxRewriter/Rewriters/ImplementAutoProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ public override SyntaxNode VisitAssignmentExpression(AssignmentExpressionSyntax
var node_P = node.WithRight((ExpressionSyntax)node.Right.Accept(this));

var lhsSym = _semaModel.GetSymbolInfo(node.Left).Symbol;
if (ValidateSymbol(lhsSym)
&& lhsSym is IPropertySymbol propSym
if (lhsSym is IPropertySymbol propSym
&& propSym.IsReadOnly
&& propSym.DeclaringSyntaxReferences.Any())
{
Expand Down
2 changes: 1 addition & 1 deletion src/SharpSyntaxRewriter/Rewriters/ImposeThisPrefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private SyntaxNode VisitSimpleNameCommon(SimpleNameSyntax node)
return node;

var sym = _semaModel.GetSymbolInfo(node).Symbol;
if (!ValidateSymbol(sym) || sym.IsStatic)
if (sym == null || sym.IsStatic)
return node;

switch (sym)
Expand Down
35 changes: 17 additions & 18 deletions src/SharpSyntaxRewriter/Rewriters/Types/SymbolicRewriter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2021 ShiftLeft, Inc.
// Author: Leandro T. C. Melo

#define DEBUG_INACCURATE_REWRITES

using System;
using System.Diagnostics;
using System.Text;
Expand Down Expand Up @@ -40,27 +38,28 @@ public virtual SyntaxTree Apply(SyntaxTree tree,

protected void SymbolIsInvalid(ISymbol sym)
{
__wasRewriteAcurate = false;

if (!__reliableSemaModel)
return;

StringBuilder sb = new("invalid symbol");
foreach (var loc in sym?.Locations)
if (sym != null)
{
if (loc.IsInMetadata || !loc.IsInSource)
foreach (var loc in sym.Locations)
{
sb.Append($" (metadata or unknown: {loc.Kind})");
continue;
if (loc.IsInMetadata || !loc.IsInSource)
{
sb.Append($" (metadata or unknown: {loc.Kind})");
continue;
}

sb.Append(loc.SourceTree.FilePath);
sb.Append(' ');
sb.Append(loc.SourceSpan);
}

sb.Append(loc.SourceTree.FilePath);
sb.Append(' ');
sb.Append(loc.SourceSpan);
}

#if DEBUG_INACCURATE_REWRITES
Console.WriteLine(sb.ToString());
#endif

__wasRewriteAcurate = false;
if (__reliableSemaModel)
throw new UnexpectedInaccurateRewriteException(sb.ToString());
throw new UnexpectedInaccurateRewriteException(sb.ToString());
}

protected bool ValidateSymbol(ISymbol sym)
Expand Down
2 changes: 1 addition & 1 deletion src/SharpSyntaxRewriter/SharpSyntaxRewriter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>SharpSyntaxRewriter</PackageId>
<PackageVersion>1.0.41</PackageVersion>
<PackageVersion>1.0.44</PackageVersion>
<Authors>Leandro T. C. Melo</Authors>
<Copyright>ShiftLeft Inc.</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
Expand Down
28 changes: 28 additions & 0 deletions tests/TestImplementAutoProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,34 @@ public readonly struct ImageDimensions
{
public readonly int Height { get { return ____LT____Height____GT____k_BackingField; } init{____LT____Height____GT____k_BackingField=value;} } public readonly int ____LT____Height____GT____k_BackingField;
}
";

TestRewrite_LinePreserve(original, expected);
}

[TestMethod]
public void TestImplementAutoPropertyDontTweakPropertyInLHSOfAssignment()
{
var original = @"
public class A
{
public A()
{
int[] aaa = new int[] { 1 };
aaa[0] = 0;
}
}
";

var expected = @"
public class A
{
public A()
{
int[] aaa = new int[] { 1 };
aaa[0] = 0;
}
}
";

TestRewrite_LinePreserve(original, expected);
Expand Down
26 changes: 26 additions & 0 deletions tests/TestImposeThisPrefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,32 @@ public void f()
var inc = [return: Example(1)] (int s) => s++;
}
}
";

TestRewrite_LinePreserve(original, expected);
}

[TestMethod]
public void TestImposeThisPrefixWithNameof()
{
var original = @"
public class Destroyer
{
public void f()
{
var x = nameof(f);
}
}
";

var expected = @"
public class Destroyer
{
public void f()
{
var x = nameof(f);
}
}
";

TestRewrite_LinePreserve(original, expected);
Expand Down