Skip to content

Commit

Permalink
feat(check-template-names): check callback/function tag blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Aug 2, 2024
1 parent 12fca71 commit 058018b
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .README/rules/check-template-names.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# `check-template-names`

Checks that any `@template` names are actually used in the connected
`@typedef` or type alias.
`@typedef`, `@callback`, `@function` or type structure.

Currently checks `ClassDeclaration`, `FunctionDeclaration`,
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:
Expand Down
47 changes: 46 additions & 1 deletion docs/rules/check-template-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# <code>check-template-names</code>

Checks that any `@template` names are actually used in the connected
`@typedef` or type alias.
`@typedef`, `@callback`, `@function` or type structure.

Currently checks `ClassDeclaration`, `FunctionDeclaration`,
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:
Expand Down Expand Up @@ -190,6 +190,30 @@ export default class <NumType> {
add: (x: NumType, y: NumType) => NumType;
}
// Message: @template D not in use

/**
* @template D
* @template V
* @callback
* @returns {[X, Y | undefined]}
*/
// Message: @template D not in use

/**
* @template D
* @template V
* @function
* @returns {[X, Y | undefined]}
*/
// Message: @template D not in use

/**
* @template D
* @template V
* @function
* @param {[X, Y | undefined]} someParam
*/
// Message: @template D not in use
````


Expand Down Expand Up @@ -327,5 +351,26 @@ export default class <NumType> {
*/
export function mapGroupBy(array, callbackFn) {
}

/**
* @template D
* @template V
* @callback
* @returns {[D, V | undefined]}
*/

/**
* @template D
* @template V
* @function
* @returns {[D, V | undefined]}
*/

/**
* @template D
* @template V
* @function
* @param {[D, V | undefined]} someParam
*/
````

65 changes: 41 additions & 24 deletions src/rules/checkTemplateNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ export default iterateJsdoc(({
});
};

const checkParamsAndReturnsTags = () => {
const paramName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'param',
}));
const paramTags = utils.getTags(paramName);
for (const paramTag of paramTags) {
checkForUsedTypes(paramTag.type);
}

const returnsName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'returns',
}));
const returnsTags = utils.getTags(returnsName);
for (const returnsTag of returnsTags) {
checkForUsedTypes(returnsTag.type);
}
};

const checkTemplateTags = () => {
for (const tag of templateTags) {
const {name} = tag;
const names = name.split(/,\s*/);
for (const name of names) {
if (!usedNames.has(name)) {
report(`@template ${name} not in use`, null, tag);
}
}
}
};

/**
* @param {import('@typescript-eslint/types').TSESTree.FunctionDeclaration|
* import('@typescript-eslint/types').TSESTree.ClassDeclaration|
Expand All @@ -57,31 +87,10 @@ export default iterateJsdoc(({
usedNames.add(name);
}
if (checkParamsAndReturns) {
const paramName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'param',
}));
const paramTags = utils.getTags(paramName);
for (const paramTag of paramTags) {
checkForUsedTypes(paramTag.type);
}

const returnsName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'returns',
}));
const returnsTags = utils.getTags(returnsName);
for (const returnsTag of returnsTags) {
checkForUsedTypes(returnsTag.type);
}
}
for (const tag of templateTags) {
const {name} = tag;
const names = name.split(/,\s*/);
for (const name of names) {
if (!usedNames.has(name)) {
report(`@template ${name} not in use`, null, tag);
}
}
checkParamsAndReturnsTags();
}

checkTemplateTags();
};

const handleTypeAliases = () => {
Expand Down Expand Up @@ -116,6 +125,14 @@ export default iterateJsdoc(({
}
};

const callbackTags = utils.getTags('callback');
const functionTags = utils.getTags('function');
if (callbackTags.length || functionTags.length) {
checkParamsAndReturnsTags();
checkTemplateTags();
return;
}

const typedefTags = utils.getTags('typedef');
if (!typedefTags.length || typedefTags.length >= 2) {
handleTypeAliases();
Expand Down
90 changes: 90 additions & 0 deletions test/rules/assertions/checkTemplateNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,66 @@ export default {
parser: typescriptEslintParser
},
},
{
code: `
/**
* @template D
* @template V
* @callback
* @returns {[X, Y | undefined]}
*/
`,
errors: [
{
line: 3,
message: '@template D not in use',
},
{
line: 4,
message: '@template V not in use',
},
],
},
{
code: `
/**
* @template D
* @template V
* @function
* @returns {[X, Y | undefined]}
*/
`,
errors: [
{
line: 3,
message: '@template D not in use',
},
{
line: 4,
message: '@template V not in use',
},
],
},
{
code: `
/**
* @template D
* @template V
* @function
* @param {[X, Y | undefined]} someParam
*/
`,
errors: [
{
line: 3,
message: '@template D not in use',
},
{
line: 4,
message: '@template V not in use',
},
],
},
],
valid: [
{
Expand Down Expand Up @@ -624,5 +684,35 @@ export default {
parser: typescriptEslintParser
},
},
{
code: `
/**
* @template D
* @template V
* @callback
* @returns {[D, V | undefined]}
*/
`,
},
{
code: `
/**
* @template D
* @template V
* @function
* @returns {[D, V | undefined]}
*/
`,
},
{
code: `
/**
* @template D
* @template V
* @function
* @param {[D, V | undefined]} someParam
*/
`,
},
],
};

0 comments on commit 058018b

Please sign in to comment.