Skip to content

Commit 4f2ec35

Browse files
committed
fix: fixing of missing parent should go between child and grandparent
Co-authored by Co-pilot
1 parent c9a22b6 commit 4f2ec35

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

docs/rules/require-param.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,15 @@ function quux ({ foo, bar: { baz }}) {
629629
}
630630
// Message: Missing JSDoc @param "root0" declaration.
631631

632+
/**
633+
* @param root0
634+
* @param root0.foo
635+
* @param root0.bar.baz
636+
*/
637+
function quux ({ foo, bar: { baz }}) {
638+
}
639+
// Message: Missing JSDoc @param "root0.bar" declaration.
640+
632641
/**
633642
*
634643
*/

src/rules/requireParam.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,36 @@ export default iterateJsdoc(({
181181
* }}
182182
*/
183183
const findExpectedIndex = (jsdocTags, indexAtFunctionParams) => {
184-
const remainingRoots = functionParameterNames.slice(indexAtFunctionParams || 0);
184+
// Get the parameters that come after the current index in the flattened order
185+
const remainingFlattenedRoots = flattenedRoots.slice((indexAtFunctionParams || 0) + 1);
186+
187+
// Find the first existing tag that comes after the current parameter in the flattened order
185188
const foundIndex = jsdocTags.findIndex(({
186189
name,
187190
newAdd,
188191
}) => {
189-
return !newAdd && remainingRoots.some((remainingRoot) => {
190-
if (Array.isArray(remainingRoot)) {
191-
return (
192-
/**
193-
* @type {import('../jsdocUtils.js').FlattendRootInfo & {
194-
* annotationParamName?: string|undefined;
195-
* }}
196-
*/ (remainingRoot[1]).names.includes(name)
197-
);
192+
if (newAdd) {
193+
return false;
194+
}
195+
196+
// Check if the tag name matches any of the remaining flattened roots
197+
return remainingFlattenedRoots.some((flattenedRoot) => {
198+
// The flattened roots don't have the root prefix (e.g., "bar", "bar.baz")
199+
// but JSDoc tags do (e.g., "root0", "root0.bar", "root0.bar.baz")
200+
// So we need to check if the tag name ends with the flattened root
201+
202+
// Check if tag name ends with ".<flattenedRoot>"
203+
if (name.endsWith(`.${flattenedRoot}`)) {
204+
return true;
198205
}
199206

200-
if (typeof remainingRoot === 'object') {
201-
return name === remainingRoot.name;
207+
// Also check if tag name exactly matches the flattenedRoot
208+
// (for single-level params)
209+
if (name === flattenedRoot) {
210+
return true;
202211
}
203212

204-
return name === remainingRoot;
213+
return false;
205214
});
206215
});
207216

test/rules/assertions/requireParam.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,33 @@ export default /** @type {import('../index.js').TestCases} */ ({
339339
}
340340
`,
341341
},
342+
{
343+
code: `
344+
/**
345+
* @param root0
346+
* @param root0.foo
347+
* @param root0.bar.baz
348+
*/
349+
function quux ({ foo, bar: { baz }}) {
350+
}
351+
`,
352+
errors: [
353+
{
354+
line: 2,
355+
message: 'Missing JSDoc @param "root0.bar" declaration.',
356+
},
357+
],
358+
output: `
359+
/**
360+
* @param root0
361+
* @param root0.foo
362+
* @param root0.bar
363+
* @param root0.bar.baz
364+
*/
365+
function quux ({ foo, bar: { baz }}) {
366+
}
367+
`,
368+
},
342369
{
343370
code: `
344371
/**
@@ -2244,8 +2271,8 @@ export default /** @type {import('../index.js').TestCases} */ ({
22442271
/**
22452272
* Description.
22462273
* @param {Object} options
2247-
* @param options.foo
22482274
* @param {FooBar} foo
2275+
* @param options.foo
22492276
* @param options.foo.bar
22502277
*/
22512278
function quux ({ foo: { bar } }) {}

0 commit comments

Comments
 (0)