Skip to content

Commit 6fb4a9a

Browse files
authored
fix(ts/comma-dangle): allow tailing comma in generic in TSX (#167)
1 parent d43105b commit 6fb4a9a

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/eslint-plugin-ts/rules/comma-dangle/comma-dangle.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ type Qux = [string,
9595
},
9696
],
9797
},
98+
99+
// https://github.com/eslint-stylistic/eslint-stylistic/issues/35
100+
{
101+
code: 'const id = <T,>(x: T) => x;',
102+
parserOptions: {
103+
ecmaFeatures: {
104+
jsx: true,
105+
},
106+
},
107+
},
108+
{
109+
code: 'const id = <T,R>(x: T) => x;',
110+
parserOptions: {
111+
ecmaFeatures: {
112+
jsx: true,
113+
},
114+
},
115+
},
98116
],
99117
invalid: [
100118
// base rule
@@ -254,5 +272,18 @@ type Qux = [string,
254272
options: ['only-multiline'],
255273
errors: [{ messageId: 'unexpected' }],
256274
},
275+
276+
// https://github.com/eslint-stylistic/eslint-stylistic/issues/35
277+
// When there is more than one generic, we don't need to workaround it
278+
{
279+
code: 'const id = <T,R,>(x: T) => x;',
280+
output: 'const id = <T,R>(x: T) => x;',
281+
errors: [{ messageId: 'unexpected' }],
282+
parserOptions: {
283+
ecmaFeatures: {
284+
jsx: true,
285+
},
286+
},
287+
},
257288
],
258289
})

packages/eslint-plugin-ts/rules/comma-dangle/comma-dangle.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ export default createRule<RuleOptions, MessageIds>({
9393
const rules = baseRule.create(context)
9494
const sourceCode = context.sourceCode
9595
const normalizedOptions = normalizeOptions(options)
96+
const isTSX = context.parserOptions?.ecmaFeatures?.jsx
97+
&& context.filename?.endsWith('.tsx')
9698

9799
const predicate = {
98100
'always': forceComma,
99101
'always-multiline': forceCommaIfMultiline,
100102
'only-multiline': allowCommaIfMultiline,
101103
'never': forbidComma,
102104
// https://github.com/typescript-eslint/typescript-eslint/issues/7220
103-
104105
'ignore': () => {},
105106
}
106107

@@ -134,6 +135,19 @@ export default createRule<RuleOptions, MessageIds>({
134135
}
135136

136137
function forbidComma(node: TSESTree.Node): void {
138+
/**
139+
* We allow tailing comma in TSTypeParameterDeclaration in TSX,
140+
* because it's used to differentiate JSX tags from generics.
141+
*
142+
* https://github.com/microsoft/TypeScript/issues/15713#issuecomment-499474386
143+
* https://github.com/eslint-stylistic/eslint-stylistic/issues/35
144+
*/
145+
if (isTSX && node.type === AST_NODE_TYPES.TSTypeParameterDeclaration && node.params.length === 1)
146+
return
147+
148+
/**
149+
* Forbid tailing comma
150+
*/
137151
const last = getLastItem(node)
138152
const trailing = getTrailingToken(node)
139153
if (last && trailing && isCommaToken(trailing)) {

0 commit comments

Comments
 (0)