Skip to content

Commit 2a4e7d4

Browse files
committed
Add rule template and documentation
1 parent f6cecef commit 2a4e7d4

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

‎docs/rule-template-docs.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 `### Parameter` for each setting after `## Configure rule`.
49+
Use an H3 heading for each setting and document what it controls, accepted values, and its default
50+
value.
51+
52+
Include `## Suppression` after the `## Configure rule` section only when the rule needs specific
53+
suppression syntax or examples. Otherwise, link readers to the general _Suppressing rules_ guidance
54+
from **Configure rule**.
55+
56+
In the `## Further reading` section, provide links to additional resources that help readers
57+
understand the rule, its context, or related topics.
58+
59+
## Final checks
60+
61+
- Include `## Configure rule` for every rule article.
62+
- Include `### Parameters` only for configurable settings that need individual documentation.
63+
- Pair noncompliant code with a practical compliant alternative.
64+
- Remove all unused optional headings, placeholder text, and link definitions.

‎docs/rule-template.md‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
## Example
25+
26+
### Noncompliant
27+
28+
```powershell
29+
{{Code that produces the diagnostic}}
30+
```
31+
32+
### Compliant
33+
34+
```powershell
35+
{{Equivalent code that doesn't produce the diagnostic}}
36+
```
37+
38+
## Examples
39+
40+
### {{Scenario name}}
41+
42+
#### Noncompliant
43+
44+
```powershell
45+
{{Code that produces the diagnostic for this scenario}}
46+
```
47+
48+
#### Compliant
49+
50+
```powershell
51+
{{Equivalent compliant code for this scenario}}
52+
```
53+
54+
## Configure rule
55+
56+
{{For a configurable rule, use the following configuration and include the Parameters
57+
section. For a nonconfigurable rule, replace this text with an explanation that the
58+
rule isn't configurable and describe available exclusion or suppression options.}}
59+
60+
```powershell
61+
@{
62+
Rules = @{
63+
PS{{RuleName}} = @{
64+
Enable = $true
65+
{SettingName} = {Value}
66+
}
67+
}
68+
}
69+
```
70+
71+
## Parameters
72+
73+
### {{SettingName}}
74+
75+
{{Explain what the setting controls, its accepted value type or values, and its
76+
default value. Add another H3 section for each setting. Remove this section for a
77+
nonconfigurable rule.}}
78+
79+
## Suppression
80+
81+
{{Explain any rule-specific suppression syntax or examples. Remove this section when
82+
general suppression guidance linked from Configure rule is enough.}}
83+
84+
```powershell
85+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PS{{RuleName}}', '')]
86+
```
87+
88+
## Further reading
89+
90+
- {{[Article title][01]}}
91+
- [Using PSScriptAnalyzer][02]
92+
93+
<!-- Link references -->
94+
[01]: {{URL or absolute path}}
95+
[02]: ../using-scriptanalyzer.md

0 commit comments

Comments
 (0)