Skip to content

Commit e74ea46

Browse files
author
Kapil Borle
committed
Merge pull request #453 from PowerShell/FixUseToExportFieldsInManifest
Fixes to UseToExportFieldsInManifest rule.
2 parents 38b9e99 + 0f82359 commit e74ea46

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

Rules/UseToExportFieldsInManifest.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212

1313
using System;
14+
using System.Linq;
1415
using System.Collections.Generic;
1516
using System.Management.Automation.Language;
1617
using System.Management.Automation;
@@ -83,6 +84,30 @@ private bool IsValidManifest(Ast ast, string fileName)
8384
return !missingManifestRule.AnalyzeScript(ast, fileName).GetEnumerator().MoveNext();
8485

8586
}
87+
88+
/// <summary>
89+
/// Checks if the ast contains wildcard character.
90+
/// </summary>
91+
/// <param name="ast"></param>
92+
/// <returns></returns>
93+
private bool HasWildcardInExpression(Ast ast)
94+
{
95+
var strConstExprAst = ast as StringConstantExpressionAst;
96+
return strConstExprAst != null && WildcardPattern.ContainsWildcardCharacters(strConstExprAst.Value);
97+
}
98+
99+
/// <summary>
100+
/// Checks if the ast contains null expression.
101+
/// </summary>
102+
/// <param name="ast"></param>
103+
/// <returns></returns>
104+
private bool HasNullInExpression(Ast ast)
105+
{
106+
var varExprAst = ast as VariableExpressionAst;
107+
return varExprAst != null
108+
&& varExprAst.VariablePath.IsUnqualified
109+
&& varExprAst.VariablePath.UserPath.Equals("null", StringComparison.OrdinalIgnoreCase);
110+
}
86111

87112
/// <summary>
88113
/// Checks if the *ToExport fields are explicitly set to arrays, eg. @(...), and the array entries do not contain any wildcard.
@@ -97,32 +122,41 @@ private bool HasAcceptableExportField(string key, HashtableAst hast, string scri
97122
extent = null;
98123
foreach (var pair in hast.KeyValuePairs)
99124
{
100-
if (key.Equals(pair.Item1.Extent.Text.Trim(), StringComparison.OrdinalIgnoreCase))
125+
var keyStrConstAst = pair.Item1 as StringConstantExpressionAst;
126+
if (keyStrConstAst != null && keyStrConstAst.Value.Equals(key, StringComparison.OrdinalIgnoreCase))
101127
{
102-
// checks if the right hand side of the assignment is an array.
103-
var arrayAst = pair.Item2.Find(x => x is ArrayLiteralAst || x is ArrayExpressionAst, true);
104-
if (arrayAst == null)
105-
{
106-
extent = pair.Item2.Extent;
128+
// Checks for wildcard character in the entry.
129+
var astWithWildcard = pair.Item2.Find(HasWildcardInExpression, false);
130+
if (astWithWildcard != null)
131+
{
132+
extent = astWithWildcard.Extent;
107133
return false;
108-
}
134+
}
109135
else
110136
{
111-
//checks if any entry within the array has a wildcard.
112-
var elementWithWildcard = arrayAst.Find(x => x is StringConstantExpressionAst
113-
&& x.Extent.Text.Contains("*"), false);
114-
if (elementWithWildcard != null)
137+
// Checks for $null in the entry.
138+
var astWithNull = pair.Item2.Find(HasNullInExpression, false);
139+
if (astWithNull != null)
115140
{
116-
extent = elementWithWildcard.Extent;
141+
extent = astWithNull.Extent;
117142
return false;
118-
}
119-
return true;
143+
}
144+
else
145+
{
146+
return true;
147+
}
120148
}
121149
}
122150
}
123151
return true;
124152
}
125153

154+
155+
/// <summary>
156+
/// Gets the error string of the rule.
157+
/// </summary>
158+
/// <param name="field"></param>
159+
/// <returns></returns>
126160
public string GetError(string field)
127161
{
128162
return string.Format(CultureInfo.CurrentCulture, Strings.UseToExportFieldsInManifestError, field);

0 commit comments

Comments
 (0)