Skip to content

Commit 1de6d8e

Browse files
author
MPCoreDeveloper
committed
updated the deafult list even more
1 parent b673048 commit 1de6d8e

2 files changed

Lines changed: 171 additions & 97 deletions

File tree

Posseth.NamedArguments.AnalyzerAndFixer/NamedArgumentsAnalyzer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public class NamedArgumentsAnalyzer : DiagnosticAnalyzer
8686
"char.ToUpper",
8787
"char.ToUpperInvariant",
8888
"char.GetNumericValue",
89+
"Console.Write",
90+
"Console.WriteLine",
91+
"Debug.WriteLine",
92+
"Trace.WriteLine",
8993
"string.Contains",
9094
"string.EndsWith",
9195
"string.StartsWith",
@@ -102,6 +106,9 @@ public class NamedArgumentsAnalyzer : DiagnosticAnalyzer
102106
"string.Substring",
103107
"string.IsNullOrEmpty",
104108
"string.IsNullOrWhiteSpace",
109+
"string.Join",
110+
"string.Concat",
111+
"string.Format",
105112
"StringBuilder.Append",
106113
"StringBuilder.AppendLine",
107114
"StringBuilder.Insert",
@@ -110,6 +117,10 @@ public class NamedArgumentsAnalyzer : DiagnosticAnalyzer
110117
"CharUnicodeInfo.GetUnicodeCategory",
111118
"CharUnicodeInfo.GetDigitValue",
112119
"CharUnicodeInfo.GetNumericValue",
120+
"Math.Min",
121+
"Math.Max",
122+
"List.Add",
123+
"Dictionary.Add",
113124
"Enumerable.Where",
114125
"Enumerable.Select",
115126
"Enumerable.FirstOrDefault",

README.md

Lines changed: 160 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,147 @@
11
# Posseth NamedArguments AnalyzerAndFixer
22

3-
This is a tool that can be used to analyze and fix named arguments in codebase.
4-
It is implemented as a Analyzer and CodeFix provider for Roslyn.
3+
This is a Roslyn analyzer and code fix provider that enforces the use of named arguments in C# method calls and object creations. It helps improve code readability by requiring named arguments for methods with multiple parameters, while allowing exclusions for commonly used methods where positional arguments are preferred.
4+
5+
## Features
6+
7+
- **Diagnostic**: Warns when positional arguments are used in method calls or object creations with multiple parameters.
8+
- **Code Fix**: Automatically adds named arguments to fix the diagnostic.
9+
- **Configurable**: Supports configuration via .editorconfig files to customize behavior.
10+
- **Exclusions**: Built-in list of excluded methods where positional arguments are acceptable, with options to add custom exclusions.
11+
- **Record Support**: Optional mode to only analyze record types.
512

