Skip to content

Commit 5de8ab9

Browse files
author
Kapil Borle
committed
Add AlignAssignmentStatement rule skeleton
1 parent f7ed15c commit 5de8ab9

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# AlignAssignmentStatement
2+
**Severity Level: Warning**
3+
4+
## Description
5+
6+
## How to Fix
7+
8+
## Example
9+
### Wrong:
10+
```PowerShell
11+
12+
```
13+
14+
### Correct:
15+
```PowerShell
16+
17+
```

Rules/AlignAssignmentStatement.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) Microsoft Corporation.
2+
//
3+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
6+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
9+
// THE SOFTWARE.
10+
11+
using System;
12+
using System.Collections.Generic;
13+
#if !CORECLR
14+
using System.ComponentModel.Composition;
15+
#endif
16+
using System.Globalization;
17+
using System.Linq;
18+
using System.Management.Automation.Language;
19+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
20+
21+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
22+
{
23+
/// <summary>
24+
/// A class to walk an AST to check if consecutive assignment statements are aligned.
25+
/// </summary>
26+
#if !CORECLR
27+
[Export(typeof(IScriptRule))]
28+
#endif
29+
class AlignAssignmentStatement : ConfigurableRule
30+
{
31+
/// <summary>
32+
/// Analyzes the given ast to find if consecutive assignment statements are aligned.
33+
/// </summary>
34+
/// <param name="ast">AST to be analyzed. This should be non-null</param>
35+
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
36+
/// <returns>A an enumerable type containing the violations</returns>
37+
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
38+
{
39+
if (ast == null)
40+
{
41+
throw new ArgumentNullException("ast");
42+
}
43+
44+
// your code goes here
45+
yield break;
46+
}
47+
48+
/// <summary>
49+
/// Retrieves the common name of this rule.
50+
/// </summary>
51+
public override string GetCommonName()
52+
{
53+
return string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementCommonName);
54+
}
55+
56+
/// <summary>
57+
/// Retrieves the description of this rule.
58+
/// </summary>
59+
public override string GetDescription()
60+
{
61+
return string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementDescription);
62+
}
63+
64+
/// <summary>
65+
/// Retrieves the name of this rule.
66+
/// </summary>
67+
public override string GetName()
68+
{
69+
return string.Format(
70+
CultureInfo.CurrentCulture,
71+
Strings.NameSpaceFormat,
72+
GetSourceName(),
73+
Strings.AlignAssignmentStatementName);
74+
}
75+
76+
/// <summary>
77+
/// Retrieves the severity of the rule: error, warning or information.
78+
/// </summary>
79+
public override RuleSeverity GetSeverity()
80+
{
81+
return RuleSeverity.Warning;
82+
}
83+
84+
/// <summary>
85+
/// Gets the severity of the returned diagnostic record: error, warning, or information.
86+
/// </summary>
87+
/// <returns></returns>
88+
public DiagnosticSeverity GetDiagnosticSeverity()
89+
{
90+
return DiagnosticSeverity.Warning;
91+
}
92+
93+
/// <summary>
94+
/// Retrieves the name of the module/assembly the rule is from.
95+
/// </summary>
96+
public override string GetSourceName()
97+
{
98+
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
99+
}
100+
101+
/// <summary>
102+
/// Retrieves the type of the rule, Builtin, Managed or Module.
103+
/// </summary>
104+
public override SourceType GetSourceType()
105+
{
106+
return SourceType.Builtin;
107+
}
108+
}
109+
}

Rules/ScriptAnalyzerBuiltinRules.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<Compile Include="PlaceCloseBrace.cs" />
122122
<Compile Include="UseConsistentIndentation.cs" />
123123
<Compile Include="UseConsistentWhitespace.cs" />
124+
<Compile Include="AlignAssignmentStatement.cs" />
124125
</ItemGroup>
125126
<ItemGroup>
126127
<ProjectReference Include="..\Engine\ScriptAnalyzerEngine.csproj">

Rules/Strings.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,4 +942,16 @@
942942
<data name="UseConsistentWhitespaceErrorSeparatorSemi" xml:space="preserve">
943943
<value>Use space after a semicolon.</value>
944944
</data>
945+
<data name="AlignAssignmentStatementName" xml:space="preserve">
946+
<value>AlignAssignmentStatement</value>
947+
</data>
948+
<data name="AlignAssignmentStatementCommonName" xml:space="preserve">
949+
<value>Align assignment statement</value>
950+
</data>
951+
<data name="AlignAssignmentStatementDescription" xml:space="preserve">
952+
<value>Line up assignment statements such that the assignment operator are aligned.</value>
953+
</data>
954+
<data name="AlignAssignmentStatementError" xml:space="preserve">
955+
<value>Assignment statements are not aligned</value>
956+
</data>
945957
</root>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Import-Module PSScriptAnalyzer
2+
$ruleName = "AlignAssignmentStatement"
3+
4+
Describe "AlignAssignmentStatement" {
5+
Context "" {
6+
It "" {
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)