eslint-plugin-jsdoc 65.3.12 → 65.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -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
|
-
|
|
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
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
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)) {
|