Skip to content

Commit 3faa4fd

Browse files
committed
Handle constant declaration
1 parent 5d2207f commit 3faa4fd

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

.codeclimate.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ engines:
1111
enabled: true
1212
phpmd:
1313
enabled: true
14+
checks:
15+
Design/CouplingBetweenObjects:
16+
enabled: false
1417
exclude_fingerprints:
1518
# Ignore Stream checks
1619
- 862522819408aad57533cfe39d74fde2

examples/calcul.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
a = 4;
1+
A = 4;
22
b = 1;
33
c = 3;
44

5-
return round(Math.PI * 100) + b + a * c;
5+
return round(Math.PI * 100) + b + A * c;

src/JsPhpize/Compiler/Compiler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public function compileDependencies($dependencies)
8787

8888
protected function visitAssignation(Assignation $assignation, $indent)
8989
{
90+
if ($assignation->leftHand instanceof Constant && $assignation->leftHand->type === 'constant') {
91+
return 'define(' .
92+
var_export(strval($assignation->leftHand->value), true) . ', ' .
93+
$this->visitNode($assignation->rightHand, $indent) .
94+
')';
95+
}
96+
9097
return $this->visitNode($assignation->leftHand, $indent) .
9198
' ' . $assignation->operator .
9299
' ' . $this->visitNode($assignation->rightHand, $indent);

src/JsPhpize/Parser/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ protected function parseInstructions($block)
304304
if ($token->is($endToken)) {
305305
break;
306306
}
307-
if ($token->is('var')) {
307+
if ($token->isIn('var', 'const')) {
308308
continue;
309309
}
310310
if ($token->is('let')) {

src/JsPhpize/Parser/TokenExtractor.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,42 @@
22

33
namespace JsPhpize\Parser;
44

5+
use JsPhpize\Lexer\Token;
56
use JsPhpize\Nodes\Assignation;
67
use JsPhpize\Nodes\Constant;
78
use JsPhpize\Nodes\Dyiade;
89
use JsPhpize\Nodes\FunctionCall;
910

1011
abstract class TokenExtractor extends TokenCrawler
1112
{
12-
protected function getBracketsArrayItemKeyFromToken($token)
13+
protected function getStringExport($value)
1314
{
14-
$type = null;
15+
return array('string', var_export($value, true));
16+
}
1517

18+
protected function getTypeAndValueFromToken(Token $token)
19+
{
1620
if ($token->is('keyword')) {
17-
$type = 'string';
18-
$value = var_export($token->value, true);
19-
} elseif ($token->isValue()) {
21+
return $this->getStringExport($token->value);
22+
}
23+
24+
if ($token->isValue()) {
2025
$type = $token->type;
2126
$value = $token->value;
27+
2228
if ($type === 'variable') {
23-
$type = 'string';
24-
$value = var_export($value, true);
29+
return $this->getStringExport($value);
2530
}
31+
32+
return array($token->type, $token->value);
2633
}
2734

35+
return array(null, null);
36+
}
37+
protected function getBracketsArrayItemKeyFromToken($token)
38+
{
39+
list($type, $value) = $this->getTypeAndValueFromToken($token);
40+
2841
if ($type) {
2942
$token = $this->next();
3043
if (!$token) {

0 commit comments

Comments
 (0)