Skip to content

Commit 288f0ae

Browse files
committedAug 14, 2024·
fix(check-param-names): check against whitelist of acceptable function nodes so that non-function global contexts do not err; fixes #1303
Also adds warning in docs for use of global settings contexts.
1 parent b039833 commit 288f0ae

File tree

7 files changed

+93
-9
lines changed

7 files changed

+93
-9
lines changed
 

‎.README/settings.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,9 @@ values are objects with the following optional properties:
329329
### `contexts`
330330

331331
`settings.jsdoc.contexts` can be used as the default for any rules
332-
with a `contexts` property option. See the "AST and Selectors" section
333-
for more on this format.
332+
with a `contexts` property option. **Please note**: This will replace any
333+
default contexts, including for function rules, so if, for example, you exclude
334+
`FunctionDeclaration` here, rules like `require-param` will not check
335+
function declarations.
336+
337+
See the "AST and Selectors" section for more on this format.

‎docs/rules/check-param-names.md

+10
Original file line numberDiff line numberDiff line change
@@ -1119,5 +1119,15 @@ function quux (foo, bar) {
11191119
export function fn(...[type, arg]: FnArgs): void {
11201120
// ...
11211121
}
1122+
1123+
/**
1124+
* @param c c
1125+
* @param d d
1126+
*/
1127+
const inner = (c: number, d: string): void => {
1128+
console.log(c);
1129+
console.log(d);
1130+
};
1131+
// Settings: {"jsdoc":{"contexts":["VariableDeclaration"]}}
11221132
````
11231133

‎docs/rules/require-param.md

+9
Original file line numberDiff line numberDiff line change
@@ -1804,5 +1804,14 @@ function a (b) {}
18041804
function sumDestructure(this: unknown, { a, b }: { a: number, b: number }) {
18051805
return a + b;
18061806
}
1807+
1808+
/**
1809+
*
1810+
*/
1811+
const inner = (c: number, d: string): void => {
1812+
console.log(c);
1813+
console.log(d);
1814+
};
1815+
// Settings: {"jsdoc":{"contexts":["VariableDeclaration"]}}
18071816
````
18081817

‎docs/settings.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -358,5 +358,9 @@ values are objects with the following optional properties:
358358
### <code>contexts</code>
359359

360360
`settings.jsdoc.contexts` can be used as the default for any rules
361-
with a `contexts` property option. See the "AST and Selectors" section
362-
for more on this format.
361+
with a `contexts` property option. **Please note**: This will replace any
362+
default contexts, including for function rules, so if, for example, you exclude
363+
`FunctionDeclaration` here, rules like `require-param` will not check
364+
function declarations.
365+
366+
See the "AST and Selectors" section for more on this format.

‎src/rules/checkParamNames.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,18 @@ const validateParameterNamesDeep = (
354354
});
355355
};
356356

357+
const allowedNodes = [
358+
'ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'TSDeclareFunction',
359+
// Add this to above defaults
360+
'TSMethodSignature'
361+
];
362+
357363
export default iterateJsdoc(({
358364
context,
359365
jsdoc,
360366
report,
361367
utils,
368+
node,
362369
}) => {
363370
const {
364371
allowExtraTrailingParamDocs,
@@ -371,6 +378,15 @@ export default iterateJsdoc(({
371378
disableMissingParamChecks = false,
372379
} = context.options[0] || {};
373380

381+
// Although we might just remove global settings contexts from applying to
382+
// this rule (as they can cause problems with `getFunctionParameterNames`
383+
// checks if they are not functions but say variables), the user may
384+
// instead wish to narrow contexts in those settings, so this check
385+
// is still useful
386+
if (!allowedNodes.includes(/** @type {import('estree').Node} */ (node).type)) {
387+
return;
388+
}
389+
374390
const checkTypesRegex = utils.getRegexFromString(checkTypesPattern);
375391

376392
const jsdocParameterNamesDeep = utils.getJsdocTagsDeep('param');
@@ -406,11 +422,7 @@ export default iterateJsdoc(({
406422
targetTagName, allowExtraTrailingParamDocs, jsdocParameterNamesDeep, jsdoc, report,
407423
);
408424
}, {
409-
contextDefaults: [
410-
'ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'TSDeclareFunction',
411-
// Add this to above defaults
412-
'TSMethodSignature'
413-
],
425+
contextDefaults: allowedNodes,
414426
meta: {
415427
docs: {
416428
description: 'Ensures that parameter names in JSDoc match those in the function declaration.',

‎test/rules/assertions/checkParamNames.js

+23
Original file line numberDiff line numberDiff line change
@@ -2015,5 +2015,28 @@ export default {
20152015
sourceType: 'module',
20162016
},
20172017
},
2018+
{
2019+
code: `
2020+
/**
2021+
* @param c c
2022+
* @param d d
2023+
*/
2024+
const inner = (c: number, d: string): void => {
2025+
console.log(c);
2026+
console.log(d);
2027+
};
2028+
`,
2029+
languageOptions: {
2030+
parser: typescriptEslintParser,
2031+
sourceType: 'module',
2032+
},
2033+
settings: {
2034+
jsdoc: {
2035+
contexts: [
2036+
'VariableDeclaration',
2037+
]
2038+
}
2039+
},
2040+
},
20182041
],
20192042
};

‎test/rules/assertions/requireParam.js

+22
Original file line numberDiff line numberDiff line change
@@ -3582,5 +3582,27 @@ export default {
35823582
parser: typescriptEslintParser,
35833583
},
35843584
},
3585+
{
3586+
code: `
3587+
/**
3588+
*
3589+
*/
3590+
const inner = (c: number, d: string): void => {
3591+
console.log(c);
3592+
console.log(d);
3593+
};
3594+
`,
3595+
languageOptions: {
3596+
parser: typescriptEslintParser,
3597+
sourceType: 'module',
3598+
},
3599+
settings: {
3600+
jsdoc: {
3601+
contexts: [
3602+
'VariableDeclaration',
3603+
]
3604+
}
3605+
},
3606+
},
35853607
],
35863608
};

0 commit comments

Comments
 (0)
Please sign in to comment.