Skip to content

Commit 9261183

Browse files
committed
Init var/let/const variable with null
1 parent 0180bcc commit 9261183

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/JsPhpize/Parser/Parser.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use JsPhpize\JsPhpize;
66
use JsPhpize\Lexer\Lexer;
7+
use JsPhpize\Nodes\Assignation;
78
use JsPhpize\Nodes\Block;
89
use JsPhpize\Nodes\BracketsArray;
910
use JsPhpize\Nodes\Constant;
@@ -358,30 +359,40 @@ protected function parseLet()
358359

359360
protected function parseInstructions($block)
360361
{
362+
$initNext = false;
363+
361364
while ($token = $this->next()) {
362365
if ($token->is($block->multipleInstructions ? '}' : ';')) {
363366
break;
364367
}
365368

366369
if ($token->type === 'keyword') {
367370
if ($token->isIn('var', 'const')) {
371+
$initNext = true;
372+
368373
continue;
369374
}
370375

371376
if ($token->value === 'let') {
377+
$initNext = true;
372378
$block->let($this->parseLet($token));
373379

374380
continue;
375381
}
376382
}
377383

378384
if ($instruction = $this->getInstructionFromToken($token)) {
385+
if ($initNext && $instruction instanceof Variable) {
386+
$instruction = new Assignation('=', $instruction, new Constant('constant', 'null'));
387+
}
388+
$initNext = false;
379389
$block->addInstruction($instruction);
380390

381391
continue;
382392
}
383393

384394
if ($token->is(';') || !$this->engine->getOption('strict')) {
395+
$initNext = false;
385396
$block->endInstruction();
386397

387398
continue;

0 commit comments

Comments
 (0)