Skip to content
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

Allow comments after commas in value lists when requiring newlines. F… #4482

Merged
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
Allow comments after commas in value lists when requiring newlines. F…
…ixes #2906.
  • Loading branch information
dom111 committed Dec 13, 2019
commit 9fbf313cf6f6a31670db29125f8e4f21d80a104b
36 changes: 36 additions & 0 deletions lib/rules/value-list-comma-newline-after/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ testRule(rule, {
code: '$grid-breakpoints: (\n(xs),\n(sm, 768px)\n) !default;',
description: 'ignores scss maps',
},
{
code: 'a { background-size: 0, //\n0; }',
description: 'ignores single line comments',
},
{
code: 'a { background-size: 0, /**/\n0; }',
description: 'ignores multi line comments',
},
],

reject: [
Expand All @@ -62,6 +70,14 @@ testRule(rule, {
line: 1,
column: 23,
},
{
code: 'a { background-size: 0, /**/0; }',
fixed: 'a { background-size: 0, /**/\n0; }',
message: messages.expectedAfter(),
description: 'ignores multi line comments',
line: 1,
column: 28,
},
],
});

Expand All @@ -74,6 +90,10 @@ testRule(rule, {
{
code: 'a { background-size: 0,\n0,\n0; }',
},
{
code: 'a { background-size: 0, //\n0, /**/\n0; }',
description: 'with comments',
},
{
code: 'a { background-size: 0 ,\n 0,\n0; }',
},
Expand All @@ -93,6 +113,10 @@ testRule(rule, {
code: 'a { background-size: 0, 0;\r\n}',
description: 'ignores single-line list, multi-line block with CRLF',
},
{
code: 'a { background-size: 0, /**/ 0; }',
description: 'ignores single-line list, multi-line block with comment',
},
],

reject: [
Expand All @@ -103,6 +127,14 @@ testRule(rule, {
line: 2,
column: 2,
},
{
code: 'a { background-size: 0, //\n0, /**/0; }',
fixed: 'a { background-size: 0, //\n0, /**/\n0; }',
description: 'with comments',
message: messages.expectedAfterMultiLine(),
line: 2,
column: 7,
},
{
code: 'a { background-size: 0,\n0, 0; }',
fixed: 'a { background-size: 0,\n0,\n 0; }',
Expand Down Expand Up @@ -137,6 +169,10 @@ testRule(rule, {
{
code: 'a { background-size: 0\n,0\n,0; }',
},
{
code: 'a { background-size: 0 //\n,0 /**/\n,0; }',
description: 'with comments',
},
{
code: 'a { background-size: 0\r\n,0\r\n,0; }',
description: 'CRLF',
Expand Down
14 changes: 14 additions & 0 deletions lib/rules/value-list-comma-newline-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ const rule = function(expectation, options, context) {
return true;
}
: null,
determineIndex: (declString, match) => {
const nextChars = declString.substr(match.endIndex, declString.length - match.endIndex);

// If there's a // comment, that means there has to be a newline
// ending the comment so we're fine
if (nextChars.match(/^[ \t]*\/\//)) {
return false;
}

// If there are spaces and then a comment begins, look for the newline
return nextChars.match(/^[ \t]*\/\*/)
? declString.indexOf('*/', match.endIndex) + 1
: match.startIndex;
},
});

if (fixData) {
Expand Down
14 changes: 12 additions & 2 deletions lib/rules/valueListCommaWhitespaceChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ module.exports = function(opts) {
return;
}

const declString = decl.toString();

styleSearch(
{
source: decl.toString(),
source: declString,
target: ',',
functionArguments: 'skip',
},
(match) => {
checkComma(decl.toString(), match.startIndex, decl);
const indexToCheckAfter = opts.determineIndex
? opts.determineIndex(declString, match)
: match.startIndex;

if (indexToCheckAfter === false) {
return;
}

checkComma(declString, indexToCheckAfter, decl);
},
);
});
Expand Down