Skip to content

Commit 21dbafd

Browse files
authored
fix cannot suppress example analysis (#20512)
caused by duplicated "Az." prefix of module name
1 parent bfdeb17 commit 21dbafd

File tree

1 file changed

+57
-44
lines changed
  • tools/StaticAnalysis/ExampleAnalyzer

1 file changed

+57
-44
lines changed

tools/StaticAnalysis/ExampleAnalyzer/utils.ps1

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class AnalysisOutput{
3030
[int]$Example
3131
[string]$Line
3232
[string]$RuleName
33-
[int]$ProblemID
33+
[int]$ProblemId
3434
[int]$Severity
3535
[string]$Description
3636
[string]$Extent
3737
[String]$Remediation
38-
}
38+
}
3939

4040
<#
4141
.SYNOPSIS
@@ -59,18 +59,18 @@ function Get-ExamplesDetailsFromMd {
5959
$indexOfParameters = $fileContent.IndexOf($PARAMETERS_HEADING)
6060
if($indexOfExamples -eq -1){
6161
$RuleName = "MissingExamplesHeading"
62-
$ProblemID = 5060
62+
$ProblemId = 5060
6363
$Description = "'## EXAMPLES' is missing."
6464
$Remediation = "Mark '## EXAMPLES' for your reference docs."
65-
$errors += Set-AnalysisOutput $Module $Cmdlet 0 0 $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
65+
$errors += Set-AnalysisOutput $Module $Cmdlet 0 0 $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
6666
return
6767
}
6868
if($indexOfParameters -eq -1){
6969
$RuleName = "MissingParametersHeading"
70-
$ProblemID = 5061
70+
$ProblemId = 5061
7171
$Description = "'## PARAMETERS' is missing."
7272
$Remediation = "Mark '## PARAMETERS' for your reference docs."
73-
$errors += Set-AnalysisOutput $Module $Cmdlet 0 0 $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
73+
$errors += Set-AnalysisOutput $Module $Cmdlet 0 0 $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
7474
return
7575
}
7676

@@ -84,10 +84,10 @@ function Get-ExamplesDetailsFromMd {
8484
if(!$firstExample -and $needRecord){
8585
if(!$codeBlockComplete -or !$outputBlockComplete -or !$otherTypeBlockComplete){
8686
$RuleName = "BlockIdentifiersNotMatched"
87-
$ProblemID = 5064
87+
$ProblemId = 5064
8888
$Description = "The start and end identifiers of the code block do not match."
8989
$Remediation = "Please check whether the start or end identifier (triple backticks) of the code block is missing."
90-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
90+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
9191
if(!$codeBlockComplete){
9292
$exampleCodes = "#BlockIdentifiersNotMatched"
9393
}
@@ -127,10 +127,10 @@ function Get-ExamplesDetailsFromMd {
127127
$blockType = ($exampleLine -split "``````")[1]
128128
if($blockType -notin $blockTypeList){
129129
$RuleName = "BlockTypeUnsupported"
130-
$ProblemID = 5062
130+
$ProblemId = 5062
131131
$Description = "The language identifier $blockType is not supported."
132132
$Remediation = "Please check the spelling or contact Azure PowerShell team."
133-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
133+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
134134
}
135135
$otherTypeBlockComplete = $false
136136
}
@@ -143,10 +143,10 @@ function Get-ExamplesDetailsFromMd {
143143
}
144144
else{
145145
$RuleName = "UnclearBlockType"
146-
$ProblemID = 5063
146+
$ProblemId = 5063
147147
$Description = "The code blocks are missing language identifiers."
148148
$Remediation = "The type of the code block needs to be clearly indicated with 'powershell' or 'output'."
149-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
149+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
150150
$otherTypeBlockComplete = $false
151151
}
152152
}
@@ -180,18 +180,18 @@ function Get-ExamplesDetailsFromMd {
180180
else{
181181
# Find descriptions
182182
if($exampleLine.Trim() -ne ""){
183-
$exampleDescriptions += $exampleLine
183+
$exampleDescriptions += $exampleLine
184184
}
185185
}
186186
}
187187
}
188188
if($needRecord){
189189
if(!$codeBlockComplete -or !$outputBlockComplete -or !$otherTypeBlockComplete){
190190
$RuleName = "BlockIdentifiersNotMatched"
191-
$ProblemID = 5064
191+
$ProblemId = 5064
192192
$Description = "The start and end identifiers of the code block do not match."
193193
$Remediation = "Please check whether the start or end identifier (triple backticks) of the code block is missing."
194-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
194+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNumber 0 $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
195195
if(!$codeBlockComplete){
196196
$exampleCodes = "#BlockIdentifiersNotMatched"
197197
}
@@ -273,9 +273,9 @@ function Measure-SectionMissingAndOutputScript {
273273
if($missingSynopsis -ne 0){
274274
$Description = "Synopsis is missing."
275275
$RuleName = "MissingSynopsis"
276-
$ProblemID = 5040
276+
$ProblemId = 5040
277277
$Remediation = "Add Synopsis. Remove any placeholders."
278-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNum $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
278+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNum $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
279279
}
280280

281281
# If Description section exists
@@ -294,19 +294,19 @@ function Measure-SectionMissingAndOutputScript {
294294
if($missingDescription -ne 0){
295295
$Description = "Description is missing."
296296
$RuleName = "MissingDescription"
297-
$ProblemID = 5041
297+
$ProblemId = 5041
298298
$Remediation = "Add Description. Remove any placeholders."
299-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNum $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
299+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNum $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
300300
}
301301

302302
# If no examples
303303
if ($examplesDetails.Count -eq 0) {
304304
if($fileContent -notmatch "<!-- Aladdin Generated Example -->" -and $fileContent -notmatch "<!-- Skip.*-->"){
305305
$Description = "Example is missing."
306306
$RuleName = "MissingExample"
307-
$ProblemID = 5042
307+
$ProblemId = 5042
308308
$Remediation = "Add Example. Remove any placeholders."
309-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNum $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
309+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleNum $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
310310
}
311311
}
312312
else {
@@ -322,38 +322,38 @@ function Measure-SectionMissingAndOutputScript {
322322
{$exampleDetails.Title -eq "" -or $missingExampleTitle -ne 0} {
323323
$Description = "Title of the example is missing."
324324
$RuleName = "MissingExampleTitle"
325-
$ProblemID = 5043
325+
$ProblemId = 5043
326326
$Remediation = "Add title for the example. Remove any placeholders."
327-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
327+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
328328
}
329329
{$exampleDetails.Codes.Count -eq 0 -or $exampleDetails.Codes.Trim().Length -eq 0 -or $missingExampleCode -ne 0} {
330330
$missingExampleCode = 1
331331
$Description = "Code of the example is missing."
332332
$RuleName = "MissingExampleCode"
333-
$ProblemID = 5044
333+
$ProblemId = 5044
334334
$Remediation = "Add code for the example. Remove any placeholders."
335-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
335+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
336336
}
337337
{($exampleDetails.OutputBlock -and ($exampleDetails.Outputs.Count -eq 0 -or $exampleDetails.Outputs.Trim().Length -eq 0)) -or $missingExampleOutput -ne 0} {
338338
$Description = "Output of the example is missing."
339339
$RuleName = "MissingExampleOutput"
340-
$ProblemID = 5045
340+
$ProblemId = 5045
341341
$Remediation = "Add output for the example. Remove any placeholders."
342-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
342+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
343343
}
344344
{$exampleDetails.Description -eq "" -or $missingExampleDescription -ne 0} {
345345
$Description = "Description of the example is missing."
346346
$RuleName = "MissingExampleDescription"
347-
$ProblemID = 5046
347+
$ProblemId = 5046
348348
$Remediation = "Add description for the example. Remove any placeholders."
349-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
349+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
350350
}
351351
{$needDeleting -ne 0}{
352352
$Description = "The prompt of example need to be deleted."
353353
$RuleName = "NeedDeleting"
354-
$ProblemID = 5050
354+
$ProblemId = 5050
355355
$Remediation = "Delete the prompt of example."
356-
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemID $missingSeverity $Description $Extent $Remediation
356+
$errors += Set-AnalysisOutput $Module $Cmdlet $exampleDetails.Num $exampleLine $RuleName $ProblemId $missingSeverity $Description $Extent $Remediation
357357
$exampleCodes = $exampleCodes | ForEach-Object{ $_ -replace "^([A-Za-z \t\\:>])*(PS|[A-Za-z]:)(\w|[\\/\[\].\- ])*(>|&gt;)+( PS)*[ \t]*"}
358358
}
359359
}
@@ -381,7 +381,7 @@ function Measure-SectionMissingAndOutputScript {
381381

382382
<#
383383
.SYNOPSIS
384-
Merge the example codes or scripts into one PowerShell script and generate the code map.
384+
Merge the example codes or scripts into one PowerShell script and generate the code map.
385385
#>
386386
function Merge-Contents {
387387
param(
@@ -420,7 +420,7 @@ function Merge-Contents {
420420

421421
<#
422422
.SYNOPSIS
423-
Merge PowerShell scripts into one and generate the code map.
423+
Merge PowerShell scripts into one and generate the code map.
424424
#>
425425
function Merge-Scripts {
426426
param(
@@ -435,7 +435,7 @@ function Merge-Scripts {
435435
$fileName = (Get-Item -Path $_.FullName).Name
436436
$scriptContent = Get-Content $_
437437
($tempCodeMap, $TotalLine) = Merge-Contents -Content $scriptContent -Module $fileName -TotalLine $TotalLine -TempScriptPath $TempScriptPath
438-
$codeMap += $tempCodeMap
438+
$codeMap += $tempCodeMap
439439
}
440440
}
441441
return $codeMap
@@ -452,19 +452,19 @@ function Set-AnalysisOutput {
452452
[int]$Example,
453453
[string]$Line,
454454
[string]$RuleName,
455-
[int]$ProblemID,
455+
[int]$ProblemId,
456456
[int]$Severity,
457457
[string]$Description,
458458
[string]$Extent,
459459
[String]$Remediation
460460
)
461461
$result = [AnalysisOutput]@{
462-
Module = "Az.$Module"
462+
Module = EnsureModuleName($Module)
463463
Cmdlet = $Cmdlet
464464
Example = $Example
465465
Line = $Line
466466
RuleName = $RuleName
467-
ProblemID = $ProblemID
467+
ProblemId = $ProblemId
468468
Severity = $Severity
469469
Description = $Description
470470
Extent = $Extent
@@ -491,7 +491,7 @@ function Get-ScriptAnalyzerResult {
491491
if (!(Test-Path $ScriptPath -PathType Leaf)) {
492492
throw "Cannot find cached script file '$ScriptPath'."
493493
}
494-
494+
495495
# Invoke PSScriptAnalyzer : input scriptblock, output error set in $result with property: RuleName, Message, Extent
496496
if ($null -eq $RulePaths) {
497497
$analysisResults = Invoke-ScriptAnalyzer -Path $ScriptPath -IncludeDefaultRules:$IncludeDefaultRules.IsPresent
@@ -516,33 +516,33 @@ function Get-ScriptAnalyzerResult {
516516
$locationMessage = $CodeMap[$analysisResult.Line - 1]
517517
if($analysisResult.RuleSuppressionID -ge 5000 -and $analysisResult.RuleSuppressionID -le 5199){
518518
$result = [AnalysisOutput]@{
519-
Module = "Az.$($locationMessage.Module)"
519+
Module = EnsureModuleName($locationMessage.Module)
520520
Cmdlet = $locationMessage.Cmdlet
521521
Example = $locationMessage.Example
522522
Line = $locationMessage.Line
523523
RuleName = $analysisResult.RuleName
524524
Description = ($analysisResult.Message -split "#@#")[0] -replace "`"","`'" -replace "`n"," " -replace "`r"," "
525525
Severity = $Severity
526526
Extent = $analysisResult.Extent.ToString().Trim() -replace "`"","`'" -replace "`n"," " -replace "`r"," "
527-
ProblemID = $analysisResult.RuleSuppressionID
527+
ProblemId = $analysisResult.RuleSuppressionID
528528
Remediation = ($analysisResult.Message -split "#@#")[1] -replace "`"","`'" -replace "`n"," " -replace "`r"," "
529529
}
530530
}
531531
else{
532532
$result = [AnalysisOutput]@{
533-
Module = "Az.$($locationMessage.Module)"
533+
Module = EnsureModuleName($locationMessage.Module)
534534
Cmdlet = $locationMessage.Cmdlet
535535
Example = $locationMessage.Example
536536
Line = $locationMessage.Line
537537
RuleName = $analysisResult.RuleName
538538
Description = $analysisResult.Message -replace "`"","`'" -replace "`n"," " -replace "`r"," "
539539
Severity = $Severity
540540
Extent = $analysisResult.Extent.ToString().Trim() -replace "`"","`'" -replace "`n"," " -replace "`r"," "
541-
ProblemID = 5200
541+
ProblemId = 5200
542542
Remediation = "Unexpected Error. Please check [Trouble Shotting for Unexpected Errors in Example Issues](https://github.com/Azure/azure-powershell/blob/main/documentation/Debugging-StaticAnalysis-Errors.md#Troubleshotting-Example-Issues) for more details."
543543
}
544544
}
545-
$errors += $result
545+
$errors += $result
546546
}
547547
# Except records in allow list
548548
$errors = Get-RecordsNotInAllowList $errors
@@ -564,7 +564,7 @@ function Get-NonExceptionRecord{
564564
$errors = @()
565565
foreach($record in $records){
566566
$needAdd = $true
567-
$exceptionPath = Join-Path -Path $exceptionPaths -ChildPath "Az.$($record.Module)" -AdditionalChildPath "ExampleIssues.csv"
567+
$exceptionPath = Join-Path -Path $exceptionPaths -ChildPath (EnsureModuleName($record.Module)) -AdditionalChildPath "ExampleIssues.csv"
568568
if(Test-Path -Path $exceptionPath){
569569
$exceptionContents = Import-Csv -Path $exceptionPath
570570
foreach($exceptionContent in $exceptionContents) {
@@ -606,3 +606,16 @@ function Get-RecordsNotInAllowList{
606606
return $true
607607
}
608608
}
609+
610+
<#
611+
.SYNOPSIS
612+
Prepend "Az." to $moduleName if it doesn't already have one.
613+
#>
614+
function EnsureModuleName {
615+
param([string]$moduleName)
616+
if (-not [System.String]::IsNullOrEmpty($moduleName) -and -not $moduleName.StartsWith("Az."))
617+
{
618+
return "Az.$moduleName"
619+
}
620+
return $moduleName
621+
}

0 commit comments

Comments
 (0)