Skip to content

Commit

Permalink
Bumped version v1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
FaustVX committed Nov 19, 2023
1 parent 0e815f9 commit 48a29ca
Show file tree
Hide file tree
Showing 32 changed files with 358 additions and 14 deletions.
6 changes: 1 addition & 5 deletions ConsoleApp1/ConsoleApp1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<PropertyGroup>
<RestoreSources Condition="Exists('../PrimaryParameter.SG/bin/Debug')">$(RestoreSources);../PrimaryParameter.SG/bin/Debug</RestoreSources>
</PropertyGroup>

<ItemGroup>
<!-- Exclude the output of source generators from the compilation -->
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FaustVX.PrimaryParameter.SG" Version="1.3.0" />
<PackageReference Include="FaustVX.PrimaryParameter.SG" Version="1.3.1" />
<!-- <ProjectReference Include="..\PrimaryParameter.SG\PrimaryParameter.SG.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="All" /> -->
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion PrimaryParameter.SG/PrimaryParameter.SG.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>FaustVX.PrimaryParameter.SG</PackageId>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Authors>FaustVX</Authors>
<RepositoryUrl>https://github.com/FaustVX/PrimaryParameter</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
26 changes: 19 additions & 7 deletions PrimaryParameter.SG/ReportErrorWhenAccessingPrimaryParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public override void VisitIdentifierName(IdentifierNameSyntax node)
var nodeSymbol = semanticModel.GetSymbolInfo(node).Symbol;
if (_paramSymbol.Equals(nodeSymbol, SymbolEqualityComparer.Default))
{
if (allowInMemberInit && node.Parent?.Parent?.Parent?.Parent switch
{
FieldDeclarationSyntax { Declaration.Variables: [{ Initializer.Value: var init }] } => init == node,
PropertyDeclarationSyntax { Initializer.Value: var init } => init == node,
_ => false,
})
return;
if (allowInMemberInit && Contains(node, static node => node switch
{
FieldDeclarationSyntax => true,
PropertyDeclarationSyntax { Initializer: not null, ExpressionBody: null } => true,
_ => false,
}))
return;
if (!parameter.FieldNames.Any(n => n.Name == _paramSymbol.Name) && !IsIOperation<INameOfOperation>(node) && !IsInParameterListSyntax((ParameterListSyntax)_parameterSyntax.Parent!, node))
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.ErrorWhenAccessingPrimaryParameter, node.GetLocation(), ImmutableDictionary.Create<string, string?>().Add("fields", string.Join(" ", parameter.FieldNames.Select(static n => n.Name))), nodeSymbol.Name, string.Join(" or ", parameter.FieldNames.Select(static n => $"'{n.Name}'"))));
}
Expand All @@ -34,4 +34,16 @@ private bool IsInParameterListSyntax(ParameterListSyntax parameterList, SyntaxNo
private bool IsIOperation<TOp>(SyntaxNode node)
where TOp : IOperation
=> semanticModel.GetOperation(node) is TOp || (node.Parent is not null && IsIOperation<TOp>(node.Parent));


private bool Contains(SyntaxNode node, Func<SyntaxNode, bool> contains)
{
if (node == null)
return false;
if (contains(node))
return true;
if (node.Parent is not null)
return Contains(node.Parent, contains);
return false;
}
}
69 changes: 68 additions & 1 deletion PrimaryParameter.Tests/PrimaryParameterSnapshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public partial class C([DontUse] int i)
}

