Skip to content

Commit c251641

Browse files
committed
feat(require-param): add checkDestructured option; fixes #530
1 parent 0aa6667 commit c251641

File tree

2 files changed

+87
-27
lines changed

2 files changed

+87
-27
lines changed

src/rules/requireParam.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import iterateJsdoc from '../iterateJsdoc';
23

34
type T = [string, () => T];
@@ -48,11 +49,14 @@ export default iterateJsdoc(({
4849
}
4950

5051
const {
52+
autoIncrementBase = 0,
53+
checkRestProperty = false,
54+
checkDestructured = true,
55+
checkTypesPattern = '/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/',
5156
enableFixer = true,
5257
enableRootFixer = true,
53-
checkRestProperty = false,
5458
enableRestElementFixer = true,
55-
checkTypesPattern = '/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/',
59+
unnamedRootBase = ['root'],
5660
} = context.options[0] || {};
5761

5862
const lastSlashPos = checkTypesPattern.lastIndexOf('/');
@@ -94,10 +98,6 @@ export default iterateJsdoc(({
9498
return paramTags.length;
9599
};
96100

97-
const {
98-
autoIncrementBase = 0,
99-
unnamedRootBase = ['root'],
100-
} = context.options[0] || {};
101101
let [nextRootName, incremented, namer] = rootNamer([...unnamedRootBase], autoIncrementBase);
102102

103103
functionParameterNames.forEach((functionParameterName, functionParameterIdx) => {
@@ -126,32 +126,39 @@ export default iterateJsdoc(({
126126
}
127127

128128
names.forEach((paramName, idx) => {
129-
if (jsdocParameterNames && !jsdocParameterNames.find(({name}) => {
129+
// Add root if the root name is not in the docs (and is not already
130+
// in the tags to be fixed)
131+
if (!jsdocParameterNames.find(({name}) => {
130132
return name === rootName;
133+
}) && !missingTags.find(({functionParameterName: fpn}) => {
134+
return fpn === rootName;
131135
})) {
132-
if (!missingTags.find(({functionParameterName: fpn}) => {
133-
return fpn === rootName;
134-
})) {
135-
const emptyParamIdx = jsdocParameterNames.findIndex(({name}) => {
136-
return !name;
137-
});
136+
const emptyParamIdx = jsdocParameterNames.findIndex(({name}) => {
137+
return !name;
138+
});
138139

139-
if (emptyParamIdx > -1) {
140-
missingTags.push({
141-
functionParameterIdx: emptyParamIdx,
142-
functionParameterName: rootName,
143-
inc,
144-
remove: true,
145-
});
146-
} else {
147-
missingTags.push({
148-
functionParameterIdx: paramIndex[rootName],
149-
functionParameterName: rootName,
150-
inc,
151-
});
152-
}
140+
if (emptyParamIdx > -1) {
141+
missingTags.push({
142+
functionParameterIdx: emptyParamIdx,
143+
functionParameterName: rootName,
144+
inc,
145+
remove: true,
146+
});
147+
} else {
148+
missingTags.push({
149+
functionParameterIdx: _.has(paramIndex, rootName) ?
150+
paramIndex[rootName] :
151+
paramIndex[paramName],
152+
functionParameterName: rootName,
153+
inc,
154+
});
153155
}
154156
}
157+
158+
if (!checkDestructured) {
159+
return;
160+
}
161+
155162
if (!checkRestProperty && rests[idx]) {
156163
return;
157164
}
@@ -249,6 +256,9 @@ export default iterateJsdoc(({
249256
default: true,
250257
type: 'boolean',
251258
},
259+
checkDestructured: {
260+
type: 'boolean',
261+
},
252262
checkGetters: {
253263
default: false,
254264
type: 'boolean',

test/rules/assertions/requireParam.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,39 @@ export default {
8484
}
8585
`,
8686
},
87+
{
88+
code: `
89+
/**
90+
* @param foo
91+
*/
92+
function quux (foo, bar, {baz}) {
93+
94+
}
95+
`,
96+
errors: [
97+
{
98+
message: 'Missing JSDoc @param "bar" declaration.',
99+
},
100+
{
101+
message: 'Missing JSDoc @param "root0" declaration.',
102+
},
103+
],
104+
options: [
105+
{
106+
checkDestructured: false,
107+
},
108+
],
109+
output: `
110+
/**
111+
* @param foo
112+
* @param bar
113+
* @param root0
114+
*/
115+
function quux (foo, bar, {baz}) {
116+
117+
}
118+
`,
119+
},
87120
{
88121
code: `
89122
/**
@@ -2696,5 +2729,22 @@ export default {
26962729
`,
26972730
parser: require.resolve('@typescript-eslint/parser'),
26982731
},
2732+
{
2733+
code: `
2734+
/**
2735+
* @param foo
2736+
* @param bar
2737+
* @param cfg
2738+
*/
2739+
function quux (foo, bar, {baz}) {
2740+
2741+
}
2742+
`,
2743+
options: [
2744+
{
2745+
checkDestructured: false,
2746+
},
2747+
],
2748+
},
26992749
],
27002750
};

0 commit comments

Comments
 (0)