Skip to content

Commit 77688a2

Browse files
authored
Support Unity (#1349)
1 parent 2354f1f commit 77688a2

File tree

112 files changed

+13095
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+13095
-413
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ jobs:
109109
working-directory: src/${{ matrix.component.name }}.CodeFixes
110110
steps:
111111
- uses: actions/checkout@v3
112-
- run: dotnet restore
113-
- run: dotnet build --no-restore /p:Roslynator${{ matrix.component.propertyName }}NuGet=true
112+
- run: dotnet restore --force /p:RoslynVersion=roslyn3.8
113+
- run: dotnet build --no-restore /p:Roslynator${{ matrix.component.propertyName }}NuGet=true /p:RoslynVersion=roslyn3.8
114+
- run: dotnet restore --force /p:RoslynVersion=roslyn4.7
115+
- run: dotnet build --no-restore /p:Roslynator${{ matrix.component.propertyName }}NuGet=true /p:RoslynVersion=roslyn4.7
114116
- run: dotnet pack --no-build
115117
- uses: actions/upload-artifact@v3
116118
with:
@@ -158,7 +160,7 @@ jobs:
158160
steps:
159161
- uses: actions/checkout@v3
160162
- run: dotnet restore
161-
- run: dotnet build --no-restore /p:DefineConstants=VSCODE
163+
- run: dotnet build --no-restore
162164
- run: |
163165
mkdir package/roslyn/analyzers
164166
mkdir package/roslyn/refactorings

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add support for Unity ([PR](https://github.com/dotnet/roslynator/pull/1349))
13+
- [Unity uses Roslyn 3.8](https://docs.unity3d.com/Manual/roslyn-analyzers.html) and this version is now supported by Roslynator NuGet packages with analyzers (Roslynator.Analyzers etc.)
14+
1015
### Fixed
1116

1217
- Fix analyzer [RCS0034](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0034) ([PR](https://github.com/dotnet/roslynator/pull/1351))

src/Analyzers.CodeFixes/Analyzers.CodeFixes.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<PropertyGroup Condition="'$(RoslynVersion)' != ''">
8+
<BaseOutputPath>bin\$(RoslynVersion)\</BaseOutputPath>
9+
</PropertyGroup>
10+
711
<PropertyGroup>
812
<Version>$(RoslynatorAnalyzersVersion)</Version>
913
<AssemblyName>Roslynator.CSharp.Analyzers.CodeFixes</AssemblyName>
@@ -12,7 +16,7 @@
1216
<NuspecProperties>configuration=$(Configuration);version=$(RoslynatorAnalyzersPackageVersion)</NuspecProperties>
1317
<IsPackable>true</IsPackable>
1418
<IncludeSymbols>false</IncludeSymbols>
15-
</PropertyGroup>
19+
</PropertyGroup>
1620

1721
<ItemGroup>
1822
<None Remove="tools\install.ps1" />

src/Analyzers.CodeFixes/CSharp/CodeFixes/ImplementNonGenericCounterpartCodeFixProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ private static async Task<Document> RefactorAsync(
303303
newTypeDeclaration = recordDeclaration.WithBaseList(baseList.WithTypes(baseTypes));
304304
}
305305
else if (kind == SyntaxKind.StructDeclaration
306-
|| kind == SyntaxKind.RecordStructDeclaration)
306+
#if ROSLYN_4_0
307+
|| kind == SyntaxKind.RecordStructDeclaration
308+
#endif
309+
)
307310
{
308311
var structDeclaration = (StructDeclarationSyntax)newTypeDeclaration;
309312

src/Analyzers.CodeFixes/CSharp/CodeFixes/ParameterNameDiffersFromBaseCodeFixProvider.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
8383

8484
CodeAction codeAction = CodeAction.Create(
8585
$"Rename '{oldName}' to '{newName}'",
86-
ct => Renamer.RenameSymbolAsync(
87-
context.Document.Solution(),
88-
parameterSymbol,
89-
default(SymbolRenameOptions),
90-
newName,
91-
ct),
86+
ct =>
87+
#if ROSLYN_4_4
88+
Renamer.RenameSymbolAsync(context.Document.Solution(), parameterSymbol, default(SymbolRenameOptions), newName, ct),
89+
#else
90+
Renamer.RenameSymbolAsync(context.Document.Solution(), parameterSymbol, newName, default(Microsoft.CodeAnalysis.Options.OptionSet), ct),
91+
#endif
9292
GetEquivalenceKey(diagnostic));
9393

9494
context.RegisterCodeFix(codeAction, diagnostic);

src/Analyzers.CodeFixes/CSharp/CodeFixes/RemoveEmptySyntaxCodeFixProvider.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4040
case SyntaxKind.EmptyStatement:
4141
case SyntaxKind.FinallyClause:
4242
case SyntaxKind.NamespaceDeclaration:
43+
#if ROSLYN_4_0
4344
case SyntaxKind.FileScopedNamespaceDeclaration:
45+
#endif
4446
case SyntaxKind.ObjectCreationExpression:
4547
case SyntaxKind.RegionDirectiveTrivia:
4648
return true;
@@ -98,6 +100,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
98100
context.RegisterCodeFix(codeAction, diagnostic);
99101
break;
100102
}
103+
#if ROSLYN_4_0
101104
case BaseNamespaceDeclarationSyntax namespaceDeclaration:
102105
{
103106
CodeAction codeAction = CodeAction.Create(
@@ -108,6 +111,18 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
108111
context.RegisterCodeFix(codeAction, diagnostic);
109112
break;
110113
}
114+
#else
115+
case NamespaceDeclarationSyntax namespaceDeclaration:
116+
{
117+
CodeAction codeAction = CodeAction.Create(
118+
"Remove empty namespace declaration",
119+
ct => document.RemoveNodeAsync(namespaceDeclaration, ct),
120+
GetEquivalenceKey(diagnostic));
121+
122+
context.RegisterCodeFix(codeAction, diagnostic);
123+
break;
124+
}
125+
#endif
111126
case RegionDirectiveTriviaSyntax regionDirective:
112127
{
113128
CodeAction codeAction = CodeAction.Create(

src/Analyzers.CodeFixes/CSharp/CodeFixes/UnnecessaryRawStringLiteralCodeFixProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
1+
#if ROSLYN_4_2
2+
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
23

34
using System.Collections.Immutable;
45
using System.Composition;
@@ -96,3 +97,4 @@ private static Task<Document> RefactorAsync(
9697
return document.WithTextChangeAsync(interpolatedString.Span, newText, cancellationToken);
9798
}
9899
}
100+
#endif

src/Analyzers.CodeFixes/CSharp/CodeFixes/UnusedParameterCodeFixProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ private static async Task<Solution> RefactorAsync(
6767

6868
string newName = NameGenerators.UnderscoreSuffix.EnsureUniqueParameterName("_", anonymousFunctionSymbol, semanticModel, cancellationToken: cancellationToken);
6969

70+
#if ROSLYN_4_4
7071
return await Renamer.RenameSymbolAsync(document.Solution(), parameterSymbol, default(SymbolRenameOptions), newName, cancellationToken).ConfigureAwait(false);
72+
#else
73+
return await Renamer.RenameSymbolAsync(document.Solution(), parameterSymbol, newName, default(Microsoft.CodeAnalysis.Options.OptionSet), cancellationToken).ConfigureAwait(false);
74+
#endif
7175
}
7276
}

src/Analyzers.CodeFixes/CSharp/CodeFixes/UseExplicitlyOrImplicitlyTypedArrayCodeFixProvider.cs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public override ImmutableArray<string> FixableDiagnosticIds
3131
get { return ImmutableArray.Create(DiagnosticIdentifiers.UseExplicitlyOrImplicitlyTypedArray); }
3232
}
3333

34+
#if ROSLYN_4_0
3435
public override FixAllProvider GetFixAllProvider()
3536
{
3637
return FixAllProvider.Create(async (context, document, diagnostics) => await FixAllAsync(document, diagnostics, context.CancellationToken).ConfigureAwait(false));
@@ -51,6 +52,7 @@ static async Task<Document> FixAllAsync(
5152
return document;
5253
}
5354
}
55+
#endif
5456

5557
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5658
{
@@ -80,39 +82,50 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
8082
diagnostic.Location.SourceSpan,
8183
out SyntaxNode node,
8284
predicate: f => f.IsKind(
85+
#if ROSLYN_4_7
86+
SyntaxKind.CollectionExpression,
87+
#endif
8388
SyntaxKind.ImplicitArrayCreationExpression,
84-
SyntaxKind.ArrayCreationExpression,
85-
SyntaxKind.CollectionExpression)))
89+
SyntaxKind.ArrayCreationExpression)))
8690
{
8791
throw new InvalidOperationException();
8892
}
8993

9094
if (node is ArrayCreationExpressionSyntax arrayCreation)
9195
{
96+
#if ROSLYN_4_7
9297
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ExplicitToCollectionExpression))
9398
{
9499
return (ct => ConvertToCollectionExpressionAsync(document, arrayCreation, ct), UseCollectionExpressionTitle);
95100
}
96101
else
97102
{
103+
#endif
98104
return (ct => ConvertToImplicitAsync(document, arrayCreation, ct), UseImplicitlyTypedArrayTitle);
105+
#if ROSLYN_4_7
99106
}
107+
#endif
100108
}
101109
else if (node is ImplicitArrayCreationExpressionSyntax implicitArrayCreation)
102110
{
103111
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.VarToExplicit))
104112
{
105113
return (ct => ConvertToExplicitAndUseVarAsync(document, implicitArrayCreation, ct), UseCollectionExpressionTitle);
106114
}
115+
#if ROSLYN_4_7
107116
else if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ImplicitToCollectionExpression))
108117
{
109118
return (ct => ConvertToCollectionExpressionAsync(document, implicitArrayCreation, ct), UseCollectionExpressionTitle);
110119
}
111120
else
112121
{
122+
#endif
113123
return (ct => ConvertToExplicitAsync(document, implicitArrayCreation, ct), UseExplicitlyTypedArrayTitle);
124+
#if ROSLYN_4_7
114125
}
126+
#endif
115127
}
128+
#if ROSLYN_4_7
116129
else if (node is CollectionExpressionSyntax collectionExpression)
117130
{
118131
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.CollectionExpressionToImplicit))
@@ -124,6 +137,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
124137
return (ct => ConvertToExplicitAsync(document, collectionExpression, ct), UseExplicitlyTypedArrayTitle);
125138
}
126139
}
140+
#endif
127141
else
128142
{
129143
throw new InvalidOperationException();
@@ -180,23 +194,6 @@ private static async Task<ArrayCreationExpressionSyntax> CreateArrayCreationAsyn
180194
newInitializer);
181195
}
182196

