Skip to content

Commit 7925910

Browse files
author
James Brundage
committed
RegexLiteral: Not expanding literals within AttributeAST (Fixes #290)
1 parent cfc479d commit 7925910

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Transpilers/Syntax/RegexLiteral.psx.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,20 @@ $($Keywords -join '|') # followed by keywords
8585
(?<HereDoc>@){0,1} # Optional heredoc end
8686
$ # string end.
8787
'@, Options='IgnoreCase,IgnorePatternWhitespace, RightToLeft')]
88+
[ValidateScript({
89+
$validating = $_
90+
if ($validating.Parent -is [Management.Automation.Language.AttributeAST]) {
91+
return $false
92+
}
93+
return $true
94+
})]
8895
param(
96+
# A RegexLiteral can be any string constant expression (as long as it's not in an attribute).
8997
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='StringConstantExpressionAST')]
9098
[Management.Automation.Language.StringConstantExpressionAST]
9199
$StringConstantExpression,
92100

101+
# It can also by any expandable string, which allows you to construct Regexes using PowerShell variables and subexpressions.
93102
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='ExpandableStringExpressionAst')]
94103
[Management.Automation.Language.ExpandableStringExpressionAst]
95104
$ExpandableStringExpression
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe RegexLiteral {
2+
it 'Enables you write RegexLiterals in PipeScript' {
3+
{'/a|b/'} | .>Pipescript | Should -Be "[regex]::new('a|b', 'IgnoreCase')"
4+
}
5+
it 'Can turn expandable strings into RegExes' {
6+
{"/[$a$b]/"} | .>PipeScript | Should -Be '[regex]::new("[$a$b]", ''IgnoreCase'')'
7+
}
8+
context 'Potential pitfalls' {
9+
it 'Will not escape a single slash' {
10+
{'/'} | .>Pipescript | Should -Be "'/'"
11+
}
12+
it 'Will not process a RegexLiteral inside of an attribute' {
13+
{[Reflection.AssemblyMetadata('Key','/value/')]$v}.Transpile()
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)