[Fact]
public Task DontGeneratesPC01WithDontUseOnMember()
public Task DontGeneratesPC01WithDontUseOnMember_Simple()
{
// The source code to test
var source = """
Expand All @@ -173,6 +173,73 @@ public partial class C([DontUse(AllowInMemberInit = true)] int i)
return TestHelper.Verify(source);
}

[Fact]
public Task DontGeneratesPC01WithDontUseOnMember_Complex()
{
// The source code to test
var source = """
using PrimaryParameter.SG;
public partial class C([DontUse(AllowInMemberInit = true)] int i)
{
string L = i.ToString();
}
""";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
}

[Fact]
public Task DontGeneratesPC01WithDontUseOnPropertyInitializer()
{
// The source code to test
var source = """
using PrimaryParameter.SG;
public partial class C([DontUse(AllowInMemberInit = true)] int i)
{
string L { get; } = i.ToString();
}
""";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
}

[Fact]
public Task DoGeneratesPC01WithDontUseOnPropertyBody()
{
// The source code to test
var source = """
using PrimaryParameter.SG;
public partial class C([DontUse] int i)
{
int M => i;
}
""";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
}

[Fact]
public Task DoGeneratesPC01WithDontUseOnPropertyGet()
{
// The source code to test
var source = """
using PrimaryParameter.SG;
public partial class C([DontUse] int i)
{
int M
{
get => i;
}
}
""";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
}

[Fact]
public Task DoGeneratesPC01WithDontUseOnMember()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Diagnostics: [
{
Id: PC01,
Title: Accessing a Primary Parameter,
Severity: Error,
WarningLevel: 0,
Location: : (3,13)-(3,14),
MessageFormat: Can't access a primary parameter ('{0}') with a [Field] [RefField], [Property] or [DontUse] attribute, use {1},
Message: Can't access a primary parameter ('i') with a [Field] [RefField], [Property] or [DontUse] attribute, use ,
Category: tests
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: DontUseAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
sealed class DontUseAttribute : Attribute
{
public bool AllowInMemberInit { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//HintName: FaustVX.PrimaryParameter.SG.g.cs
// <auto-generated/>
partial class C
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//HintName: FieldAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class FieldAttribute : Attribute
{
public string Name { get; init; }
public string AssignFormat { get; init; }
public Type Type { get; init; }
public bool IsReadonly { get; init; }
public string Scope { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//HintName: PropertyAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class PropertyAttribute : Attribute
{
public string Name { get; init; }
public string AssignFormat { get; init; }
public Type Type { get; init; }
public string Setter { get; init; }
public string Scope { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//HintName: RefFieldAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class RefFieldAttribute : Attribute
{
public string Name { get; init; }
public string Scope { get; init; }
public bool IsReadonlyRef { get; init; }
public bool IsRefReadonly { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Diagnostics: [
{
Id: PC01,
Title: Accessing a Primary Parameter,
Severity: Error,
WarningLevel: 0,
Location: : (5,15)-(5,16),
MessageFormat: Can't access a primary parameter ('{0}') with a [Field] [RefField], [Property] or [DontUse] attribute, use {1},
Message: Can't access a primary parameter ('i') with a [Field] [RefField], [Property] or [DontUse] attribute, use ,
Category: tests
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: DontUseAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
sealed class DontUseAttribute : Attribute
{
public bool AllowInMemberInit { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//HintName: FaustVX.PrimaryParameter.SG.g.cs
// <auto-generated/>
partial class C
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//HintName: FieldAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class FieldAttribute : Attribute
{
public string Name { get; init; }
public string AssignFormat { get; init; }
public Type Type { get; init; }
public bool IsReadonly { get; init; }
public string Scope { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//HintName: PropertyAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class PropertyAttribute : Attribute
{
public string Name { get; init; }
public string AssignFormat { get; init; }
public Type Type { get; init; }
public string Setter { get; init; }
public string Scope { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//HintName: RefFieldAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class RefFieldAttribute : Attribute
{
public string Name { get; init; }
public string Scope { get; init; }
public bool IsReadonlyRef { get; init; }
public bool IsRefReadonly { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: DontUseAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
sealed class DontUseAttribute : Attribute
{
public bool AllowInMemberInit { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//HintName: FaustVX.PrimaryParameter.SG.g.cs
// <auto-generated/>
partial class C
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//HintName: FieldAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class FieldAttribute : Attribute
{
public string Name { get; init; }
public string AssignFormat { get; init; }
public Type Type { get; init; }
public bool IsReadonly { get; init; }
public string Scope { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//HintName: PropertyAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class PropertyAttribute : Attribute
{
public string Name { get; init; }
public string AssignFormat { get; init; }
public Type Type { get; init; }
public string Setter { get; init; }
public string Scope { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//HintName: RefFieldAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class RefFieldAttribute : Attribute
{
public string Name { get; init; }
public string Scope { get; init; }
public bool IsReadonlyRef { get; init; }
public bool IsRefReadonly { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: DontUseAttribute.g.cs
// <auto-generated/>
using global::System;
namespace PrimaryParameter.SG
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
sealed class DontUseAttribute : Attribute
{
public bool AllowInMemberInit { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//HintName: FaustVX.PrimaryParameter.SG.g.cs
// <auto-generated/>
partial class C
{
}
Loading

0 comments on commit 48a29ca

Please sign in to comment.