Skip to content

Commit 342b6f6

Browse files
committed
fix(no-unnecessarry-type-assertion): avoid treating as redundant object annotations which clarify method argument types
1 parent 543b24f commit 342b6f6

5 files changed

Lines changed: 109 additions & 7 deletions

File tree

‎docs/rules/no-unnecessary-type-assertion.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,5 +511,36 @@ const viewTypes = ['single-panel', 'panels-dialogs', 'user'];
511511
*/
512512

513513
const viewTypes = /** @type {ViewType[]} */ (['single-panel', 'panels-dialogs', 'user']);
514+
515+
/**
516+
* @type {{
517+
* main: (...arg: string[]) => string[]
518+
* }}
519+
*/
520+
const keyRange = {
521+
main (...args) {
522+
return args;
523+
},
524+
};
525+
526+
const keyRange = /** @type {{
527+
main: (...arg: string[]) => string[]
528+
}} */ ({
529+
main (...args) {
530+
return args;
531+
},
532+
});
533+
534+
const base = {};
535+
536+
/**
537+
* @type {{
538+
* run: (arg: string) => string
539+
* }}
540+
*/
541+
const keyRange = {
542+
...base,
543+
run: (arg) => arg,
544+
};
514545
````
515546

‎docs/rules/require-jsdoc.md‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ to insert.
4141

4242
A single options object has the following properties.
4343

44-
Has the following optional keys.
45-
46-
4744
<a name="user-content-require-jsdoc-options-checkallfunctionexpressions"></a>
4845
<a name="require-jsdoc-options-checkallfunctionexpressions"></a>
4946
### <code>checkAllFunctionExpressions</code>

‎src/rules/noUnnecessaryTypeAssertion.js‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export default iterateJsdoc(({
365365

366366
/**
367367
* An array-literal annotation whose element type is a literal (or union of
368-
* literals) — `/** @type {ViewType[]} *\/` on `['single-panel', 'user']` —
368+
* literals) — `type {ViewType[]}` on `['single-panel', 'user']` —
369369
* narrows the element type that the bare literal would widen (`string[]` ->
370370
* `ViewType[]`). Like the tuple idiom, the literal takes that element type
371371
* only *from* the annotation, so the contextually-typed initializer echoes
@@ -385,6 +385,29 @@ export default iterateJsdoc(({
385385
return isLiteralType(elementType);
386386
};
387387

388+
/**
389+
* Whether `operand` is an object literal carrying a method or function-valued
390+
* property. Under a surrounding `@type` object annotation such a function is
391+
* contextually typed by the annotation — its otherwise-implicit-`any`
392+
* parameters (and hence its signature) come straight from it — so
393+
* `getTypeAtLocation` echoes the asserted object shape back and a genuine
394+
* typing looks redundant. As elsewhere in this rule the uncontaminated type
395+
* cannot be recovered here, so such literals are left alone (at the cost of
396+
* missing the rare object annotation that only restates a fully
397+
* self-evident method signature).
398+
* @param {import('@typescript-eslint/utils').TSESTree.Node | null | undefined} operand
399+
* @returns {boolean}
400+
*/
401+
const hasContextuallyTypedFunctionMember = (operand) => {
402+
return operand?.type === 'ObjectExpression' &&
403+
operand.properties.some((property) => {
404+
return property.type === 'Property' && (
405+
property.value.type === 'FunctionExpression' ||
406+
property.value.type === 'ArrowFunctionExpression'
407+
);
408+
});
409+
};
410+
388411
/**
389412
* Whether every element of `tupleType` is a literal type, so a `const`
390413
* assertion would reproduce the same element types (`['foo', 1]` but not
@@ -467,7 +490,10 @@ export default iterateJsdoc(({
467490
return;
468491
}
469492

470-
if (narrowsArrayLiteralElement(declAssertedType, decl.init)) {
493+
if (
494+
narrowsArrayLiteralElement(declAssertedType, decl.init) ||
495+
hasContextuallyTypedFunctionMember(decl.init)
496+
) {
471497
return;
472498
}
473499

@@ -548,7 +574,10 @@ export default iterateJsdoc(({
548574
return;
549575
}
550576

551-
if (narrowsArrayLiteralElement(castAssertedType, node)) {
577+
if (
578+
narrowsArrayLiteralElement(castAssertedType, node) ||
579+
hasContextuallyTypedFunctionMember(node)
580+
) {
552581
return;
553582
}
554583

‎src/rules/requireJsdoc.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
/** @type {import('json-schema').JSONSchema4} */
3535
const OPTIONS_SCHEMA = {
3636
additionalProperties: false,
37-
description: 'Has the following optional keys.\n',
3837
properties: {
3938
checkAllFunctionExpressions: {
4039
default: false,

‎test/rules/assertions/noUnnecessaryTypeAssertion.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,5 +1026,51 @@ export default /** @type {import('../index.js').TestCases} */ ({
10261026
filename: 'dummy.js',
10271027
languageOptions,
10281028
},
1029+
{
1030+
code: `
1031+
/**
1032+
* @type {{
1033+
* main: (...arg: string[]) => string[]
1034+
* }}
1035+
*/
1036+
const keyRange = {
1037+
main (...args) {
1038+
return args;
1039+
},
1040+
};
1041+
`,
1042+
filename: 'dummy.js',
1043+
languageOptions,
1044+
},
1045+
{
1046+
code: `
1047+
const keyRange = /** @type {{
1048+
main: (...arg: string[]) => string[]
1049+
}} */ ({
1050+
main (...args) {
1051+
return args;
1052+
},
1053+
});
1054+
`,
1055+
filename: 'dummy.js',
1056+
languageOptions,
1057+
},
1058+
{
1059+
code: `
1060+
const base = {};
1061+
1062+
/**
1063+
* @type {{
1064+
* run: (arg: string) => string
1065+
* }}
1066+
*/
1067+
const keyRange = {
1068+
...base,
1069+
run: (arg) => arg,
1070+
};
1071+
`,
1072+
filename: 'dummy.js',
1073+
languageOptions,
1074+
},
10291075
],
10301076
});

0 commit comments

Comments
 (0)