Skip to content

Commit fe65a43

Browse files
Merge pull request #117 from StevePJp/development
fixed locating strings in splat vars
2 parents aa0e1e6 + 947bb2d commit fe65a43

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

PSModuleDevelopment/functions/refactor/Export-PSMDString.ps1

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,67 @@
7272
StringValues = $stringValueParamValue
7373
}
7474
}
75-
75+
76+
# Additional checks for splatted commands
77+
# find all splatted commands
78+
$splattedVariables = $ast.FindAll( {
79+
if ($args[0] -isnot [System.Management.Automation.Language.VariableExpressionAst ]) { return $false }
80+
if (-not ($args[0].Splatted -eq $true)) { return $false }
81+
$true
82+
}, $true)
83+
84+
foreach ($splattedVariable in $splattedVariables)
85+
{
86+
#get the variable name
87+
$splatParamName = $splattedVariable.VariablePath.UserPath
88+
if ($splatParamName)
89+
{
90+
# match the $param = @{
91+
$splatParamNameRegex = "^\s?\`$$($splatParamName)\s?=\s?\@\{"
92+
# get all variable assignments where the
93+
# left side matches our param
94+
# operator is =
95+
# matches our assignment regex
96+
$splatAssignmentAsts = $ast.FindAll( {
97+
if ($args[0] -isnot [System.Management.Automation.Language.AssignmentStatementAst ]) { return $false }
98+
if (-not ($args[0].Left -match $splatParamName)) { return $false }
99+
if (-not ($args[0].Operator -eq 'Equals')) { return $false }
100+
if (-not ($args[0].Extent -match $splatParamNameRegex)) { return $false }
101+
$true
102+
}, $true)
103+
foreach ($splatAssignmentAst in $splatAssignmentAsts)
104+
{
105+
# get the hashtable
106+
$splatHashTable = $splatAssignmentAst.Right.Expression
107+
# see if its an empty assignment or null
108+
if ($splatHashTable -and $splatHashTable.KeyValuePairs.Count -gt 0)
109+
{
110+
# find any String or ActionString
111+
$splatParam = $splatAssignmentAst.Right.Expression.KeyValuePairs | Where-Object Item1 -match '^String$|^ActionString$'
112+
# The kvp.item.extent.text returns nested quotes where as the commandast.extent.text doesn't so strip them off
113+
$splatParamValue = $splatParam.Item2.Extent.Text.Trim('"').Trim("'")
114+
# find any StringValue or ActionStringValue
115+
$splatValueParam = $splatAssignmentAst.Right.Expression.KeyValuePairs | Where-Object Item1 -match '^StringValues$|^ActionStringValues$'
116+
if ($splatValueParam)
117+
{
118+
# The kvp.item.extent.text returns nested quotes whereas the commandast.extent.text doesn't so strip them off
119+
$splatValueParamValue = $splatValueParam.Item2.Extent.Text.Trim('"').Trim("'")
120+
}
121+
else { $splatValueParamValue = '' }
122+
123+
[PSCustomObject]@{
124+
PSTypeName = 'PSModuleDevelopment.String.ParsedItem'
125+
File = $file.FullName
126+
Line = $splatHashTable.Extent.StartLineNumber
127+
CommandName = $splattedVariable.Parent.CommandElements[0].Value
128+
String = $splatParamValue
129+
StringValues = $splatValueParamValue
130+
}
131+
}
132+
}
133+
}
134+
}
135+
76136
$validateAsts = $ast.FindAll({
77137
if ($args[0] -isnot [System.Management.Automation.Language.AttributeAst]) { return $false }
78138
if ($args[0].TypeName -notmatch '^PsfValidateScript$|^PsfValidatePattern$') { return $false }

0 commit comments

Comments
 (0)