Skip to content

Commit 1404796

Browse files
fix(eslint-plugin): [no-unnecessary-type-constraint] correctly fix in cts/mts files (typescript-eslint#6795)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent d67fd6d commit 1404796

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
22
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
3+
import { extname } from 'path';
4+
import * as ts from 'typescript';
35

46
import * as util from '../util';
57

@@ -38,7 +40,24 @@ export default util.createRule({
3840
[AST_NODE_TYPES.TSUnknownKeyword, 'unknown'],
3941
]);
4042

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+
4261
const source = context.getSourceCode();
4362

4463
const checkNode = (
@@ -47,7 +66,7 @@ export default util.createRule({
4766
): void => {
4867
const constraint = unnecessaryConstraints.get(node.constraint.type);
4968
function shouldAddTrailingComma(): boolean {
50-
if (!inArrowFunction || !inJsx) {
69+
if (!inArrowFunction || !requiresGenericDeclarationDisambiguation) {
5170
return false;
5271
}
5372
// Only <T>() => {} would need trailing comma

packages/eslint-plugin/tests/rules/no-unnecessary-type-constraint.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,46 @@ function data<T extends TODO>() {}
128128
},
129129
},
130130
},
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+
},
131171
{
132172
code: noFormat`const data = <T extends any,>() => {};`,
133173
errors: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy