Skip to content

AC-1100 Catch badly put newlines in LESS files #258

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
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
23 changes: 12 additions & 11 deletions Magento2/Sniffs/Less/ColonSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public function process(File $phpcsFile, $stackPtr)
*/
private function needValidateSpaces(File $phpcsFile, $stackPtr, $tokens)
{
$nextSemicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);

if (false === $nextSemicolon
|| ($tokens[$nextSemicolon]['line'] !== $tokens[$stackPtr]['line'])
|| TokenizerSymbolsInterface::BITWISE_AND === $tokens[$stackPtr - 1]['content']
) {
if (TokenizerSymbolsInterface::BITWISE_AND === $tokens[$stackPtr - 1]['content']) {
return false;
}

Expand All @@ -80,7 +75,10 @@ private function needValidateSpaces(File $phpcsFile, $stackPtr, $tokens)
}

/**
* Validate Colon Spacing according to requirements
* Validate Colon Spacing according to requirements:
* - No spaces before colon
* - Exactly 1 space after colon
* - No property definition scattered among several lines
*
* @param File $phpcsFile
* @param int $stackPtr
Expand All @@ -94,19 +92,22 @@ private function validateSpaces(File $phpcsFile, $stackPtr, array $tokens)
$phpcsFile->addError('There must be no space before a colon in a style definition', $stackPtr, 'Before');
}

$nextSemicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
if (false !== $nextSemicolon && ($tokens[$nextSemicolon]['line'] !== $tokens[$stackPtr]['line'])) {
$error = 'Expected 1 space after colon in style definition; newline found';
$phpcsFile->addError($error, $stackPtr, 'AfterNewline');
}

if (T_WHITESPACE !== $tokens[($stackPtr + 1)]['code']) {
$phpcsFile->addError('Expected 1 space after colon in style definition; 0 found', $stackPtr, 'NoneAfter');
} else {
$content = $tokens[($stackPtr + 1)]['content'];
if (false === strpos($content, $phpcsFile->eolChar)) {
if (false !== strpos($content, ' ')) {
$length = strlen($content);
if ($length !== 1) {
$error = sprintf('Expected 1 space after colon in style definition; %s found', $length);
$phpcsFile->addError($error, $stackPtr, 'After');
}
} else {
$error = 'Expected 1 space after colon in style definition; newline found';
$phpcsFile->addError($error, $stackPtr, 'AfterNewline');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Magento2/Tests/Less/ColonSpacingUnitTest.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ div#foo {
}

.blah {
foo :'xyz';
foo :'jkl';
}

.foo {
Expand Down
1 change: 1 addition & 0 deletions Magento2/Tests/Less/ColonSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function getErrorList()
8 => 1,
12 => 1,
16 => 2,
20 => 1,
];
}

Expand Down