Skip to content

Commit 8830b2e

Browse files
SulimanAbdulrazzaqbrettz9
authored andcommitted
fix(ts-prefer-function-type): do not throw on call signatures inside object fields, parameters or return types, and keep the type when fixing
The fixer only handled a call signature object that is an element of an array-like parent and otherwise threw "Rule currently unable to handle type ...", which aborts linting of the file because ESLint builds the fix at report time. Replace the object in its parent generically (array slot or property) and parenthesize the new function type unless the parent is a function return type, an angle-bracket generic, a key/value, an object field, a parenthesis or a tuple, so that the written type is the same type. The expected output for the union case was `(string | (arg: string) => void)`, which TypeScript rejects (TS1385); it is now `(string | ((arg: string) => void))`.
1 parent 0a1b435 commit 8830b2e

3 files changed

Lines changed: 167 additions & 15 deletions

File tree

‎docs/rules/ts-prefer-function-type.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ The following patterns are considered problems:
5656
* })} someName
5757
*/
5858
// Message: Call signature found; function type preferred.
59+
60+
/**
61+
* @param {{(): void}[]} someName
62+
*/
63+
// Message: Call signature found; function type preferred.
64+
65+
/**
66+
* @param {{(x: number): string} | null} someName
67+
*/
68+
// Message: Call signature found; function type preferred.
69+
70+
/**
71+
* @param {{a: {(): void}}} someName
72+
*/
73+
// Message: Call signature found; function type preferred.
74+
75+
/**
76+
* @param {(cb: {(err: Error): void}) => void} someName
77+
*/
78+
// Message: Call signature found; function type preferred.
79+
80+
/**
81+
* @returns {() => {(): void}}
82+
*/
83+
// Message: Call signature found; function type preferred.
5984
````
6085

6186

‎src/rules/tsPreferFunctionType.js‎

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ import {
1010
parse as espree,
1111
} from 'espree';
1212

13+
/**
14+
* Parents within which a function type needs no parentheses.
15+
*/
16+
const noParenthesesParents = new Set([
17+
'JsdocTypeFunction',
18+
'JsdocTypeGeneric',
19+
'JsdocTypeKeyValue',
20+
'JsdocTypeObjectField',
21+
'JsdocTypeParenthesis',
22+
'JsdocTypeTuple',
23+
]);
24+
1325
export default iterateJsdoc(({
1426
context,
1527
indent,
@@ -40,9 +52,13 @@ export default iterateJsdoc(({
4052
return;
4153
}
4254

43-
traverse(parsedType, (nde, parentNode) => {
44-
// @ts-expect-error Adding our own property for use below
55+
traverse(parsedType, (nde, parentNode, property, index) => {
56+
// @ts-expect-error Adding our own properties for use below
4557
nde.parentNode = parentNode;
58+
// @ts-expect-error Adding our own properties for use below
59+
nde.parentProperty = property;
60+
// @ts-expect-error Adding our own properties for use below
61+
nde.parentIndex = index;
4662
});
4763

4864
traverse(parsedType, (nde, parentNode, property, index) => {
@@ -69,19 +85,40 @@ export default iterateJsdoc(({
6985
typeParameters: nde.typeParameters,
7086
});
7187

72-
if (property && 'parentNode' in object && object.parentNode) {
73-
if (typeof object.parentNode === 'object' &&
74-
'elements' in object.parentNode &&
75-
Array.isArray(object.parentNode.elements)
76-
) {
77-
const idx = object.parentNode.elements.indexOf(object);
78-
object.parentNode.elements[idx] = func;
79-
/* c8 ignore next 6 -- Guard */
88+
const {
89+
parentIndex,
90+
parentNode: objectParent,
91+
parentProperty,
92+
} = /**
93+
* @type {{
94+
* parentIndex?: number,
95+
* parentNode?: import('jsdoc-type-pratt-parser').NonRootResult,
96+
* parentProperty?: string
97+
* }}
98+
*/ (object);
99+
100+
if (objectParent && parentProperty) {
101+
// A function type must be parenthesized wherever it would
102+
// otherwise bind differently, e.g., in a union or as the
103+
// element of an array type
104+
const replacement = noParenthesesParents.has(objectParent.type) &&
105+
!(
106+
objectParent.type === 'JsdocTypeGeneric' &&
107+
objectParent.meta.brackets === 'square'
108+
) ?
109+
func :
110+
/** @type {import('jsdoc-type-pratt-parser').ParenthesisResult} */ ({
111+
element: func,
112+
type: 'JsdocTypeParenthesis',
113+
});
114+
115+
const parentRecord = /** @type {Record<string, unknown>} */ (
116+
/** @type {unknown} */ (objectParent)
117+
);
118+
if (typeof parentIndex === 'number') {
119+
/** @type {unknown[]} */ (parentRecord[parentProperty])[parentIndex] = replacement;
80120
} else {
81-
throw new Error(
82-
// @ts-expect-error Ok
83-
`Rule currently unable to handle type ${object.parentNode.type}`,
84-
);
121+
parentRecord[parentProperty] = replacement;
85122
}
86123
} else {
87124
parsedType = func;

‎test/rules/assertions/tsPreferFunctionType.js‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,97 @@ export default {
5656
],
5757
output: `
5858
/**
59-
* @param {(string | (arg: string) => void)} someName
59+
* @param {(string | ((arg: string) => void))} someName
60+
*/
61+
`,
62+
},
63+
{
64+
code: `
65+
/**
66+
* @param {{(): void}[]} someName
67+
*/
68+
`,
69+
errors: [
70+
{
71+
line: 3,
72+
message: 'Call signature found; function type preferred.',
73+
},
74+
],
75+
output: `
76+
/**
77+
* @param {(() => void)[]} someName
78+
*/
79+
`,
80+
},
81+
{
82+
code: `
83+
/**
84+
* @param {{(x: number): string} | null} someName
85+
*/
86+
`,
87+
errors: [
88+
{
89+
line: 3,
90+
message: 'Call signature found; function type preferred.',
91+
},
92+
],
93+
output: `
94+
/**
95+
* @param {((x: number) => string) | null} someName
96+
*/
97+
`,
98+
},
99+
{
100+
code: `
101+
/**
102+
* @param {{a: {(): void}}} someName
103+
*/
104+
`,
105+
errors: [
106+
{
107+
line: 3,
108+
message: 'Call signature found; function type preferred.',
109+
},
110+
],
111+
output: `
112+
/**
113+
* @param {{a: () => void}} someName
114+
*/
115+
`,
116+
},
117+
{
118+
code: `
119+
/**
120+
* @param {(cb: {(err: Error): void}) => void} someName
121+
*/
122+
`,
123+
errors: [
124+
{
125+
line: 3,
126+
message: 'Call signature found; function type preferred.',
127+
},
128+
],
129+
output: `
130+
/**
131+
* @param {(cb: (err: Error) => void) => void} someName
132+
*/
133+
`,
134+
},
135+
{
136+
code: `
137+
/**
138+
* @returns {() => {(): void}}
139+
*/
140+
`,
141+
errors: [
142+
{
143+
line: 3,
144+
message: 'Call signature found; function type preferred.',
145+
},
146+
],
147+
output: `
148+
/**
149+
* @returns {() => () => void}
60150
*/
61151
`,
62152
},

0 commit comments

Comments
 (0)