183-
private static async Task<Document> ConvertToExplicitAsync(
184-
Document document,
185-
CollectionExpressionSyntax collectionExpression,
186-
CancellationToken cancellationToken)
187-
{
188-
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
189-
ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(collectionExpression, cancellationToken).ConvertedType;
190-
191-
ArrayCreationExpressionSyntax arrayCreation = ArrayCreationExpression(
192-
Token(SyntaxKind.NewKeyword),
193-
(ArrayTypeSyntax)typeSymbol.ToTypeSyntax().WithSimplifierAnnotation(),
194-
ConvertCollectionExpressionToInitializer(collectionExpression, SyntaxKind.ArrayInitializerExpression))
195-
.WithTriviaFrom(collectionExpression);
196-
197-
return await document.ReplaceNodeAsync(collectionExpression, arrayCreation, cancellationToken).ConfigureAwait(false);
198-
}
199-
200197
private static async Task<Document> ConvertToImplicitAsync(
201198
Document document,
202199
ArrayCreationExpressionSyntax arrayCreation,
@@ -233,6 +230,24 @@ private static async Task<Document> ConvertToImplicitAsync(
233230
return await document.ReplaceNodeAsync(arrayCreation, implicitArrayCreation, cancellationToken).ConfigureAwait(false);
234231
}
235232

233+
#if ROSLYN_4_7
234+
private static async Task<Document> ConvertToExplicitAsync(
235+
Document document,
236+
CollectionExpressionSyntax collectionExpression,
237+
CancellationToken cancellationToken)
238+
{
239+
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
240+
ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(collectionExpression, cancellationToken).ConvertedType;
241+
242+
ArrayCreationExpressionSyntax arrayCreation = ArrayCreationExpression(
243+
Token(SyntaxKind.NewKeyword),
244+
(ArrayTypeSyntax)typeSymbol.ToTypeSyntax().WithSimplifierAnnotation(),
245+
ConvertCollectionExpressionToInitializer(collectionExpression, SyntaxKind.ArrayInitializerExpression))
246+
.WithTriviaFrom(collectionExpression);
247+
248+
return await document.ReplaceNodeAsync(collectionExpression, arrayCreation, cancellationToken).ConfigureAwait(false);
249+
}
250+
236251
private static async Task<Document> ConvertToImplicitAsync(
237252
Document document,
238253
CollectionExpressionSyntax collectionExpression,
@@ -269,4 +284,5 @@ private static async Task<Document> ConvertToCollectionExpressionAsync(
269284

270285
return await document.ReplaceNodeAsync(implicitArrayCreation, collectionExpression, cancellationToken).ConfigureAwait(false);
271286
}
287+
#endif
272288
}

src/Analyzers.CodeFixes/CSharp/CodeFixes/UseImplicitOrExplicitObjectCreationCodeFixProvider.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
4040
context.Span,
4141
out SyntaxNode node,
4242
predicate: f => f.IsKind(
43+
#if ROSLYN_4_7
44+
SyntaxKind.CollectionExpression,
45+
#endif
4346
SyntaxKind.ObjectCreationExpression,
44-
SyntaxKind.ImplicitObjectCreationExpression,
45-
SyntaxKind.CollectionExpression)))
47+
SyntaxKind.ImplicitObjectCreationExpression)))
48+
4649
{
4750
return;
4851
}
@@ -52,27 +55,35 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
5255

