Skip to content

Commit 0409005

Browse files
committed
Fix multi-line function blocks
1 parent 63c07ef commit 0409005

File tree

8 files changed

+56
-33
lines changed

8 files changed

+56
-33
lines changed

examples/block-ternary.js

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

examples/block-ternary.return

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

examples/sub-child.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
a = {
2+
b: {
3+
c: function () {
4+
return {
5+
d: [null, null, {
6+
foo: function () {
7+
return 'bar';
8+
}
9+
}]
10+
}
11+
}
12+
}
13+
};
14+
15+
return a.b['c']().d[2].foo();

examples/sub-child.return

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

src/JsPhpize/Nodes/FunctionCall.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace JsPhpize\Nodes;
44

5+
use JsPhpize\Parser\Exception;
6+
57
class FunctionCall extends Value
68
{
79
/**
8-
* @var Value
10+
* @var Value|Block
911
*/
1012
protected $function;
1113

@@ -24,8 +26,12 @@ class FunctionCall extends Value
2426
*/
2527
protected $applicant;
2628

27-
public function __construct(Value $function, array $arguments, array $children, $applicant = null)
29+
public function __construct(Node $function, array $arguments, array $children, $applicant = null)
2830
{
31+
if (!($function instanceof Value || $function instanceof Block)) {
32+
throw new Exception('Unexpected called type ' . get_class($function), 24);
33+
}
34+
2935
$this->function = $function;
3036
$this->arguments = $arguments;
3137
$this->applicant = $applicant;

src/JsPhpize/Parser/Parser.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ protected function parseLambda(Value $parameters)
6262
return $lambda;
6363
}
6464

65-
protected function parseValueCompletion(&$base, $token)
66-
{
67-
if ($token && $token->is('?')) {
68-
$base = $this->parseTernary($base);
69-
}
70-
71-
$this->appendFunctionsCalls($base);
72-
}
73-
7465
protected function parseParentheses()
7566
{
7667
$parentheses = new Parenthesis();
@@ -291,9 +282,10 @@ protected function parseValue($token)
291282
: new Constant($token->type, $token->value);
292283
}
293284

294-
protected function parseFunction($token)
285+
protected function parseFunction()
295286
{
296287
$function = new Block('function');
288+
$function->enableMultipleInstructions();
297289
$token = $this->get(0);
298290
if ($token && $token->type === 'variable') {
299291
$this->skip();
@@ -313,7 +305,6 @@ protected function parseFunction($token)
313305

314306
$this->skip();
315307
$this->parseBlock($function);
316-
$this->skip();
317308

318309
return $function;
319310
}
@@ -367,9 +358,8 @@ protected function parseLet()
367358

368359
protected function parseInstructions($block)
369360
{
370-
$endToken = $this->getEndTokenFromBlock($block);
371361
while ($token = $this->next()) {
372-
if ($token->is($endToken)) {
362+
if ($token->is($block->multipleInstructions ? '}' : ';')) {
373363
break;
374364
}
375365

@@ -391,16 +381,13 @@ protected function parseInstructions($block)
391381
continue;
392382
}
393383

394-
if ($token->isIn('?', '(', '[') || $token->isOperator()) {
395-
$value = $block->value;
396-
$this->parseValueCompletion($value, $token);
397-
$block->addInstruction($value);
398-
399-
continue;
400-
}
401384

402385
if ($token->is(';') || !$this->engine->getOption('strict')) {
403386
$block->endInstruction();
387+
// $next = $this->get(0);
388+
// if ($block->type !== 'function' && $next && $next->is('}')) {
389+
// break;
390+
// }
404391

405392
continue;
406393
}
@@ -412,11 +399,6 @@ protected function parseInstructions($block)
412399
public function parseBlock($block)
413400
{
414401
$this->stack[] = $block;
415-
$next = $this->get(0);
416-
if ($next && $next->is('(') && !($block instanceof Main)) {
417-
$this->skip();
418-
$block->setValue($this->parseParentheses());
419-
}
420402
if (!$block->multipleInstructions) {
421403
$next = $this->get(0);
422404
if ($next && $next->is('{')) {

src/JsPhpize/Parser/TokenExtractor.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ protected function getVariableChildFromToken(Token $token)
6363
}
6464
}
6565

66-
protected function getEndTokenFromBlock($block)
67-
{
68-
return $block->multipleInstructions ? '}' : ';';
69-
}
70-
7166
protected function getInstructionFromToken($token)
7267
{
7368
if ($token->type === 'keyword') {
@@ -110,7 +105,7 @@ protected function handleParentheses($keyword, $afterKeyword)
110105
protected function getInitialValue($token)
111106
{
112107
if ($token->isFunction()) {
113-
return $this->parseFunction($token);
108+
return $this->parseFunction();
114109
}
115110
if ($token->is('(')) {
116111
return $this->parseParentheses();

tests/badSyntaxes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
use JsPhpize\JsPhpize;
4+
use JsPhpize\Nodes\FunctionCall;
5+
use JsPhpize\Nodes\Instruction;
46

57
class BadSyntaxesTest extends \PHPUnit_Framework_TestCase
68
{
@@ -93,4 +95,13 @@ public function testTernaryMissingFalseValue()
9395
$jsPhpize = new JsPhpize();
9496
$jsPhpize->render('a = true ? true :');
9597
}
98+
99+
/**
100+
* @expectedException \JsPhpize\Parser\Exception
101+
* @expectedExceptionCode 24
102+
*/
103+
public function testBadFunctionCall()
104+
{
105+
new FunctionCall(new Instruction(), array(), array());
106+
}
96107
}

0 commit comments

Comments
 (0)