Skip to content

Support for parser for blade component attributes #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion app/Parsers/InlineHtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
use App\Contexts\Blade;
use App\Parser\Parse;
use App\Parser\Settings;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Microsoft\PhpParser\Node\Statement\InlineHtml;
use Microsoft\PhpParser\Parser;
use Microsoft\PhpParser\PositionUtilities;
use Microsoft\PhpParser\Range;
use Stillat\BladeParser\Document\Document;
use Stillat\BladeParser\Nodes\BaseNode;
use Stillat\BladeParser\Nodes\Components\ComponentNode;
use Stillat\BladeParser\Nodes\Components\ParameterNode;
use Stillat\BladeParser\Nodes\DirectiveNode;
use Stillat\BladeParser\Nodes\EchoNode;
use Stillat\BladeParser\Nodes\EchoType;
Expand Down Expand Up @@ -76,10 +80,56 @@ protected function parseBladeContent($node)
$this->parseEchoNode($child);
}

if ($child instanceof ComponentNode) {
$this->parseComponentNode($child);
}

$this->parseBladeContent($child);
}
}

protected function parseComponentNode(ComponentNode $node)
{
/** @var Collection<int, ParameterNode> $parameters */
$parameters = $node->getParameters();

foreach ($parameters as $parameter) {
$prefix = Str::match('/^({{{|{{|{!!)/', $parameter->value);

$snippet = "<?php\n" . str_repeat(' ', $node->getStartIndentationLevel()) . str_replace($prefix, '', $parameter->value) . ';';

$sourceFile = (new Parser)->parseSourceFile($snippet);

Settings::$calculatePosition = function (Range $range) use ($node, $parameter) {
if ($range->start->line === 1) {
$rangeCharacters = $range->end->character - $range->start->character;
// If component has />, then we need to remove 1 character
$selfClosingCharacter = $node->getIsSelfClosing() ? 1 : 0;

$firstQuotePosition = strpos($parameter->content, "'");

$range->start->character = $parameter->position->startColumn + $firstQuotePosition - $selfClosingCharacter;
$range->end->character = $parameter->position->startColumn + $firstQuotePosition + $rangeCharacters - $selfClosingCharacter;
}

$range->start->line += $this->startLine + $parameter->position->startLine - 2;
$range->end->line += $this->startLine + $parameter->position->startLine - 2;

return $range;
};

$result = Parse::parse($sourceFile);

if (count($result->children) === 0) {
continue;
}

$child = $result->children[0];

$this->items[] = $child;
}
}

protected function doEchoParse(BaseNode $node, $prefix, $content)
{
$snippet = "<?php\n" . str_repeat(' ', $node->getStartIndentationLevel()) . str_replace($prefix, '', $content) . ';';
Expand All @@ -95,7 +145,7 @@ protected function doEchoParse(BaseNode $node, $prefix, $content)
}

$range->start->line += $this->startLine + $node->position->startLine - 2;
$range->end->line += $this->startLine + $node->position->startLine - 2;
$range->end->line += $this->startLine + $node->position->startLine - 2;

return $range;
};
Expand Down