Skip to content

- Enhancement: Rule to prevent types on @param/@returns #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Finally, enable all of the rules that you would like to use.
"jsdoc/implements-on-classes": 1,
"jsdoc/match-description": 1,
"jsdoc/newline-after-description": 1,
"jsdoc/no-types": 1,
"jsdoc/no-undefined-types": 1,
"jsdoc/require-description": 1,
"jsdoc/require-description-complete-sentence": 1,
Expand Down Expand Up @@ -316,6 +317,7 @@ Finally, the following rule pertains to inline disable directives:
{"gitdown": "include", "file": "./rules/implements-on-classes.md"}
{"gitdown": "include", "file": "./rules/match-description.md"}
{"gitdown": "include", "file": "./rules/newline-after-description.md"}
{"gitdown": "include", "file": "./rules/no-types.md"}
{"gitdown": "include", "file": "./rules/no-undefined-types.md"}
{"gitdown": "include", "file": "./rules/require-description-complete-sentence.md"}
{"gitdown": "include", "file": "./rules/require-description.md"}
Expand Down
14 changes: 14 additions & 0 deletions .README/rules/no-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### `no-types`

This rule reports types being used on `@param` or `@returns`.

The rule is intended to prevent the indication of types on tags where
the type information would be redundant with TypeScript.

|||
|---|---|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
|Tags|`param`, `returns`|
|Aliases|`arg`, `argument`, `return`|

<!-- assertions noTypes -->
2 changes: 1 addition & 1 deletion .README/rules/valid-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Also impacts behaviors on namepath (or event)-defining and pointing tags:
1. Name(path)-pointing tags (which may have value without namepath): `@listens`, `@fires`, `@emits`
1. Name(path)-pointing tags (multiple names in one): `@borrows`

The following apply to the above sets:
...with the following applying to the above sets:

- Expect tags in set 1-4 to have a valid namepath if present
- Prevent sets 3-4 from being empty by setting `allowEmptyNamepaths` to `false` as these tags might have some indicative value without a path (but sets 1-2 will always fail if empty)
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import checkTypes from './rules/checkTypes';
import implementsOnClasses from './rules/implementsOnClasses';
import matchDescription from './rules/matchDescription';
import newlineAfterDescription from './rules/newlineAfterDescription';
import noTypes from './rules/noTypes';
import noUndefinedTypes from './rules/noUndefinedTypes';
import requireDescriptionCompleteSentence from './rules/requireDescriptionCompleteSentence';
import requireDescription from './rules/requireDescription';
Expand Down Expand Up @@ -39,6 +40,7 @@ export default {
'jsdoc/implements-on-classes': 'warn',
'jsdoc/match-description': 'off',
'jsdoc/newline-after-description': 'warn',
'jsdoc/no-types': 'off',
'jsdoc/no-undefined-types': 'warn',
'jsdoc/require-description': 'off',
'jsdoc/require-description-complete-sentence': 'off',
Expand Down Expand Up @@ -68,6 +70,7 @@ export default {
'implements-on-classes': implementsOnClasses,
'match-description': matchDescription,
'newline-after-description': newlineAfterDescription,
'no-types': noTypes,
'no-undefined-types': noUndefinedTypes,
'require-description': requireDescription,
'require-description-complete-sentence': requireDescriptionCompleteSentence,
Expand Down
15 changes: 15 additions & 0 deletions src/rules/noTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import iterateJsdoc from '../iterateJsdoc';

export default iterateJsdoc(({
jsdoc,
report
}) => {
const tags = jsdoc.tags.filter((tag) => {
return ['param', 'arg', 'argument', 'returns', 'return'].includes(tag.tag);
});
tags.forEach((tag) => {
if (tag.type) {
report(`Types are not permitted on @${tag.tag}.`, null, tag);
}
});
});
46 changes: 46 additions & 0 deletions test/rules/assertions/noTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export default {
invalid: [
{
code: `
/**
* @param {number} foo
*/
function quux (foo) {

}
`,
errors: [
{
message: 'Types are not permitted on @param.'
}
]
},
{
code: `
/**
* @returns {number}
*/
function quux () {

}
`,
errors: [
{
message: 'Types are not permitted on @returns.'
}
]
}
],
valid: [
{
code: `
/**
* @param foo
*/
function quux (foo) {

}
`
}
]
};
1 change: 1 addition & 0 deletions test/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ruleTester = new RuleTester();
'implements-on-classes',
'match-description',
'newline-after-description',
'no-types',
'no-undefined-types',
'require-description',
'require-description-complete-sentence',
Expand Down