From 401e3a3717e389204e9ce25d5ccb87b76933478a Mon Sep 17 00:00:00 2001 From: MoChilia Date: Thu, 14 Jul 2022 15:29:31 +0800 Subject: [PATCH 1/3] fix Capitalization Conventions --- .../AnalyzeRules/CommandName.psm1 | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 index e2ba7e672b67..32e723b72335 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 @@ -52,6 +52,7 @@ function Measure-CommandName { if ($CommandAst.InvocationOperator -eq "Unknown") { $CommandName = $CommandAst.CommandElements[0].Extent.Text $GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue + $ActualName = $GetCommand.Name if ($null -eq $GetCommand) { # CommandName is not valid. # Redo import-module @@ -74,10 +75,10 @@ function Measure-CommandName { } return $true } - if ($CommandName -cnotmatch "^([A-Z][a-z]+)+-([A-Z][a-z0-9]*)+$") { + if ($CommandName -cnotmatch $ActualName) { # CommandName doesn't follow the Capitalization Conventions. $global:CommandParameterPair += @{ - CommandName = $CommandName + CommandName = "$CommandName#@#$ActualName" ParameterName = "" ModuleCmdletExNum = $ModuleCmdletExNum } @@ -108,20 +109,11 @@ function Measure-CommandName { $Severity = "Warning" } if ($global:CommandParameterPair[$i].ParameterName -eq "") { - $Message = "$($CommandParameterPair[$i].CommandName) doesn't follow the Capitalization Conventions." + $CommandName = $($CommandParameterPair[$i].CommandName -split "#@#")[0] + $CorrectName = $($CommandParameterPair[$i].CommandName -split "#@#")[1] + $Message = "$CommandName doesn't follow the Capitalization Conventions." $RuleName = [RuleNames]::Capitalization_Conventions_Violated $RuleSuppressionID = "5101" - $name = $($CommandParameterPair[$i].CommandName) - $textInfo = (Get-Culture).TextInfo - $CorrectName = $textInfo.ToTitleCase(($name -split "-")[0]) - if($name -match "Az"){ - $CorrectName += "-Az" - $CorrectName += $textInfo.ToTitleCase(($name -split "Az")[1]) - } - else{ - $CorrectName += "-" - $CorrectName += $textInfo.ToTitleCase(($name -split "-")[1]) - } $Remediation = "Check the Capitalization Conventions. Suggest format: $CorrectName" $Severity = "Warning" } From cf491ebb263c084173701403481d168ff0d7ea8e Mon Sep 17 00:00:00 2001 From: MoChilia Date: Thu, 14 Jul 2022 15:46:24 +0800 Subject: [PATCH 2/3] fix severity --- .../ExampleAnalyzer/AnalyzeRules/CommandName.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 index 32e723b72335..1b90755a2ac4 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 @@ -115,7 +115,7 @@ function Measure-CommandName { $RuleName = [RuleNames]::Capitalization_Conventions_Violated $RuleSuppressionID = "5101" $Remediation = "Check the Capitalization Conventions. Suggest format: $CorrectName" - $Severity = "Warning" + $Severity = "Error" } $ModuleCmdletExNum = $($CommandParameterPair[$i].ModuleCmdletExNum) $Result = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ From 37013b8e974ab38191ec0d360d22a28bb5c25b55 Mon Sep 17 00:00:00 2001 From: Shiying Chen <72982571+MoChilia@users.noreply.github.com> Date: Thu, 14 Jul 2022 17:10:32 +0800 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Yeming Liu <11371776+isra-fel@users.noreply.github.com> --- .../ExampleAnalyzer/AnalyzeRules/CommandName.psm1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 index 1b90755a2ac4..e85d43d6ce22 100644 --- a/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 +++ b/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1 @@ -75,7 +75,7 @@ function Measure-CommandName { } return $true } - if ($CommandName -cnotmatch $ActualName) { + if ($CommandName -cne $ActualName) { # CommandName doesn't follow the Capitalization Conventions. $global:CommandParameterPair += @{ CommandName = "$CommandName#@#$ActualName" @@ -109,8 +109,7 @@ function Measure-CommandName { $Severity = "Warning" } if ($global:CommandParameterPair[$i].ParameterName -eq "") { - $CommandName = $($CommandParameterPair[$i].CommandName -split "#@#")[0] - $CorrectName = $($CommandParameterPair[$i].CommandName -split "#@#")[1] + $CommandName, $CorrectName = $CommandParameterPair[$i].CommandName -split "#@#" $Message = "$CommandName doesn't follow the Capitalization Conventions." $RuleName = [RuleNames]::Capitalization_Conventions_Violated $RuleSuppressionID = "5101"