Skip to content

Commit 03ca016

Browse files
author
Kapil Borle
authored
Merge pull request #630 from PowerShell/kapilmb/FixUsePSCredentialType
Fix PSUsePSCredentialType rule when attributes on same line
2 parents 03bdc23 + 29aa753 commit 03ca016

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Rules/UsePSCredentialType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private bool WrongCredentialUsage(ParameterAst parameter)
115115
var credentialAttribute = parameter.Attributes.FirstOrDefault(paramAttribute => paramAttribute.TypeName.GetReflectionType() == typeof(CredentialAttribute));
116116

117117
// check that both exists and pscredentialtype comes before credential attribute
118-
if (psCredentialType != null && credentialAttribute != null && psCredentialType.Extent.EndOffset < credentialAttribute.Extent.StartOffset)
118+
if (psCredentialType != null && credentialAttribute != null && psCredentialType.Extent.EndOffset <= credentialAttribute.Extent.StartOffset)
119119
{
120120
return false;
121121
}

Tests/Rules/PSCredentialType.tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ Describe "PSCredentialType" {
1414
It "has the correct description message" {
1515
$violations[0].Message | Should Be $violationMessage
1616
}
17+
18+
It "detects attributes on the same line without space" {
19+
$scriptDef = @'
20+
function Get-Credential
21+
{
22+
param(
23+
[PSCredential][System.Management.Automation.Credential()]
24+
$Credential
25+
)
26+
}
27+
'@
28+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule $violationName
29+
$violations.Count | Should Be 0
30+
}
31+
1732
}
1833

1934
$expectedViolationCount = 0

0 commit comments

Comments
 (0)