5356
if (node is ObjectCreationExpressionSyntax objectCreation)
5457
{
58+
#if ROSLYN_4_7
5559
bool useCollectionExpression = diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ExplicitToCollectionExpression);
56-
60+
#endif
5761
CodeAction codeAction = CodeAction.Create(
62+
#if ROSLYN_4_7
5863
(useCollectionExpression)
5964
? UseCollectionExpressionTitle
6065
: UseImplicitObjectCreationTitle,
66+
#else
67+
UseImplicitObjectCreationTitle,
68+
#endif
6169
ct =>
6270
{
6371
SyntaxNode newNode;
64-
72+
#if ROSLYN_4_7
6573
if (useCollectionExpression)
6674
{
6775
newNode = ConvertInitializerToCollectionExpression(objectCreation.Initializer).WithFormatterAnnotation();
6876
}
6977
else
7078
{
79+
#endif
7180
newNode = ImplicitObjectCreationExpression(
7281
objectCreation.NewKeyword.WithTrailingTrivia(objectCreation.NewKeyword.TrailingTrivia.EmptyIfWhitespace()),
7382
objectCreation.ArgumentList ?? ArgumentList().WithTrailingTrivia(objectCreation.Type.GetTrailingTrivia()),
7483
objectCreation.Initializer);
84+
#if ROSLYN_4_7
7585
}
86+
#endif
7687

7788
if (objectCreation.IsParentKind(SyntaxKind.EqualsValueClause)
7889
&& objectCreation.Parent.IsParentKind(SyntaxKind.VariableDeclarator)
@@ -90,7 +101,14 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
90101
return document.ReplaceNodeAsync(objectCreation, newNode, ct);
91102
}
92103
},
93-
GetEquivalenceKey(diagnostic, (useCollectionExpression) ? UseCollectionExpressionEquivalenceKey : null));
104+
GetEquivalenceKey(
105+
diagnostic,
106+
#if ROSLYN_4_7
107+
(useCollectionExpression) ? UseCollectionExpressionEquivalenceKey : null
108+
#else
109+
null
110+
#endif
111+
));
94112

95113
context.RegisterCodeFix(codeAction, diagnostic);
96114
}
@@ -126,6 +144,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
126144

127145
context.RegisterCodeFix(codeAction, diagnostic);
128146
}
147+
#if ROSLYN_4_7
129148
else if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.ImplicitToCollectionExpression))
130149
{
131150
CodeAction codeAction = CodeAction.Create(
@@ -146,6 +165,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
146165

147166
context.RegisterCodeFix(codeAction, diagnostic);
148167
}
168+
#endif
149169
else
150170
{
151171
CodeAction codeAction = CodeAction.Create(
@@ -174,6 +194,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
174194
context.RegisterCodeFix(codeAction, diagnostic);
175195
}
176196
}
197+
#if ROSLYN_4_7
177198
else if (node is CollectionExpressionSyntax collectionExpression)
178199
{
179200
if (diagnostic.Properties.ContainsKey(DiagnosticPropertyKeys.CollectionExpressionToImplicit))
@@ -228,5 +249,6 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
228249
context.RegisterCodeFix(codeAction, diagnostic);
229250
}
230251
}
252+
#endif
231253
}
232254
}

0 commit comments

Comments
 (0)