Skip to content

Commit 8a65e41

Browse files
authored
Replace red-herring warning around process aliasing get-process with warning around invalid syntax (#1638)
1 parent ebf79d4 commit 8a65e41

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

Rules/AvoidAlias.cs

+21-8
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,27 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
143143
commandType: CommandTypes.Cmdlet | CommandTypes.Function | CommandTypes.Script);
144144
if (cmdletNameIfCommandWasMissingGetPrefix != null)
145145
{
146-
yield return new DiagnosticRecord(
147-
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix),
148-
GetCommandExtent(cmdAst),
149-
GetName(),
150-
DiagnosticSeverity.Warning,
151-
fileName,
152-
commandName,
153-
suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix));
146+
if (commandName.Equals("process", StringComparison.OrdinalIgnoreCase))
147+
{
148+
yield return new DiagnosticRecord(
149+
Strings.InvalidSyntaxAroundProcessBlockError,
150+
GetCommandExtent(cmdAst),
151+
"InvalidSyntaxAroundProcessBlock",
152+
DiagnosticSeverity.ParseError,
153+
fileName,
154+
commandName);
155+
}
156+
else
157+
{
158+
yield return new DiagnosticRecord(
159+
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix),
160+
GetCommandExtent(cmdAst),
161+
GetName(),
162+
DiagnosticSeverity.Warning,
163+
fileName,
164+
commandName,
165+
suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix));
166+
}
154167
}
155168

156169
}

Rules/Strings.Designer.cs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rules/Strings.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1149,4 +1149,7 @@
11491149
<data name="AvoidUsingDoubleQuotesForConstantStringError" xml:space="preserve">
11501150
<value>Use single quotes when a string is constant.</value>
11511151
</data>
1152-
</root>
1152+
<data name="InvalidSyntaxAroundProcessBlockError" xml:space="preserve">
1153+
<value>When using an explicit process block, no preceding code is allowed, only begin, end and dynamicparams blocks.</value>
1154+
</data>
1155+
</root>

Tests/Rules/AvoidUsingAlias.tests.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,12 @@ Configuration MyDscConfiguration {
117117

118118
$violations.Count | Should -Be $expectedViolations
119119
}
120+
121+
It 'Warn about incorrect syntax around process block' {
122+
$scriptDefinition = { function foo { IShouldNotBeHere; process {} } }
123+
$violations = Invoke-ScriptAnalyzer -IncludeRule PSAvoidUsingCmdletAliases -ScriptDefinition "$scriptDefinition"
124+
$violations.Count | Should -Be 1
125+
$violations.Severity | Should -Be ([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity]::ParseError)
126+
}
120127
}
121128
}

0 commit comments

Comments
 (0)