Skip to content

Commit 7982e64

Browse files
committed
Merge pull request #7 from yutingc/master
Add rule documentations for several rules
2 parents 15bafe4 + e067b4f commit 7982e64

8 files changed

+205
-0
lines changed

RuleDocumentation/AvoidAlias.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#AvoidAlias
2+
**Severity Level: Warning**
3+
4+
5+
##Description
6+
7+
An alias is an alternate name or nickname for a cmdlet or for a command element, such as a function, script, file, or executable file. But when writing scripts that will potentially need to be maintained over time, either by the original author or another Windows PowerShell scripter, please consider using full cmdlet name instead of alias. Aliases can introduce these problems, readability, understandability and availability.
8+
9+
##How to Fix
10+
11+
Please consider using full cmdlet name instead of alias.
12+
13+
##Example
14+
15+
Wrong: gps | where-object {$_.WorkingSet -gt 20000000}
16+
17+
Correct: get-process | where-object {$_.WorkingSet -gt 20000000}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#AvoidDefaultTrueValueSwitchParameter
2+
**Severity Level: Warning**
3+
4+
5+
##Description
6+
7+
Switch Parameters Should Not Default To True
8+
9+
10+
##How to Fix
11+
12+
Please change the default value of the switch parameter to be false.
13+
14+
##Example
15+
16+
Wrong:
17+
18+
Param
19+
( …
20+
$Param1,
21+
[switch]
22+
$switch=$true
23+
)
24+
25+
Correct:
26+
27+
Param
28+
( …
29+
$Param1,
30+
[switch]
31+
$switch=$false
32+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#AvoidEmptyCatchBlock
2+
**Severity Level: Warning**
3+
4+
##Description
5+
6+
Empty catch blocks are considered poor design decisions because if an error occurs in the try block, this error is simply swallowed and not acted upon. While this does not inherently lead to bad things. It can and this should be avoided if possible.
7+
8+
##How to Fix
9+
10+
To fix a violation of this rule, using Write-Error or throw statements in catch blocks.
11+
12+
##Example
13+
Wrong:
14+
15+
try
16+
{
17+
1/0
18+
}
19+
catch [DivideByZeroException]
20+
{
21+
}
22+
23+
Correct:
24+
25+
try
26+
{
27+
1/0
28+
}
29+
catch [DivideByZeroException]
30+
{
31+
Write-Error "DivideByZeroException"
32+
}

RuleDocumentation/AvoidGlobalVars.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#AvoidGlobalVars
2+
**Severity Level: Warning**
3+
4+
##Description
5+
6+
A variable is a unit of memory in which values are stored. Windows PowerShell controls access to variables, functions, aliases, and drives through a mechanism known as scoping. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles.A variable is a unit of memory in which values are stored. Windows PowerShell controls access to variables, functions, aliases, and drives through a mechanism known as scoping. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles.
7+
8+
##How to Fix
9+
10+
To fix a violation of this rule, please consider to use other scope modifiers.
11+
12+
##Example
13+
Wrong:
14+
15+
$Global:var1 = $null
16+
function NotGlobal ($var)
17+
{
18+
$a = $var + $var1
19+
}
20+
21+
Correct:
22+
23+
$Global:var1 = $null
24+
function NotGlobal ($var1,$var2)
25+
{
26+
$a = $var1 + $var2
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#AvoidInvokingEmptyMembers
2+
**Severity Level: Warning**
3+
4+
5+
##Description
6+
7+
Invoking non-constant members would cause potential bugs. Please double check the syntax to make sure members invoked are non-constant.
8+
9+
10+
##How to Fix
11+
12+
To fix a violation of this rule, please provide requested members for given types or classes.
13+
14+
##Example
15+
16+
Wrong:
17+
18+
"abc".('len'+'gth')
19+
20+
Correct:
21+
22+
"abc".('length')
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#AvoidReservedCharInCmdlet
2+
**Severity Level: Error**
3+
4+
5+
##Description
6+
7+
You cannot use following reserved characters in a function name. These characters usually cause a parsing error. Otherwise they will generally cause runtime errors.
8+
'#,(){}[]&/\\$^;:\"'<>|?@`*%+=~'
9+
10+
11+
##How to Fix
12+
13+
To fix a violation of this rule, please remove reserved characters from your advanced function name.
14+
15+
##Example
16+
17+
Wrong:
18+
19+
function test[1]
20+
{...}
21+
22+
Correct:
23+
24+
function test
25+
{...}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#AvoidReservedParams
2+
**Severity Level: Error**
3+
4+
5+
##Description
6+
7+
You cannot use reserved common parameters in an advanced function. If these parameters are defined by the user, an error generally occurs.
8+
9+
##How to Fix
10+
11+
To fix a violation of this rule, please change the name of your parameter.
12+
13+
##Example
14+
15+
Wrong:
16+
17+
function test
18+
{
19+
20+
[CmdletBinding]
21+
Param($ErrorVariable, $b)
22+
}
23+
24+
Correct:
25+
26+
function test
27+
{
28+
29+
[CmdletBinding]
30+
Param($err, $b)
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#AvoidUsingPositionalParameters
2+
**Severity Level: Info**
3+
4+
##Description
5+
6+
To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.
7+
8+
##How to Fix
9+
10+
To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.
11+
12+
##Example
13+
Wrong:
14+
15+
Get-ChildItem *.txt
16+
17+
Correct:
18+
19+
Get-Content -Path *.txt

0 commit comments

Comments
 (0)