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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { extname } from 'path';
import * as ts from 'typescript';

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

Expand Down Expand Up @@ -38,7 +40,24 @@ export default util.createRule({
[AST_NODE_TYPES.TSUnknownKeyword, 'unknown'],
]);

const inJsx = context.getFilename().toLowerCase().endsWith('tsx');
function checkRequiresGenericDeclarationDisambiguation(
filename: string,
): boolean {
const pathExt = extname(filename).toLocaleLowerCase();
switch (pathExt) {
case ts.Extension.Cts:
case ts.Extension.Mts:
case ts.Extension.Tsx:
return true;

default:
return false;
}
}

const requiresGenericDeclarationDisambiguation =
checkRequiresGenericDeclarationDisambiguation(context.getFilename());

const source = context.getSourceCode();

const checkNode = (
Expand All @@ -47,7 +66,7 @@ export default util.createRule({
): void => {
const constraint = unnecessaryConstraints.get(node.constraint.type);
function shouldAddTrailingComma(): boolean {
if (!inArrowFunction || !inJsx) {
if (!inArrowFunction || !requiresGenericDeclarationDisambiguation) {
return false;
}
// Only <T>() => {} would need trailing comma
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,46 @@ function data<T extends TODO>() {}
},
},
},
{
code: 'const data = <T extends any>() => {};',
errors: [
{
data: { constraint: 'any', name: 'T' },
messageId: 'unnecessaryConstraint',
endColumn: 28,
column: 15,
line: 1,
suggestions: [
{
messageId: 'removeUnnecessaryConstraint',
data: { constraint: 'any' },
output: `const data = <T,>() => {};`,
},
],
},
],
filename: 'file.mts',
},
{
code: 'const data = <T extends any>() => {};',
errors: [
{
data: { constraint: 'any', name: 'T' },
messageId: 'unnecessaryConstraint',
endColumn: 28,
column: 15,
line: 1,
suggestions: [
{
messageId: 'removeUnnecessaryConstraint',
data: { constraint: 'any' },
output: `const data = <T,>() => {};`,
},
],
},
],
filename: 'file.cts',
},
{
code: noFormat`const data = <T extends any,>() => {};`,
errors: [
Expand Down