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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! A lint rule for trailing commas in lists/objects.
use wdl_analysis::Diagnostics;
use wdl_analysis::VisitReason;
use wdl_analysis::Visitor;
use wdl_ast::AstNode;
use wdl_ast::Diagnostic;
use wdl_ast::Span;
use wdl_ast::SyntaxElement;
use wdl_ast::SyntaxKind;
use wdl_ast::v1::CallStatement;
use wdl_ast::v1::Expr;
use wdl_ast::v1::LiteralExpr;
use wdl_ast::v1::MetadataArray;
use crate::Rule;
use crate::Tag;
use crate::TagSet;
/// The identifier for the trailing comma rule.
const ID: &str = "TrailingComma";
/// Diagnostic message for missing trailing comma.
fn missing_trailing_comma(span: Span) -> Diagnostic {
Diagnostic::note("item missing trailing comma")
.with_rule(ID)
.with_highlight(span)
.with_fix("add a trailing comma")
}
/// Diagnostic message for extraneous content before trailing comma.
fn extraneous_content(span: Span) -> Diagnostic {
Diagnostic::note("extraneous whitespace and/or comments before trailing comma")
.with_rule(ID)
.with_highlight(span)
.with_fix("remove the extraneous content before the trailing comma")
}
/// Detects missing trailing commas.
#[derive(Default, Debug, Clone, Copy)]
pub struct TrailingCommaRule;
impl Rule for TrailingCommaRule {
fn id(&self) -> &'static str {
ID
}
fn description(&self) -> &'static str {
"Ensures that lists and objects have a trailing comma and that there's not extraneous \
whitespace and/or comments before the trailing comma."
}
fn explanation(&self) -> &'static str {
"All items in a comma-delimited object or list should be followed by a comma, including \
the last item. An exception is made for lists for which all items are on the same line, \
in which case there should not be a trailing comma following the last item. Note that \
single-line lists are not allowed in the `meta` or `parameter_meta` sections. This method \
checks `arrays` and `objects` in `meta` and `parameter_meta` sections. It also checks \
`call` input blocks as well as `Array`, `Map`, `Object`, and `Struct` literals."
}
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Style])
}
fn exceptable_nodes(&self) -> Option<&'static [SyntaxKind]> {
Some(&[
SyntaxKind::VersionStatementNode,
SyntaxKind::MetadataSectionNode,
SyntaxKind::ParameterMetadataSectionNode,
SyntaxKind::MetadataArrayNode,
SyntaxKind::MetadataObjectNode,
SyntaxKind::CallStatementNode,
SyntaxKind::LiteralStructNode,
SyntaxKind::LiteralArrayNode,
SyntaxKind::LiteralMapNode,
SyntaxKind::LiteralObjectNode,
])
}
fn related_rules(&self) -> &[&'static str] {
&[]
}
}
impl Visitor for TrailingCommaRule {
fn reset(&mut self) {
*self = Self;
}
fn metadata_object(
&mut self,
diagnostics: &mut Diagnostics,
reason: VisitReason,
item: &wdl_ast::v1::MetadataObject,
) {
if reason == VisitReason::Exit {
return;
}
// Check if object is multi-line
if item.inner().to_string().contains('\n') && item.items().count() > 1 {
let last_child = item.items().last();
if let Some(last_child) = last_child {
let (next_comma, comma_is_next) = find_next_comma(last_child.inner());
match next_comma {
Some(comma) => {
if !comma_is_next {
// Comma found, but not next, extraneous trivia
diagnostics.exceptable_add(
extraneous_content(Span::new(
last_child.inner().text_range().end().into(),
(comma.text_range().start()
- last_child.inner().text_range().end())
.into(),
)),
SyntaxElement::from(item.inner().clone()),
&self.exceptable_nodes(),
);
}
}
_ => {
// No comma found, report missing
diagnostics.exceptable_add(
missing_trailing_comma(
last_child
.inner()
.last_token()
.expect("object should have tokens")
.text_range()
.into(),
),
SyntaxElement::from(item.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
}
}
fn metadata_array(
&mut self,
diagnostics: &mut Diagnostics,
reason: VisitReason,
item: &MetadataArray,
) {
if reason == VisitReason::Exit {
return;
}
// Check if array is multi-line
if item.inner().to_string().contains('\n') && item.elements().count() > 1 {
let last_child = item.elements().last();
if let Some(last_child) = last_child {
let (next_comma, comma_is_next) = find_next_comma(last_child.inner());
match next_comma {
Some(comma) => {
if !comma_is_next {
// Comma found, but not next, extraneous trivia
diagnostics.exceptable_add(
extraneous_content(Span::new(
last_child.inner().text_range().end().into(),
(comma.text_range().start()
- last_child.inner().text_range().end())
.into(),
)),
SyntaxElement::from(item.inner().clone()),
&self.exceptable_nodes(),
);
}
}
_ => {
// No comma found, report missing
diagnostics.exceptable_add(
missing_trailing_comma(
last_child
.inner()
.last_token()
.expect("array should have tokens")
.text_range()
.into(),
),
SyntaxElement::from(item.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
}
}
fn call_statement(
&mut self,
diagnostics: &mut Diagnostics,
reason: VisitReason,
call: &CallStatement,
) {
if reason == VisitReason::Exit {
return;
}
let inputs = call.inputs().count();
if inputs < 2 {
return;
}
call.inputs().for_each(|input| {
// check each input for trailing comma
let (next_comma, comma_is_next) = find_next_comma(input.inner());
match next_comma {
Some(nc) => {
if !comma_is_next {
diagnostics.exceptable_add(
extraneous_content(Span::new(
input.inner().text_range().end().into(),
(nc.text_range().start() - input.inner().text_range().end()).into(),
)),
SyntaxElement::from(call.inner().clone()),
&self.exceptable_nodes(),
);
}
}
_ => {
diagnostics.exceptable_add(
missing_trailing_comma(
input
.inner()
.last_token()
.expect("input should have tokens")
.text_range()
.into(),
),
SyntaxElement::from(call.inner().clone()),
&self.exceptable_nodes(),
);
}
}
});
}
fn expr(&mut self, diagnostics: &mut Diagnostics, reason: VisitReason, expr: &Expr) {
if reason == VisitReason::Exit {
return;
}
if let Expr::Literal(l) = expr {
match l {
// items: map, object, struct
// elements: array
LiteralExpr::Array(_)
| LiteralExpr::Map(_)
| LiteralExpr::Object(_)
| LiteralExpr::Struct(_) => {
// Check if array is multi-line
if l.inner().to_string().contains('\n') && l.inner().children().count() > 1 {
let last_child = l.inner().children().last();
if let Some(last_child) = last_child {
let (next_comma, comma_is_next) = find_next_comma(&last_child);
match next_comma {
Some(comma) => {
if !comma_is_next {
// Comma found, but not next, extraneous trivia
diagnostics.exceptable_add(
extraneous_content(Span::new(
last_child.text_range().end().into(),
(comma.text_range().start()
- last_child.text_range().end())
.into(),
)),
SyntaxElement::from(l.inner().clone()),
&self.exceptable_nodes(),
);
}
}
_ => {
// No comma found, report missing
diagnostics.exceptable_add(
missing_trailing_comma(
last_child
.last_token()
.expect("item should have tokens")
.text_range()
.into(),
),
SyntaxElement::from(l.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
}
}
_ => {}
}
}
}
}
/// Find the next comma by consuming until we find a comma or a node.
pub(crate) fn find_next_comma(node: &wdl_ast::SyntaxNode) -> (Option<wdl_ast::SyntaxToken>, bool) {
let mut next = node.next_sibling_or_token();
let mut comma_is_next = true;
while let Some(next_node) = next {
// If we find a node before a comma, then treat as no comma
// If we find other tokens, then mark that they precede any potential comma
if next_node.as_node().is_some() {
return (None, false);
} else if next_node.kind() == wdl_ast::SyntaxKind::Comma {
return (Some(next_node.into_token().unwrap()), comma_is_next);
} else {
comma_is_next = false;
}
next = next_node.next_sibling_or_token();
}
(None, false)
}