eslint-plugin-jsdoc 65.3.12 → 65.3.14
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
package/src/jsdocUtils.js
CHANGED
|
@@ -1707,6 +1707,14 @@ const getContextObject = (contexts, checkJsdoc, handler, convertAny) => {
|
|
|
1707
1707
|
lastIndex: idx,
|
|
1708
1708
|
selector: prop.context,
|
|
1709
1709
|
};
|
|
1710
|
+
if (prop.comment && !handler) {
|
|
1711
|
+
throw new Error(
|
|
1712
|
+
'The `comment` property of a `contexts` entry (e.g., one from ' +
|
|
1713
|
+
'`settings.jsdoc.contexts`) is not supported by this rule; give the ' +
|
|
1714
|
+
'rule its own `contexts` option instead.',
|
|
1715
|
+
);
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1710
1718
|
if (prop.comment) {
|
|
1711
1719
|
property = /** @type {string} */ (prop.context);
|
|
1712
1720
|
value = checkJsdoc.bind(
|
|
@@ -648,7 +648,7 @@ export default iterateJsdoc(({
|
|
|
648
648
|
// checks if they are not functions but say variables), the user may
|
|
649
649
|
// instead wish to narrow contexts in those settings, so this check
|
|
650
650
|
// is still useful
|
|
651
|
-
if (!allowedNodes.includes(nde.type) ||
|
|
651
|
+
if (!nde || !allowedNodes.includes(nde.type) ||
|
|
652
652
|
nde.type === 'TSPropertySignature' &&
|
|
653
653
|
(/** @type {import('@typescript-eslint/types').TSESTree.TSPropertySignature} */ (
|
|
654
654
|
nde
|
|
@@ -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)) {
|