|
16 | 16 | class Compiler extends PrettyPrinterAbstract |
17 | 17 | { |
18 | 18 | public $vars = array(); |
| 19 | + public $mode = 'js'; |
19 | 20 | // Special nodes |
20 | 21 |
|
21 | 22 | protected function pParam(Node\Param$node) |
@@ -591,6 +592,10 @@ protected function pExpr_Cast_Unset(Cast\Unset_$node) |
591 | 592 |
|
592 | 593 | protected function pExpr_FuncCall(Expr\FuncCall$node) |
593 | 594 | { |
| 595 | + $compiled = $this->compilefn($node); |
| 596 | + if($compiled){ |
| 597 | + return $compiled; |
| 598 | + } |
594 | 599 | return $this->pCallLhs($node->name) |
595 | 600 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
596 | 601 | } |
@@ -636,13 +641,15 @@ protected function pExpr_Eval(Expr\Eval_$node) |
636 | 641 | protected function pExpr_Include(Expr\Include_$node) |
637 | 642 | { |
638 | 643 | 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', |
643 | 648 | ]; |
644 | 649 |
|
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."'"; |
646 | 653 | } |
647 | 654 |
|
648 | 655 | protected function pExpr_List(Expr\List_$node) |
@@ -942,11 +949,21 @@ protected function pStmt_ClassConst(Stmt\ClassConst$node) |
942 | 949 |
|
943 | 950 | protected function pStmt_Function(Stmt\Function_$node) |
944 | 951 | { |
| 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 | + |
945 | 960 | return $this->pAttrGroups($node->attrGroups) |
| 961 | + . ($is_async ? 'async ' : '') |
946 | 962 | . 'function ' . ($node->byRef ? '&' : '') . $node->name |
947 | 963 | . '(' . $this->pCommaSeparated($node->params) . ')' |
948 | 964 | . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
949 | 965 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
| 966 | + |
950 | 967 | } |
951 | 968 |
|
952 | 969 | protected function pStmt_Const(Stmt\Const_$node) |
@@ -1095,7 +1112,7 @@ protected function pStmt_Static(Stmt\Static_$node) |
1095 | 1112 |
|
1096 | 1113 | protected function pStmt_Global(Stmt\Global_$node) |
1097 | 1114 | { |
1098 | | - return 'global ' . $this->pCommaSeparated($node->vars) . ';'; |
| 1115 | + return 'export ' . $this->pCommaSeparated($node->vars) . ';'; |
1099 | 1116 | } |
1100 | 1117 |
|
1101 | 1118 | protected function pStmt_StaticVar(Stmt\StaticVar$node) |
@@ -1277,4 +1294,23 @@ protected function pAttrGroups(array $nodes, bool $inline = false): string |
1277 | 1294 |
|
1278 | 1295 | return $result; |
1279 | 1296 | } |
| 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 | + } |
1280 | 1316 | } |
0 commit comments