Skip to content

Commit 5d2198a

Browse files
committed
Fix function calls
1 parent e2119bb commit 5d2198a

File tree

5 files changed

+74
-56
lines changed

5 files changed

+74
-56
lines changed

examples/function.anonymous.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a = function () {
2+
return () => "foo";
3+
};
4+
5+
return a()();

examples/function.anonymous.return

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

src/JsPhpize/Compiler/Compiler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,20 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
183183
$function = $functionCall->function;
184184
$arguments = $functionCall->arguments;
185185
$arguments = $this->visitNodesArray($arguments, $indent, ', ');
186+
$call = 'call_user_func(' .
187+
$this->visitNode($function, $indent) .
188+
($arguments === '' ? '' : ', ' . $arguments) .
189+
')';
186190

187191
if ($function instanceof Variable) {
188192
$name = $function->name;
189193

190194
return 'function_exists(' . var_export($name, true) . ') ? ' .
191195
$name . '(' . $arguments . ') : ' .
192-
'call_user_func(' .
193-
$this->visitNode($function, $indent) .
194-
($arguments === '' ? '' : ', ' . $arguments) .
195-
')';
196+
$call;
196197
}
197198

198-
return $this->visitNode($function, $indent) . '(' . $arguments . ')';
199+
return $call;
199200
}
200201

201202
protected function visitHooksArray(HooksArray $array, $indent)

src/JsPhpize/Nodes/Block.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Block extends Node
1010
protected $type;
1111

1212
/**
13-
* @var Value
13+
* @var Node
1414
*/
1515
protected $value;
1616

@@ -108,7 +108,7 @@ public function endInstruction()
108108
$this->inInstruction = false;
109109
}
110110

111-
public function setValue(Value $value)
111+
public function setValue(Node $value)
112112
{
113113
if ($this->type === 'for') {
114114
$value->setSeparator(';');

src/JsPhpize/Parser/Parser.php

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,61 @@ protected function parseTernary(Node $condition)
325325

326326
protected function parseValue($token)
327327
{
328-
$value = $token->is('variable')
328+
return $token->is('variable')
329329
? $this->parseVariable($token->value)
330330
: new Constant($token->type, $token->value);
331+
}
332+
333+
protected function getInitialValue($token)
334+
{
335+
if ($token->is('function')) {
336+
$function = new Block('function');
337+
$token = $this->get(0);
338+
if ($token->is('variable')) {
339+
$this->skip();
340+
$token = $this->get(0);
341+
}
342+
if (!$token->is('(')) {
343+
$this->unexpected($token);
344+
}
345+
$this->skip();
346+
$function->setValue($this->parseParentheses());
347+
$token = $this->get(0);
348+
if (!$token->is('{')) {
349+
$this->unexpected($token);
350+
}
351+
$this->skip();
352+
$this->parseBlock($function);
353+
$this->skip();
354+
355+
return $function;
356+
}
357+
if ($token->is('(')) {
358+
return $this->parseParentheses();
359+
}
360+
if ($token->is('[')) {
361+
return $this->parseHooksArray();
362+
}
363+
if ($token->is('{')) {
364+
return $this->parseBracketsArray();
365+
}
366+
if ($token->isOperator() && $token->isIn('~', '!', '--', '++', '-', '+', 'delete', 'typeof', 'void')) {
367+
$value = $this->expectValue($this->next(), $token);
368+
$value->prepend($token->type);
331369

370+
return $value;
371+
}
372+
if ($token->isValue()) {
373+
return $this->parseValue($token);
374+
}
375+
}
376+
377+
protected function appendFunctionsCalls(&$value)
378+
{
332379
while ($token = $this->get(0)) {
333380
if ($token->isValue()) {
334381
$this->unexpected($this->next());
335382
}
336-
if ($token->is('(')) {
337-
$this->skip();
338-
$arguments = array();
339-
$value = new FunctionCall($value, $this->parseParentheses()->nodes);
340-
341-
continue;
342-
}
343383
if ($token->is('{') || $token->expectNoLeftMember()) {
344384
$this->unexpected($this->next());
345385
}
@@ -349,6 +389,13 @@ protected function parseValue($token)
349389

350390
continue;
351391
}
392+
if ($token->is('(')) {
393+
$this->skip();
394+
$arguments = array();
395+
$value = new FunctionCall($value, $this->parseParentheses()->nodes);
396+
397+
continue;
398+
}
352399
if ($token->isOperator()) {
353400
if ($token->isIn('++', '--')) {
354401
$value->append($this->next()->type);
@@ -374,52 +421,16 @@ protected function parseValue($token)
374421

375422
break;
376423
}
377-
378-
return $value;
379424
}
380425

381426
protected function getValueFromToken($token)
382427
{
383-
if ($token->is('function')) {
384-
$function = new Block('function');
385-
$token = $this->get(0);
386-
if ($token->is('variable')) {
387-
$this->skip();
388-
$token = $this->get(0);
389-
}
390-
if (!$token->is('(')) {
391-
$this->unexpected($token);
392-
}
393-
$this->skip();
394-
$function->setValue($this->parseParentheses());
395-
$token = $this->get(0);
396-
if (!$token->is('{')) {
397-
$this->unexpected($token);
398-
}
399-
$this->skip();
400-
$this->parseBlock($function);
401-
$this->skip();
402-
403-
return $function;
404-
}
405-
if ($token->is('(')) {
406-
return $this->parseParentheses();
407-
}
408-
if ($token->is('[')) {
409-
return $this->parseHooksArray();
428+
$value = $this->getInitialValue($token);
429+
if ($value) {
430+
$this->appendFunctionsCalls($value);
410431
}
411-
if ($token->is('{')) {
412-
return $this->parseBracketsArray();
413-
}
414-
if ($token->isOperator() && $token->isIn('~', '!', '--', '++', '-', '+', 'delete', 'typeof', 'void')) {
415-
$value = $this->expectValue($this->next(), $token);
416-
$value->prepend($token->type);
417432

418-
return $value;
419-
}
420-
if ($token->isValue()) {
421-
return $this->parseValue($token);
422-
}
433+
return $value;
423434
}
424435

425436
public function parseBlock($block)
@@ -458,7 +469,7 @@ public function parseBlock($block)
458469
case 'continue':
459470
case 'break':
460471
$afterKeyword = $this->get(0);
461-
if ($afterKeyword && $afterKeyword->isValue()) {
472+
if (!$afterKeyword->is(';')) {
462473
$value = $this->expectValue($this->next());
463474
$keyword->setValue($value);
464475
}

0 commit comments

Comments
 (0)