Skip to content

Commit

Permalink
fix(eslint-plugin): [comma-spacing] allow no space after trailing com…
Browse files Browse the repository at this point in the history
…ma 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
  • Loading branch information
islandryu committed Jul 8, 2023
1 parent 5a347a5 commit 24bdacc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/eslint-plugin/src/rules/comma-spacing.ts
Expand Up @@ -3,6 +3,8 @@ import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';

import {
createRule,
isClosingBraceToken,
isClosingBracketToken,
isClosingParenToken,
isCommaToken,
isTokenOnSameLine,
Expand Down Expand Up @@ -135,6 +137,14 @@ export default createRule<Options, MessageIds>({
return;
}

if (
spaceAfter &&
nextToken &&
(isClosingBraceToken(nextToken) || isClosingBracketToken(nextToken))
) {
return;
}

if (!spaceAfter && nextToken && nextToken.type === AST_TOKEN_TYPES.Line) {
return;
}
Expand Down
49 changes: 49 additions & 0 deletions packages/eslint-plugin/tests/rules/comma-spacing.test.ts
Expand Up @@ -282,6 +282,55 @@ ruleTester.run('comma-spacing', rule, {
'interface Foo<T, T1,>{}',
'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: '<a>Hello, world</a>',
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: [
Expand Down

0 comments on commit 24bdacc

Please sign in to comment.