diff --git a/src/Compiler.php b/src/Compiler.php index 14b6034..5fa1826 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -16,6 +16,7 @@ class Compiler extends PrettyPrinterAbstract { public $vars = array(); + public $mode = 'js'; // Special nodes protected function pParam(Node\Param$node) @@ -591,6 +592,10 @@ protected function pExpr_Cast_Unset(Cast\Unset_$node) protected function pExpr_FuncCall(Expr\FuncCall$node) { + $compiled = $this->compilefn($node); + if($compiled){ + return $compiled; + } return $this->pCallLhs($node->name) . '(' . $this->pMaybeMultiline($node->args) . ')'; } @@ -636,13 +641,15 @@ protected function pExpr_Eval(Expr\Eval_$node) protected function pExpr_Include(Expr\Include_$node) { static $map = [ - Expr\Include_::TYPE_INCLUDE => 'include', - Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once', - Expr\Include_::TYPE_REQUIRE => 'require', - Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once', + Expr\Include_::TYPE_INCLUDE => 'import', + Expr\Include_::TYPE_INCLUDE_ONCE => 'import', + Expr\Include_::TYPE_REQUIRE => 'import', + Expr\Include_::TYPE_REQUIRE_ONCE => 'import', ]; - return $map[$node->type] . ' ' . $this->p($node->expr); + $expr = trim($this->p($node->expr),"'"); + + return $map[$node->type] . ' ' . $expr ." from './".$expr.'.'.$this->mode."'"; } protected function pExpr_List(Expr\List_$node) @@ -942,11 +949,21 @@ protected function pStmt_ClassConst(Stmt\ClassConst$node) protected function pStmt_Function(Stmt\Function_$node) { + $is_async = false; + foreach ($node->getComments() as $comment){ + $comment = strtolower(trim(trim($comment,'/*'))); + if($comment == '@async'){ + $is_async = true; + } + } + return $this->pAttrGroups($node->attrGroups) + . ($is_async ? 'async ' : '') . 'function ' . ($node->byRef ? '&' : '') . $node->name . '(' . $this->pCommaSeparated($node->params) . ')' . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; + } protected function pStmt_Const(Stmt\Const_$node) @@ -1095,7 +1112,7 @@ protected function pStmt_Static(Stmt\Static_$node) protected function pStmt_Global(Stmt\Global_$node) { - return 'global ' . $this->pCommaSeparated($node->vars) . ';'; + return 'export ' . $this->pCommaSeparated($node->vars) . ';'; } protected function pStmt_StaticVar(Stmt\StaticVar$node) @@ -1277,4 +1294,23 @@ protected function pAttrGroups(array $nodes, bool $inline = false): string return $result; } + + private function compilefn($node){ + // $this->pCallLhs($node->name) + // . '(' . $this->pMaybeMultiline($node->args) . ')'; + $pArgs = []; + foreach ($node->args as $arg) { + if (null === $arg) { + $pArgs[] = ''; + } else { + $pArgs[] = $this->p($arg); + } + } + + if($node->name == 'import_from'){ + return "import ".trim($pArgs[0],"'")." from ".$pArgs[1]; + } + return false; + + } } diff --git a/src/Php2js.php b/src/Php2js.php index 00e9b42..81a095c 100644 --- a/src/Php2js.php +++ b/src/Php2js.php @@ -7,21 +7,24 @@ class PHP2JS { - public static function compile($code){ + public static function compile($code,$mode = 'js'){ $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); - try { - $ast = $parser->parse('getMessage()}\n"; - return; - } + $errorHandler = new \PhpParser\ErrorHandler\Collecting; + + $ast = $parser->parse('hasErrors()) { + foreach ($errorHandler->getErrors() as $error) { + // $error is an ordinary PhpParser\Error + } + } $compiler= new Compiler; + $compiler->mode = $mode; $jscode = $compiler->prettyPrint($ast); return $jscode; } - public static function compileFile($input,$output=null){ + public static function compileFile($input,$output=null,$mode='js'){ $code = @file_get_contents($input); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); try { @@ -32,6 +35,7 @@ public static function compileFile($input,$output=null){ } $compiler= new Compiler; + $compiler->mode = $mode; $jscode = $compiler->prettyPrint($ast); if($output){ file_put_contents($output,$jscode);