-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add analyser which checks uniqueness of translation keys in a single file #63
Merged
smoogipoo
merged 3 commits into
ppy:master
from
bdach:check-localisation-keys-for-uniqueness
May 17, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
LocalisationAnalyser.Tests/Analysers/LocalisationKeyUsedMultipleTimesInClassAnalyserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Threading.Tasks; | ||
using LocalisationAnalyser.Analysers; | ||
using Xunit; | ||
|
||
namespace LocalisationAnalyser.Tests.Analysers | ||
{ | ||
public class LocalisationKeyUsedMultipleTimesInClassAnalyserTests : AbstractAnalyserTests<LocalisationKeyUsedMultipleTimesInClassAnalyser> | ||
{ | ||
[Theory] | ||
[InlineData("DuplicatedLocalisationKeys")] | ||
public Task RunTest(string name) => Check(name); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...lyser.Tests/Resources/Analysers/DuplicatedLocalisationKeys/Localisation/CommonStrings.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using osu.Framework.Localisation; | ||
|
||
namespace TestProject.Localisation | ||
{ | ||
public static class CommonStrings | ||
{ | ||
private const string prefix = @"TestProject.Localisation.Common"; | ||
|
||
/// <summary> | ||
/// "first string" | ||
/// </summary> | ||
public static LocalisableString FirstString => new TranslatableString(getKey([|@"first_string"|]), @"first string"); | ||
|
||
/// <summary> | ||
/// "second string" | ||
/// </summary> | ||
public static LocalisableString SecondString => new TranslatableString(getKey([|@"first_string"|]), @"second string"); | ||
|
||
/// <summary> | ||
/// "third string" | ||
/// </summary> | ||
public static LocalisableString ThirdString => new TranslatableString(getKey(@"third_string"), @"third string"); | ||
|
||
/// <summary> | ||
/// "first string with arguments (like {0})" | ||
/// </summary> | ||
public static LocalisableString FirstStringWithArguments(string test) => new TranslatableString(getKey([|@"first_string"|]), @"first string with arguments (like {0})"); | ||
|
||
/// <summary> | ||
/// "second string with arguments (like {0})" | ||
/// </summary> | ||
public static LocalisableString SecondStringWithArguments(string test) => new TranslatableString(getKey(@"second_string_with_args"), @"second string with arguments (like {0})"); | ||
|
||
private static string getKey(string key) => $@"{prefix}:{key}"; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
LocalisationAnalyser/Analysers/LocalisationKeyUsedMultipleTimesInClassAnalyser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using LocalisationAnalyser.Localisation; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace LocalisationAnalyser.Analysers | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class LocalisationKeyUsedMultipleTimesInClassAnalyser : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
ImmutableArray.Create(DiagnosticRules.LOCALISATION_KEY_USED_MULTIPLE_TIMES_IN_CLASS); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Analyzer%20Actions%20Semantics.md for more information | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
|
||
context.RegisterSyntaxTreeAction(analyseSyntaxTree); | ||
} | ||
|
||
private void analyseSyntaxTree(SyntaxTreeAnalysisContext context) | ||
{ | ||
// Optimisation to not inspect too many files. | ||
if (!context.Tree.FilePath.EndsWith("Strings.cs")) | ||
return; | ||
|
||
if (!LocalisationFile.TryRead(context.Tree, out var file, out _)) | ||
return; | ||
|
||
var duplicateKeys = findDuplicateKeys(file).ToImmutableHashSet(); | ||
|
||
var root = context.Tree.GetRoot(); | ||
|
||
foreach (var property in root.DescendantNodes().OfType<PropertyDeclarationSyntax>()) | ||
markPropertyIfDuplicate(context, property, file, duplicateKeys); | ||
|
||
foreach (var method in root.DescendantNodes().OfType<MethodDeclarationSyntax>()) | ||
markMethodIfDuplicate(context, method, file, duplicateKeys); | ||
} | ||
|
||
private IEnumerable<string> findDuplicateKeys(LocalisationFile localisationFile) | ||
{ | ||
var hashSet = new HashSet<string>(); | ||
|
||
foreach (var member in localisationFile.Members) | ||
{ | ||
if (!hashSet.Add(member.Key)) | ||
yield return member.Key; | ||
} | ||
} | ||
|
||
private void markMethodIfDuplicate(SyntaxTreeAnalysisContext context, MethodDeclarationSyntax method, | ||
LocalisationFile localisationFile, ImmutableHashSet<string> duplicateKeys) | ||
{ | ||
string? name = method.Identifier.Text; | ||
if (name == null) | ||
return; | ||
|
||
var member = localisationFile.Members.SingleOrDefault(m => | ||
m.Name == name && m.Parameters.Length == method.ParameterList.Parameters.Count); | ||
|
||
if (member == null) | ||
return; | ||
|
||
if (!duplicateKeys.Contains(member.Key)) | ||
return; | ||
|
||
var creationExpression = (ObjectCreationExpressionSyntax)method.ExpressionBody.Expression; | ||
var keyArgument = creationExpression.ArgumentList!.Arguments[0]; | ||
|
||
if (keyArgument.Expression is not InvocationExpressionSyntax methodInvocation | ||
|| (methodInvocation.Expression as IdentifierNameSyntax)?.Identifier.Text != "getKey" | ||
|| methodInvocation.ArgumentList.Arguments.Count != 1) | ||
{ | ||
return; | ||
} | ||
|
||
var keyString = methodInvocation.ArgumentList.Arguments[0]; | ||
|
||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.LOCALISATION_KEY_USED_MULTIPLE_TIMES_IN_CLASS, keyString.GetLocation(), member.Key)); | ||
} | ||
|
||
private void markPropertyIfDuplicate(SyntaxTreeAnalysisContext context, PropertyDeclarationSyntax property, | ||
LocalisationFile localisationFile, ImmutableHashSet<string> duplicateKeys) | ||
{ | ||
string? name = property.Identifier.Text; | ||
if (name == null) | ||
return; | ||
|
||
var member = localisationFile.Members.SingleOrDefault(m => m.Name == name && m.Parameters.Length == 0); | ||
|
||
if (member == null) | ||
return; | ||
|
||
if (!duplicateKeys.Contains(member.Key)) | ||
return; | ||
|
||
var creationExpression = (ObjectCreationExpressionSyntax)property.ExpressionBody.Expression; | ||
var keyArgument = creationExpression.ArgumentList!.Arguments[0]; | ||
|
||
if (keyArgument.Expression is not InvocationExpressionSyntax methodInvocation | ||
|| (methodInvocation.Expression as IdentifierNameSyntax)?.Identifier.Text != "getKey" | ||
|| methodInvocation.ArgumentList.Arguments.Count != 1) | ||
{ | ||
return; | ||
} | ||
|
||
var keyString = methodInvocation.ArgumentList.Arguments[0]; | ||
|
||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.LOCALISATION_KEY_USED_MULTIPLE_TIMES_IN_CLASS, keyString.GetLocation(), member.Key)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This purposefully does not inherit
AbstractMemberAnalyser
. I wanted to run the duplicate check exactly once per file, and I don't trust that any other method of passing data inside an analyser other than the stack is safe (as I have no idea what guarantees - if any - there are wrt threading and/or analyser lifetime, and there are indications online that there aren't any to guarantee that anything other than the stack is safe), so in order to not polluteAbstractMemberAnalyser
with things specific to this check, I decided to just do away with inheritance and duplicate the few lines of logic it takes to reimplement it from scratch.