Skip to content
This repository has been archived by the owner on Nov 11, 2019. It is now read-only.

Commit

Permalink
Extended sniff to make sure there is one space before a curly (#27)
Browse files Browse the repository at this point in the history
* Extended sniff to make sure there is one space before a curly

* Clarified message
  • Loading branch information
yannickl88 authored and linaori committed Dec 5, 2017
1 parent a9e1eca commit 82e2aba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Sniff/CurlySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public function process(File $file, int $stack_ptr): void

$token = $file->get($stack_ptr);

// check the token before
if ($stack_ptr > 1) {
$before = $file->get($stack_ptr - 1);

if ($before->type !== Token::T_WHITESPACE || $before->chars !== ' ') {
$file->addViolation(
self::class,
'There must be one space before a curly bracket.',
$token->lines[0],
$token->offsets[0],
$token->offsets[0] + strlen($token->chars)
);
return;
}
}

// do we have one line?
[
$contains_returns,
Expand Down
3 changes: 3 additions & 0 deletions test/Sniff/CurlySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function testSniff()
),
new Violation(CurlySniff::class, 'Media query should always be one multiple lines.', 36, 50, 51),
new Violation(CurlySniff::class, 'Media query should always be one multiple lines.', 37, 52, 53),
new Violation(CurlySniff::class, 'There must be one space before a curly bracket.', 38, 6, 7),
new Violation(CurlySniff::class, 'There must be one space before a curly bracket.', 39, 8, 9),
new Violation(CurlySniff::class, 'There must be one space before a curly bracket.', 41, 1, 2),
], $file->getViolations());
}
}
4 changes: 4 additions & 0 deletions test/Sniff/fixtures/curly.less
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
}
@media only screen and (min-device-width: 320px) {.bad {}}
@media only screen and (min-device-width: @{size}) {.bad {}}
.bad4{ color: #ffffff; }
.bad5 { color: #ffffff; }
.bad6
{ color: #ffffff; }

0 comments on commit 82e2aba

Please sign in to comment.