Skip to content

Commit 335a870

Browse files
committed
Handle RegExp flags compatible with PHP
1 parent 41a3f8e commit 335a870

File tree

10 files changed

+59
-52
lines changed

10 files changed

+59
-52
lines changed

examples/replace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
var url = 'https://www.pugjs.org';
22

3-
return url.replace('https://', '').replace(/^www\./, '');
3+
return url.replace('https://', '').replace(/^WWW\./i, '');

src/JsPhpize/Compiler/Compiler.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ protected function getBlockHead(Block $block, $indent)
9999
' => $__current_value)';
100100
}
101101

102-
return $block->type . ($block->value
102+
return $block->type . (
103+
$block->value
103104
? ' ' . $this->visitNode($block->value, $indent)
104105
: ''
105106
);
@@ -272,9 +273,11 @@ protected function visitInstruction(Instruction $group, $indent)
272273
$value = $visitNode($instruction, $indent);
273274

274275
return $indent .
275-
($instruction instanceof Block && $instruction->handleInstructions()
276+
(
277+
$instruction instanceof Block && $instruction->handleInstructions()
276278
? $value
277-
: ($isReturnPrepended && !preg_match('/^\s*return(?![a-zA-Z0-9_])/', $value)
279+
: (
280+
$isReturnPrepended && !preg_match('/^\s*return(?![a-zA-Z0-9_])/', $value)
278281
? ' return '
279282
: ''
280283
) . $value . ';'

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function ($base) {
1313
}
1414
};
1515
$getRegExp = function ($value) {
16-
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp : null;
16+
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null;
1717
};
1818
$fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) {
1919
if (is_string($base)) {

src/JsPhpize/Compiler/Helpers/Dot.ref.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function (&$base) {
1313
}
1414
};
1515
$getRegExp = function ($value) {
16-
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp : null;
16+
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null;
1717
};
1818
$fallbackDot = function (&$base, $key) use ($getCallable, $getRegExp) {
1919
if (is_string($base)) {

src/JsPhpize/Compiler/Helpers/DotObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function ($base) {
1313
}
1414
};
1515
$getRegExp = function ($value) {
16-
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp : null;
16+
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null;
1717
};
1818
$fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) {
1919
if (is_string($base)) {

src/JsPhpize/Compiler/Helpers/DotObject.ref.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function (&$base) {
1313
}
1414
};
1515
$getRegExp = function ($value) {
16-
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp : null;
16+
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null;
1717
};
1818
$fallbackDot = function (&$base, $key) use ($getCallable, $getRegExp) {
1919
if (is_string($base)) {

src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function ($base) {
9292
}
9393
};
9494
$getRegExp = function ($value) {
95-
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp : null;
95+
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null;
9696
};
9797
$fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) {
9898
if (is_string($base)) {

src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.ref.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function (&$base) {
9292
}
9393
};
9494
$getRegExp = function ($value) {
95-
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp : null;
95+
return is_object($value) && isset($value->isRegularExpression) && $value->isRegularExpression ? $value->regExp . $value->flags : null;
9696
};
9797
$fallbackDot = function (&$base, $key) use ($getCallable, $getRegExp) {
9898
if (is_string($base)) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
function ($value) {
2+
$chunks = strlen($value) ? explode(substr($value, 0, 1), $value) : [''];
3+
$flags = preg_replace('/[^imsxeADSUXJu]/', '', end($chunks));
4+
25
return (object) array(
36
'isRegularExpression' => true,
7+
'flags' => $flags,
48
'regExp' => rtrim($value, 'gimuy'),
59
);
610
}

tests/helpers.php

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -72,73 +72,73 @@ public function testArrayValue()
7272
$dotHelper = $this->getDotHelper();
7373

7474
$this->assertSame(42, $dotHelper([
75-
'foo' => 42,
76-
], 'foo'));
75+
'foo' => 42,
76+
], 'foo'));
7777
$this->assertSame('biz', $dotHelper([
78-
'foo' => [
79-
'bar' => 'biz',
80-
],
81-
], 'foo', 'bar'));
78+
'foo' => [
79+
'bar' => 'biz',
80+
],
81+
], 'foo', 'bar'));
8282
$this->assertSame(null, $dotHelper([
83-
'foo' => [
84-
],
85-
], 'foo', 'bar'));
83+
'foo' => [
84+
],
85+
], 'foo', 'bar'));
8686
$this->assertSame(null, $dotHelper([
87-
'foo' => [
88-
],
89-
], 'biz', 'bar'));
87+
'foo' => [
88+
],
89+
], 'biz', 'bar'));
9090
}
9191

9292
public function testObjectMember()
9393
{
9494
$dotHelper = $this->getDotHelper();
9595

9696
$this->assertSame(42, $dotHelper((object) [
97-
'foo' => 42,
98-
], 'foo'));
97+
'foo' => 42,
98+
], 'foo'));
9999
$this->assertSame('biz', $dotHelper((object) [
100-
'foo' => (object) [
101-
'bar' => 'biz',
102-
],
103-
], 'foo', 'bar'));
100+
'foo' => (object) [
101+
'bar' => 'biz',
102+
],
103+
], 'foo', 'bar'));
104104
$this->assertSame(null, $dotHelper((object) [
105-
'foo' => (object) [
106-
],
107-
], 'foo', 'bar'));
105+
'foo' => (object) [
106+
],
107+
], 'foo', 'bar'));
108108
$this->assertSame(null, $dotHelper((object) [
109-
'foo' => (object) [
110-
],
111-
], 'biz', 'bar'));
109+
'foo' => (object) [
110+
],
111+
], 'biz', 'bar'));
112112
}
113113

114114
public function testMixedObjectMemberAndArayValue()
115115
{
116116
$dotHelper = $this->getDotHelper();
117117

118118
$this->assertSame('biz', $dotHelper([
119-
'foo' => (object) [
120-
'bar' => 'biz',
121-
],
122-
], 'foo', 'bar'));
119+
'foo' => (object) [
120+
'bar' => 'biz',
121+
],
122+
], 'foo', 'bar'));
123123
$this->assertSame('biz', $dotHelper((object) [
124-
'foo' => [
125-
'bar' => 'biz',
126-
],
127-
], 'foo', 'bar'));
124+
'foo' => [
125+
'bar' => 'biz',
126+
],
127+
], 'foo', 'bar'));
128128
$this->assertSame(42, $dotHelper((object) [
129-
'foo' => [
130-
'bar' => (object) [
131-
'biz' => 42,
132-
],
129+
'foo' => [
130+
'bar' => (object) [
131+
'biz' => 42,
133132
],
134-
], 'foo', 'bar', 'biz'));
133+
],
134+
], 'foo', 'bar', 'biz'));
135135
$this->assertSame(42, $dotHelper([
136-
'foo' => (object) [
137-
'bar' => [
138-
'biz' => 42,
139-
],
136+
'foo' => (object) [
137+
'bar' => [
138+
'biz' => 42,
140139
],
141-
], 'foo', 'bar', 'biz'));
140+
],
141+
], 'foo', 'bar', 'biz'));
142142
}
143143

144144
public function testMagicMethod()

0 commit comments

Comments
 (0)