Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [no-unnecessary-type-parameters] should parenthesize type in suggestion fixer if necessary#10907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
681ac8e
fix: parenthesize type in suggestion fixer if necessary
y-hsgwMar 2, 2025
e5cbe8e
test: add test
y-hsgwMar 2, 2025
cc8af2e
feat: support TSIndexedAccessType
y-hsgwMar 8, 2025
1a60c5b
test: add test
y-hsgwMar 8, 2025
91dd50d
fix: add "!" type assertion
y-hsgwMar 10, 2025
4cb399a
test: add test
y-hsgwMar 10, 2025
dd18051
fix: correct type precedence logic to consider broader context
y-hsgwMar 20, 2025
8505b54
fix: use getWrappingFixer
y-hsgwMar 20, 2025
8086924
chore: revert comment
y-hsgwMar 20, 2025
40a139f
Merge branch 'main' into fix/no-unnecessary-type-parameters
kirkwaiblingerApr 28, 2025
90ad87b
Merge branch 'main' into fix/no-unnecessary-type-parameters
kirkwaiblingerApr 28, 2025
66a4988
address cov
kirkwaiblingerApr 28, 2025
30b0c4f
Merge branch 'main' into fix/no-unnecessary-type-parameters
kirkwaiblingerApr 28, 2025
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import type { MakeRequired } from '../util';
import {
createRule,
getParserServices,
getWrappingFixer,
nullThrows,
NullThrowsReasons,
} from '../util';
Expand DownExpand Up@@ -108,7 +109,28 @@ export default createRule({
for (const reference of smTypeParameterVariable.references) {
if (reference.isTypeReference) {
const referenceNode = reference.identifier;
yield fixer.replaceText(referenceNode, constraintText);
const isComplexType =
constraint?.type === AST_NODE_TYPES.TSUnionType ||
constraint?.type === AST_NODE_TYPES.TSIntersectionType ||
constraint?.type === AST_NODE_TYPES.TSConditionalType;
const hasMatchingAncestorType = [
AST_NODE_TYPES.TSArrayType,
AST_NODE_TYPES.TSIndexedAccessType,
AST_NODE_TYPES.TSIntersectionType,
AST_NODE_TYPES.TSUnionType,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
].some(type => referenceNode.parent.parent!.type === type);
if (isComplexType && hasMatchingAncestorType) {
const fixResult = getWrappingFixer({
node: referenceNode,
innerNode: constraint,
sourceCode: context.sourceCode,
wrap: constraintNode => constraintNode,
})(fixer);
yield fixResult;
} else {
yield fixer.replaceText(referenceNode, constraintText);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/util/getWrappingFixer.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ interface WrappingFixerParams {
*/
export function getWrappingFixer(
params: WrappingFixerParams,
): TSESLint.ReportFixFunction {
):(fixer:TSESLint.RuleFixer) => TSESLint.RuleFix {
const { node, innerNode = node, sourceCode, wrap } = params;
const innerNodes = Array.isArray(innerNode) ? innerNode : [innerNode];

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1744,5 +1744,172 @@ class Joiner {
},
],
},
{
code: `
function join<T extends string | number>(els: T[]) {
return els.map(el => '' + el).join(',');
}
`,
errors: [
{
data: { descriptor: 'function', name: 'T', uses: 'used only once' },
messageId: 'sole',
suggestions: [
{
messageId: 'replaceUsagesWithConstraint',
output: `
function join(els: (string | number)[]) {
return els.map(el => '' + el).join(',');
}
`,
},
],
},
],
},
{
code: `
function join<T extends string & number>(els: T[]) {
return els.map(el => '' + el).join(',');
}
`,
errors: [
{
data: { descriptor: 'function', name: 'T', uses: 'used only once' },
messageId: 'sole',
suggestions: [
{
messageId: 'replaceUsagesWithConstraint',
output: `
function join(els: (string & number)[]) {
return els.map(el => '' + el).join(',');
}
`,
},
],
},
],
},
{
code: `
function join<T extends (string & number) | boolean>(els: T[]) {
return els.map(el => '' + el).join(',');
}
`,
errors: [
{
data: { descriptor: 'function', name: 'T', uses: 'used only once' },
messageId: 'sole',
suggestions: [
{
messageId: 'replaceUsagesWithConstraint',
output: `
function join(els: ((string & number) | boolean)[]) {
return els.map(el => '' + el).join(',');
}
`,
},
],
},
],
},
{
code: noFormat`
function join<T extends (string | number)>(els: T[]) {
return els.map(el => '' + el).join(',');
}
`,
errors: [
{
data: { descriptor: 'function', name: 'T', uses: 'used only once' },
messageId: 'sole',
suggestions: [
{
messageId: 'replaceUsagesWithConstraint',
output: `
function join(els: (string | number)[]) {
return els.map(el => '' + el).join(',');
}
`,
},
],
},
],
},
{
code: `
function join<T extends { hoge: string } | { hoge: number }>(els: T['hoge'][]) {
return els.map(el => '' + el).join(',');
}
`,
errors: [
{
data: { descriptor: 'function', name: 'T', uses: 'used only once' },
messageId: 'sole',
suggestions: [
{
messageId: 'replaceUsagesWithConstraint',
output: `
function join(els: ({ hoge: string } | { hoge: number })['hoge'][]) {
return els.map(el => '' + el).join(',');
}
`,
},
],
},
],
},
{
code: `
type A = string;
type B = string;
type C = string;
declare function f<T extends A | B>(): T & C;
`,
errors: [
{
data: { descriptor: 'function', name: 'T', uses: 'used only once' },
messageId: 'sole',
suggestions: [
{
messageId: 'replaceUsagesWithConstraint',
output: `
type A = string;
type B = string;
type C = string;
declare function f(): (A | B) & C;
`,
},
],
},
],
},
{
code: `
type A = string;
type B = string;
type C = string;
type D = string;
declare function f<T extends A extends B ? C : D>(): T | null;
`,
errors: [
{
data: { descriptor: 'function', name: 'T', uses: 'used only once' },
messageId: 'sole',
suggestions: [
{
messageId: 'replaceUsagesWithConstraint',
output: `
type A = string;
type B = string;
type C = string;
type D = string;
declare function f(): (A extends B ? C : D) | null;
`,
},
],
},
],
},
],
});

[8]ページ先頭

©2009-2025 Movatter.jp