Skip to content
This repository was archived by the owner on Apr 8, 2019. It is now read-only.

Commit 0b8d93d

Browse files
committed
Modify original code to work in standalone analyzer context
1 parent b024b1f commit 0b8d93d

File tree

12 files changed

+965
-415
lines changed

12 files changed

+965
-415
lines changed

PublicApiAnalyzer/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<PropertyGroup>
21-
<LangVersion>6</LangVersion>
21+
<LangVersion>7.1</LangVersion>
2222
<Features>strict</Features>
2323
</PropertyGroup>
2424

PublicApiAnalyzer/PublicApiAnalyzer.CodeFixes/ApiDesign/DeclarePublicAPIFix.cs

Lines changed: 99 additions & 93 deletions
Large diffs are not rendered by default.

PublicApiAnalyzer/PublicApiAnalyzer.Test/ApiDesign/DeclarePublicAPIAnalyzerTests.cs

Lines changed: 377 additions & 4 deletions
Large diffs are not rendered by default.

PublicApiAnalyzer/PublicApiAnalyzer.Test/Helpers/DiagnosticVerifier.Helper.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ protected virtual Solution CreateSolution(ProjectId projectId, string language)
127127
if (publicApi != null)
128128
{
129129
var documentId = DocumentId.CreateNewId(projectId);
130-
solution = solution.AddAdditionalDocument(documentId, DeclarePublicAPIAnalyzer.UnshippedFileName, publicApi);
130+
solution = solution.AddAdditionalDocument(documentId, DeclarePublicAPIAnalyzer.UnshippedFileName, publicApi, filePath: this.GetUnshippedPublicApiFilePath());
131131
}
132132

133133
publicApi = this.GetShippedPublicApi();
134134
if (publicApi != null)
135135
{
136136
var documentId = DocumentId.CreateNewId(projectId);
137-
solution = solution.AddAdditionalDocument(documentId, DeclarePublicAPIAnalyzer.ShippedFileName, publicApi);
137+
solution = solution.AddAdditionalDocument(documentId, DeclarePublicAPIAnalyzer.ShippedFileName, publicApi, filePath: this.GetShippedPublicApiFilePath());
138138
}
139139

140140
ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions;
@@ -159,6 +159,11 @@ protected virtual string GetUnshippedPublicApi()
159159
return null;
160160
}
161161

162+
protected virtual string GetUnshippedPublicApiFilePath()
163+
{
164+
return null;
165+
}
166+
162167
/// <summary>
163168
/// Gets the content of the settings file to use.
164169
/// </summary>
@@ -168,6 +173,11 @@ protected virtual string GetShippedPublicApi()
168173
return null;
169174
}
170175

176+
protected virtual string GetShippedPublicApiFilePath()
177+
{
178+
return null;
179+
}
180+
171181
protected DiagnosticResult CSharpDiagnostic(string diagnosticId = null)
172182
{
173183
var analyzers = this.GetCSharpDiagnosticAnalyzers();

PublicApiAnalyzer/PublicApiAnalyzer/ApiDesign/DeclarePublicAPIAnalyzer.Impl.cs

Lines changed: 179 additions & 169 deletions
Large diffs are not rendered by default.

PublicApiAnalyzer/PublicApiAnalyzer/ApiDesign/DeclarePublicAPIAnalyzer.cs

Lines changed: 145 additions & 145 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace PublicApiAnalyzer.Helpers
5+
{
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Microsoft.CodeAnalysis;
9+
10+
internal static class IMethodSymbolExtensions
11+
{
12+
public static bool HasOptionalParameters(this IMethodSymbol methodSymbol)
13+
{
14+
return methodSymbol.Parameters.Any(p => p.IsOptional);
15+
}
16+
17+
public static IEnumerable<IMethodSymbol> GetOverloads(this IMethodSymbol method)
18+
{
19+
foreach (var member in method?.ContainingType?.GetMembers(method.Name).OfType<IMethodSymbol>())
20+
{
21+
if (!member.Equals(method))
22+
{
23+
yield return member;
24+
}
25+
}
26+
}
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace PublicApiAnalyzer.Helpers
5+
{
6+
using System.Collections.Immutable;
7+
using Microsoft.CodeAnalysis;
8+
9+
internal static class ISymbolExtensions
10+
{
11+
public static bool IsDefaultConstructor(this ISymbol symbol)
12+
{
13+
return symbol.IsConstructor() && symbol.GetParameters().Length == 0;
14+
}
15+
16+
public static bool IsConstructor(this ISymbol symbol)
17+
{
18+
return (symbol as IMethodSymbol)?.MethodKind == MethodKind.Constructor;
19+
}
20+
21+
public static ImmutableArray<IParameterSymbol> GetParameters(this ISymbol symbol)
22+
{
23+
return symbol.TypeSwitch(
24+
(IMethodSymbol m) => m.Parameters,
25+
(IPropertySymbol p) => p.Parameters,
26+
_ => ImmutableArray.Create<IParameterSymbol>());
27+
}
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace PublicApiAnalyzer.Helpers
5+
{
6+
using System;
7+
8+
internal static class ObjectExtensions
9+
{
10+
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TBaseType, TResult> defaultFunc = null)
11+
where TDerivedType1 : TBaseType
12+
where TDerivedType2 : TBaseType
13+
{
14+
if (obj is TDerivedType1 derived1)
15+
{
16+
return matchFunc1(derived1);
17+
}
18+
else if (obj is TDerivedType2 derived2)
19+
{
20+
return matchFunc2(derived2);
21+
}
22+
else if (defaultFunc != null)
23+
{
24+
return defaultFunc(obj);
25+
}
26+
else
27+
{
28+
return default;
29+
}
30+
}
31+
}
32+
}

PublicApiAnalyzer/PublicApiAnalyzer/RoslynDiagnosticIds.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ internal static class RoslynDiagnosticIds
1010
public const string ExposedNoninstantiableTypeRuleId = "RS0022";
1111
public const string PublicApiFilesInvalid = "RS0024";
1212
public const string DuplicatedSymbolInPublicApiFiles = "RS0025";
13+
public const string AvoidMultipleOverloadsWithOptionalParameters = "RS0026";
14+
public const string OverloadWithOptionalParametersShouldHaveMostParameters = "RS0027";
1315
}
1416
}

0 commit comments

Comments
 (0)