From 24bdacc7e5df40c92031a1bd7e9815d66a35b31d Mon Sep 17 00:00:00 2001 From: SHIMA RYUHEI <65934663+islandryu@users.noreply.github.com> Date: Sun, 9 Jul 2023 07:02:52 +0900 Subject: [PATCH] fix(eslint-plugin): [comma-spacing] allow no space after trailing comma in objects and arrays (#6938) * feat(eslint-plugin):[comma-spacing]allow no space after trailing comma in objects and arrays * feat(eslint-plugin):[comma-spacing]add test cases --- .../eslint-plugin/src/rules/comma-spacing.ts | 10 ++++ .../tests/rules/comma-spacing.test.ts | 49 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/packages/eslint-plugin/src/rules/comma-spacing.ts b/packages/eslint-plugin/src/rules/comma-spacing.ts index a1ebcc181f2..19133a2039f 100644 --- a/packages/eslint-plugin/src/rules/comma-spacing.ts +++ b/packages/eslint-plugin/src/rules/comma-spacing.ts @@ -3,6 +3,8 @@ import { AST_TOKEN_TYPES } from '@typescript-eslint/utils'; import { createRule, + isClosingBraceToken, + isClosingBracketToken, isClosingParenToken, isCommaToken, isTokenOnSameLine, @@ -135,6 +137,14 @@ export default createRule({ return; } + if ( + spaceAfter && + nextToken && + (isClosingBraceToken(nextToken) || isClosingBracketToken(nextToken)) + ) { + return; + } + if (!spaceAfter && nextToken && nextToken.type === AST_TOKEN_TYPES.Line) { return; } diff --git a/packages/eslint-plugin/tests/rules/comma-spacing.test.ts b/packages/eslint-plugin/tests/rules/comma-spacing.test.ts index 37eb6e2e3ad..0adc181072c 100644 --- a/packages/eslint-plugin/tests/rules/comma-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/comma-spacing.test.ts @@ -282,6 +282,55 @@ ruleTester.run('comma-spacing', rule, { 'interface Foo{}', 'interface A<> {}', 'let foo,', + 'const arr = [,];', + 'const arr = [ ,];', + 'const arr = [ , ];', + 'const arr = [1,];', + 'const arr = [ , 2];', + 'const arr = [,,];', + 'const arr = [ ,,];', + 'const arr = [, ,];', + 'const arr = [,, ];', + 'const arr = [ , ,];', + 'const arr = [ ,, ];', + 'const arr = [ , , ];', + 'const arr = [,, 3];', + 'const arr = [1, 2, 3,];', + 'const arr = [1, 2, 3, ];', + "const obj = {'foo':'bar', 'baz':'qur', };", + "const obj = {'foo':'bar', 'baz':'qur',};", + { code: 'const arr = [ ,];', options: [{ before: true, after: false }] }, + { code: 'const arr = [, ];', options: [{ before: true, after: false }] }, + { code: 'const arr = [ , ];', options: [{ before: true, after: false }] }, + { code: 'const arr = [ ,,];', options: [{ before: true, after: false }] }, + { code: 'const arr = [, ,];', options: [{ before: true, after: false }] }, + { code: 'const arr = [,, ];', options: [{ before: true, after: false }] }, + { code: 'const arr = [ , ,];', options: [{ before: true, after: false }] }, + { code: 'const arr = [ ,, ];', options: [{ before: true, after: false }] }, + { code: 'const arr = [, , ];', options: [{ before: true, after: false }] }, + { code: 'const arr = [ , , ];', options: [{ before: true, after: false }] }, + { + code: 'const arr = [ , , ];', + options: [{ before: false, after: false }], + }, + { code: 'const [a, b,] = [1, 2];', parserOptions: { ecmaVersion: 6 } }, + { + code: 'Hello, world', + options: [{ before: true, after: false }], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, + }, + { code: '[a, /**/ , ]', options: [{ before: false, after: true }] }, + { code: '[a , /**/, ]', options: [{ before: true, after: true }] }, + { + code: '[a, /**/ , ] = foo', + options: [{ before: false, after: true }], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: '[a , /**/, ] = foo', + options: [{ before: true, after: true }], + parserOptions: { ecmaVersion: 6 }, + }, ], invalid: [