Skip to content

Commit

Permalink
Another strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 22, 2024
1 parent 703deeb commit 188fbc6
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 84 deletions.
24 changes: 9 additions & 15 deletions src/Runner/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Twig\Source;
use TwigCsFixer\Cache\Manager\CacheManagerInterface;
use TwigCsFixer\Cache\Manager\NullCacheManager;
use TwigCsFixer\Environment\StubbedEnvironment;
use TwigCsFixer\Exception\CannotFixFileException;
use TwigCsFixer\Exception\CannotTokenizeException;
use TwigCsFixer\Report\Report;
Expand Down Expand Up @@ -146,23 +145,10 @@ public function run(iterable $files, Ruleset $ruleset, ?FixerInterface $fixer =

private function parseTemplate(string $content, string $filePath, Report $report): ?ModuleNode
{
if (!StubbedEnvironment::satisfiesTwigVersion(3, 10)) {
// @codeCoverageIgnoreStart
$violation = new Violation(
Violation::LEVEL_FATAL,
'Node visitor rules require twig/twig >= 3.10.0',
$filePath,
);
$report->addViolation($violation);

return null;
// @codeCoverageIgnoreEnd
}

try {
$twigSource = new Source($content, $filePath);

return $this->env->parse($this->env->tokenize($twigSource));
$node = $this->env->parse($this->env->tokenize($twigSource));
} catch (Error $error) {
$violation = new Violation(
Violation::LEVEL_FATAL,
Expand All @@ -176,6 +162,14 @@ private function parseTemplate(string $content, string $filePath, Report $report

return null;
}

// BC fix for twig/twig < 3.10.
$sourceContext = $node->getSourceContext();
if (null !== $sourceContext) {
$node->setSourceContext($sourceContext);
}

return $node;
}

private function setErrorHandler(Report $report, string $file): void
Expand Down
11 changes: 0 additions & 11 deletions tests/Rules/Node/ForbiddenBlock/ForbiddenBlockRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@

namespace TwigCsFixer\Tests\Rules\Node\ForbiddenBlock;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ForbiddenBlockRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ForbiddenBlockRuleTest extends AbstractRuleTestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testConfiguration(): void
{
static::assertSame(
Expand Down
11 changes: 0 additions & 11 deletions tests/Rules/Node/ForbiddenFilter/ForbiddenFilterRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@

namespace TwigCsFixer\Tests\Rules\Node\ForbiddenFilter;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ForbiddenFilterRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ForbiddenFilterRuleTest extends AbstractRuleTestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testConfiguration(): void
{
static::assertSame(
Expand Down
11 changes: 0 additions & 11 deletions tests/Rules/Node/ForbiddenFunction/ForbiddenFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@

namespace TwigCsFixer\Tests\Rules\Node\ForbiddenFunction;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ForbiddenFunctionRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ForbiddenFunctionRuleTest extends AbstractRuleTestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testConfiguration(): void
{
static::assertSame(
Expand Down
11 changes: 0 additions & 11 deletions tests/Rules/Node/NodeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace TwigCsFixer\Tests\Rules\Node;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Node\Node;
Expand All @@ -22,15 +20,6 @@

final class NodeRuleTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testEnterNodeRule(): void
{
$report = new Report([new \SplFileInfo('fakeFile.html.twig')]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@

namespace TwigCsFixer\Tests\Rules\Node\ValidConstantFunction;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ValidConstantFunctionRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ValidConstantFunctionRuleTest extends AbstractRuleTestCase
{
public const SOME_CONSTANT = 'Foo';

protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testRule(): void
{
$this->checkRule(new ValidConstantFunctionRule(), [
Expand Down
14 changes: 0 additions & 14 deletions tests/Runner/LinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace TwigCsFixer\Tests\Runner;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Twig\Environment;
use Twig\Error\SyntaxError;
use TwigCsFixer\Cache\Manager\CacheManagerInterface;
Expand Down Expand Up @@ -276,10 +274,6 @@ public function testFileIsNotCachedWhenReportHasErrors(): void

public function testViolationsFromNodeVisitorRule(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}

$filePath = $this->getTmpPath(__DIR__.'/Fixtures/Linter/node_visitor.twig');

$ruleset = new Ruleset();
Expand Down Expand Up @@ -324,10 +318,6 @@ public function testViolationsFromNodeVisitorRule(): void

public function testNodeVisitorWithInvalidFiles(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}

$filePath = $this->getTmpPath(__DIR__.'/Fixtures/Linter/file.twig');
$filePath2 = $this->getTmpPath(__DIR__.'/Fixtures/Linter/file2.twig');

Expand Down Expand Up @@ -360,10 +350,6 @@ public function testNodeVisitorWithInvalidFiles(): void

public function testNodeVisitorWithBuggyFixer(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}

$filePath = $this->getTmpPath(__DIR__.'/Fixtures/Linter/file.twig');

$ruleset = new Ruleset();
Expand Down

0 comments on commit 188fbc6

Please sign in to comment.