From b6e28db692ec0f2af2089eb7d90dcb9506d0f79d Mon Sep 17 00:00:00 2001 From: Christopher Pitt Date: Mon, 8 Oct 2018 18:49:28 +0200 Subject: [PATCH] Update plugin --- composer.json | 4 +- composer.lock | 108 +----------------- source/Parser.php | 22 +--- source/Printer.php | 81 ------------- tests/CompileTest.php | 4 +- tests/TestCase.php | 2 +- tests/fixtures/compile/can-compile.php | 14 +-- .../supports-nested-component-names.php | 12 +- tests/fixtures/nodes/can-arrange-nodes.php | 6 +- tests/fixtures/nodes/can-arrange-nodes.txt | 2 +- .../tokens/can-handle-complex-props.php | 8 +- .../tokens/can-handle-complex-props.txt | 2 +- 12 files changed, 33 insertions(+), 232 deletions(-) delete mode 100644 source/Printer.php diff --git a/composer.json b/composer.json index c5d0a66..f3c70bb 100644 --- a/composer.json +++ b/composer.json @@ -3,9 +3,7 @@ "type": "pre-compiler", "license": "MIT", "require": { - "pre/plugin": "^0.11.0", - "nikic/php-parser": "^4.0", - "gajus/dindent": "^2.0" + "pre/plugin": "^0.11.3" }, "require-dev": { "phpunit/phpunit": "^5.0|^6.0" diff --git a/composer.lock b/composer.lock index 66595d1..73617ce 100644 --- a/composer.lock +++ b/composer.lock @@ -4,116 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f5f0f5e0de23f57b10304394c4bd84ef", + "content-hash": "6052a4ca9ed8a01a5e7d87c4e4639835", "packages": [ - { - "name": "gajus/dindent", - "version": "2.0.2", - "source": { - "type": "git", - "url": "https://github.com/gajus/dindent.git", - "reference": "d81c3a6f78fbe1ab26f5e753098bbbef6b6a9f3c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/gajus/dindent/zipball/d81c3a6f78fbe1ab26f5e753098bbbef6b6a9f3c", - "reference": "d81c3a6f78fbe1ab26f5e753098bbbef6b6a9f3c", - "shasum": "" - }, - "require": { - "php": ">=5.3" - }, - "require-dev": { - "satooshi/php-coveralls": "dev-master" - }, - "type": "library", - "autoload": { - "psr-4": { - "Gajus\\Dindent\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Gajus Kuizinas", - "email": "gk@anuary.com" - } - ], - "description": "HTML indentation library for development and testing.", - "homepage": "https://github.com/gajus/dindent", - "keywords": [ - "format", - "html", - "indent" - ], - "time": "2014-10-08T10:03:04+00:00" - }, - { - "name": "nikic/php-parser", - "version": "v4.0.3", - "source": { - "type": "git", - "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "bd088dc940a418f09cda079a9b5c7c478890fb8d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/bd088dc940a418f09cda079a9b5c7c478890fb8d", - "reference": "bd088dc940a418f09cda079a9b5c7c478890fb8d", - "shasum": "" - }, - "require": { - "ext-tokenizer": "*", - "php": ">=7.0" - }, - "require-dev": { - "phpunit/phpunit": "^6.5 || ^7.0" - }, - "bin": [ - "bin/php-parse" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "4.0-dev" - } - }, - "autoload": { - "psr-4": { - "PhpParser\\": "lib/PhpParser" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Nikita Popov" - } - ], - "description": "A PHP parser written in PHP", - "keywords": [ - "parser", - "php" - ], - "time": "2018-07-15T17:25:16+00:00" - }, { "name": "pre/plugin", - "version": "0.11.0", + "version": "0.11.3", "source": { "type": "git", "url": "https://github.com/preprocess/pre-plugin.git", - "reference": "74bf7893cea59077cca120ca73987c0297820efc" + "reference": "5195958886ab8ef54c444b18126081021f45f500" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/preprocess/pre-plugin/zipball/74bf7893cea59077cca120ca73987c0297820efc", - "reference": "74bf7893cea59077cca120ca73987c0297820efc", + "url": "https://api.github.com/repos/preprocess/pre-plugin/zipball/5195958886ab8ef54c444b18126081021f45f500", + "reference": "5195958886ab8ef54c444b18126081021f45f500", "shasum": "" }, "require": { @@ -146,7 +50,7 @@ "license": [ "MIT" ], - "time": "2018-09-10T11:17:14+00:00" + "time": "2018-10-08T15:34:29+00:00" } ], "packages-dev": [ diff --git a/source/Parser.php b/source/Parser.php index 025e302..d7b7c4e 100644 --- a/source/Parser.php +++ b/source/Parser.php @@ -3,21 +3,11 @@ namespace Pre\Phpx; use Exception; -use PhpParser\Error; -use PhpParser\NodeDumper; -use PhpParser\ParserFactory; class Parser { private $printer; - public function __construct($printer = null) - { - if (is_null($printer)) { - $this->printer = new Printer(); - } - } - public function tokens($code) { $tokens = []; @@ -353,15 +343,7 @@ public function translate($nodes, $quoteLiterals = false, $combineChildren = fal public function format($code) { - $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); - - try { - $parsed = $parser->parse($code); - } catch (Exception $e) { - // can't format, but we can still return... - return $code; - } - - return $this->printer->prettyPrintFile($parsed); + // left to avoid a version bump + return $code; } } diff --git a/source/Printer.php b/source/Printer.php deleted file mode 100644 index c33f0d8..0000000 --- a/source/Printer.php +++ /dev/null @@ -1,81 +0,0 @@ -nl; - return preg_replace("#\{\s*{$nl}\s*{$nl}#", "{{$nl}", parent::pStmts($nodes, $indent)); - } - - protected function pStmt_Function(Stmt\Function_ $node) - { - return $this->nl . parent::pStmt_Function($node); - } - - protected function pExpr_Print(Expr\Print_ $node) - { - return $this->nl . $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr); - } - - protected function pStmt_Namespace(Stmt\Namespace_ $node) - { - $nl = $this->nl; - $name = is_null($node->name) ? "" : " " . $this->p($node->name); - - if ($this->canUseSemicolonNamespaces) { - return "{$nl}namespace{$name};{$nl}" . $this->pStmts($node->stmts, false); - } - - return "{$nl}namespace{$name}{$nl}{" . $this->pStmts($node->stmts) . "{$nl}}"; - } - - protected function pExpr_Ternary(Expr\Ternary $node) - { - $nl = $this->nl; - - if (is_null($node->if)) { - $results = $this->pInfixOp( - Expr\Ternary::class, - $node->cond, - "?: ", - $node->else - ); - } else { - $results = $this->pInfixOp( - Expr\Ternary::class, - $node->cond, - " ?{$nl}" . $this->p($node->if) . " :{$nl}", - $node->else - ); - } - - return $this->indentMore($results); - } - - protected function indentMore($code, $indent = " ") - { - $nl = $this->nl; - return preg_replace("#({$nl})(\s*)#", "$1{$indent}$2", $code); - } - - protected function pExpr_Array(Expr\Array_ $node) - { - if (count($node->items) < 1) { - return "[]"; - } - - return "[" . $this->pMaybeMultiline($node->items, true) . "]"; - } - - private function pMaybeMultiline(array $nodes) - { - return $this->pCommaSeparatedMultiline($nodes, true) . $this->nl; - } -} diff --git a/tests/CompileTest.php b/tests/CompileTest.php index c163491..23df6da 100644 --- a/tests/CompileTest.php +++ b/tests/CompileTest.php @@ -13,7 +13,7 @@ public function test_can_compile() { $this->assertEquals( $this->fixtureCode("compile/can-compile", "php"), - \Pre\Phpx\compile($this->fixtureCode("compile/can-compile", "pre")) + \Pre\Plugin\format(\Pre\Phpx\compile($this->fixtureCode("compile/can-compile", "pre"))) ); } @@ -21,7 +21,7 @@ public function test_supports_nested_component_names() { $this->assertEquals( $this->fixtureCode("compile/supports-nested-component-names", "php"), - \Pre\Phpx\compile($this->fixtureCode("compile/supports-nested-component-names", "pre")) + \Pre\Plugin\format(\Pre\Phpx\compile($this->fixtureCode("compile/supports-nested-component-names", "pre"))) ); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index d6c8860..1ab2741 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,7 +11,7 @@ protected function parser() protected function fixtureCode($path, $extension = "txt") { - return trim(file_get_contents(__DIR__ . "/fixtures/{$path}.{$extension}")); + return file_get_contents(__DIR__ . "/fixtures/{$path}.{$extension}"); } protected function fixtureData($path, $extension = "php") diff --git a/tests/fixtures/compile/can-compile.php b/tests/fixtures/compile/can-compile.php index 914d33c..94a71c5 100644 --- a/tests/fixtures/compile/can-compile.php +++ b/tests/fixtures/compile/can-compile.php @@ -7,17 +7,17 @@ function Error($props) "render" => function ($error) { return render("span", [ "className" => "error", - "children" => $error, + "children" => $error ]); }, "children" => [ render("span", [ - "className" => "icon", + "className" => "icon" ]), "You forgot the", $props->name, - "field", - ], + "field" + ] ]); } @@ -26,15 +26,15 @@ function ErrorRenderer($props) return render("Error", [ "children" => function ($error) { return render("span", [ - "children" => $error, + "children" => $error ]); - }, + } ]); } function InputRenderer($props) { return render("input", [ - "type" => $props->type, + "type" => $props->type ]); } diff --git a/tests/fixtures/compile/supports-nested-component-names.php b/tests/fixtures/compile/supports-nested-component-names.php index ef3afc5..fa6b221 100644 --- a/tests/fixtures/compile/supports-nested-component-names.php +++ b/tests/fixtures/compile/supports-nested-component-names.php @@ -1,21 +1,19 @@ "hello world", + "children" => "hello world" ]); } } -namespace -{ +namespace { function render($name, $props = null) { print $name; } - - print render("Example\\MyComponent", []); + + print render("Example\MyComponent", []); } diff --git a/tests/fixtures/nodes/can-arrange-nodes.php b/tests/fixtures/nodes/can-arrange-nodes.php index d1de304..368d85d 100644 --- a/tests/fixtures/nodes/can-arrange-nodes.php +++ b/tests/fixtures/nodes/can-arrange-nodes.php @@ -16,7 +16,7 @@ function Error(\$props) { [ [ "type" => "literal", - "value" => "\$error ⇒", + "value" => "\$error ~>", ], [ "type" => "tag", @@ -38,11 +38,11 @@ function Error(\$props) { "value" => "\$error", ], ], - "started" => 37, + "started" => 36, ], ], "name" => "span", - "started" => 30, + "started" => 29, ], ], ], diff --git a/tests/fixtures/nodes/can-arrange-nodes.txt b/tests/fixtures/nodes/can-arrange-nodes.txt index f72c827..17d8dea 100644 --- a/tests/fixtures/nodes/can-arrange-nodes.txt +++ b/tests/fixtures/nodes/can-arrange-nodes.txt @@ -1,6 +1,6 @@ function Error($props) { return ( -
{$error}}> +
{$error}}> You forgot the {$props->name} field
); diff --git a/tests/fixtures/tokens/can-handle-complex-props.php b/tests/fixtures/tokens/can-handle-complex-props.php index dbaf46d..061cb0b 100644 --- a/tests/fixtures/tokens/can-handle-complex-props.php +++ b/tests/fixtures/tokens/can-handle-complex-props.php @@ -16,12 +16,12 @@ function Hello(\$props) { [ [ "type" => "literal", - "value" => "\$error ⇒", + "value" => "\$error ~>", ], [ "type" => "tag", "value" => "", - "started" => 16, + "started" => 15, ], [ "type" => "expression", @@ -31,12 +31,12 @@ function Hello(\$props) { "value" => "\$error", ], ], - "started" => 23, + "started" => 22, ], [ "type" => "tag", "value" => "", - "started" => 29, + "started" => 28, ], ] ], diff --git a/tests/fixtures/tokens/can-handle-complex-props.txt b/tests/fixtures/tokens/can-handle-complex-props.txt index 4257b49..500ae91 100644 --- a/tests/fixtures/tokens/can-handle-complex-props.txt +++ b/tests/fixtures/tokens/can-handle-complex-props.txt @@ -1,3 +1,3 @@ function Hello($props) { - return
{$error}}>You forgot a field
; + return
{$error}}>You forgot a field
; }