613
## Installation
7-
There should be a NuGet package available for this project.
8-
9-
## configuration
10-
<b>How to Configure Settings for NamedArgumentsAnalyzer</b>
11-
12-
OnlyForRecords is a boolean and standard value is false.
13-
setting that determines whether the analyzer should only apply to record types. If set to true,
14-
the analyzer will only analyze named arguments in record types. If set to false,
15-
it will analyze named arguments in all types.
16-
17-
UseDefaultExcludedMethods is a boolean and standard value is true.
18-
if UseDefaultExcludedMethods is true, the following methods will be excluded by default:
19-
"System.Char.Equals",
20-
"System.Char.CompareTo",
21-
"System.Char.GetUnicodeCategory",
22-
"System.Char.IsControl",
23-
"System.Char.IsDigit",
24-
"System.Char.IsLetter",
25-
"System.Char.IsLetterOrDigit",
26-
"System.Char.IsLower",
27-
"System.Char.IsNumber",
28-
"System.Char.IsPunctuation",
29-
"System.Char.IsSeparator",
30-
"System.Char.IsSurrogate",
31-
"System.Char.IsSymbol",
32-
"System.Char.IsUpper",
33-
"System.Char.IsWhiteSpace",
34-
"System.Char.ToLower",
35-
"System.Char.ToLowerInvariant",
36-
"System.Char.ToUpper",
37-
"System.Char.ToUpperInvariant",
38-
"System.Char.GetNumericValue",
39-
"System.String.Contains",
40-
"System.String.EndsWith",
41-
"System.String.StartsWith",
42-
"System.String.Equals",
43-
"System.String.IndexOf",
44-
"System.String.LastIndexOf",
45-
"System.String.Replace",
46-
"System.String.Split",
47-
"System.String.Trim",
48-
"System.String.TrimStart",
49-
"System.String.TrimEnd",
50-
"System.String.Insert",
51-
"System.String.Remove",
52-
"System.String.Substring",
53-
"System.String.IsNullOrEmpty",
54-
"System.String.IsNullOrWhiteSpace",
55-
"System.Text.StringBuilder.Append",
56-
"System.Text.StringBuilder.AppendLine",
57-
"System.Text.StringBuilder.Insert",
58-
"System.Text.StringBuilder.Replace",
59-
"System.Text.StringBuilder.Remove",
60-
"System.Globalization.CharUnicodeInfo.GetUnicodeCategory",
61-
"System.Globalization.CharUnicodeInfo.GetDigitValue",
62-
"System.Globalization.CharUnicodeInfo.GetNumericValue",
63-
"System.Linq.Enumerable.Where",
64-
"System.Linq.Enumerable.Select",
65-
"System.Linq.Enumerable.FirstOrDefault",
66-
"System.Linq.Enumerable.First",
67-
"System.Linq.Enumerable.Any",
68-
"System.Linq.Enumerable.OrderBy",
69-
"System.Linq.Enumerable.OrderByDescending",
70-
"System.Linq.Enumerable.GroupBy",
71-
"System.Linq.Enumerable.ToList",
72-
"System.Linq.Enumerable.ToArray",
73-
"System.Linq.Enumerable.Contains",
74-
"System.Linq.Enumerable.ElementAt",
75-
"System.Linq.Enumerable.ElementAtOrDefault",
76-
"System.Linq.Enumerable.All",
77-
"System.Linq.Enumerable.Count",
78-
"System.Linq.Enumerable.Last",
79-
"System.Linq.Enumerable.LastOrDefault"
80-
81-
82-
ExcludedMethodNames is a comma-separated list of method names you want to exclude from the analyzer.
83-
default value is "" thus empty.
84-
85-
the two are NOT mutually exclusive so if you set UseDefaultExcludedMethods to true and also provide a list of method names in ExcludedMethodNames,
86-
both sets of exclusions will be applied.
87-
88-
You can configure the OnlyForRecords and ExcludedMethodNames settings using an .editorconfig file. Here's how to do it:
89-
90-
Option 1: Using .editorconfig File
91-
92-
1. Create or open an .editorconfig file in the root of your solution or project.
93-
2. Add the following configuration entries:
14+
15+
### NuGet Package
16+
17+
Install the analyzer via NuGet:
18+
19+
```bash
20+
dotnet add package Posseth.NamedArguments.AnalyzerAndFixer
21+
```
22+
23+
Or search for "Posseth.NamedArguments.AnalyzerAndFixer" in the NuGet Package Manager.
24+
25+
### Manual Installation
26+
27+
1. Clone the repository.
28+
2. Build the project.
29+
3. Reference the analyzer DLLs in your project or use the VSIX for Visual Studio integration.
30+
31+
## Usage
32+
33+
The analyzer will automatically run during compilation and show warnings for method calls that should use named arguments.
34+
35+
### Example
36+
37+
**Before (triggers diagnostic):**
38+
```csharp
39+
public void Example()
40+
{
41+
var result = string.Replace("hello world", "world", "universe");
42+
}
43+
```
44+
45+
**After (fixed):**
46+
```csharp
47+
public void Example()
48+
{
49+
var result = string.Replace(oldValue: "world", newValue: "universe", input: "hello world");
50+
}
51+
```
52+
53+
Note: `string.Replace` is excluded by default, so this example won't trigger in practice. Use custom methods or disable exclusions to see the behavior.
54+
55+
## Configuration
56+
57+
The analyzer supports several configuration options via .editorconfig files.
58+
59+
### Options
60+
61+
- **OnlyForRecords** (boolean, default: false): If true, only analyzes method calls on record types.
62+
- **UseDefaultExcludedMethods** (boolean, default: true): If true, uses the built-in list of excluded methods.
63+
- **ExcludedMethodNames** (string, default: ""): Comma-separated list of additional method names to exclude.
64+
65+
If `UseDefaultExcludedMethods` is true, the following methods are excluded by default:
66+
"char.Equals",
67+
"char.CompareTo",
68+
"char.GetUnicodeCategory",
69+
"char.IsControl",
70+
"char.IsDigit",
71+
"char.IsLetter",
72+
"char.IsLetterOrDigit",
73+
"char.IsLower",
74+
"char.IsNumber",
75+
"char.IsPunctuation",
76+
"char.IsSeparator",
77+
"char.IsSurrogate",
78+
"char.IsSymbol",
79+
"char.IsUpper",
80+
"char.IsWhiteSpace",
81+
"char.ToLower",
82+
"char.ToLowerInvariant",
83+
"char.ToUpper",
84+
"char.ToUpperInvariant",
85+
"char.GetNumericValue",
86+
"Console.Write",
87+
"Console.WriteLine",
88+
"Debug.WriteLine",
89+
"Trace.WriteLine",
90+
"string.Contains",
91+
"string.EndsWith",
92+
"string.StartsWith",
93+
"string.Equals",
94+
"string.IndexOf",
95+
"string.LastIndexOf",
96+
"string.Replace",
97+
"string.Split",
98+
"string.Trim",
99+
"string.TrimStart",
100+
"string.TrimEnd",
101+
"string.Insert",
102+
"string.Remove",
103+
"string.Substring",
104+
"string.IsNullOrEmpty",
105+
"string.IsNullOrWhiteSpace",
106+
"string.Join",
107+
"string.Concat",
108+
"string.Format",
109+
"StringBuilder.Append",
110+
"StringBuilder.AppendLine",
111+
"StringBuilder.Insert",
112+
"StringBuilder.Replace",
113+
"StringBuilder.Remove",
114+
"CharUnicodeInfo.GetUnicodeCategory",
115+
"CharUnicodeInfo.GetDigitValue",
116+
"CharUnicodeInfo.GetNumericValue",
117+
"Math.Min",
118+
"Math.Max",
119+
"List.Add",
120+
"Dictionary.Add",
121+
"Enumerable.Where",
122+
"Enumerable.Select",
123+
"Enumerable.FirstOrDefault",
124+
"Enumerable.First",
125+
"Enumerable.Any",
126+
"Enumerable.OrderBy",
127+
"Enumerable.OrderByDescending",
128+
"Enumerable.GroupBy",
129+
"Enumerable.ToList",
130+
"Enumerable.ToArray",
131+
"Enumerable.Contains",
132+
"Enumerable.ElementAt",
133+
"Enumerable.ElementAtOrDefault",
134+
"Enumerable.All",
135+
"Enumerable.Count",
136+
"Enumerable.Last",
137+
"Enumerable.LastOrDefault"
138+
139+
### Configuration Methods
140+
141+
#### Option 1: Using .editorconfig File (Recommended)
142+
143+
1. Create or open an .editorconfig file in the root of your solution or project.
144+
2. Add the following configuration entries:
94145

