From 25dfe7403212d038cb4541b083ce515a9cd72a69 Mon Sep 17 00:00:00 2001 From: islandryu Date: Thu, 20 Apr 2023 09:15:03 +0900 Subject: [PATCH 1/2] feat(eslint-plugin):[comma-spacing]allow no space after trailing comma in objects and arrays --- packages/eslint-plugin/src/rules/comma-spacing.ts | 10 ++++++++++ .../eslint-plugin/tests/rules/comma-spacing.test.ts | 2 ++ 2 files changed, 12 insertions(+) diff --git a/packages/eslint-plugin/src/rules/comma-spacing.ts b/packages/eslint-plugin/src/rules/comma-spacing.ts index a1ebcc181f28..19133a2039f0 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 37eb6e2e3adc..12be6ab09634 100644 --- a/packages/eslint-plugin/tests/rules/comma-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/comma-spacing.test.ts @@ -282,6 +282,8 @@ ruleTester.run('comma-spacing', rule, { 'interface Foo{}', 'interface A<> {}', 'let foo,', + "['value', 'value2', ]", + "const obj = {value1: '',};", ], invalid: [ From 9e4e9aab4e214d7a69ed1643f2f8fa822bbd067e Mon Sep 17 00:00:00 2001 From: islandryu Date: Thu, 20 Apr 2023 11:25:02 +0900 Subject: [PATCH 2/2] feat(eslint-plugin):[comma-spacing]add test cases --- .../tests/rules/comma-spacing.test.ts | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/comma-spacing.test.ts b/packages/eslint-plugin/tests/rules/comma-spacing.test.ts index 12be6ab09634..0adc181072ce 100644 --- a/packages/eslint-plugin/tests/rules/comma-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/comma-spacing.test.ts @@ -282,8 +282,55 @@ ruleTester.run('comma-spacing', rule, { 'interface Foo{}', 'interface A<> {}', 'let foo,', - "['value', 'value2', ]", - "const obj = {value1: '',};", + '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: [