-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from preprocess/refactor-for-new-plugin
Refactor for new plugin
- Loading branch information
Showing
9 changed files
with
140 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
macro ·recursion { | ||
·chain( | ||
defer, | ||
·between( | ||
·token("{"), ·layer()·body, ·token("}") | ||
), | ||
·optional(·token(";")), | ||
·_()·scope, | ||
·_()·simple | ||
) | ||
} >> function($ast) { | ||
$bound = false; | ||
$scope = new \Yay\Ast("·scope"); | ||
|
||
$pushed = []; | ||
|
||
foreach ($ast->{"·body"} as $token) { | ||
$name = $token->value(); | ||
|
||
if (!$token->is(T_VARIABLE)) { | ||
continue; | ||
} | ||
|
||
if (isset($pushed[$name])) { | ||
continue; | ||
} | ||
|
||
if (substr($name, 1) === "this") { | ||
continue; | ||
} | ||
|
||
$scope->push(new \Yay\Ast("·var", $token)); | ||
$pushed[$name] = true; | ||
$bound = true; | ||
} | ||
|
||
if ($bound) { | ||
$ast->append($scope); | ||
} else { | ||
$simple = new \Yay\Ast("·simple"); | ||
$simple->push(new \Yay\Ast()); | ||
|
||
$ast->append($simple); | ||
} | ||
} >> { | ||
··collapse(··trim( | ||
·scope ?·{ | ||
$deferred = new \Pre\Deferred\Deferred([·scope ···(, ) { ·var = ·var ?? null}, "fn" => function () use (··trim(·scope ···(, ) {&·var})) { | ||
··trim(·body) | ||
}]["fn"]); | ||
} | ||
|
||
·simple ?·{ | ||
$deferred = new \Pre\Deferred\Deferred(function () { | ||
··trim(·body) | ||
}); | ||
} | ||
)) | ||
} | ||
|
||
macro ·recursion { | ||
·chain( | ||
defer, | ||
·ns()·function, | ||
·between( | ||
·token("("), ·layer()·parameters, ·token(")") | ||
), | ||
·token(";") | ||
) | ||
} >> { | ||
$deferred = ··unsafe(new \Pre\Deferred\Deferred((function (...$parameters) { | ||
return function () use ($parameters) { | ||
·function(...$parameters); | ||
}; | ||
})(·parameters))); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
<?php | ||
|
||
namespace Pre\Deferred; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use Pre\Plugin\Testing\Runner; | ||
|
||
class MacroTest extends Runner | ||
class MacroTest extends TestCase | ||
{ | ||
protected function path(): string | ||
/** | ||
* @test | ||
* @dataProvider specs | ||
*/ | ||
public function can_transform_code($from, $expected) | ||
{ | ||
\Pre\Plugin\addMacro(__DIR__ . "/../source/macros.yay"); | ||
|
||
$actual = \Pre\plugin\format(\Pre\Plugin\parse($this->format($from))); | ||
$this->assertEquals($this->format($expected), $actual); | ||
} | ||
|
||
private function format($code) | ||
{ | ||
return __DIR__ . "/specs"; | ||
return "<?php\n\n" . trim($code) . "\n"; | ||
} | ||
|
||
public static function specs() | ||
{ | ||
$specs = []; | ||
|
||
$files = [ | ||
__DIR__ . "/specs/deferred.spec", | ||
]; | ||
|
||
foreach ($files as $file) { | ||
$contents = file_get_contents($file); | ||
|
||
foreach (explode("---", $contents) as $spec) { | ||
array_push($specs, explode("~~~", $spec)); | ||
} | ||
} | ||
|
||
return $specs; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,23 @@ | ||
--DESCRIPTION-- | ||
defer unlink("path/to/file"); | ||
|
||
Test deferred | ||
~~~ | ||
|
||
--GIVEN-- | ||
$deferred·0 = new \Pre\Deferred\Deferred((function (...$parameters) { | ||
return function () use ($parameters) { | ||
unlink(...$parameters); | ||
}; | ||
})("path/to/file")); | ||
|
||
defer unlink("path/to/file"); | ||
--- | ||
|
||
defer { | ||
fclose($handle); | ||
print "all done"; | ||
} | ||
|
||
--EXPECT-- | ||
~~~ | ||
|
||
$deferred·0 = new \Pre\Deferred\Deferred(call_user_func(function () { | ||
$context·0 = func_get_args(); | ||
|
||
return function () use ($context·0) { | ||
call_user_func_array('unlink', $context·0); | ||
}; | ||
}, "path/to/file")); | ||
|
||
$deferred·1 = new \Pre\Deferred\Deferred(call_user_func(function ($context·1) { | ||
return function () use ($context·1) { | ||
extract($context·1); | ||
fclose($handle); | ||
print "all done"; | ||
}; | ||
}, get_defined_vars())); | ||
$deferred·0 = new \Pre\Deferred\Deferred([$handle = $handle ?? null, "fn" => function () use (&$handle) { | ||
fclose($handle); | ||
print "all done"; | ||
}]["fn"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Yay | ||
{ | ||
function md5($foo) | ||
{ | ||
return $foo; | ||
} | ||
} |