Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace red-herring warning around process aliasing get-process with warning around invalid syntax #1638

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}