File tree 2 files changed +61
-2
lines changed
2 files changed +61
-2
lines changed Original file line number Diff line number Diff line change 1
1
import type { TSESLint , TSESTree } from '@typescript-eslint/utils' ;
2
2
import { AST_NODE_TYPES } from '@typescript-eslint/utils' ;
3
+ import { extname } from 'path' ;
4
+ import * as ts from 'typescript' ;
3
5
4
6
import * as util from '../util' ;
5
7
@@ -38,7 +40,24 @@ export default util.createRule({
38
40
[ AST_NODE_TYPES . TSUnknownKeyword , 'unknown' ] ,
39
41
] ) ;
40
42
41
- const inJsx = context . getFilename ( ) . toLowerCase ( ) . endsWith ( 'tsx' ) ;
43
+ function checkRequiresGenericDeclarationDisambiguation (
44
+ filename : string ,
45
+ ) : boolean {
46
+ const pathExt = extname ( filename ) . toLocaleLowerCase ( ) ;
47
+ switch ( pathExt ) {
48
+ case ts . Extension . Cts :
49
+ case ts . Extension . Mts :
50
+ case ts . Extension . Tsx :
51
+ return true ;
52
+
53
+ default :
54
+ return false ;
55
+ }
56
+ }
57
+
58
+ const requiresGenericDeclarationDisambiguation =
59
+ checkRequiresGenericDeclarationDisambiguation ( context . getFilename ( ) ) ;
60
+
42
61
const source = context . getSourceCode ( ) ;
43
62
44
63
const checkNode = (
@@ -47,7 +66,7 @@ export default util.createRule({
47
66
) : void => {
48
67
const constraint = unnecessaryConstraints . get ( node . constraint . type ) ;
49
68
function shouldAddTrailingComma ( ) : boolean {
50
- if ( ! inArrowFunction || ! inJsx ) {
69
+ if ( ! inArrowFunction || ! requiresGenericDeclarationDisambiguation ) {
51
70
return false ;
52
71
}
53
72
// Only <T>() => {} would need trailing comma
Original file line number Diff line number Diff line change @@ -128,6 +128,46 @@ function data<T extends TODO>() {}
128
128
} ,
129
129
} ,
130
130
} ,
131
+ {
132
+ code : 'const data = <T extends any>() => {};' ,
133
+ errors : [
134
+ {
135
+ data : { constraint : 'any' , name : 'T' } ,
136
+ messageId : 'unnecessaryConstraint' ,
137
+ endColumn : 28 ,
138
+ column : 15 ,
139
+ line : 1 ,
140
+ suggestions : [
141
+ {
142
+ messageId : 'removeUnnecessaryConstraint' ,
143
+ data : { constraint : 'any' } ,
144
+ output : `const data = <T,>() => {};` ,
145
+ } ,
146
+ ] ,
147
+ } ,
148
+ ] ,
149
+ filename : 'file.mts' ,
150
+ } ,
151
+ {
152
+ code : 'const data = <T extends any>() => {};' ,
153
+ errors : [
154
+ {
155
+ data : { constraint : 'any' , name : 'T' } ,
156
+ messageId : 'unnecessaryConstraint' ,
157
+ endColumn : 28 ,
158
+ column : 15 ,
159
+ line : 1 ,
160
+ suggestions : [
161
+ {
162
+ messageId : 'removeUnnecessaryConstraint' ,
163
+ data : { constraint : 'any' } ,
164
+ output : `const data = <T,>() => {};` ,
165
+ } ,
166
+ ] ,
167
+ } ,
168
+ ] ,
169
+ filename : 'file.cts' ,
170
+ } ,
131
171
{
132
172
code : noFormat `const data = <T extends any,>() => {};` ,
133
173
errors : [
You can’t perform that action at this time.
0 commit comments