Skip to content

Commit cd8176e

Browse files
author
Quoc Truong
committed
Add RuleSuppressionIDs for rules
1 parent 6ab5099 commit cd8176e

9 files changed

+31
-112
lines changed

Engine/Generic/AvoidParameterGeneric.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3535
{
3636
if (ParameterCondition(cmdAst, ceAst))
3737
{
38-
yield return new DiagnosticRecord(GetError(fileName, cmdAst), cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
38+
yield return new DiagnosticRecord(GetError(fileName, cmdAst), cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, cmdAst.GetCommandName());
3939
}
4040
}
4141
}

Engine/Generic/RuleSuppression.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public class RuleSuppression
2626
{
2727
private string _ruleName;
2828

29+
/// <summary>
30+
/// The start offset of the rule suppression attribute (not where it starts to apply)
31+
/// </summary>
32+
public int StartAttributeLine
33+
{
34+
get;
35+
set;
36+
}
37+
2938
/// <summary>
3039
/// The start offset of the rule suppression
3140
/// </summary>
@@ -133,6 +142,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
133142

134143
if (attrAst != null)
135144
{
145+
StartAttributeLine = attrAst.Extent.StartLineNumber;
136146
var positionalArguments = attrAst.PositionalArguments;
137147
var namedArguments = attrAst.NamedArguments;
138148

@@ -262,24 +272,26 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
262272

263273
if (!String.IsNullOrWhiteSpace(Error))
264274
{
265-
Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, attrAst.Extent.StartLineNumber,
275+
Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, StartAttributeLine,
266276
System.IO.Path.GetFileName(attrAst.Extent.File), Error);
267277
}
268278
}
269279

270280
/// <summary>
271-
/// Constructs rule expression from rule name, id, start and end
281+
/// Constructs rule expression from rule name, id, start, end and startAttributeLine
272282
/// </summary>
273283
/// <param name="ruleName"></param>
274284
/// <param name="ruleSuppressionID"></param>
275285
/// <param name="start"></param>
276286
/// <param name="end"></param>
277-
public RuleSuppression(string ruleName, string ruleSuppressionID, int start, int end)
287+
/// <param name="startAttributeLine"></param>
288+
public RuleSuppression(string ruleName, string ruleSuppressionID, int start, int end, int startAttributeLine)
278289
{
279290
RuleName = ruleName;
280291
RuleSuppressionID = ruleSuppressionID;
281292
StartOffset = start;
282293
EndOffset = end;
294+
StartAttributeLine = startAttributeLine;
283295
}
284296

285297
/// <summary>
@@ -336,15 +348,15 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
336348
{
337349
if (targetAsts.Count() == 0)
338350
{
339-
ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSupp.StartOffset,
351+
ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSupp.StartAttributeLine,
340352
System.IO.Path.GetFileName(scopeAst.Extent.File), String.Format(Strings.TargetCannotBeFoundError, ruleSupp.Target, ruleSupp.Scope));
341353
result.Add(ruleSupp);
342354
continue;
343355
}
344356

345357
foreach (Ast targetAst in targetAsts)
346358
{
347-
result.Add(new RuleSuppression(ruleSupp.RuleName, ruleSupp.RuleSuppressionID, targetAst.Extent.StartOffset, targetAst.Extent.EndOffset));
359+
result.Add(new RuleSuppression(ruleSupp.RuleName, ruleSupp.RuleSuppressionID, targetAst.Extent.StartOffset, targetAst.Extent.EndOffset, attributeAst.Extent.StartLineNumber));
348360
}
349361
}
350362

Engine/Helper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
773773
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
774774
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
775775
{
776-
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartOffset,
776+
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartAttributeLine,
777777
System.IO.Path.GetFileName(record.Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
778778
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
779779
}
@@ -811,7 +811,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
811811
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
812812
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
813813
{
814-
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartOffset,
814+
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartAttributeLine,
815815
System.IO.Path.GetFileName(record.Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
816816
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
817817
}

Rules/AvoidAlias.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,18 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3535
foreach (Ast foundAst in foundAsts)
3636
{
3737
CommandAst cmdAst = (CommandAst)foundAst;
38+
string aliasName = cmdAst.GetCommandName();
3839
// Handles the exception caused by commands like, {& $PLINK $args 2> $TempErrorFile}.
3940
// You can also review the remark section in following document,
4041
// MSDN: CommandAst.GetCommandName Method
41-
if (cmdAst.GetCommandName() == null) continue;
42+
if (aliasName == null) continue;
4243

43-
string cmdletName = Microsoft.Windows.Powershell.ScriptAnalyzer.Helper.Instance.GetCmdletNameFromAlias(cmdAst.GetCommandName());
44+
string cmdletName = Microsoft.Windows.Powershell.ScriptAnalyzer.Helper.Instance.GetCmdletNameFromAlias(aliasName);
4445

4546
if (!String.IsNullOrEmpty(cmdletName))
4647
{
47-
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesError, cmdAst.GetCommandName(), cmdletName),
48-
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
48+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesError, aliasName, cmdletName),
49+
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, aliasName);
4950
}
5051
}
5152
}

Rules/AvoidGlobalVars.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4040
yield return
4141
new DiagnosticRecord(
4242
string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalVarsError,
43-
varAst.VariablePath.UserPath), varAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
43+
varAst.VariablePath.UserPath), varAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, varAst.VariablePath.UserPath);
4444
}
4545
}
4646
}

Rules/AvoidParameterGeneric.cs

Lines changed: 0 additions & 96 deletions
This file was deleted.

Rules/AvoidPositionalParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4545
&& Helper.Instance.PositionalParameterUsed(cmdAst))
4646
{
4747
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersError, cmdAst.GetCommandName()),
48-
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
48+
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, cmdAst.GetCommandName());
4949
}
5050
}
5151
}

Rules/AvoidReservedParams.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
6363
StringComparison.OrdinalIgnoreCase))
6464
{
6565
string funcName = string.Format(CultureInfo.CurrentCulture,Strings.ReservedParamsCmdletPrefix, (parentAst as FunctionDefinitionAst).Name);
66-
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcName,paramName), paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
66+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcName,paramName),
67+
paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramName);
6768

6869
}
6970
}

Rules/UseDeclaredVarsMoreThanAssignments.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
9393

9494
foreach (string key in assignments.Keys)
9595
{
96-
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseDeclaredVarsMoreThanAssignmentsError, key), assignments[key].Extent, GetName(), DiagnosticSeverity.Warning, fileName);
96+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseDeclaredVarsMoreThanAssignmentsError, key),
97+
assignments[key].Extent, GetName(), DiagnosticSeverity.Warning, fileName, key);
9798
}
9899
}
99100

0 commit comments

Comments
 (0)