From 2da7bbd25783e9bf7a464232e05232b4793702b7 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 7 Feb 2021 18:04:16 +0000 Subject: [PATCH 1/2] Replace red-herring warning around process aliasing get-process with warning around invalid syntax --- Rules/AvoidAlias.cs | 29 +++++++++++++++++++-------- Rules/Strings.Designer.cs | 9 +++++++++ Rules/Strings.resx | 3 +++ Tests/Rules/AvoidUsingAlias.tests.ps1 | 7 +++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Rules/AvoidAlias.cs b/Rules/AvoidAlias.cs index 232a01817..9efe0e916 100644 --- a/Rules/AvoidAlias.cs +++ b/Rules/AvoidAlias.cs @@ -143,14 +143,27 @@ public IEnumerable 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)); + } } } diff --git a/Rules/Strings.Designer.cs b/Rules/Strings.Designer.cs index d39ce63fd..70cda2d94 100644 --- a/Rules/Strings.Designer.cs +++ b/Rules/Strings.Designer.cs @@ -1284,6 +1284,15 @@ internal static string DscTestsPresentNoTestsError { } } + /// + /// Looks up a localized string similar to When using a process block, only a begin or end block is allowed inside the function but no code.. + /// + internal static string InvalidSyntaxAroundProcessBlockError { + get { + return ResourceManager.GetString("InvalidSyntaxAroundProcessBlockError", resourceCulture); + } + } + /// /// Looks up a localized string similar to Misleading Backtick. /// diff --git a/Rules/Strings.resx b/Rules/Strings.resx index 588e9e3dd..defb94dd4 100644 --- a/Rules/Strings.resx +++ b/Rules/Strings.resx @@ -1149,4 +1149,7 @@ Use single quotes when a string is constant. + + When using a process block, only a begin or end block is allowed inside the function but no code. + \ No newline at end of file diff --git a/Tests/Rules/AvoidUsingAlias.tests.ps1 b/Tests/Rules/AvoidUsingAlias.tests.ps1 index ca16cf6a8..11992e6e0 100644 --- a/Tests/Rules/AvoidUsingAlias.tests.ps1 +++ b/Tests/Rules/AvoidUsingAlias.tests.ps1 @@ -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) + } } } From 0ecf7ee139cd4a5757c5c70d4adc63d9f3f186a2 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Wed, 21 Apr 2021 13:39:45 -0700 Subject: [PATCH 2/2] Update Rules/Strings.resx --- Rules/Strings.resx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rules/Strings.resx b/Rules/Strings.resx index defb94dd4..120ead158 100644 --- a/Rules/Strings.resx +++ b/Rules/Strings.resx @@ -1150,6 +1150,6 @@ Use single quotes when a string is constant. - When using a process block, only a begin or end block is allowed inside the function but no code. + When using an explicit process block, no preceding code is allowed, only begin, end and dynamicparams blocks. - \ No newline at end of file +