Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 24bdacc

Browse files
authored
fix(eslint-plugin): [comma-spacing] allow no space after trailing comma in objects and arrays (typescript-eslint#6938)
* feat(eslint-plugin):[comma-spacing]allow no space after trailing comma in objects and arrays * feat(eslint-plugin):[comma-spacing]add test cases
1 parent 5a347a5 commit 24bdacc

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

packages/eslint-plugin/src/rules/comma-spacing.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';
33

44
import {
55
createRule,
6+
isClosingBraceToken,
7+
isClosingBracketToken,
68
isClosingParenToken,
79
isCommaToken,
810
isTokenOnSameLine,
@@ -135,6 +137,14 @@ export default createRule<Options, MessageIds>({
135137
return;
136138
}
137139

140+
if (
141+
spaceAfter &&
142+
nextToken &&
143+
(isClosingBraceToken(nextToken) || isClosingBracketToken(nextToken))
144+
) {
145+
return;
146+
}
147+
138148
if (!spaceAfter && nextToken && nextToken.type === AST_TOKEN_TYPES.Line) {
139149
return;
140150
}

packages/eslint-plugin/tests/rules/comma-spacing.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,55 @@ ruleTester.run('comma-spacing', rule, {
282282
'interface Foo<T, T1,>{}',
283283
'interface A<> {}',
284284
'let foo,',
285+
'const arr = [,];',
286+
'const arr = [ ,];',
287+
'const arr = [ , ];',
288+
'const arr = [1,];',
289+
'const arr = [ , 2];',
290+
'const arr = [,,];',
291+
'const arr = [ ,,];',
292+
'const arr = [, ,];',
293+
'const arr = [,, ];',
294+
'const arr = [ , ,];',
295+
'const arr = [ ,, ];',
296+
'const arr = [ , , ];',
297+
'const arr = [,, 3];',
298+
'const arr = [1, 2, 3,];',
299+
'const arr = [1, 2, 3, ];',
300+
"const obj = {'foo':'bar', 'baz':'qur', };",
301+
"const obj = {'foo':'bar', 'baz':'qur',};",
302+
{ code: 'const arr = [ ,];', options: [{ before: true, after: false }] },
303+
{ code: 'const arr = [, ];', options: [{ before: true, after: false }] },
304+
{ code: 'const arr = [ , ];', options: [{ before: true, after: false }] },
305+
{ code: 'const arr = [ ,,];', options: [{ before: true, after: false }] },
306+
{ code: 'const arr = [, ,];', options: [{ before: true, after: false }] },
307+
{ code: 'const arr = [,, ];', options: [{ before: true, after: false }] },
308+
{ code: 'const arr = [ , ,];', options: [{ before: true, after: false }] },
309+
{ code: 'const arr = [ ,, ];', options: [{ before: true, after: false }] },
310+
{ code: 'const arr = [, , ];', options: [{ before: true, after: false }] },
311+
{ code: 'const arr = [ , , ];', options: [{ before: true, after: false }] },
312+
{
313+
code: 'const arr = [ , , ];',
314+
options: [{ before: false, after: false }],
315+
},
316+
{ code: 'const [a, b,] = [1, 2];', parserOptions: { ecmaVersion: 6 } },
317+
{
318+
code: '<a>Hello, world</a>',
319+
options: [{ before: true, after: false }],
320+
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
321+
},
322+
{ code: '[a, /**/ , ]', options: [{ before: false, after: true }] },
323+
{ code: '[a , /**/, ]', options: [{ before: true, after: true }] },
324+
{
325+
code: '[a, /**/ , ] = foo',
326+
options: [{ before: false, after: true }],
327+
parserOptions: { ecmaVersion: 6 },
328+
},
329+
{
330+
code: '[a , /**/, ] = foo',
331+
options: [{ before: true, after: true }],
332+
parserOptions: { ecmaVersion: 6 },
333+
},
285334
],
286335

287336
invalid: [

0 commit comments

Comments
 (0)