Skip to content

Commit a9eb6f6

Browse files
author
its-pranjalpandey
committed
feat: import feature and async function
1 parent db28908 commit a9eb6f6

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/Compiler.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
class Compiler extends PrettyPrinterAbstract
1717
{
1818
public $vars = array();
19+
public $mode = 'js';
1920
// Special nodes
2021

2122
protected function pParam(Node\Param$node)
@@ -591,6 +592,10 @@ protected function pExpr_Cast_Unset(Cast\Unset_$node)
591592

592593
protected function pExpr_FuncCall(Expr\FuncCall$node)
593594
{
595+
$compiled = $this->compilefn($node);
596+
if($compiled){
597+
return $compiled;
598+
}
594599
return $this->pCallLhs($node->name)
595600
. '(' . $this->pMaybeMultiline($node->args) . ')';
596601
}
@@ -636,13 +641,15 @@ protected function pExpr_Eval(Expr\Eval_$node)
636641
protected function pExpr_Include(Expr\Include_$node)
637642
{
638643
static $map = [
639-
Expr\Include_::TYPE_INCLUDE => 'include',
640-
Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
641-
Expr\Include_::TYPE_REQUIRE => 'require',
642-
Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
644+
Expr\Include_::TYPE_INCLUDE => 'import',
645+
Expr\Include_::TYPE_INCLUDE_ONCE => 'import',
646+
Expr\Include_::TYPE_REQUIRE => 'import',
647+
Expr\Include_::TYPE_REQUIRE_ONCE => 'import',
643648
];
644649

645-
return $map[$node->type] . ' ' . $this->p($node->expr);
650+
$expr = trim($this->p($node->expr),"'");
651+
652+
return $map[$node->type] . ' ' . $expr ." from './".$expr.'.'.$this->mode."'";
646653
}
647654

648655
protected function pExpr_List(Expr\List_$node)
@@ -942,11 +949,21 @@ protected function pStmt_ClassConst(Stmt\ClassConst$node)
942949

943950
protected function pStmt_Function(Stmt\Function_$node)
944951
{
952+
$is_async = false;
953+
foreach ($node->getComments() as $comment){
954+
$comment = strtolower(trim(trim($comment,'/*')));
955+
if($comment == '@async'){
956+
$is_async = true;
957+
}
958+
}
959+
945960
return $this->pAttrGroups($node->attrGroups)
961+
. ($is_async ? 'async ' : '')
946962
. 'function ' . ($node->byRef ? '&' : '') . $node->name
947963
. '(' . $this->pCommaSeparated($node->params) . ')'
948964
. (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
949965
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
966+
950967
}
951968

952969
protected function pStmt_Const(Stmt\Const_$node)
@@ -1095,7 +1112,7 @@ protected function pStmt_Static(Stmt\Static_$node)
10951112

10961113
protected function pStmt_Global(Stmt\Global_$node)
10971114
{
1098-
return 'global ' . $this->pCommaSeparated($node->vars) . ';';
1115+
return 'export ' . $this->pCommaSeparated($node->vars) . ';';
10991116
}
11001117

11011118
protected function pStmt_StaticVar(Stmt\StaticVar$node)
@@ -1277,4 +1294,23 @@ protected function pAttrGroups(array $nodes, bool $inline = false): string
12771294

12781295
return $result;
12791296
}
1297+
1298+
private function compilefn($node){
1299+
// $this->pCallLhs($node->name)
1300+
// . '(' . $this->pMaybeMultiline($node->args) . ')';
1301+
$pArgs = [];
1302+
foreach ($node->args as $arg) {
1303+
if (null === $arg) {
1304+
$pArgs[] = '';
1305+
} else {
1306+
$pArgs[] = $this->p($arg);
1307+
}
1308+
}
1309+
1310+
if($node->name == 'import_from'){
1311+
return "import ".trim($pArgs[0],"'")." from ".$pArgs[1];
1312+
}
1313+
return false;
1314+
1315+
}
12801316
}

src/Php2js.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@
77

88
class PHP2JS
99
{
10-
public static function compile($code){
10+
public static function compile($code,$mode = 'js'){
1111
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
12-
try {
13-
$ast = $parser->parse('<?php'.PHP_EOL.$code);
14-
} catch (Error $error) {
15-
echo "Parse error: {$error->getMessage()}\n";
16-
return;
17-
}
12+
$errorHandler = new \PhpParser\ErrorHandler\Collecting;
13+
14+
$ast = $parser->parse('<?php'.PHP_EOL.$code, $errorHandler);
15+
if ($errorHandler->hasErrors()) {
16+
foreach ($errorHandler->getErrors() as $error) {
17+
// $error is an ordinary PhpParser\Error
18+
}
19+
}
1820

1921
$compiler= new Compiler;
22+
$compiler->mode = $mode;
2023
$jscode = $compiler->prettyPrint($ast);
2124
return $jscode;
2225
}
2326

24-
public static function compileFile($input,$output=null){
27+
public static function compileFile($input,$output=null,$mode='js'){
2528
$code = @file_get_contents($input);
2629
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2730
try {
@@ -32,6 +35,7 @@ public static function compileFile($input,$output=null){
3235
}
3336

3437
$compiler= new Compiler;
38+
$compiler->mode = $mode;
3539
$jscode = $compiler->prettyPrint($ast);
3640
if($output){
3741
file_put_contents($output,$jscode);

0 commit comments

Comments
 (0)