-
-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathrequire_param_description.rs
278 lines (253 loc) · 7.36 KB
/
require_param_description.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
context::LintContext,
rule::Rule,
utils::{
collect_params, get_function_nearest_jsdoc_node, should_ignore_as_internal,
should_ignore_as_private, ParamKind,
},
AstNode,
};
fn missing_type_diagnostic(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"eslint-plugin-jsdoc(require-param-description): Missing JSDoc `@param` description.",
)
.with_help("Add description to `@param` tag.")
.with_labels([span0.into()])
}
#[derive(Debug, Default, Clone)]
pub struct RequireParamDescription;
declare_oxc_lint!(
/// ### What it does
/// Requires that each `@param` tag has a description value.
///
/// ### Why is this bad?
/// The description of a param should be documented.
///
/// ### Example
/// ```javascript
/// // Passing
/// /** @param foo Foo. */
/// function quux (foo) {}
///
/// // Failing
/// /** @param foo */
/// function quux (foo) {}
/// ```
RequireParamDescription,
pedantic,
);
impl Rule for RequireParamDescription {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
// Collected targets from `FormalParameters`
let params_to_check = match node.kind() {
AstKind::Function(func) if !func.is_typescript_syntax() => collect_params(&func.params),
AstKind::ArrowFunctionExpression(arrow_func) => collect_params(&arrow_func.params),
// If not a function, skip
_ => return,
};
// If no JSDoc is found, skip
let Some(jsdocs) = get_function_nearest_jsdoc_node(node, ctx)
.and_then(|node| ctx.jsdoc().get_all_by_node(node))
else {
return;
};
let settings = &ctx.settings().jsdoc;
let resolved_param_tag_name = settings.resolve_tag_name("param");
let mut root_count = 0;
for jsdoc in jsdocs
.iter()
.filter(|jsdoc| !should_ignore_as_internal(jsdoc, settings))
.filter(|jsdoc| !should_ignore_as_private(jsdoc, settings))
{
for tag in jsdoc.tags() {
if tag.kind.parsed() != resolved_param_tag_name {
continue;
}
let (_, name_part, comment_part) = tag.type_name_comment();
if name_part.is_some_and(|name_part| !name_part.parsed().contains('.')) {
root_count += 1;
}
if settings.exempt_destructured_roots_from_checks {
// -1 for count to idx conversion
if let Some(ParamKind::Nested(_)) = params_to_check.get(root_count - 1) {
continue;
}
}
// If description exists, skip
if !comment_part.parsed().is_empty() {
continue;
};
ctx.diagnostic(missing_type_diagnostic(tag.kind.span));
}
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
(
"
/**
*
*/
function quux (foo) {
}
",
None,
None,
),
(
"
/**
* @param foo Foo.
*/
function quux (foo) {
}
",
None,
None,
),
(
"
/**
* @function
* @param foo
*/
",
None,
None,
),
(
"
/**
* @callback
* @param foo
*/
",
None,
None,
),
(
"
/**
* Checks if the XML document sort of equals another XML document.
* @param {Object} obj The other object.
* @param {{includeWhiteSpace: (boolean|undefined),
* ignoreElementOrder: (boolean|undefined)}} [options] The options.
* @return {expect.Assertion} The assertion.
*/
expect.Assertion.prototype.xmleql = function (obj, options) {
}
",
None,
None,
),
(
"
/**
* @param {number} foo Foo description
* @param {object} root
* @param {boolean} baz Baz description
*/
function quux (foo, {bar}, baz) {
}
",
None,
Some(
serde_json::json!({ "settings": { "jsdoc": { "exemptDestructuredRootsFromChecks": true, }, } }),
),
),
(
"
/**
* @param {number} foo Foo description
* @param {object} root
* @param {object} root.bar
*/
function quux (foo, {bar: {baz}}) {
}
",
None,
Some(
serde_json::json!({ "settings": { "jsdoc": { "exemptDestructuredRootsFromChecks": true, }, } }),
),
),
];
let fail = vec![
(
"
/**
* @param foo
*/
function quux (foo) {
}
",
None,
None,
),
(
"
/**
* @arg foo
*/
function quux (foo) {
}
",
None,
Some(
serde_json::json!({ "settings": { "jsdoc": { "tagNamePreference": { "param": "arg", }, }, } }),
),
),
(
"
/**
* @param {number} foo Foo description
* @param {object} root
* @param {boolean} baz Baz description
*/
function quux (foo, {bar}, baz) {
}
",
Some(
serde_json::json!([ { "setDefaultDestructuredRootDescription": true, }, ]),
),
None,
),
(
"
/**
* @param {number} foo Foo description
* @param {object} root
* @param {boolean} baz Baz description
*/
function quux (foo, {bar}, baz) {
}
",
Some(
serde_json::json!([ { "defaultDestructuredRootDescription": "Root description", "setDefaultDestructuredRootDescription": true, }, ]),
),
None,
),
(
"
/**
* @param {number} foo Foo description
* @param {object} root
* @param {boolean} baz Baz description
*/
function quux (foo, {bar}, baz) {
}
",
Some(
serde_json::json!([ { "setDefaultDestructuredRootDescription": false, }, ]),
),
None,
),
];
Tester::new(RequireParamDescription::NAME, pass, fail).test_and_snapshot();
}