Skip to content

Commit 168dcbd

Browse files
authored
Add rule template and documentation (#2215)
* Add rule template and documentation * Correct H-levels for Parameters * Remove duplicated examples
1 parent f6cecef commit 168dcbd

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

docs/rule-template-docs.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# PSScriptAnalyzer rule template guidance
2+
3+
The [rule-template.md](rule-template.md) file illustrates the structure of the PSScriptAnalyzer rule
4+
articles. Replace all placeholder text, then remove optional sections that don't apply.
5+
6+
## Required content
7+
8+
Every rule article starts with these YAML frontmatter fields:
9+
10+
- `description`
11+
- `ms.date`
12+
- `ms.topic: reference`
13+
- `title`
14+
15+
Place the H1, severity line, and default-state line immediately after the frontmatter. Follow them
16+
with `## Description`, which explains the diagnostic, why it matters, and the preferred alternative.
17+
18+
Each rule must have an `## Example` section. Each example includes a `### Noncompliant` code block
19+
followed by a `### Compliant` alternative. Give each scenario an H3 heading and use H4 headings for
20+
its noncompliant and compliant code pair.
21+
22+
Every rule article includes `## Configure rule` after its example or examples. For a configurable
23+
rule, include a `Rules` hashtable entry. For an always-enabled or otherwise nonconfigurable rule,
24+
use the following text.
25+
26+
```markdown
27+
This rule is always enabled and isn't configurable. Use one of the following methods to avoid using
28+
this rule:
29+
30+
- Create a custom rule configuration file to include only the rules you want or exclude the rules
31+
you don't want.
32+
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
33+
code blocks. For more information, see the _Suppressing rules_ section of
34+
[Using PSScriptAnalyzer][02].
35+
```
36+
37+
## Optional sections
38+
39+
Use an additional H2 explanatory section between `## Description` and the example when readers need
40+
context before reviewing the code. Existing articles use this space for compatibility profile
41+
information, reference tables, supported values, and remediation guidance. For examples, see the
42+
following articles.
43+
44+
- [AvoidUsingConvertToSecureStringWithPlainText](Rules/AvoidUsingConvertToSecureStringWithPlainText.md)
45+
- [UseConsistentParameterSetName](Rules/UseConsistentParameterSetName.md)
46+
- [UseConstrainedLanguageMode](Rules/UseConstrainedLanguageMode.md)
47+
48+
When the rule is configurable, include `## Parameters` section after `## Configure rule`. Use an H3
49+
heading for each setting and document what it controls, accepted values, and its default value.
50+
51+
After the `## Configure rule` and any parameter sections, include a `## Suppression` section only
52+
when the rule needs specific suppression syntax or examples. Otherwise, link readers to the general
53+
_Suppressing rules_ guidance from **Configure rule**.
54+
55+
In the `## Further reading` section, provide links to additional resources that help readers
56+
understand the rule, its context, or related topics.
57+
58+
## Final checks
59+
60+
- Include `## Configure rule` for every rule article.
61+
- Include `### Parameters` only for configurable settings that need individual documentation.
62+
- Pair noncompliant code with a practical compliant alternative.
63+
- Remove all unused optional headings, placeholder text, and link definitions.

docs/rule-template.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
description: {{Brief description of the rule behavior}}
3+
ms.date: {{MM/DD/YYYY}}
4+
ms.topic: reference
5+
title: {{RuleName}}
6+
---
7+
# {{RuleName}}
8+
9+
**Severity Level: {{Error | Warning | Information}}**
10+
11+
**Default state: {{Enabled | Disabled | Always enabled}}**
12+
13+
## Description
14+
15+
{{Explain what the rule detects, why the pattern is a problem, and the recommended
16+
practice.}}
17+
18+
## {{Optional explanatory topic}}
19+
20+
{{Add a table, supported values, compatibility information, or remediation guidance
21+
when the rule needs context before its examples. Remove this section when it doesn't
22+
apply.}}
23+
24+
## Examples
25+
26+
### Noncompliant
27+
28+
{{Describe the scenario as necessary.}}
29+
30+
```powershell
31+
{{Code that produces the diagnostic}}
32+
```
33+
34+
### Compliant
35+
36+
{{Describe the scenario as necessary.}}
37+
38+
```powershell
39+
{{Equivalent code that doesn't produce the diagnostic}}
40+
```
41+
42+
## Configure rule
43+
44+
{{For a configurable rule, use the following configuration and include the Parameters
45+
section. For a nonconfigurable rule, replace this text with an explanation that the
46+
rule isn't configurable and describe available exclusion or suppression options.}}
47+
48+
```powershell
49+
@{
50+
Rules = @{
51+
PS{{RuleName}} = @{
52+
Enable = $true
53+
{SettingName} = {Value}
54+
}
55+
}
56+
}
57+
```
58+
59+
## Parameters
60+
61+
### {{SettingName}}
62+
63+
{{Explain what the setting controls, its accepted value type or values, and its
64+
default value. Add another H3 section for each setting. Remove this section for a
65+
nonconfigurable rule.}}
66+
67+
## Suppression
68+
69+
{{Explain any rule-specific suppression syntax or examples. Remove this section when
70+
general suppression guidance linked from Configure rule is enough.}}
71+
72+
```powershell
73+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PS{{RuleName}}', '')]
74+
```
75+
76+
## Further reading
77+
78+
- {{[Article title][01]}}
79+
- [Using PSScriptAnalyzer][02]
80+
81+
<!-- Link references -->
82+
[01]: {{URL or absolute path}}
83+
[02]: ../using-scriptanalyzer.md

0 commit comments

Comments
 (0)