Skip to content

Commit

Permalink
fix(check-param-names): proper error messages for rest elements; fixes
Browse files Browse the repository at this point in the history
 #1225

Also:
- chore: update pnpm action setup
  • Loading branch information
brettz9 committed Jul 5, 2024
1 parent 14dc1bd commit 6371c4a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/feature.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
runs-on: ubuntu-latest
name: Lint
steps:
- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4
with:
version: latest
- name: setup repository
Expand All @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
name: Test (Node.js ${{ matrix.node_js_version }})
steps:
- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4
with:
version: latest
- name: setup repository
Expand All @@ -46,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
name: Build
steps:
- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4
with:
version: latest
- name: setup repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
environment: release
name: Release
steps:
- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4
with:
version: latest
- name: setup repository
Expand Down
5 changes: 4 additions & 1 deletion src/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,10 @@ const getFunctionParameterNames = (
name: /** @type {import('@typescript-eslint/types').TSESTree.Identifier} */ (
/** @type {import('@typescript-eslint/types').TSESTree.RestElement} */ (
param
).argument).name,
// @ts-expect-error Ok
).argument).name ?? param?.argument?.elements?.map(({name}) => {
return name;
}),
restElement: true,
};
}
Expand Down
30 changes: 29 additions & 1 deletion src/rules/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ const validateParameterNames = (

return true;
}
if (
typeof functionParameterName === 'object' &&
'name' in functionParameterName &&
Array.isArray(functionParameterName.name)
) {
const actualName = tag.name.trim();
const expectedName = functionParameterName.name[index];
if (actualName === expectedName) {
thisOffset--;
return false;
}
report(
`Expected @${targetTagName} name to be "${expectedName}". Got "${actualName}".`,
null,
tag,
);
return true;
}

if (Array.isArray(functionParameterName)) {
if (!checkDestructured) {
Expand Down Expand Up @@ -236,6 +254,7 @@ const validateParameterNames = (
]) => {
return name.trim();
});

const expectedNames = functionParameterNames.map((item, idx) => {
if (/**
* @type {[string|undefined, (import('../jsdocUtils.js').FlattendRootInfo & {
Expand All @@ -260,7 +279,15 @@ const validateParameterNames = (
}

report(
`Expected @${targetTagName} names to be "${expectedNames.join(', ')}". Got "${actualNames.join(', ')}".`,
`Expected @${targetTagName} names to be "${
expectedNames.map((expectedName) => {
return typeof expectedName === 'object' &&
'name' in expectedName &&
expectedName.restElement
? '...' + expectedName.name
: expectedName;
}).join(', ')
}". Got "${actualNames.join(', ')}".`,
null,
tag,
);
Expand Down Expand Up @@ -352,6 +379,7 @@ export default iterateJsdoc(({
}

const functionParameterNames = utils.getFunctionParameterNames(useDefaultObjectProperties);

const targetTagName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'param',
}));
Expand Down
56 changes: 55 additions & 1 deletion test/rules/assertions/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,46 @@ export default {
parser: typescriptEslintParser,
sourceType: 'module',
},
}
},
{
code: `
interface A {
/**
* @param params Values for the placeholders
*/
getText(key: string, ...params: string[]): string
}
`,
errors: [
{
line: 4,
message: 'Expected @param names to be "key, ...params". Got "params".',
},
],
languageOptions: {
parser: typescriptEslintParser,
},
},
{
code: `
/**
* @param arg Arg
*/
export function fn(...[type, arg]: FnArgs): void {
// ...
}
`,
errors: [
{
line: 3,
message: 'Expected @param name to be "type". Got "arg".',
},
],
languageOptions: {
parser: typescriptEslintParser,
sourceType: 'module',
},
},
],
valid: [
{
Expand Down Expand Up @@ -1961,5 +2000,20 @@ export default {
},
],
},
{
code: `
/**
* @param type Type
* @param arg Arg
*/
export function fn(...[type, arg]: FnArgs): void {
// ...
}
`,
languageOptions: {
parser: typescriptEslintParser,
sourceType: 'module',
},
},
],
};

0 comments on commit 6371c4a

Please sign in to comment.