Skip to content

Commit

Permalink
Fix no warnings for not indented lines in conditions (#111)
Browse files Browse the repository at this point in the history
Fixes #101

Co-authored-by: Martin Helmich <kontakt@martin-helmich.de>
  • Loading branch information
tillhoerner and martin-helmich authored Apr 5, 2021
1 parent 8a7b5bd commit 011ad8e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Linter/Sniff/IndentationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
}));

// Skip empty lines and conditions inside conditions.
if ($this->isEmptyLine($tokensInLine) || $this->insideCondition && $tokensInLine[0] !== TokenInterface::TYPE_CONDITION && !self::isWhitespace($tokensInLine[0])) {
if ($this->isEmptyLine($tokensInLine) || ($this->insideCondition && self::isCondition($tokensInLine[0]))) {
continue;
}

Expand Down
13 changes: 12 additions & 1 deletion src/Linter/Sniff/Inspection/TokenInspections.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,15 @@ private static function isWhitespaceOfLength(TokenInterface $token, int $length)
{
return static::isWhitespace($token) && strlen(trim($token->getValue(), "\n")) == $length;
}
}

/**
* Tests whether a token is a condition
*
* @param TokenInterface $token
* @return bool
*/
private static function isCondition(TokenInterface $token): bool
{
return $token->getType() === TokenInterface::TYPE_CONDITION || $token->getType() === TokenInterface::TYPE_CONDITION_ELSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testNoNegativeIndentationLevels()
[end]
EOF;

$sniff = new IndentationSniff(["indentConditions" => true]);
$sniff = new IndentationSniff([]);
$tokens = (new Tokenizer())->tokenizeString($code);
$file = new File("file");

Expand All @@ -97,4 +97,28 @@ public function testNoNegativeIndentationLevels()
$warnings = $file->getIssues();
assertThat($warnings, equalTo([]));
}

/**
* @see https://github.com/martin-helmich/typo3-typoscript-lint/issues/101
*/
public function testWarningIsGeneratedForNotIndentedLinesInConditions()
{
$code = <<<EOF
[globalString = GP:foo = 1]
foo.bar = 3
[global]
EOF;

$sniff = new IndentationSniff(["indentConditions" => true]);
$tokens = (new Tokenizer())->tokenizeString($code);
$file = new File("file");

$sniff->sniff($tokens, $file, new LinterConfiguration());

$warnings = $file->getIssues();

$this->assertCount(1, $warnings);
$this->assertEquals('Expected indent of 4 spaces.', $warnings[0]->getMessage());
$this->assertEquals(2, $warnings[0]->getLine());
}
}

0 comments on commit 011ad8e

Please sign in to comment.