eslint-plugin-jsdoc 65.4.0 → 65.4.1

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
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "dependencies": {
8
8
  "@es-joy/escodegen": "^4.2.0",
9
- "@es-joy/jsdoccomment": "~0.100.0",
9
+ "@es-joy/jsdoccomment": "~1.0.0",
10
10
  "@es-joy/resolve.exports": "1.2.0",
11
11
  "@typescript-eslint/utils": "^8.72.0",
12
12
  "are-docs-informative": "^0.1.1",
@@ -57,7 +57,7 @@
57
57
  "glob": "^13.0.6",
58
58
  "globals": "^17.13.0",
59
59
  "husky": "^9.1.7",
60
- "jsdoc-type-pratt-parser": "^9.3.0",
60
+ "jsdoc-type-pratt-parser": "^9.4.0",
61
61
  "json-schema": "^0.4.0",
62
62
  "json-schema-to-typescript": "^16.0.0",
63
63
  "lint-staged": "^17.6.0",
@@ -163,5 +163,5 @@
163
163
  "test-cov": "TIMING=1 c8 --reporter text pnpm run test-no-cov",
164
164
  "test-index": "pnpm run test-no-cov test/rules/index.js"
165
165
  },
166
- "version": "65.4.0"
166
+ "version": "65.4.1"
167
167
  }
@@ -10,6 +10,18 @@ import {
10
10
  parse as espree,
11
11
  } from 'espree';
12
12
 
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
+
13
25
  export default iterateJsdoc(({
14
26
  context,
15
27
  indent,
@@ -40,9 +52,13 @@ export default iterateJsdoc(({
40
52
  return;
41
53
  }
42
54
 
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
45
57
  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;
46
62
  });
47
63
 
48
64
  traverse(parsedType, (nde, parentNode, property, index) => {
@@ -69,19 +85,40 @@ export default iterateJsdoc(({
69
85
  typeParameters: nde.typeParameters,
70
86
  });
71
87
 
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;
80
120
  } 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;
85
122
  }
86
123
  } else {
87
124
  parsedType = func;