Skip to content

Commit 140f2b6

Browse files
committed
Add documentation for .editorconfig properties
1 parent cedfcf9 commit 140f2b6

File tree

1 file changed

+108
-24
lines changed

1 file changed

+108
-24
lines changed

documentation/Configuration.md

+108-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuring StyleCop Analyzers
22

3-
StyleCop Analyzers is configured using two separate mechanisms: code analysis rule set files, and **stylecop.json**.
3+
StyleCop Analyzers can be configured using multiple separate mechanisms:
44

55
1. Code analysis rule set files
66

@@ -12,7 +12,11 @@ StyleCop Analyzers is configured using two separate mechanisms: code analysis ru
1212
* Specify project-specific text, such as the name of the company and the structure to use for copyright headers
1313
* Fine-tune the behavior of certain rules
1414

15-
Code analysis rule sets are the standard way to configure most diagnostic analyzers within Visual Studio. Information about creating and customizing these files can be found in the [Using Rule Sets to Group Code Analysis Rules](https://docs.microsoft.com/visualstudio/code-quality/using-rule-sets-to-group-code-analysis-rules) documentation on docs.microsoft.com.
15+
3. **.editorconfig**
16+
17+
* Can be used in place of rule set files and **stylecop.json**
18+
19+
Code analysis rule sets have been the standard way to configure most diagnostic analyzers within Visual Studio. Information about creating and customizing these files can be found in the [Using Rule Sets to Group Code Analysis Rules](https://docs.microsoft.com/visualstudio/code-quality/using-rule-sets-to-group-code-analysis-rules) documentation on docs.microsoft.com.
1620

1721
An example rule set file containing the default StyleCop Analyzers configuration is available at <https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/rulesets/StyleCopAnalyzersDefault.ruleset>.
1822

@@ -47,8 +51,7 @@ For best results, **stylecop.json** should be included in source control. This w
4751
4852
## Indentation
4953
50-
This section describes the indentation rules which can be configured in **stylecop.json**. Each of the described
51-
properties are configured in the `indentation` object, which is shown in the following sample file.
54+
This section describes the indentation rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties can be configured in the `indentation` object of the **stylecop.json** file, which is shown in the following sample.
5255
5356
```json
5457
{
@@ -61,22 +64,22 @@ properties are configured in the `indentation` object, which is shown in the fol
6164
6265
### Basic Indentation
6366

64-
The following properties are used to configure basic indentation in StyleCop Analyzers.
67+
The following properties are used in **stylecop.json** to configure basic indentation in StyleCop Analyzers.
6568

6669
| Property | Default Value | Minimum Version | Summary |
6770
| --- | --- | --- | --- |
6871
| `indentationSize` | **4** | 1.1.0 | The number of columns to use for each indentation of code. Depending on the `useTabs` and `tabSize` settings, this will be filled with tabs and/or spaces. |
6972
| `tabSize` | **4** | 1.1.0 | The width of a hard tab character in source code. This value is used when converting between tabs and spaces. |
7073
| `useTabs` | **false** | 1.1.0 | **true** to indent using hard tabs; otherwise, **false** to indent using spaces |
7174

75+
When using an **.editorconfig** file to configure StyleCop Analyzers, the basic indentation settings (`indent_size`, `tab_width` and `indent_style`) as described at editorconfig.org can be used.
7276
> :bulb: When working in Visual Studio, the IDE will not automatically adjust editor settings according to the values in
73-
> **stylecop.json**. To provide this functionality as well, we recommend duplicating the basic indentation settings in a
74-
> [**.editorconfig**](http://editorconfig.org/) file. Users of the [EditorConfig](https://visualstudiogallery.msdn.microsoft.com/c8bccfe2-650c-4b42-bc5c-845e21f96328)
77+
> **stylecop.json**. To provide this functionality, we recommend using the **.editorconfig** file instead. Users of the [EditorConfig](https://visualstudiogallery.msdn.microsoft.com/c8bccfe2-650c-4b42-bc5c-845e21f96328)
7578
> extension for Visual Studio will not need to update their C# indentation settings in order to match your project style.
7679
7780
## Spacing Rules
7881

79-
This section describes the features of spacing rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `spacingRules` object, which is shown in the following sample file.
82+
This section describes the features of spacing rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties are configured in the `spacingRules` object of the **stylecop.json** file, which is shown in the following sample.
8083

8184
```json
8285
{
@@ -91,7 +94,7 @@ This section describes the features of spacing rules which can be configured in
9194
9295
## Readability Rules
9396

94-
This section describes the features of readability rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `readabilityRules` object, which is shown in the following sample file.
97+
This section describes the features of readability rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties are configured in the `readabilityRules` object of the **stylecop.json** file, which is shown in the following sample.
9598

9699
```json
97100
{
@@ -104,6 +107,8 @@ This section describes the features of readability rules which can be configured
104107

105108
### Aliases for Built-In Types
106109

110+
The following property is used in **stylecop.json** to configure aliases for built-in types.
111+
107112
| Property | Default Value | Minimum Version | Summary |
108113
| --- | --- | --- | --- |
109114
| `allowBuiltInTypeAliases` | **false** | 1.1.0-beta007 | Specifies whether aliases are allowed for built-in types. |
@@ -118,9 +123,14 @@ HRESULT hr = SomeNativeOperation(); // SA1121
118123

119124
The `allowBuiltInTypeAliases` configuration property can be set to `true` to allow cases like this while continuing to report diagnostics for direct references to the metadata type name, `Int32`.
120125

126+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following property can be used:
127+
```ini
128+
stylecop.readability.allowBuiltInTypeAliases = true
129+
```
130+
121131
## Ordering Rules
122132

123-
This section describes the features of ordering rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `orderingRules` object, which is shown in the following sample file.
133+
This section describes the features of ordering rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties are configured in the `orderingRules` object of the **stylecop.json** file, which is shown in the following sample.
124134

125135
```json
126136
{
@@ -133,7 +143,7 @@ This section describes the features of ordering rules which can be configured in
133143

134144
### Element Order
135145

136-
The following properties are used to configure element ordering in StyleCop Analyzers.
146+
The following properties are used in **stylecop.json** to configure element ordering in StyleCop Analyzers.
137147

138148
| Property | Default Value | Minimum Version | Summary |
139149
| --- | --- | --- | --- |
@@ -194,16 +204,25 @@ rules remain enforced.
194204
}
195205
```
196206

207+
> :bulb: This property can currently not be set in an **.editorconfig** file.
208+
197209
### Using Directives
198210

199-
The following properties are used to configure using directives in StyleCop Analyzers.
211+
The following properties are used in **stylecop.json** to configure using directives in StyleCop Analyzers.
200212

201213
| Property | Default Value | Minimum Version | Summary |
202214
| --- | --- | --- | --- |
203215
| `systemUsingDirectivesFirst` | true | 1.0.0 | Specifies whether `System` using directives are placed before other using directives |
204216
| `usingDirectivesPlacement` | `"insideNamespace"` | 1.0.0 | Specifies the desired placement of using directives |
205217
| `blankLinesBetweenUsingGroups` | `"allow"` | 1.1.0 | Specifies is blank lines are required to separate groups of using statements |
206218

219+
When using an **.editorconfig** file to configure StyleCop Analyzers, the respective properties for [formatting .NET/C#](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules) can be used:
220+
```ini
221+
dotnet_sort_system_directives_first = true
222+
csharp_using_directive_placement = inside_namespace
223+
dotnet_separate_import_directive_groups = true
224+
```
225+
207226
#### Using Directives Placement
208227

209228
The `usingDirectivesPlacement` property affects the behavior of the following rules which report incorrectly placed
@@ -281,7 +300,7 @@ In this mode, a blank line between groups for using directives is *not allowed*.
281300

282301
## Naming Rules
283302

284-
This section describes the features of naming rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `namingRules` object, which is shown in the following sample file.
303+
This section describes the features of naming rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties are configured in the `namingRules` object of the **stylecop.json** file, which is shown in the following sample.
285304

286305
```json
287306
{
@@ -294,7 +313,7 @@ This section describes the features of naming rules which can be configured in *
294313

295314
### Hungarian Notation
296315

297-
The following properties are used to configure allowable Hungarian notation prefixes in StyleCop Analyzers.
316+
The following properties are used in **stylecop.json** to configure allowable Hungarian notation prefixes in StyleCop Analyzers.
298317

299318
| Property | Default Value | Minimum Version | Summary |
300319
| --- | --- | --- | --- |
@@ -316,9 +335,15 @@ The following example shows a settings file which allows the common prefixes as
316335
}
317336
```
318337

338+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following properties can be used:
339+
```ini
340+
stylecop.naming.allowCommonHungarianPrefixes = true
341+
stylecop.naming.allowedHungarianPrefixes = cd, md
342+
```
343+
319344
### Namespace Components
320345

321-
The following property is used to configure allowable namespace components (e.g. ones that start with a lowercase letter).
346+
The following property is used in **stylecop.json** to configure allowable namespace components (e.g. ones that start with a lowercase letter).
322347

323348
| Property | Default Value | Minimum Version | Summary |
324349
| --- | --- | --- | --- |
@@ -339,10 +364,14 @@ The following example shows a settings file which allows namespace components su
339364
}
340365
```
341366

367+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following property can be used:
368+
```ini
369+
stylecop.naming.allowedNamespaceComponents = eBay, iPod
370+
```
342371

343372
### Tuple element names
344373

345-
The following properties are used to configure the behavior of the tuple element name analyzers.
374+
The following properties are used in **stylecop.json** to configure the behavior of the tuple element name analyzers.
346375

347376
| Property | Default Value | Minimum Version | Summary |
348377
| --- | --- | --- | --- |
@@ -362,6 +391,12 @@ The following example shows a settings file which requires tuple element names t
362391
}
363392
```
364393

394+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following properties can be used:
395+
```ini
396+
stylecop.naming.includeInferredTupleElementNames = true
397+
stylecop.naming.tupleElementNameCasing = camelCase
398+
```
399+
365400
#### Tuple Element Name Casing
366401
The `tupleElementNameCasing` property affects the behavior of the [SA1316 Tuple element names should use correct casing](SA1316.md) analyzer.
367402

@@ -376,7 +411,7 @@ In this mode, tuple element names must start with an uppercase letter.
376411

377412
## Maintainability Rules
378413

379-
This section describes the features of maintainability rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `maintainabilityRules` object, which is shown in the following sample file.
414+
This section describes the features of maintainability rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties are configured in the `maintainabilityRules` object of the **stylecop.json** file, which is shown in the following sample.
380415

381416
```json
382417
{
@@ -387,7 +422,7 @@ This section describes the features of maintainability rules which can be config
387422
}
388423
```
389424

390-
The following properties are used to configure maintainability rules in StyleCop Analyzers.
425+
The following properties are used in **stylecop.json** to configure maintainability rules in StyleCop Analyzers.
391426

392427
| Property | Default Value | Minimum Version | Summary |
393428
| --- | --- | --- | --- |
@@ -401,9 +436,11 @@ according to rule SA1402. The following types are supported:
401436
* `enum`
402437
* `delegate`
403438

439+
> :bulb: This property can currently not be set in an **.editorconfig** file.
440+
404441
## Layout Rules
405442

406-
This section describes the features of layout rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `layoutRules` object, which is shown in the following sample file.
443+
This section describes the features of layout rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties are configured in the `layoutRules` object of the **stylecop.json** file, which is shown in the following sample.
407444

408445
```json
409446
{
@@ -414,14 +451,20 @@ This section describes the features of layout rules which can be configured in *
414451
}
415452
```
416453

417-
The following properties are used to configure layout rules in StyleCop Analyzers.
454+
The following properties are used in **stylecop.json** to configure layout rules in StyleCop Analyzers.
418455

419456
| Property | Default Value | Minimum Version | Summary |
420457
| --- | --- | --- | --- |
421458
| `newlineAtEndOfFile` | `"allow"` | 1.0.0 | Specifies the handling for newline characters which appear at the end of a file |
422459
| `allowConsecutiveUsings` | `true` | 1.1.0 | Specifies if SA1519 will allow consecutive using statements without braces |
423460
| `allowDoWhileOnClosingBrace` | `false` | >1.2.0 | Specifies if SA1500 will allow the `while` expression of a `do`/`while` loop to be on the same line as the closing brace, as is generated by the default code snippet of Visual Studio |
424461

462+
When using an **.editorconfig** file to configure StyleCop Analyzers, the newline setting (`insert_final_newline`) as described at editorconfig.org can be used, and the following additional properties:
463+
```ini
464+
stylecop.layout.allowConsecutiveUsings = true
465+
stylecop.layout.allowDoWhileOnClosingBrace = false
466+
```
467+
425468
### Lines at End of File
426469

427470
The behavior of [SA1518](SA1518.md) can be customized regarding the manner in which newline characters at the end of a
@@ -451,7 +494,7 @@ The behavior of [SA1500](SA1500.md) can be customized regarding the manner in wh
451494

452495
## Documentation Rules
453496

454-
This section describes the features of documentation rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `documentationRules` object, which is shown in the following sample file.
497+
This section describes the features of documentation rules which can be configured in **stylecop.json** or **.editorconfig**. Each of the described properties are configured in the `documentationRules` object of the **stylecop.json** file, which is shown in the following sample.
455498

456499
```json
457500
{
@@ -464,7 +507,7 @@ This section describes the features of documentation rules which can be configur
464507

465508
### Copyright Headers
466509

467-
The following properties are used to configure copyright headers in StyleCop Analyzers.
510+
The following properties are used in **stylecop.json** to configure copyright headers in StyleCop Analyzers.
468511

469512
| Property | Default Value | Minimum Version | Summary |
470513
| --- | --- | --- | --- |
@@ -474,6 +517,18 @@ The following properties are used to configure copyright headers in StyleCop Ana
474517
| `variables` | n/a | 1.0.0 | Specifies replacement variables which can be referenced in the `copyrightText` value |
475518
| `headerDecoration` | n/a | 1.1.0 | This value can be set to add a decoration for the header comment so headers look similar to the ones generated by the StyleCop Classic ReSharper fix |
476519

520+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following properties can be used:
521+
```ini
522+
stylecop.documentation.companyName = PlaceholderCompany
523+
stylecop.documentation.copyrightText = Copyright (c) {companyName}. All rights reserved.
524+
stylecop.documentation.xmlHeader = true
525+
stylecop.documentation.headerDecoration = -----------------
526+
```
527+
528+
> :memo: Instead of `stylecop.documentation.copyrightText` the `file_header_template` property as described in [IDE0073 (Require file header)](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0073) can be used. However the StyleCop specific property will take precedence.
529+
530+
> :bulb: The `variables` property can currently not be set in an **.editorconfig** file.
531+
477532
#### Configuring Copyright Text
478533

479534
In order to successfully use StyleCop-checked file headers, most projects will need to configure the `companyName`
@@ -602,6 +657,15 @@ The following example shows a configuration file which requires developers to do
602657
}
603658
```
604659

660+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following properties can be used:
661+
```ini
662+
stylecop.documentation.documentInterfaces = true
663+
stylecop.documentation.documentExposedElements = true
664+
stylecop.documentation.documentInternalElements = true
665+
stylecop.documentation.documentPrivateElements = false
666+
stylecop.documentation.documentPrivateFields = false
667+
```
668+
605669
### Documentation Culture
606670

607671
Some documentation rules require summary texts to start with specific strings. To allow teams to document their code in their native language, **stylecop.json** contains the `documentationCulture` property.
@@ -640,9 +704,19 @@ The following values are currently supported. Unsupported values will automatica
640704
}
641705
```
642706

707+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following property can be used:
708+
```ini
709+
stylecop.documentation.documentationCulture = de-DE
710+
```
711+
643712
### File naming conventions
644713

645-
The `fileNamingConvention` property will determine how the [SA1649 File name should match type name](SA1649.md) analyzer will check file names.
714+
The `fileNamingConvention` property in **stylecop.json** will determine how the [SA1649 File name should match type name](SA1649.md) analyzer will check file names.
715+
716+
| Property | Default Value | Minimum Version | Summary |
717+
| --- | --- | --- | --- |
718+
| `fileNamingConvention` | `"stylecop"` | 1.0.0 | Specifies the convention for file names of generics. |
719+
646720
Given the following code:
647721

648722
```csharp
@@ -658,14 +732,24 @@ File naming convention | Expected file name
658732
stylecop | Class1{T1,T2,T3}.cs
659733
metadata | Class1`3.cs
660734

735+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following property can be used:
736+
```ini
737+
stylecop.documentation.fileNamingConvention = stylecop
738+
```
739+
661740
### Text ending with a period
662741

663-
The [SA1629 Documentation Text Must End With A Period](SA1629.md) analyzer checks if sections within XML documentation end with a period. The following properties can be used to control the behavior of the analyzer:
742+
The [SA1629 Documentation Text Must End With A Period](SA1629.md) analyzer checks if sections within XML documentation end with a period. The following properties can be used in **stylecop.json** to control the behavior of the analyzer:
664743

665744
| Property | Default Value | Minimum Version | Summary |
666745
| --- | --- | --- | --- |
667746
| `excludeFromPunctuationCheck` | `[ "seealso" ]` | 1.1.0 | Specifies the top-level tags within XML documentation that will be excluded from analysis. |
668747

748+
When using an **.editorconfig** file to configure StyleCop Analyzers, the following property can be used:
749+
```ini
750+
stylecop.documentation.excludeFromPunctuationCheck = seealso
751+
```
752+
669753
## Sharing configuration among solutions
670754

671755
It is possible to define your preferred configuration once and reuse it across multiple independent projects. This involves rolling out your own NuGet package,

0 commit comments

Comments
 (0)