From d7dc07a15e256cee9232183165e2f6102f2c0873 Mon Sep 17 00:00:00 2001 From: Soufiane Boutahlil Date: Fri, 30 Jul 2021 23:22:59 +0100 Subject: [PATCH] Fix: ignore lines with empty elements (fixes #12756) (#14837) * Fix: ignore lines with empty elements (fixes #12756) * Fix: ignore lines with empty elements (fixes #12756) * Fix: ignore lines with empty elements (fixes #12756) --- lib/rules/comma-style.js | 2 ++ tests/lib/rules/comma-style.js | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/rules/comma-style.js b/lib/rules/comma-style.js index fbdecccaac2..824ad89b2f9 100644 --- a/lib/rules/comma-style.js +++ b/lib/rules/comma-style.js @@ -216,6 +216,8 @@ module.exports = { previousItemToken = tokenAfterItem ? sourceCode.getTokenBefore(tokenAfterItem) : sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1]; + } else { + previousItemToken = currentItemToken; } }); diff --git a/tests/lib/rules/comma-style.js b/tests/lib/rules/comma-style.js index 1eb30825489..e0ac78d6813 100644 --- a/tests/lib/rules/comma-style.js +++ b/tests/lib/rules/comma-style.js @@ -233,7 +233,45 @@ ruleTester.run("comma-style", rule, { NewExpression: true } }] + }, + "var foo = [\n , \n 1, \n 2 \n];", + { + code: "const [\n , \n , \n a, \n b, \n] = arr;", + options: ["last", { + exceptions: { + ArrayPattern: false + } + }], + parserOptions: { + ecmaVersion: 6 + } + }, + { + code: "const [\n ,, \n a, \n b, \n] = arr;", + options: ["last", { + exceptions: { + ArrayPattern: false + } + }], + parserOptions: { + ecmaVersion: 6 + } + }, + { + code: "const arr = [\n 1 \n , \n ,2 \n]", + options: ["first"], + parserOptions: { + ecmaVersion: 6 + } + }, + { + code: "const arr = [\n ,'fifi' \n]", + options: ["first"], + parserOptions: { + ecmaVersion: 6 + } } + ], invalid: [