GeezerVison is showing github.com — fix(`no-unnecessary-type-assertion`): widen literal types on let and … · gajus/eslint-plugin-jsdoc@6f90f25
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/rules/no-unnecessary-type-assertion.md +49Lines changed: 49 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -413,6 +413,30 @@ export const first = /** @type {string} */ (text); | 413 | 413 | export const second = /** @type {string} */ (text); | 414 | 414 | // "jsdoc/no-unnecessary-type-assertion": ["error"|"warn", {"enableFixer":false}] | 415 | 415 | // Message: The @type tag declaring "string" is redundant as TypeScript infers it automatically. | | 416 | + | | 417 | +/** | | 418 | + * @type {'a' | 'b'} | | 419 | + */ | | 420 | +const mode = 'a'; | | 421 | +// Message: The @type tag declaring "'a' | 'b'" is redundant as TypeScript infers it automatically. | | 422 | + | | 423 | +/** | | 424 | + * @type {string} | | 425 | + */ | | 426 | +let mode = 'a'; | | 427 | +// Message: The @type tag declaring "string" is redundant as TypeScript infers it automatically. | | 428 | + | | 429 | +/** | | 430 | + * @type {string} | | 431 | + */ | | 432 | +var mode = 'a'; | | 433 | +// Message: The @type tag declaring "string" is redundant as TypeScript infers it automatically. | | 434 | + | | 435 | +/** | | 436 | + * @type {{mode: string}} | | 437 | + */ | | 438 | +const {mode} = {mode: 'a'}; | | 439 | +// Message: The @type tag declaring "{mode: string}" is redundant as TypeScript infers it automatically. | 416 | 440 | ```` | 417 | 441 |
| 418 | 442 |
| @@ -658,5 +682,30 @@ const keyRange = { | 658 | 682 | ...base, | 659 | 683 | run: (arg) => arg, | 660 | 684 | }; | | 685 | + | | 686 | +/** | | 687 | + * @type {'a' | 'b'} | | 688 | + */ | | 689 | +let mode = 'a'; | | 690 | + | | 691 | +/** | | 692 | + * @type {'a' | 'b'} | | 693 | + */ | | 694 | +var mode = 'a'; | | 695 | + | | 696 | +/** | | 697 | + * @type {{mode: 'a' | 'b'}} | | 698 | + */ | | 699 | +let {mode} = {mode: 'a'}; | | 700 | + | | 701 | +/** | | 702 | + * @type {{mode: 'a' | 'b'}} | | 703 | + */ | | 704 | +var {mode} = {mode: 'a'}; | | 705 | + | | 706 | +/** | | 707 | + * @type {'a' | 'b'} | | 708 | + */ | | 709 | +let mode = 'a', other = 'c'; | 661 | 710 | ```` | 662 | 711 |
|
Collapse file Copy file name to clipboardExpand all lines: src/rules/noUnnecessaryTypeAssertion.js +28-5Lines changed: 28 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -482,6 +482,23 @@ export default iterateJsdoc(({ | 482 | 482 | 'VariableDeclarator', | 483 | 483 | ]); | 484 | 484 |
| | 485 | + /** | | 486 | + * The type TypeScript infers for an initializer. A mutable binding (`let` or | | 487 | + * `var`) widens a literal (`'a'` becomes `string`, `5` becomes `number`), so | | 488 | + * a `@type` that only matches the unwidened literal is still narrowing. A | | 489 | + * `const` binding keeps the literal type. | | 490 | + * @param {any} initializerTsNode `ts.Node` | | 491 | + * @param {boolean} mutableBinding `true` when the declaration is not `const` | | 492 | + * @returns {any} `ts.Type` | | 493 | + */ | | 494 | + const inferredInitializerType = (initializerTsNode, mutableBinding) => { | | 495 | + const inferredType = checker.getTypeAtLocation(initializerTsNode); | | 496 | + | | 497 | + return mutableBinding ? | | 498 | + checker.getBaseTypeOfLiteralType(inferredType) : | | 499 | + inferredType; | | 500 | + }; | | 501 | + | 485 | 502 | // 3. `/** @type {T} */ const x = 5;` | 486 | 503 | if (node?.type === 'VariableDeclaration') { | 487 | 504 | // A leading `@type` tag only influences the first declarator (TypeScript | @@ -513,7 +530,13 @@ export default iterateJsdoc(({ | 513 | 530 | return; | 514 | 531 | } | 515 | 532 |
| 516 | | - const declInferredType = checker.getTypeAtLocation(declInitTsNode); | | 533 | + // Widen `let`/`var` the same way as an inline cast. Otherwise | | 534 | + // `/** @type {'a' | 'b'} */ let mode = 'a'` looks redundant, and removing | | 535 | + // the tag widens `mode` to `string`. | | 536 | + const declInferredType = inferredInitializerType( | | 537 | + declInitTsNode, | | 538 | + node.kind !== 'const', | | 539 | + ); | 517 | 540 | const declAssertedType = checker.getTypeFromTypeNode(jsdocTypeNode); | 518 | 541 |
| 519 | 542 | if (isTupleConversionIdiom(declAssertedType, decl.init)) { | @@ -600,10 +623,10 @@ export default iterateJsdoc(({ | 600 | 623 | // For a mutable binding (`let`/`var`) TypeScript widens the initializer, so a | 601 | 624 | // narrowing cast such as `/** @type {5} */` is doing real work; widen the | 602 | 625 | // uncast type to match before judging redundancy. | 603 | | - const mutableBinding = Boolean(declaration) && declaration.kind !== 'const'; | 604 | | - const castInferredType = mutableBinding ? | 605 | | - checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(exprTsNode)) : | 606 | | - checker.getTypeAtLocation(exprTsNode); | | 626 | + const castInferredType = inferredInitializerType( | | 627 | + exprTsNode, | | 628 | + Boolean(declaration) && declaration.kind !== 'const', | | 629 | + ); | 607 | 630 | const castAssertedType = checker.getTypeFromTypeNode(typeTag.typeExpression.type); | 608 | 631 |
| 609 | 632 | if (isTupleConversionIdiom(castAssertedType, node)) { | |
Collapse file Copy file name to clipboardExpand all lines: test/rules/assertions/noUnnecessaryTypeAssertion.js +126Lines changed: 126 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -873,6 +873,82 @@ export default /** @type {import('../index.js').TestCases} */ ({ | 873 | 873 | ], | 874 | 874 | output: null, | 875 | 875 | }, | | 876 | + { | | 877 | + code: ` | | 878 | + /** | | 879 | + * @type {'a' | 'b'} | | 880 | + */ | | 881 | + const mode = 'a'; | | 882 | + `, | | 883 | + errors: [ | | 884 | + { | | 885 | + line: 3, | | 886 | + message: 'The @type tag declaring "\'a\' | \'b\'" is redundant as TypeScript infers it automatically.', | | 887 | + }, | | 888 | + ], | | 889 | + filename: 'dummy.js', | | 890 | + languageOptions, | | 891 | + output: ` | | 892 | + const mode = 'a'; | | 893 | + `, | | 894 | + }, | | 895 | + { | | 896 | + code: ` | | 897 | + /** | | 898 | + * @type {string} | | 899 | + */ | | 900 | + let mode = 'a'; | | 901 | + `, | | 902 | + errors: [ | | 903 | + { | | 904 | + line: 3, | | 905 | + message: 'The @type tag declaring "string" is redundant as TypeScript infers it automatically.', | | 906 | + }, | | 907 | + ], | | 908 | + filename: 'dummy.js', | | 909 | + languageOptions, | | 910 | + output: ` | | 911 | + let mode = 'a'; | | 912 | + `, | | 913 | + }, | | 914 | + { | | 915 | + code: ` | | 916 | + /** | | 917 | + * @type {string} | | 918 | + */ | | 919 | + var mode = 'a'; | | 920 | + `, | | 921 | + errors: [ | | 922 | + { | | 923 | + line: 3, | | 924 | + message: 'The @type tag declaring "string" is redundant as TypeScript infers it automatically.', | | 925 | + }, | | 926 | + ], | | 927 | + filename: 'dummy.js', | | 928 | + languageOptions, | | 929 | + output: ` | | 930 | + var mode = 'a'; | | 931 | + `, | | 932 | + }, | | 933 | + { | | 934 | + code: ` | | 935 | + /** | | 936 | + * @type {{mode: string}} | | 937 | + */ | | 938 | + const {mode} = {mode: 'a'}; | | 939 | + `, | | 940 | + errors: [ | | 941 | + { | | 942 | + line: 3, | | 943 | + message: 'The @type tag declaring "{mode: string}" is redundant as TypeScript infers it automatically.', | | 944 | + }, | | 945 | + ], | | 946 | + filename: 'dummy.js', | | 947 | + languageOptions, | | 948 | + output: ` | | 949 | + const {mode} = {mode: 'a'}; | | 950 | + `, | | 951 | + }, | 876 | 952 | ], | 877 | 953 | valid: [ | 878 | 954 | { | @@ -1360,5 +1436,55 @@ export default /** @type {import('../index.js').TestCases} */ ({ | 1360 | 1436 | filename: 'dummy.js', | 1361 | 1437 | languageOptions, | 1362 | 1438 | }, | | 1439 | + { | | 1440 | + code: ` | | 1441 | + /** | | 1442 | + * @type {'a' | 'b'} | | 1443 | + */ | | 1444 | + let mode = 'a'; | | 1445 | + `, | | 1446 | + filename: 'dummy.js', | | 1447 | + languageOptions, | | 1448 | + }, | | 1449 | + { | | 1450 | + code: ` | | 1451 | + /** | | 1452 | + * @type {'a' | 'b'} | | 1453 | + */ | | 1454 | + var mode = 'a'; | | 1455 | + `, | | 1456 | + filename: 'dummy.js', | | 1457 | + languageOptions, | | 1458 | + }, | | 1459 | + { | | 1460 | + code: ` | | 1461 | + /** | | 1462 | + * @type {{mode: 'a' | 'b'}} | | 1463 | + */ | | 1464 | + let {mode} = {mode: 'a'}; | | 1465 | + `, | | 1466 | + filename: 'dummy.js', | | 1467 | + languageOptions, | | 1468 | + }, | | 1469 | + { | | 1470 | + code: ` | | 1471 | + /** | | 1472 | + * @type {{mode: 'a' | 'b'}} | | 1473 | + */ | | 1474 | + var {mode} = {mode: 'a'}; | | 1475 | + `, | | 1476 | + filename: 'dummy.js', | | 1477 | + languageOptions, | | 1478 | + }, | | 1479 | + { | | 1480 | + code: ` | | 1481 | + /** | | 1482 | + * @type {'a' | 'b'} | | 1483 | + */ | | 1484 | + let mode = 'a', other = 'c'; | | 1485 | + `, | | 1486 | + filename: 'dummy.js', | | 1487 | + languageOptions, | | 1488 | + }, | 1363 | 1489 | ], | 1364 | 1490 | }); |
|