Skip to content

Commit 63c07ef

Browse files
committed
Fix function call read variables detection
1 parent 0d555bd commit 63c07ef

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

examples/scope-nested-call.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var method = 'foo';
2+
var obj = {
3+
foo: function () {
4+
return 'bar';
5+
},
6+
};
7+
8+
var foo = function () {
9+
return obj[method]();
10+
};
11+
12+
return foo();

examples/scope-nested-call.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar

examples/scope-ternary.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var a = true;
2+
var b = [2];
3+
var c = 5;
4+
var d = 0;
5+
6+
var func = function (e) {
7+
return [a && !e ? b[d] : {c: c}];
8+
};
9+
10+
var resultA = func(false);
11+
var resultB = func(true);
12+
13+
return resultA[0] + resultB[0].c;

examples/scope-ternary.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7

src/JsPhpize/JsPhpize.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function compile($input, $filename = null)
6868
$block = $parser->parse();
6969
$php = $compiler->compile($block);
7070

71-
if ($this->flags & self::FLAG_TRUNCATED_PARENTHESES) {
71+
if ($this->hasFlag(self::FLAG_TRUNCATED_PARENTHESES)) {
7272
$php = preg_replace('/\)[\s;]*$/', '', $php);
7373
}
7474

@@ -87,6 +87,14 @@ public function compile($input, $filename = null)
8787
return preg_replace('/\{(\s*\}\s*\{)+$/', '{', $php);
8888
}
8989

90+
/**
91+
* @return int
92+
*/
93+
public function hasFlag($flag)
94+
{
95+
return $this->flags & $flag;
96+
}
97+
9098
/**
9199
* @param int $flag flag to set
92100
* @param bool $enabled flag state

src/JsPhpize/Nodes/FunctionCall.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ public function __construct(Value $function, array $arguments, array $children,
3434

3535
public function getReadVariables()
3636
{
37-
$variables = array();
37+
$variables = $this->function->getReadVariables();
3838
foreach ($this->arguments as $argument) {
3939
$variables = array_merge($variables, $argument->getReadVariables());
4040
}
41-
foreach ($this->children as $child) {
42-
$variables = array_merge($variables, $child->getReadVariables());
43-
}
4441

4542
return $variables;
4643
}

tests/helpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,15 @@ public function testDotHelperWithArrayPrototype()
281281
'246',
282282
$actual
283283
);
284+
285+
$jsPhpize = new JsPhpize();
286+
287+
$jsPhpize->setFlag(JsPhpize::FLAG_TRUNCATED_PARENTHESES, true);
288+
289+
self::assertEquals(true, $jsPhpize->hasFlag(JsPhpize::FLAG_TRUNCATED_PARENTHESES));
290+
291+
$jsPhpize->setFlag(JsPhpize::FLAG_TRUNCATED_PARENTHESES, false);
292+
293+
self::assertEquals(false, $jsPhpize->hasFlag(JsPhpize::FLAG_TRUNCATED_PARENTHESES));
284294
}
285295
}

0 commit comments

Comments
 (0)