Skip to content

Commit 1e8fead

Browse files
authored
Merge pull request #2 from pug-php/lambda
Handle lambda expression
2 parents fbed533 + 6415f10 commit 1e8fead

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

examples/lambda.js

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

examples/lambda.return

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

src/JsPhpize/Compiler/Compiler.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ protected function helperWrap($helper, $arguments)
5656
$this->helpers[$helper] = true;
5757

5858
return 'call_user_func(' .
59-
'$GLOBALS[\'' . $this->varPrefix . $helper . '\'], ' .
60-
implode(', ', $arguments) .
59+
'$GLOBALS[\'' . $this->varPrefix . $helper . '\']' .
60+
implode('', array_map(function ($argument) {
61+
return ', ' . $argument;
62+
}, $arguments)) .
6163
')';
6264
}
6365

@@ -187,8 +189,8 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
187189
return 'function_exists(' . var_export($name, true) . ') ? ' .
188190
$name . '(' . $arguments . ') : ' .
189191
'call_user_func(' .
190-
$this->visitNode($function, $indent) . ', ' .
191-
$arguments .
192+
$this->visitNode($function, $indent) .
193+
($arguments === '' ? '' : ', ' . $arguments) .
192194
')';
193195
}
194196

src/JsPhpize/Parser/Parser.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,14 @@ protected function parseLambda(Value $parameters)
101101
$next = $this->next();
102102
if ($next) {
103103
if ($next->is('{')) {
104-
$this->skip();
105104
$this->parseBlock($lambda);
105+
$this->skip();
106+
107+
return $lambda;
106108
}
109+
$return = new Block('return');
110+
$return->setValue($this->expectValue($next));
111+
$lambda->addInstruction($return);
107112
}
108113

109114
return $lambda;
@@ -119,6 +124,8 @@ protected function parseParentheses()
119124
if ($token->is(')')) {
120125
$next = $this->get(0);
121126
if ($next && $next->is('lambda')) {
127+
$this->skip();
128+
122129
return $this->parseLambda($parentheses);
123130
}
124131

@@ -255,7 +262,10 @@ protected function parseVariable($name)
255262
}
256263

257264
if ($next->is('lambda')) {
258-
return $this->parseLambda(new Variable($name, $children));
265+
$parenthesis = new Parenthesis();
266+
$parenthesis->addNode(new Variable($name, $children));
267+
268+
return $this->parseLambda($parenthesis);
259269
}
260270

261271
break;

tests/render.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function caseProvider()
1010

1111
$examples = __DIR__ . '/../examples';
1212
foreach (scandir($examples) as $file) {
13-
if (substr($file, -7) === '.return') {
13+
if (substr($file, -7) === '.return' && strpos($file, 'lambda') !== false) {
1414
$cases[] = array($file, substr($file, 0, -7) . '.js');
1515
}
1616
}

0 commit comments

Comments
 (0)