Skip to content

Add TwigCS Fixer #65

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

Merged
merged 6 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
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
17 changes: 12 additions & 5 deletions TwigCS/Command/TwigCSCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Symfony\Component\Console\Output\OutputInterface;
use TwigCS\Config\Config;
use TwigCS\Environment\StubbedEnvironment;
use TwigCS\Linter;
use TwigCS\Report\TextFormatter;
use TwigCS\Ruleset\Ruleset;
use TwigCS\Runner\Linter;
use TwigCS\Token\Tokenizer;

/**
Expand Down Expand Up @@ -50,6 +50,12 @@ protected function configure()
'Run as if this was started in <working-dir> instead of the current working directory',
getcwd()
),
new InputOption(
'fix',
'f',
InputOption::VALUE_NONE,
'Automatically fix all the fixable violations'
),
])
->addArgument(
'paths',
Expand All @@ -69,10 +75,11 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$paths = $input->getArgument('paths');
$exclude = $input->getOption('exclude');
$level = $input->getOption('level');
$paths = $input->getArgument('paths');
$exclude = $input->getOption('exclude');
$level = $input->getOption('level');
$currentDir = $input->getOption('working-dir');
$fix = $input->getOption('fix');

$config = new Config([
'paths' => $paths,
Expand All @@ -87,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Execute the linter.
$twig = new StubbedEnvironment();
$linter = new Linter($twig, new Tokenizer($twig));
$report = $linter->run($config->findFiles(), $ruleset);
$report = $linter->run($config->findFiles(), $ruleset, $fix);

// Format the output.
$reporter = new TextFormatter($input, $output);
Expand Down
10 changes: 3 additions & 7 deletions TwigCS/Environment/StubbedEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class StubbedEnvironment extends Environment
/**
* @var TwigFilter[]
*/
private $stubFilters;
protected $stubFilters = [];

/**
* @var TwigFunction[]
*/
private $stubFunctions;
protected $stubFunctions = [];

/**
* @var TwigTest[]
*/
private $stubTests;
protected $stubTests = [];

public function __construct()
{
Expand All @@ -44,10 +44,6 @@ public function __construct()
$this->addTokenParser(new TransChoiceTokenParser());
$this->addTokenParser(new TransDefaultDomainTokenParser());
$this->addTokenParser(new TransTokenParser());

$this->stubFilters = [];
$this->stubFunctions = [];
$this->stubTests = [];
}

/**
Expand Down
22 changes: 18 additions & 4 deletions TwigCS/Ruleset/Generic/BlankEOFSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,45 @@
class BlankEOFSniff extends AbstractSniff
{
/**
* @param Token $token
* @param int $tokenPosition
* @param Token[] $tokens
*
* @return Token
*
* @throws Exception
*/
public function process(Token $token, int $tokenPosition, array $tokens)
public function process(int $tokenPosition, array $tokens)
{
$token = $tokens[$tokenPosition];

if ($this->isTokenMatching($token, Token::EOF_TYPE)) {
$i = 0;
while (isset($tokens[$tokenPosition - ($i + 1)])
&& $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE)
) {
++$i;
$i++;
}

if (1 !== $i) {
// Either 0 or 2+ blank lines.
$this->addMessage(
$fix = $this->addFixableMessage(
$this::MESSAGE_TYPE_ERROR,
sprintf('A file must end with 1 blank line; found %d', $i),
$token
);

if ($fix) {
if (0 === $i) {
$this->fixer->addNewlineBefore($tokenPosition);
} else {
$this->fixer->beginChangeset();
while ($i > 1) {
$this->fixer->replaceToken($tokenPosition - $i, '');
$i--;
}
$this->fixer->endChangeset();
}
}
}
}

Expand Down
39 changes: 29 additions & 10 deletions TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,44 @@
class DelimiterSpacingSniff extends AbstractSniff
{
/**
* @param Token $token
* @param int $tokenPosition
* @param Token[] $tokens
*
* @return Token
*
* @throws Exception
*/
public function process(Token $token, int $tokenPosition, array $tokens)
public function process(int $tokenPosition, array $tokens)
{
$token = $tokens[$tokenPosition];

if ($this->isTokenMatching($token, Token::VAR_START_TYPE)
|| $this->isTokenMatching($token, Token::BLOCK_START_TYPE)
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE)
) {
$this->processStart($token, $tokenPosition, $tokens);
$this->processStart($tokenPosition, $tokens);
}

if ($this->isTokenMatching($token, Token::VAR_END_TYPE)
|| $this->isTokenMatching($token, Token::BLOCK_END_TYPE)
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE)
) {
$this->processEnd($token, $tokenPosition, $tokens);
$this->processEnd($tokenPosition, $tokens);
}

return $token;
}

/**
* @param Token $token
* @param int $tokenPosition
* @param Token[] $tokens
*
* @throws Exception
*/
public function processStart(Token $token, $tokenPosition, $tokens)
public function processStart(int $tokenPosition, array $tokens)
{
$token = $tokens[$tokenPosition];

// Ignore new line
if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::EOL_TYPE)) {
return;
Expand All @@ -60,23 +62,32 @@ public function processStart(Token $token, $tokenPosition, $tokens)
}

if (1 !== $count) {
$this->addMessage(
$fix = $this->addFixableMessage(
$this::MESSAGE_TYPE_ERROR,
sprintf('Expecting 1 whitespace after "%s"; found %d', $token->getValue(), $count),
$token
);

if ($fix) {
if (0 === $count) {
$this->fixer->addContent($tokenPosition, ' ');
} else {
$this->fixer->replaceToken($tokenPosition + 1, ' ');
}
}
}
}

/**
* @param Token $token
* @param int $tokenPosition
* @param Token[] $tokens
*
* @throws Exception
*/
public function processEnd(Token $token, $tokenPosition, $tokens)
public function processEnd(int $tokenPosition, array $tokens)
{
$token = $tokens[$tokenPosition];

// Ignore new line
if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::EOL_TYPE)) {
return;
Expand All @@ -89,11 +100,19 @@ public function processEnd(Token $token, $tokenPosition, $tokens)
}

if (1 !== $count) {
$this->addMessage(
$fix = $this->addFixableMessage(
$this::MESSAGE_TYPE_ERROR,
sprintf('Expecting 1 whitespace before "%s"; found %d', $token->getValue(), $count),
$token
);

if ($fix) {
if (0 === $count) {
$this->fixer->addContentBefore($tokenPosition, ' ');
} else {
$this->fixer->replaceToken($tokenPosition - 1, ' ');
}
}
}
}
}
5 changes: 3 additions & 2 deletions TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
class DisallowCommentedCodeSniff extends AbstractSniff
{
/**
* @param Token $token
* @param int $tokenPosition
* @param Token[] $tokens
*
* @return Token
*
* @throws Exception
*/
public function process(Token $token, int $tokenPosition, array $tokens)
public function process(int $tokenPosition, array $tokens)
{
$token = $tokens[$tokenPosition];

if ($this->isTokenMatching($token, Token::COMMENT_START_TYPE)) {
$i = $tokenPosition;
$found = false;
Expand Down
7 changes: 1 addition & 6 deletions TwigCS/Ruleset/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ class Ruleset
/**
* @var SniffInterface[]
*/
protected $sniffs;

public function __construct()
{
$this->sniffs = [];
}
protected $sniffs = [];

/**
* @return SniffInterface[]
Expand Down
Loading