Skip to content

Commit 91e261d

Browse files
mvarchdevbrettz9
andauthored
fix(requireParam): update return type to include foundIndex and tagLineCount (#1531)
* fix(requireParam): update return type to include foundIndex and tagLineCount, fixes #1530 * test(requireParam): add test case for missing JSDoc @param declaration, #1530 * fix(require-param): add missing JSDoc @param declaration for new function example * test: add test case which previously failed * chore: update devDeps. --------- Co-authored-by: Brett Zamir <brettz9@yahoo.com>
1 parent ab9fe6a commit 91e261d

File tree

5 files changed

+142
-17
lines changed

5 files changed

+142
-17
lines changed

docs/rules/require-param.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,35 @@ function quux ({
12651265
}
12661266
// "jsdoc/require-param": ["error"|"warn", {"interfaceExemptsParamsCheck":true}]
12671267
// Message: Missing JSDoc @param "root0" declaration.
1268+
1269+
/**
1270+
* @param foo
1271+
* @param baz
1272+
* @returns {number}
1273+
*/
1274+
function quux (foo, bar, baz) {
1275+
return foo + bar + baz;
1276+
}
1277+
// Message: Missing JSDoc @param "bar" declaration.
1278+
1279+
/**
1280+
* @example
1281+
* ```ts
1282+
* app.use(checkResourceOwnership({ entryPoint: 'seller_product' }));
1283+
* ```
1284+
*
1285+
* @example
1286+
* ```ts
1287+
* app.use(checkResourceOwnership({ entryPoint: 'service_zone' }));
1288+
* ```
1289+
*
1290+
* @param options - configuration
1291+
* @param options.entryPoint
1292+
* @param options.filterField
1293+
* @param options.paramIdField
1294+
*/
1295+
export const checkResourceOwnership = ({ entryPoint, filterField, paramIdField, resourceId = () => '' }) => {};
1296+
// Message: Missing JSDoc @param "options.resourceId" declaration.
12681297
````
12691298

12701299

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"url": "http://gajus.com"
66
},
77
"dependencies": {
8-
"@es-joy/jsdoccomment": "~0.61.0",
8+
"@es-joy/jsdoccomment": "~0.62.0",
99
"are-docs-informative": "^0.0.2",
1010
"comment-parser": "1.4.1",
1111
"debug": "^4.4.3",
@@ -58,7 +58,7 @@
5858
"glob": "^11.0.3",
5959
"globals": "^16.4.0",
6060
"husky": "^9.1.7",
61-
"jsdoc-type-pratt-parser": "^5.8.0",
61+
"jsdoc-type-pratt-parser": "^5.9.0",
6262
"json-schema": "^0.4.0",
6363
"json-schema-to-typescript": "^15.0.4",
6464
"lint-staged": "^16.2.1",

pnpm-lock.yaml

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rules/requireParam.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ export default iterateJsdoc(({
175175
* newAdd?: boolean
176176
* })[]} jsdocTags
177177
* @param {import('../iterateJsdoc.js').Integer} indexAtFunctionParams
178-
* @returns {import('../iterateJsdoc.js').Integer}
178+
* @returns {{
179+
* foundIndex: import('../iterateJsdoc.js').Integer,
180+
* tagLineCount: import('../iterateJsdoc.js').Integer,
181+
* }}
179182
*/
180183
const findExpectedIndex = (jsdocTags, indexAtFunctionParams) => {
181184
const remainingRoots = functionParameterNames.slice(indexAtFunctionParams || 0);
@@ -225,7 +228,10 @@ export default iterateJsdoc(({
225228
}
226229
}
227230

228-
return tagLineCount;
231+
return {
232+
foundIndex,
233+
tagLineCount,
234+
};
229235
};
230236

231237
let [
@@ -486,8 +492,22 @@ export default iterateJsdoc(({
486492
if (remove) {
487493
createTokens(functionParameterIdx, offset + functionParameterIdx, 1);
488494
} else {
489-
const expectedIdx = findExpectedIndex(jsdoc.tags, functionParameterIdx);
490-
createTokens(expectedIdx, offset + expectedIdx, 0);
495+
const {
496+
foundIndex,
497+
tagLineCount: expectedIdx,
498+
} =
499+
findExpectedIndex(jsdoc.tags, functionParameterIdx);
500+
501+
const firstParamLine = jsdoc.source.findIndex(({
502+
tokens,
503+
}) => {
504+
return tokens.tag === `@${preferredTagName}`;
505+
});
506+
const baseOffset = foundIndex > -1 || firstParamLine === -1 ?
507+
offset :
508+
firstParamLine;
509+
510+
createTokens(expectedIdx, baseOffset + expectedIdx, 0);
491511
}
492512
};
493513

test/rules/assertions/requireParam.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,82 @@ export default /** @type {import('../index.js').TestCases} */ ({
26682668
}
26692669
`,
26702670
},
2671+
{
2672+
code: `
2673+
/**
2674+
* @param foo
2675+
* @param baz
2676+
* @returns {number}
2677+
*/
2678+
function quux (foo, bar, baz) {
2679+
return foo + bar + baz;
2680+
}
2681+
`,
2682+
errors: [
2683+
{
2684+
line: 2,
2685+
message: 'Missing JSDoc @param "bar" declaration.',
2686+
},
2687+
],
2688+
output: `
2689+
/**
2690+
* @param foo
2691+
* @param bar
2692+
* @param baz
2693+
* @returns {number}
2694+
*/
2695+
function quux (foo, bar, baz) {
2696+
return foo + bar + baz;
2697+
}
2698+
`,
2699+
},
2700+
{
2701+
code: `
2702+
/**
2703+
* @example
2704+
* \`\`\`ts
2705+
* app.use(checkResourceOwnership({ entryPoint: 'seller_product' }));
2706+
* \`\`\`
2707+
*
2708+
* @example
2709+
* \`\`\`ts
2710+
* app.use(checkResourceOwnership({ entryPoint: 'service_zone' }));
2711+
* \`\`\`
2712+
*
2713+
* @param options - configuration
2714+
* @param options.entryPoint
2715+
* @param options.filterField
2716+
* @param options.paramIdField
2717+
*/
2718+
export const checkResourceOwnership = ({ entryPoint, filterField, paramIdField, resourceId = () => '' }) => {};
2719+
`,
2720+
errors: [
2721+
{
2722+
line: 2,
2723+
message: 'Missing JSDoc @param "options.resourceId" declaration.',
2724+
},
2725+
],
2726+
output: `
2727+
/**
2728+
* @example
2729+
* \`\`\`ts
2730+
* app.use(checkResourceOwnership({ entryPoint: 'seller_product' }));
2731+
* \`\`\`
2732+
*
2733+
* @example
2734+
* \`\`\`ts
2735+
* app.use(checkResourceOwnership({ entryPoint: 'service_zone' }));
2736+
* \`\`\`
2737+
*
2738+
* @param options - configuration
2739+
* @param options.entryPoint
2740+
* @param options.filterField
2741+
* @param options.paramIdField
2742+
* @param options.resourceId
2743+
*/
2744+
export const checkResourceOwnership = ({ entryPoint, filterField, paramIdField, resourceId = () => '' }) => {};
2745+
`,
2746+
},
26712747
],
26722748
valid: [
26732749
{

0 commit comments

Comments
 (0)