Skip to content

Commit

Permalink
Replace red-herring warning around process aliasing get-process with …
Browse files Browse the repository at this point in the history
…warning around invalid syntax (#1638)
  • Loading branch information
bergmeister authored Apr 21, 2021
1 parent ebf79d4 commit 8a65e41
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
29 changes: 21 additions & 8 deletions Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,27 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
commandType: CommandTypes.Cmdlet | CommandTypes.Function | CommandTypes.Script);
if (cmdletNameIfCommandWasMissingGetPrefix != null)
{
yield return new DiagnosticRecord(
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix),
GetCommandExtent(cmdAst),
GetName(),
DiagnosticSeverity.Warning,
fileName,
commandName,
suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix));
if (commandName.Equals("process", StringComparison.OrdinalIgnoreCase))
{
yield return new DiagnosticRecord(
Strings.InvalidSyntaxAroundProcessBlockError,
GetCommandExtent(cmdAst),
"InvalidSyntaxAroundProcessBlock",
DiagnosticSeverity.ParseError,
fileName,
commandName);
}
else
{
yield return new DiagnosticRecord(
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix),
GetCommandExtent(cmdAst),
GetName(),
DiagnosticSeverity.Warning,
fileName,
commandName,
suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix));
}
}

}
Expand Down
9 changes: 9 additions & 0 deletions Rules/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1149,4 +1149,7 @@
<data name="AvoidUsingDoubleQuotesForConstantStringError" xml:space="preserve">
<value>Use single quotes when a string is constant.</value>
</data>
</root>
<data name="InvalidSyntaxAroundProcessBlockError" xml:space="preserve">
<value>When using an explicit process block, no preceding code is allowed, only begin, end and dynamicparams blocks.</value>
</data>
</root>
7 changes: 7 additions & 0 deletions Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,12 @@ Configuration MyDscConfiguration {

$violations.Count | Should -Be $expectedViolations
}

It 'Warn about incorrect syntax around process block' {
$scriptDefinition = { function foo { IShouldNotBeHere; process {} } }
$violations = Invoke-ScriptAnalyzer -IncludeRule PSAvoidUsingCmdletAliases -ScriptDefinition "$scriptDefinition"
$violations.Count | Should -Be 1
$violations.Severity | Should -Be ([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity]::ParseError)
}
}
}

0 comments on commit 8a65e41

Please sign in to comment.