Skip to content

Commit

Permalink
fix(check-param-names): check against whitelist of acceptable funct…
Browse files Browse the repository at this point in the history
…ion nodes so that non-function global contexts do not err; fixes #1303

Also adds warning in docs for use of global settings contexts.
  • Loading branch information
brettz9 committed Aug 14, 2024
1 parent b039833 commit 288f0ae
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 9 deletions.
8 changes: 6 additions & 2 deletions .README/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,9 @@ values are objects with the following optional properties:
### `contexts`

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

See the "AST and Selectors" section for more on this format.
10 changes: 10 additions & 0 deletions docs/rules/check-param-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -1119,5 +1119,15 @@ function quux (foo, bar) {
export function fn(...[type, arg]: FnArgs): void {
// ...
}

/**
* @param c c
* @param d d
*/
const inner = (c: number, d: string): void => {
console.log(c);
console.log(d);
};
// Settings: {"jsdoc":{"contexts":["VariableDeclaration"]}}
````

9 changes: 9 additions & 0 deletions docs/rules/require-param.md
Original file line number Diff line number Diff line change
Expand Up @@ -1804,5 +1804,14 @@ function a (b) {}
function sumDestructure(this: unknown, { a, b }: { a: number, b: number }) {
return a + b;
}

/**
*
*/
const inner = (c: number, d: string): void => {
console.log(c);
console.log(d);
};
// Settings: {"jsdoc":{"contexts":["VariableDeclaration"]}}
````

8 changes: 6 additions & 2 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,9 @@ values are objects with the following optional properties:
### <code>contexts</code>

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

See the "AST and Selectors" section for more on this format.
22 changes: 17 additions & 5 deletions src/rules/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,18 @@ const validateParameterNamesDeep = (
});
};

const allowedNodes = [
'ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'TSDeclareFunction',
// Add this to above defaults
'TSMethodSignature'
];

export default iterateJsdoc(({
context,
jsdoc,
report,
utils,
node,
}) => {
const {
allowExtraTrailingParamDocs,
Expand All @@ -371,6 +378,15 @@ export default iterateJsdoc(({
disableMissingParamChecks = false,
} = context.options[0] || {};

// Although we might just remove global settings contexts from applying to
// this rule (as they can cause problems with `getFunctionParameterNames`
// checks if they are not functions but say variables), the user may
// instead wish to narrow contexts in those settings, so this check
// is still useful
if (!allowedNodes.includes(/** @type {import('estree').Node} */ (node).type)) {
return;
}

const checkTypesRegex = utils.getRegexFromString(checkTypesPattern);

const jsdocParameterNamesDeep = utils.getJsdocTagsDeep('param');
Expand Down Expand Up @@ -406,11 +422,7 @@ export default iterateJsdoc(({
targetTagName, allowExtraTrailingParamDocs, jsdocParameterNamesDeep, jsdoc, report,
);
}, {
contextDefaults: [
'ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'TSDeclareFunction',
// Add this to above defaults
'TSMethodSignature'
],
contextDefaults: allowedNodes,
meta: {
docs: {
description: 'Ensures that parameter names in JSDoc match those in the function declaration.',
Expand Down
23 changes: 23 additions & 0 deletions test/rules/assertions/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -2015,5 +2015,28 @@ export default {
sourceType: 'module',
},
},
{
code: `
/**
* @param c c
* @param d d
*/
const inner = (c: number, d: string): void => {
console.log(c);
console.log(d);
};
`,
languageOptions: {
parser: typescriptEslintParser,
sourceType: 'module',
},
settings: {
jsdoc: {
contexts: [
'VariableDeclaration',
]
}
},
},
],
};
22 changes: 22 additions & 0 deletions test/rules/assertions/requireParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -3582,5 +3582,27 @@ export default {
parser: typescriptEslintParser,
},
},
{
code: `
/**
*
*/
const inner = (c: number, d: string): void => {
console.log(c);
console.log(d);
};
`,
languageOptions: {
parser: typescriptEslintParser,
sourceType: 'module',
},
settings: {
jsdoc: {
contexts: [
'VariableDeclaration',
]
}
},
},
],
};

0 comments on commit 288f0ae

Please sign in to comment.