Skip to content

Commit 140d4fc

Browse files
committed
Handle let in for/while/etc. statements
1 parent c41102d commit 140d4fc

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

examples/for.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let n = 1;
2+
3+
for (let i = 0; i < 3; i++) {
4+
n *= 2;
5+
}
6+
7+
return n;

examples/for.return

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

src/JsPhpize/Lexer/DataBag.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public function is($value)
2121
return in_array($value, [$this->type, $this->value]);
2222
}
2323

24-
protected function typeIn($values)
24+
public function typeIn($values)
2525
{
2626
return in_array($this->type, $values);
2727
}
2828

29-
protected function valueIn($values)
29+
public function valueIn($values)
3030
{
3131
return in_array($this->value, $values);
3232
}

src/JsPhpize/Parser/Parser.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ protected function parseParentheses($allowedSeparators = [',', ';'])
104104
continue;
105105
}
106106

107-
if ($token->is('var')) {
107+
if ($token->type === 'keyword' && $token->valueIn(['var', 'const', 'let'])) {
108+
// @TODO handle let scope here
109+
// if ($token->value === 'let') {
110+
// $this->letForNextBlock = $this->parseLet();
111+
// }
112+
108113
continue;
109114
}
110115

@@ -367,7 +372,7 @@ protected function parseKeywordStatement($token)
367372
$keyword = new Block($name);
368373
switch ($name) {
369374
case 'typeof':
370-
exit('ici');
375+
throw new Exception('typeof keyword not supported', 26);
371376
break;
372377
case 'new':
373378
case 'clone':
@@ -430,7 +435,7 @@ protected function parseInstruction($block, $token, &$initNext)
430435

431436
if ($token->value === 'let') {
432437
$initNext = true;
433-
$block->let($this->parseLet($token));
438+
$block->let($this->parseLet());
434439

435440
return true;
436441
}
@@ -473,6 +478,10 @@ protected function parseInstructions($block)
473478
}
474479
}
475480

481+
/**
482+
* @param Block $block
483+
* @throws \JsPhpize\Lexer\Exception
484+
*/
476485
public function parseBlock($block)
477486
{
478487
$this->stack[] = $block;

0 commit comments

Comments
 (0)