Skip to content

Commit d851011

Browse files
author
Kapil Borle
committed
Update UseIdenticalParametersForDSC rule logic
If a parameter is declared as `mandatory` in any of the `Get/Set/Test` functions, then it should be a mandatory parameter in all the three functions.
1 parent b451ac1 commit d851011

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
3636
public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
3737
{
3838
private bool isDSCClassCacheInitialized = false;
39+
private Ast ast;
40+
private string fileName;
41+
private IDictionary<string, string> propAttrDict;
42+
private IEnumerable<FunctionDefinitionAst> resourceFunctions;
3943

4044
/// <summary>
4145
/// AnalyzeDSCResource: Analyzes given DSC Resource
@@ -56,10 +60,15 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
5660
}
5761

5862
// Get the keys in the corresponding mof file
59-
var propAttrDict = GetKeys(fileName);
63+
this.ast = ast;
64+
this.fileName = fileName;
65+
this.propAttrDict = GetKeys(fileName);
66+
this.resourceFunctions = Helper.Instance.DscResourceFunctions(ast)
67+
.Cast<FunctionDefinitionAst>()
68+
.ToArray();
6069

6170
// Loop through Set/Test/Get TargetResource DSC cmdlets
62-
foreach (FunctionDefinitionAst functionDefinitionAst in Helper.Instance.DscResourceFunctions(ast))
71+
foreach (var functionDefinitionAst in resourceFunctions)
6372
{
6473
var manParams = GetMandatoryParameters(functionDefinitionAst)
6574
.Select(p => p.Name.VariablePath.UserPath);
@@ -79,6 +88,47 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
7988
fileName);
8089
}
8190
}
91+
92+
var funcManParamMap = resourceFunctions
93+
.ToDictionary(
94+
f => f.Name,
95+
f => Tuple.Create(
96+
f,
97+
GetMandatoryParameters(f)
98+
.Select(p => p.Name.VariablePath.UserPath)
99+
.ToArray()));
100+
var paramFuncMap = new Dictionary<string, List<string>>();
101+
foreach (var kvp in funcManParamMap)
102+
{
103+
foreach (var param in kvp.Value.Item2)
104+
{
105+
if (!paramFuncMap.ContainsKey(param))
106+
{
107+
paramFuncMap.Add(param, new List<string>());
108+
}
109+
110+
paramFuncMap[param].Add(kvp.Key);
111+
}
112+
}
113+
114+
foreach(var param in paramFuncMap.Keys.Except(propAttrDict.Keys))
115+
{
116+
foreach(var func in funcManParamMap.Keys.Except(paramFuncMap[param]))
117+
{
118+
var functionDefinitionAst = funcManParamMap[func].Item1;
119+
yield return new DiagnosticRecord(
120+
string.Format(
121+
CultureInfo.InvariantCulture,
122+
Strings.UseIdenticalMandatoryParametersDSCError,
123+
"mandatory",
124+
param,
125+
functionDefinitionAst.Name),
126+
Helper.Instance.GetScriptExtentForFunctionName(functionDefinitionAst),
127+
GetName(),
128+
DiagnosticSeverity.Error,
129+
fileName);
130+
}
131+
}
82132
}
83133

84134
/// <summary>

Tests/Rules/DSCResourceModule/DSCResources/MSFT_WaitForAnyNoIdenticalMandatoryParameter/MSFT_WaitForAnyNoIdenticalMandatoryParameter.psm1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ function Get-TargetResource {
1818
[ValidateNotNullOrEmpty()]
1919
[PSCredential] $Credential,
2020

21+
[parameter(Mandatory)]
22+
$ParamOnlyInGet,
23+
2124
[ValidateRange(1, [Uint64]::MaxValue)]
2225
[Uint64] $RetryIntervalSec = 1,
2326

@@ -60,6 +63,9 @@ function Set-TargetResource {
6063
[ValidateNotNullOrEmpty()]
6164
[string[]] $NodeName,
6265

66+
[parameter(Mandatory)]
67+
$ParameterOnlyInSet,
68+
6369
[ValidateRange(1, [Uint64]::MaxValue)]
6470
[Uint64] $RetryIntervalSec = 1,
6571

@@ -118,6 +124,9 @@ function Test-TargetResource {
118124
[ValidateNotNullOrEmpty()]
119125
[PSCredential] $Credential,
120126

127+
[parameter(Mandatory)]
128+
$ParameterOnlyInTest,
129+
121130
[ValidateRange(1,[Uint64]::MaxValue)]
122131
[Uint64] $RetryIntervalSec = 1,
123132

Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Describe "UseIdenticalMandatoryParametersForDSC" {
1616
}
1717

1818
It "Should find a violations" {
19-
$violations.Count | Should Be 5
19+
$violations.Count | Should Be 11
2020
}
2121

2222
It "Should mark only the function name" {

0 commit comments

Comments
 (0)