95146
```ini
96147
# Named Arguments Analyzer Configuration
@@ -103,15 +154,18 @@ dotnet_diagnostic.PNA1000.UseDefaultExcludedMethods = true # or false
103154
# Configure ExcludedMethodNames option
104155
dotnet_diagnostic.PNA1000.ExcludedMethodNames = ToString,Equals,GetHashCode
105156
```
106-
Option 2: Configure in Visual Studio
107157

108-
1. Right-click on your solution in Solution Explorer
109-
2. Select Analyze > Configure Code Analysis > For Solution
110-
3. In the dialog that appears, find your analyzer (PNA1000)
111-
4. Configure the settings for OnlyForRecords and ExcludedMethodNames
158+
#### Option 2: Configure in Visual Studio
159+
160+
1. Right-click on your solution in Solution Explorer.
161+
2. Select **Analyze > Configure Code Analysis > For Solution**.
162+
3. In the dialog that appears, find the analyzer (PNA1000).
163+
4. Configure the settings for OnlyForRecords and ExcludedMethodNames.
164+
165+
#### Option 3: Configure in Project File (.csproj)
112166

113-
Option 3: Configure in Project File (.csproj)
114167
You can also add these settings to your project file:
168+
115169
```xml
116170
<PropertyGroup>
117171
<AnalysisMode>All</AnalysisMode>
@@ -123,13 +177,22 @@ You can also add these settings to your project file:
123177
<AnalyzerConfigFiles Include="$(MSBuildProjectDirectory)\.analyzer.config" />
124178
</PropertyGroup>
125179
```
180+
126181
Then create a .analyzer.config file with:
182+
127183
```ini
128184
is_global = true
129185
PNA1000.OnlyForRecords = true
130186
PNA1000.UseDefaultExcludedMethods = true
131187
PNA1000.ExcludedMethodNames = ToString,Equals,GetHashCode
132188
```
133189

134-
The most common and recommended approach is Option 1 with the .editorconfig file,
135-
as it's the standard way to configure analyzer settings in modern .NET projects.
190+
The most common and recommended approach is Option 1 with the .editorconfig file, as it's the standard way to configure analyzer settings in modern .NET projects.
191+
192+
## Contributing
193+
194+
Contributions are welcome! Please open issues or submit pull requests on GitHub.
195+
196+
## License
197+
198+
This project is licensed under the MIT License.

0 commit comments

Comments
 (0)