Skip to content

Commit 614f2fb

Browse files
mrmlncgajus
authored andcommitted
feat: Add "require-param-name" rule (#50)
* feat(rules): Add "require-param-name" rule * docs(README): Generate README.md
1 parent ed1c103 commit 614f2fb

File tree

7 files changed

+173
-3
lines changed

7 files changed

+173
-3
lines changed

.README/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This table maps the rules between `eslint-plugin-jsdoc` and `jscs-jsdoc`.
2323
| [`require-hyphen-before-param-description`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-hyphen-before-param-description) | [`requireHyphenBeforeDescription`](https://github.com/jscs-dev/jscs-jsdoc#requirehyphenbeforedescription) |
2424
| [`require-param`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param) | [`checkParamExistence`](https://github.com/jscs-dev/jscs-jsdoc#checkparamexistence) |
2525
| [`require-param-description`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param-description) | [`requireParamDescription`](https://github.com/jscs-dev/jscs-jsdoc#requireparamdescription) |
26+
| [`require-param-name`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param-name) | N/A |
2627
| [`require-param-type`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param-type) | [`requireParamTypes`](https://github.com/jscs-dev/jscs-jsdoc#requireparamtypes) |
2728
| [`require-returns-description`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-returns-description) | [`requireReturnDescription`](https://github.com/jscs-dev/jscs-jsdoc#requirereturndescription) |
2829
| [`require-returns-type`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-returns-type) | [`requireReturnTypes`](https://github.com/jscs-dev/jscs-jsdoc#requirereturntypes) |
@@ -73,6 +74,7 @@ Finally, enable all of the rules that you would like to use.
7374
"jsdoc/require-hyphen-before-param-description": 1,
7475
"jsdoc/require-param": 1,
7576
"jsdoc/require-param-description": 1,
77+
"jsdoc/require-param-name": 1,
7678
"jsdoc/require-param-type": 1,
7779
"jsdoc/require-returns-description": 1,
7880
"jsdoc/require-returns-type": 1
@@ -129,6 +131,7 @@ Use `settings.jsdoc.additionalTagNames` to configure additional, allowed JSDoc t
129131
{"gitdown": "include", "file": "./rules/require-hyphen-before-param-description.md"}
130132
{"gitdown": "include", "file": "./rules/require-param.md"}
131133
{"gitdown": "include", "file": "./rules/require-param-description.md"}
134+
{"gitdown": "include", "file": "./rules/require-param-name.md"}
132135
{"gitdown": "include", "file": "./rules/require-param-type.md"}
133136
{"gitdown": "include", "file": "./rules/require-returns-description.md"}
134137
{"gitdown": "include", "file": "./rules/require-returns-type.md"}

.README/rules/require-param-name.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### `require-param-name`
2+
3+
Requires that all function parameters have name.
4+
5+
> The `@param` tag requires you to specify the name of the parameter you are documenting. You can also include the parameter's type, enclosed in curly brackets, and a description of the parameter.
6+
>
7+
> [JSDoc](http://usejsdoc.org/tags-param.html#overview)
8+
9+
|||
10+
|---|---|
11+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
12+
|Tags|`param`|
13+
14+
<!-- assertions requireParamName -->

README.md

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ JSDoc linting rules for ESLint.
2424
* [`require-hyphen-before-param-description`](#eslint-plugin-jsdoc-rules-require-hyphen-before-param-description)
2525
* [`require-param`](#eslint-plugin-jsdoc-rules-require-param)
2626
* [`require-param-description`](#eslint-plugin-jsdoc-rules-require-param-description)
27+
* [`require-param-name`](#eslint-plugin-jsdoc-rules-require-param-name)
2728
* [`require-param-type`](#eslint-plugin-jsdoc-rules-require-param-type)
2829
* [`require-returns-description`](#eslint-plugin-jsdoc-rules-require-returns-description)
2930
* [`require-returns-type`](#eslint-plugin-jsdoc-rules-require-returns-type)
@@ -45,6 +46,7 @@ This table maps the rules between `eslint-plugin-jsdoc` and `jscs-jsdoc`.
4546
| [`require-hyphen-before-param-description`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-hyphen-before-param-description) | [`requireHyphenBeforeDescription`](https://github.com/jscs-dev/jscs-jsdoc#requirehyphenbeforedescription) |
4647
| [`require-param`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param) | [`checkParamExistence`](https://github.com/jscs-dev/jscs-jsdoc#checkparamexistence) |
4748
| [`require-param-description`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param-description) | [`requireParamDescription`](https://github.com/jscs-dev/jscs-jsdoc#requireparamdescription) |
49+
| [`require-param-name`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param-name) | N/A |
4850
| [`require-param-type`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param-type) | [`requireParamTypes`](https://github.com/jscs-dev/jscs-jsdoc#requireparamtypes) |
4951
| [`require-returns-description`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-returns-description) | [`requireReturnDescription`](https://github.com/jscs-dev/jscs-jsdoc#requirereturndescription) |
5052
| [`require-returns-type`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-returns-type) | [`requireReturnTypes`](https://github.com/jscs-dev/jscs-jsdoc#requirereturntypes) |
@@ -97,6 +99,7 @@ Finally, enable all of the rules that you would like to use.
9799
"jsdoc/require-hyphen-before-param-description": 1,
98100
"jsdoc/require-param": 1,
99101
"jsdoc/require-param-description": 1,
102+
"jsdoc/require-param-name": 1,
100103
"jsdoc/require-param-type": 1,
101104
"jsdoc/require-returns-description": 1,
102105
"jsdoc/require-returns-type": 1
@@ -475,7 +478,7 @@ function quux (foo) {
475478

476479
}
477480

478-
/**
481+
/**
479482
* @abstract
480483
* @access
481484
* @alias
@@ -563,9 +566,9 @@ RegExp
563566
<a name="eslint-plugin-jsdoc-rules-check-types-why-not-capital-case-everything"></a>
564567
#### Why not capital case everything?
565568

566-
Why are `boolean`, `number` and `string` exempt from starting with a capital letter? Let's take `string` as an example. In Javascript, everything is an object. The string Object has prototypes for string functions such as `.toUpperCase()`.
569+
Why are `boolean`, `number` and `string` exempt from starting with a capital letter? Let's take `string` as an example. In Javascript, everything is an object. The string Object has prototypes for string functions such as `.toUpperCase()`.
567570

568-
Fortunately we don't have to write `new String()` everywhere in our code. Javascript will automatically wrap string primitives into string Objects when we're applying a string function to a string primitive. This way the memory footprint is a tiny little bit smaller, and the [GC](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)) has less work to do.
571+
Fortunately we don't have to write `new String()` everywhere in our code. Javascript will automatically wrap string primitives into string Objects when we're applying a string function to a string primitive. This way the memory footprint is a tiny little bit smaller, and the [GC](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)) has less work to do.
569572

570573
So in a sense, there two types of strings in Javascript; `{string}` literals, also called primitives and `{String}` Objects. We use the primitives because it's easier to write and uses less memory. `{String}` and `{string}` are technically both valid, but they are not the same.
571574

@@ -756,6 +759,14 @@ function quux () {
756759
}
757760
// Message: Paragraph must start with an uppercase character.
758761

762+
/**
763+
* тест.
764+
*/
765+
function quux () {
766+
767+
}
768+
// Message: Description must start with an uppercase character.
769+
759770
/**
760771
* Foo
761772
*/
@@ -828,6 +839,13 @@ function quux () {
828839

829840
}
830841

842+
/**
843+
* Тест.
844+
*/
845+
function quux () {
846+
847+
}
848+
831849
/**
832850
* Foo
833851
* bar.
@@ -1057,6 +1075,59 @@ function quux (foo) {
10571075
```
10581076

10591077

1078+
<a name="eslint-plugin-jsdoc-rules-require-param-name"></a>
1079+
### <code>require-param-name</code>
1080+
1081+
Requires that all function parameters have name.
1082+
1083+
> The `@param` tag requires you to specify the name of the parameter you are documenting. You can also include the parameter's type, enclosed in curly brackets, and a description of the parameter.
1084+
>
1085+
> [JSDoc](http://usejsdoc.org/tags-param.html#overview)
1086+
1087+
|||
1088+
|---|---|
1089+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
1090+
|Tags|`param`|
1091+
1092+
The following patterns are considered problems:
1093+
1094+
```js
1095+
/**
1096+
* @param
1097+
*/
1098+
function quux (foo) {
1099+
1100+
}
1101+
// Message: There must be an identifier after @param type.
1102+
1103+
/**
1104+
* @param {string}
1105+
*/
1106+
function quux (foo) {
1107+
1108+
}
1109+
// Message: There must be an identifier after @param tag.
1110+
```
1111+
1112+
The following patterns are not considered problems:
1113+
1114+
```js
1115+
/**
1116+
* @param foo
1117+
*/
1118+
function quux (foo) {
1119+
1120+
}
1121+
1122+
/**
1123+
* @param {string} foo
1124+
*/
1125+
function quux (foo) {
1126+
1127+
}
1128+
```
1129+
1130+
10601131
<a name="eslint-plugin-jsdoc-rules-require-param-type"></a>
10611132
### <code>require-param-type</code>
10621133

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import newlineAfterDescription from './rules/newlineAfterDescription';
55
import requireDescriptionCompleteSentence from './rules/requireDescriptionCompleteSentence';
66
import requireExample from './rules/requireExample';
77
import requireHyphenBeforeParamDescription from './rules/requireHyphenBeforeParamDescription';
8+
import requireParamName from './rules/requireParamName';
89
import requireParam from './rules/requireParam';
910
import requireParamDescription from './rules/requireParamDescription';
1011
import requireParamType from './rules/requireParamType';
@@ -22,6 +23,7 @@ export default {
2223
'require-hyphen-before-param-description': requireHyphenBeforeParamDescription,
2324
'require-param': requireParam,
2425
'require-param-description': requireParamDescription,
26+
'require-param-name': requireParamName,
2527
'require-param-type': requireParamType,
2628
'require-returns-description': requireReturnsDescription,
2729
'require-returns-type': requireReturnsType
@@ -36,6 +38,7 @@ export default {
3638
'require-hyphen-before-param-description': 0,
3739
'require-param': 0,
3840
'require-param-description': 0,
41+
'require-param-name': 0,
3942
'require-param-type': 0,
4043
'require-returns-description': 0,
4144
'require-returns-type': 0

src/rules/requireParamName.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import _ from 'lodash';
2+
import iterateJsdoc from '../iterateJsdoc';
3+
4+
export default iterateJsdoc(({
5+
jsdoc,
6+
report,
7+
utils
8+
}) => {
9+
const targetTagName = utils.getPreferredTagName('param');
10+
11+
const jsdocParameters = _.filter(jsdoc.tags, {
12+
tag: targetTagName
13+
});
14+
15+
_.forEach(jsdocParameters, (jsdocParameter) => {
16+
if (jsdocParameter.tag && jsdocParameter.name === '') {
17+
report('There must be an identifier after @param ' + (jsdocParameter.type === '' ? 'type' : 'tag') + '.');
18+
}
19+
});
20+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-disable no-restricted-syntax */
2+
3+
export default {
4+
invalid: [
5+
{
6+
code: `
7+
/**
8+
* @param
9+
*/
10+
function quux (foo) {
11+
12+
}
13+
`,
14+
errors: [
15+
{
16+
message: 'There must be an identifier after @param type.'
17+
}
18+
]
19+
},
20+
{
21+
code: `
22+
/**
23+
* @param {string}
24+
*/
25+
function quux (foo) {
26+
27+
}
28+
`,
29+
errors: [
30+
{
31+
message: 'There must be an identifier after @param tag.'
32+
}
33+
]
34+
}
35+
],
36+
valid: [
37+
{
38+
code: `
39+
/**
40+
* @param foo
41+
*/
42+
function quux (foo) {
43+
44+
}
45+
`
46+
},
47+
{
48+
code: `
49+
/**
50+
* @param {string} foo
51+
*/
52+
function quux (foo) {
53+
54+
}
55+
`
56+
}
57+
]
58+
};

test/rules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ _.forEach([
1616
'require-hyphen-before-param-description',
1717
'require-param',
1818
'require-param-description',
19+
'require-param-name',
1920
'require-param-type',
2021
'require-returns-description',
2122
'require-returns-type'

0 commit comments

Comments
 (0)