Skip to content

Commit

Permalink
Merge pull request #121 from VincentLanglet/fixTwig
Browse files Browse the repository at this point in the history
🐛 Multiple bugFix
  • Loading branch information
VincentLanglet authored Apr 2, 2020
2 parents 514a2c3 + 91afa52 commit 40f7f70
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
8 changes: 4 additions & 4 deletions TwigCS/src/Report/TextFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ public function display(Report $report, string $level = null): void
}

/**
* @param string $template
* @param int $line
* @param int $context
* @param string $template
* @param int|null $line
* @param int $context
*
* @return array
*/
protected function getContext(string $template, int $line, int $context): array
protected function getContext(string $template, ?int $line, int $context): array
{
$lines = explode("\n", $template);

Expand Down
2 changes: 1 addition & 1 deletion TwigCS/src/Runner/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function fixFile(string $file): bool
try {
$twigSource = new Source($contents, 'TwigCS');
$stream = $this->tokenizer->tokenize($twigSource);
} catch (Exception $e) {
} catch (Exception $exception) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions TwigCS/src/Runner/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ protected function processTemplate(string $file, Ruleset $ruleset, Report $repor
// Tokenizer.
try {
$stream = $this->tokenizer->tokenize($twigSource);
} catch (Exception $e) {
} catch (Exception $exception) {
$sniffViolation = new SniffViolation(
Report::MESSAGE_TYPE_FATAL,
sprintf('Unable to tokenize file: %s', $e->getMessage()),
sprintf('Unable to tokenize file: %s', $exception->getMessage()),
$file
);

Expand Down
5 changes: 5 additions & 0 deletions TwigCS/src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,11 @@ protected function lexPunctuation(): void
array_pop($this->bracketsAndTernary);
$lastBracket = end($this->bracketsAndTernary);
} while ('?' === $lastBracket[0]);

// This is maybe the end of the variable, start again.
$this->lexVariable();

return;
}
}

Expand Down
4 changes: 2 additions & 2 deletions TwigCS/tests/Ruleset/AbstractSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protected function checkSniff(SniffInterface $sniff, array $expects): void

$ruleset->addSniff($sniff);
$report = $linter->run([$file], $ruleset);
} catch (Exception $e) {
self::fail($e->getMessage());
} catch (Exception $exception) {
self::fail($exception->getMessage());

return;
}
Expand Down
12 changes: 11 additions & 1 deletion TwigCS/tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,17 @@ public function tokenizeDataProvider(): array
75 => Token::WHITESPACE_TYPE,
76 => Token::BLOCK_END_TYPE,
77 => Token::EOL_TYPE,
78 => Token::EOF_TYPE,
78 => Token::VAR_START_TYPE,
79 => Token::WHITESPACE_TYPE,
80 => Token::NAME_TYPE,
81 => Token::WHITESPACE_TYPE,
82 => Token::OPERATOR_TYPE,
83 => Token::WHITESPACE_TYPE,
84 => Token::STRING_TYPE,
85 => Token::WHITESPACE_TYPE,
86 => Token::VAR_END_TYPE,
87 => Token::EOL_TYPE,
88 => Token::EOF_TYPE,
],
],
[
Expand Down
1 change: 1 addition & 0 deletions TwigCS/tests/Token/Tokenizer/TokenizerTest3.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
{{ bar ? 'string' : "string#" }}
{{ baz ?: { a:[1, 2] } }}
{% set a={foo: bar ? baz ? 1, test: 1} %}
{{ a ? ',' }}

0 comments on commit 40f7f70

Please sign in to comment.