Commit dba2226f authored by Jesse McGrew's avatar Jesse McGrew
Browse files

Fixed analyzer builds.

Updated NuGet packages.
parent 83fc21e0a5a4
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net471</TargetFrameworks>
<TargetFrameworks>net472</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
......
......@@ -38,7 +38,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ZilfAnalyzers.Vsix</RootNamespace>
<AssemblyName>ZilfAnalyzers</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<GeneratePkgDefFile>false</GeneratePkgDefFile>
<IncludeAssemblyInVSIXContainer>false</IncludeAssemblyInVSIXContainer>
<IncludeDebugSymbolsInVSIXContainer>false</IncludeDebugSymbolsInVSIXContainer>
......
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
......@@ -132,7 +133,7 @@ namespace ZilfAnalyzers
var prefix = match.Groups["prefix"].Value;
var rest = match.Groups["rest"].Value;
format = "{0}" + MessageConstantCodeFixProvider.IncrementFormatTokens(rest);
format = "{0}" + IncrementFormatTokens(rest);
newArgs = newArgs.Insert(
0,
SyntaxFactory.LiteralExpression(
......@@ -232,6 +233,15 @@ namespace ZilfAnalyzers
yield return expr;
}
}
[NotNull]
public static string IncrementFormatTokens([NotNull] string format)
{
return MessageConstantAnalyzer.FormatTokenRegex.Replace(
format,
match =>
$"{{{int.Parse(match.Groups["number"].Value) + 1}{match.Groups["suffix"].Value}}}");
}
}
public class LiteralCreation
......
......@@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.Diagnostics;
namespace ZilfAnalyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
[UsedImplicitly]
public class MessageConstantAnalyzer : DiagnosticAnalyzer
{
static readonly DiagnosticDescriptor Rule_DuplicateMessageCode = new DiagnosticDescriptor(
......@@ -67,48 +68,44 @@ namespace ZilfAnalyzers
{
foreach (var attr in field.DescendantNodes().OfType<AttributeSyntax>())
{
if (context.SemanticModel.GetTypeInfo(attr).Type?.Name == "MessageAttribute")
if (IsMessageAttribute(context.SemanticModel, attr) && attr.ArgumentList.Arguments.Count >= 1)
{
if (attr.ArgumentList.Arguments.Count >= 1)
var formatExpr = attr.ArgumentList.Arguments[0].Expression;
var constValue = context.SemanticModel.GetConstantValue(formatExpr);
if (!constValue.HasValue || !(constValue.Value is string formatStr))
continue;
// check for duplicate message
Diagnostic diagnostic;
if (seenFormats.ContainsKey(formatStr))
{
diagnostic = Diagnostic.Create(
Rule_DuplicateMessageFormat,
formatExpr.GetLocation(),
new[] { seenFormats[formatStr] },
classDecl.Identifier);
context.ReportDiagnostic(diagnostic);
}
else
{
var formatExpr = attr.ArgumentList.Arguments[0].Expression;
var constValue = context.SemanticModel.GetConstantValue(formatExpr);
if (constValue.HasValue)
{
if (constValue.Value is string formatStr)
{
// check for duplicate message
if (seenFormats.ContainsKey(formatStr))
{
var diagnostic = Diagnostic.Create(
Rule_DuplicateMessageFormat,
formatExpr.GetLocation(),
new[] { seenFormats[formatStr] },
classDecl.Identifier);
context.ReportDiagnostic(diagnostic);
}
else
{
seenFormats = seenFormats.Add(formatStr, formatExpr.GetLocation());
}
// check for prefixed message
var match = PrefixedMessageFormatRegex.Match(formatStr);
if (match.Success)
{
var diagnostic = Diagnostic.Create(
Rule_PrefixedMessageFormat,
formatExpr.GetLocation(),
match.Groups["prefix"].Value);
context.ReportDiagnostic(diagnostic);
}
}
}
seenFormats = seenFormats.Add(formatStr, formatExpr.GetLocation());
}
// check for prefixed message
var match = PrefixedMessageFormatRegex.Match(formatStr);
if (!match.Success)
continue;
diagnostic = Diagnostic.Create(
Rule_PrefixedMessageFormat,
formatExpr.GetLocation(),
match.Groups["prefix"].Value);
context.ReportDiagnostic(diagnostic);
}
}
......@@ -118,31 +115,46 @@ namespace ZilfAnalyzers
{
var constValue = context.SemanticModel.GetConstantValue(varDecl.Initializer.Value);
if (constValue.HasValue)
if (!constValue.HasValue)
continue;
// check for duplicate code
var value = (int)constValue.Value;
if (seenCodes.Contains(value))
{
// check for duplicate code
var value = (int)constValue.Value;
if (seenCodes.Contains(value))
{
var diagnostic = Diagnostic.Create(
Rule_DuplicateMessageCode,
varDecl.GetLocation(),
varDecl.Initializer.Value,
classDecl.Identifier);
context.ReportDiagnostic(diagnostic);
}
else
{
seenCodes = seenCodes.Add(value);
}
var diagnostic = Diagnostic.Create(
Rule_DuplicateMessageCode,
varDecl.GetLocation(),
varDecl.Initializer.Value,
classDecl.Identifier);
context.ReportDiagnostic(diagnostic);
}
else
{
seenCodes = seenCodes.Add(value);
}
}
}
}
}
public static bool IsMessageAttribute(SemanticModel semanticModel, [NotNull] AttributeSyntax attr)
{
var type = semanticModel.GetTypeInfo(attr).Type;
while (type != null)
{
if (type.Name == "MessageAttribute")
return true;
type = type.BaseType;
}
return false;
}
[NotNull]
static IEnumerable<FieldDeclarationSyntax> GetConstIntFields([NotNull] ClassDeclarationSyntax classDecl, SemanticModel semanticModel)
{
......
......@@ -15,6 +15,7 @@ using Microsoft.CodeAnalysis.Rename;
namespace ZilfAnalyzers
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MessageConstantCodeFixProvider)), Shared]
[UsedImplicitly]
public class MessageConstantCodeFixProvider : CodeFixProvider
{
const string Title = "Move prefix to call sites";
......@@ -84,30 +85,36 @@ namespace ZilfAnalyzers
fieldDecl.WithAdditionalAnnotations(fieldDeclAnnotation)));
var solution = document.Project.Solution;
FieldDeclarationSyntax FindFieldDecl(SyntaxNode node) =>
node.DescendantNodes()
SemanticModel semanticModel;
FieldDeclarationSyntax FindFieldDecl(SyntaxNode node)
{
return node.DescendantNodes()
.OfType<FieldDeclarationSyntax>()
.Single(n => n.HasAnnotation(fieldDeclAnnotation));
}
ExpressionSyntax FindFormatExpr(SyntaxNode node) =>
FindFieldDecl(node)
ExpressionSyntax FindFormatExpr(SyntaxNode node)
{
return FindFieldDecl(node)
.DescendantNodes()
.OfType<AttributeSyntax>()
.First(a => a.Name.ToString() == "Message" || a.Name.ToString() == "MessageAttribute")
.First(a => MessageConstantAnalyzer.IsMessageAttribute(semanticModel, a))
.DescendantNodes()
.OfType<AttributeArgumentSyntax>()
.First()
.Expression;
}
// get format string and split it into prefix + rest
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
semanticModel = await document.GetSemanticModelAsync(cancellationToken);
root = await document.GetSyntaxRootAsync(cancellationToken);
var formatStr = (string)semanticModel.GetConstantValue(FindFormatExpr(root), cancellationToken).Value;
var match = MessageConstantAnalyzer.PrefixedMessageFormatRegex.Match(formatStr);
var prefix = match.Groups["prefix"].Value;
var rest = match.Groups["rest"].Value;
var newFormatStr = "{0}" + IncrementFormatTokens(rest);
var newFormatStr = "{0}" + ErrorExceptionUsageAnalyzer.IncrementFormatTokens(rest);
// replace format string
var newFormatExpr = SyntaxFactory.LiteralExpression(
......@@ -209,14 +216,5 @@ namespace ZilfAnalyzers
return new PendingReplacement(location.Document, argumentListExpr, newArgumentListExpr);
}
[NotNull]
public static string IncrementFormatTokens([NotNull] string format)
{
return MessageConstantAnalyzer.FormatTokenRegex.Replace(
format,
match =>
$"{{{int.Parse(match.Groups["number"].Value) + 1}{match.Groups["suffix"].Value}}}");
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<TargetFramework>net472</TargetFramework>
<IncludeBuildOutput>false</IncludeBuildOutput>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
......@@ -23,8 +23,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.10.0" PrivateAssets="all" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.1.0" PrivateAssets="all" />
<!--<PackageReference Update="NETStandard.Library" PrivateAssets="all" />-->
</ItemGroup>
......
......@@ -16,7 +16,7 @@
<TargetFrameworkProfile />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0">
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3">
<PrivateAssets>all</PrivateAssets>
<ExcludeAssets>runtime</ExcludeAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers</IncludeAssets>
......
......@@ -17,7 +17,7 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0">
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3">
<PrivateAssets>all</PrivateAssets>
<ExcludeAssets>runtime</ExcludeAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers</IncludeAssets>
......
......@@ -19,7 +19,7 @@
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0">
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3">
<PrivateAssets>all</PrivateAssets>
<ExcludeAssets>runtime</ExcludeAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers</IncludeAssets>
......
......@@ -19,13 +19,13 @@
<Deterministic>false</Deterministic>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0">
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3">
<PrivateAssets>all</PrivateAssets>
<ExcludeAssets>runtime</ExcludeAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="MSBuildTasks" Version="1.5.0.235" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="ReadLine" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
......
......@@ -16,11 +16,11 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="NSubstitute" Version="4.0.0" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src\Zapf\Zapf.csproj" />
......
......@@ -16,9 +16,9 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Moq" Version="4.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
......
......@@ -17,8 +17,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="difflib" Version="2017.7.26.1241" />
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
......
......@@ -17,8 +17,8 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment