Skip to content
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

CA2241: check strings from resx-generated resources #7413

Open
LukasGelke opened this issue Sep 16, 2024 · 0 comments
Open

CA2241: check strings from resx-generated resources #7413

LukasGelke opened this issue Sep 16, 2024 · 0 comments

Comments

@LukasGelke
Copy link

Analyzer

Diagnostic ID: CA2241

Describe the improvement

currently, the Analyzer only checks for constants:

IArgumentOperation formatStringArgument = invocation.Arguments[info.FormatStringIndex];
if (!Equals(formatStringArgument.Value.Type, formatInfo.String) ||
!(formatStringArgument.Value.ConstantValue.Value is string stringFormat))
{
// wrong argument or not a constant
return;
}

As we have applications that are localized through simple resx-Strings (with multiple corresponding resx files for different languages), or contain larger texts with placeholders in resx (that both get ultimatively get passed to string.Format) it would be great to have the analyzer check all corresponding strings, to ensure they're all correctly written. (Should also apply for single/neutral language resx).

Describe suggestions on how to achieve the rule

Not sure if a mechanism like this already exists, but:

  • add resx to AdditionalFiles, so the Analyzer has access to them(?)
  • optionally enable a editorconfig option, if that feature should not be enabled by default
  • instead of returning, check that setting
  • check if it is instead a PropertyReference to a string of a resource class
  • determine the corresponding resx file(s) ("the magic")
  • parse (or cache) the resx as such, and find the corresponding value to the key/property
  • check the value the same as you would the const

Additional context

(none)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant