diff --git a/lib/rules/value-list-comma-newline-after/__tests__/index.js b/lib/rules/value-list-comma-newline-after/__tests__/index.js index fa87c21bc3..37c34da025 100644 --- a/lib/rules/value-list-comma-newline-after/__tests__/index.js +++ b/lib/rules/value-list-comma-newline-after/__tests__/index.js @@ -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: [ @@ -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, + }, ], }); @@ -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; }', }, @@ -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: [ @@ -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; }', @@ -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', diff --git a/lib/rules/value-list-comma-newline-after/index.js b/lib/rules/value-list-comma-newline-after/index.js index b1225ae3f2..59bf4e07ce 100644 --- a/lib/rules/value-list-comma-newline-after/index.js +++ b/lib/rules/value-list-comma-newline-after/index.js @@ -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) { diff --git a/lib/rules/valueListCommaWhitespaceChecker.js b/lib/rules/valueListCommaWhitespaceChecker.js index 6ef80c6bfb..d55f680acf 100644 --- a/lib/rules/valueListCommaWhitespaceChecker.js +++ b/lib/rules/valueListCommaWhitespaceChecker.js @@ -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); }, ); });