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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
//! A lint rule for spacing of expressions.
use rowan::Direction;
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::Expr;
use wdl_ast::v1::LiteralExpr;
use crate::Rule;
use crate::Tag;
use crate::TagSet;
/// The identifier for the expression spacing rule.
const ID: &str = "ExpressionSpacing";
/// Reports disallowed whitespace after prefix operators.
fn prefix_whitespace(span: Span) -> Diagnostic {
Diagnostic::note("prefix operators may not contain whitespace")
.with_rule(ID)
.with_highlight(span)
.with_fix("remove the whitespace")
}
/// Reports missing following whitespace around operators
fn missing_surrounding_whitespace(span: Span) -> Diagnostic {
Diagnostic::note("operators must be surrounded by whitespace")
.with_rule(ID)
.with_highlight(span)
.with_fix("add whitespace around this operator")
}
/// Reports missing preceding whitespace around operators
fn missing_preceding_whitespace(span: Span) -> Diagnostic {
Diagnostic::note("operators must be preceded by whitespace")
.with_rule(ID)
.with_highlight(span)
.with_fix("add whitespace before this operator")
}
/// Reports missing following whitespace around operators
fn missing_following_whitespace(span: Span) -> Diagnostic {
Diagnostic::note("operators must be followed by whitespace")
.with_rule(ID)
.with_highlight(span)
.with_fix("add whitespace after this operator")
}
/// Report disallowed space
fn disallowed_space(span: Span) -> Diagnostic {
Diagnostic::note("this space is not allowed")
.with_rule(ID)
.with_highlight(span)
.with_fix("remove the space")
}
/// Reports missing preceding whitespace around assignments
fn assignment_missing_preceding_whitespace(span: Span) -> Diagnostic {
Diagnostic::note("assignments must be preceded by whitespace")
.with_rule(ID)
.with_highlight(span)
.with_fix("add whitespace before this assignment")
}
/// Reports missing following whitespace around assignments
fn assignment_missing_following_whitespace(span: Span) -> Diagnostic {
Diagnostic::note("assignments must be followed by whitespace")
.with_rule(ID)
.with_highlight(span)
.with_fix("add whitespace after this assignment")
}
/// Reports missing surrounding whitespace around assignments
fn assignment_missing_surrounding_whitespace(span: Span) -> Diagnostic {
Diagnostic::note("assignments must be surrounded by whitespace")
.with_rule(ID)
.with_highlight(span)
.with_fix("add whitespace around this assignment")
}
/// Reports missing open paren for multiline if...then...else constructs
fn multiline_if_open_paren(span: Span) -> Diagnostic {
Diagnostic::note("multi-line if...then...else must have a preceding parenthesis and newline")
.with_rule(ID)
.with_highlight(span)
.with_fix("add a open parenthesis and newline prior to this if")
}
/// Reports missing newline prior to then in multiline if...then...else
/// constructs
fn multiline_then_space(span: Span) -> Diagnostic {
Diagnostic::note("multi-line if...then...else must have a preceding space")
.with_rule(ID)
.with_highlight(span)
.with_fix("add a newline before the then keyword")
}
/// Reports missing newline prior to else in multiline if...then...else
/// constructs
fn multiline_else_space(span: Span) -> Diagnostic {
Diagnostic::note("multi-line if...then...else must have a preceding space")
.with_rule(ID)
.with_highlight(span)
.with_fix("add a newline before the else keyword")
}
/// Reports missing close paren for multiline if...then...else constructs
fn multiline_if_close_paren(span: Span) -> Diagnostic {
Diagnostic::note("multi-line if...then...else must have a following newline and parenthesis")
.with_rule(ID)
.with_highlight(span)
.with_fix("add a newline and close parenthesis after to this else clause")
}
/// Reports missing newline following open brace/bracket/paren for multiline
/// array/map/object literals
fn multiline_literal_open_newline(span: Span) -> Diagnostic {
Diagnostic::note(
"multi-line array/map/object literals must have a newline following the opening token",
)
.with_rule(ID)
.with_highlight(span)
.with_fix("add a newline after the opening brace/bracket/parenthesis")
}
/// Reports missing newline before close brace/bracket/paren for multiline
/// array/map/object literals
fn multiline_literal_close_newline(span: Span) -> Diagnostic {
Diagnostic::note(
"multi-line array/map/object literals must have a newline preceding the closing token",
)
.with_rule(ID)
.with_highlight(span)
.with_fix("add a newline before the closing brace/bracket/parenthesis")
}
/// Detects improperly spaced expressions.
#[derive(Default, Debug, Clone, Copy)]
pub struct ExpressionSpacingRule;
impl Rule for ExpressionSpacingRule {
fn id(&self) -> &'static str {
ID
}
fn description(&self) -> &'static str {
"Ensures that WDL expressions are properly spaced."
}
fn explanation(&self) -> &'static str {
"Proper spacing is important for readability and consistency. This rule ensures that \
expressions are spaced properly.
The following tokens should be surrounded by whitespace when used as an infix: `=`, `==`, \
`!=`, `&&`, `||`, `<`, `<=`, `>`, `>=`, `+`, `-`, `*`, `/`, and `%`.
The following tokens should not be followed by whitespace when used as a prefix: `-`, and \
`!`.
Opening brackets (`(`, `[`, and `{`) should not be followed by a space, but may be \
followed by a newline. Closing brackets (`)`, `]`, and `}`) should not be preceded by a \
space, but may be preceded by a newline.
Sometimes a long expression will exceed the maximum line width. In these cases, one or \
more linebreaks must be introduced. Line continuations should be indented one more level \
than the beginning of the expression. There should never be more than one level of \
indentation change per-line.
If bracketed content (things between `()`, `[]`, or `{}`) must be split onto multiple \
lines, a newline should follow the opening bracket, the contents should be indented an \
additional level, then the closing bracket should be de-indented to match the indentation \
of the opening bracket. If you are line splitting an expression on an infix operator, the \
operator and at least the beginning of the RHS operand should be on the continued line. \
(i.e. an operator should not be on a line by itself.)
If you are using the `if...then...else...` construct as part of your expression and it \
needs to be line split, the entire construct should be wrapped in parentheses (`()`). The \
opening parenthesis should be immediately followed by a newline. `if`, `then`, and `else` \
should all start a line one more level of indentation than the wrapping parentheses. The \
closing parenthesis should be on the same level of indentation as the opening \
parenthesis. If you are using the `if...then...else...` construct on one line, it does \
not need to be wrapped in parentheses. However, if any of the 3 clauses are more complex \
than a single identifier, they should be wrapped in parentheses.
Sometimes a developer will choose to line split an expression despite it being able to \
all fit on one line that is <=90 characters wide. That is perfectly acceptable. There is \
'wiggle' room allowed by the above rules. This is intentional, and allows developers to \
choose a more compact or a more spaced out expression."
}
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Spacing])
}
fn exceptable_nodes(&self) -> Option<&'static [SyntaxKind]> {
None
}
fn related_rules(&self) -> &[&'static str] {
&["LineWidth"]
}
}
impl Visitor for ExpressionSpacingRule {
fn reset(&mut self) {
*self = Self;
}
fn expr(&mut self, diagnostics: &mut Diagnostics, reason: VisitReason, expr: &Expr) {
if reason == VisitReason::Exit {
return;
}
match expr {
Expr::LogicalNot(_) | Expr::Negation(_) => {
// No following spacing allowed
if expr
.inner()
.children_with_tokens()
.filter(|t| t.kind() == SyntaxKind::Whitespace)
.count()
> 0
{
diagnostics.exceptable_add(
prefix_whitespace(expr.span()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
}
Expr::Parenthesized(_) => {
// Find the actual open and close parentheses
let open = expr
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::OpenParen)
.expect("parenthesized expression should have an opening parenthesis");
let close = expr
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::CloseParen)
.expect("parenthesized expression should have an closing parenthesis");
// The opening parenthesis can be preceded by whitespace, another open
// parenthesis, or a negation (!). The parenthesized expression
// can be the first thing at its level if it is wrapped in a
// EqualityExpressionNode.
let mut prev = expr.inner().prev_sibling_or_token();
if prev.is_none() {
// No prior elements, so we need to go up a level.
match expr.inner().parent() {
Some(parent) => {
if let Some(parent_prev) = parent.prev_sibling_or_token() {
prev = Some(parent_prev);
}
}
_ => {
unreachable!(
"parenthesized expression should have a prior sibling or a parent"
);
}
}
}
if let Some(prev) = prev {
match prev.kind() {
SyntaxKind::Whitespace
| SyntaxKind::OpenParen
| SyntaxKind::NegationExprNode
| SyntaxKind::Exclamation
| SyntaxKind::NameRefExprNode // Function calls can precede without whitespace
| SyntaxKind::PlaceholderOpen // Opening placeholders can precede a paren
| SyntaxKind::Plus // This and all below will report on those tokens.
| SyntaxKind::Minus
| SyntaxKind::Asterisk
| SyntaxKind::Exponentiation
| SyntaxKind::Slash
| SyntaxKind::Less
| SyntaxKind::LessEqual
| SyntaxKind::Greater
| SyntaxKind::GreaterEqual
| SyntaxKind::Percent
| SyntaxKind::LogicalAnd
| SyntaxKind::LogicalOr => {}
_ => {
// opening parens should be preceded by whitespace
diagnostics.exceptable_add(missing_preceding_whitespace(open.text_range().into()), SyntaxElement::from(expr.inner().clone()), &self.exceptable_nodes());
}
}
}
// Opening parenthesis cannot be followed by a space, but can be followed by a
// newline. Except in the case of an in-line comment.
if let Some(open_next) = open.next_sibling_or_token() {
if open_next.kind() == SyntaxKind::Whitespace {
let token = open_next.as_token().expect("should be a token");
if token.text().starts_with(' ')
&& token
.next_sibling_or_token()
.is_some_and(|t| t.kind() != SyntaxKind::Comment)
{
// opening parens should not be followed by non-newline whitespace
diagnostics.exceptable_add(
disallowed_space(token.text_range().into()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
// Closing parenthesis should not be preceded by a space, but can be preceded by
// a newline.
if let Some(close_prev) = close.prev_sibling_or_token() {
if close_prev.kind() == SyntaxKind::Whitespace
&& !close_prev
.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
// closing parenthesis should not be preceded by whitespace without a
// newline
diagnostics.exceptable_add(
disallowed_space(close_prev.text_range().into()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
Expr::LogicalAnd(_) | Expr::LogicalOr(_) => {
// find the operator
let op = expr
.inner()
.children_with_tokens()
.find(|t| matches!(t.kind(), SyntaxKind::LogicalAnd | SyntaxKind::LogicalOr))
.expect("expression node should have an operator");
check_required_surrounding_ws(diagnostics, &op, &self.exceptable_nodes());
}
Expr::Equality(_) | Expr::Inequality(_) => {
// find the operator
let op = expr
.inner()
.children_with_tokens()
.find(|t| matches!(t.kind(), SyntaxKind::Equal | SyntaxKind::NotEqual))
.expect("expression node should have an operator");
check_required_surrounding_ws(diagnostics, &op, &self.exceptable_nodes());
}
Expr::Addition(_)
| Expr::Subtraction(_)
| Expr::Multiplication(_)
| Expr::Division(_)
| Expr::Modulo(_)
| Expr::Exponentiation(_) => {
// find the operator
let op = expr
.inner()
.children_with_tokens()
.find(|t| {
matches!(
t.kind(),
SyntaxKind::Plus
| SyntaxKind::Minus
| SyntaxKind::Asterisk
| SyntaxKind::Slash
| SyntaxKind::Percent
| SyntaxKind::Exponentiation
)
})
.expect("expression node should have an operator");
// Infix operators must be surrounded by whitespace
check_required_surrounding_ws(diagnostics, &op, &self.exceptable_nodes());
}
Expr::Less(_) | Expr::LessEqual(_) | Expr::Greater(_) | Expr::GreaterEqual(_) => {
// find the operator
let op = expr
.inner()
.children_with_tokens()
.find(|t| {
matches!(
t.kind(),
SyntaxKind::Less
| SyntaxKind::LessEqual
| SyntaxKind::Greater
| SyntaxKind::GreaterEqual
)
})
.expect("expression node should have an operator");
check_required_surrounding_ws(diagnostics, &op, &self.exceptable_nodes());
}
Expr::If(_) => {
// find the if keyword
let if_keyword = expr
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::IfKeyword)
.expect("if expression node should have an if keyword");
let then_keyword = expr
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::ThenKeyword)
.expect("if expression node should have a then keyword");
let else_keyword = expr
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::ElseKeyword)
.expect("if expression node should have an else keyword");
let newlines = expr
.inner()
.descendants_with_tokens()
.filter(|t| {
if t.kind() == SyntaxKind::Whitespace
&& t.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
return true;
}
false
})
.count();
// If..then..else expression contains newlines, so we need to check the
// formatting.
if newlines > 0 {
// If expression should be preceded by a opening parenthesis and a newline (plus
// indentation whitespace).
let prior = expr.inner().siblings_with_tokens(Direction::Prev).skip(1);
let mut newline = false;
let mut open_paren = false;
for t in prior {
// if keyword in a multi-line if..then..else should only be preceded by
// whitespace, an opening parenthesis, or a comment.
if !matches!(
t.kind(),
SyntaxKind::Whitespace | SyntaxKind::OpenParen | SyntaxKind::Comment
) {
// if should be preceded by an opening parenthesis and a newline
break;
} else if t.kind() == SyntaxKind::Whitespace
&& t.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
newline = true;
} else if t.kind() == SyntaxKind::OpenParen {
open_paren = true;
break;
}
}
if !open_paren || !newline {
diagnostics.exceptable_add(
multiline_if_open_paren(if_keyword.text_range().into()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
// check the then keyword
let then_ws = then_keyword
.prev_sibling_or_token()
.expect("should have a prior sibling");
if then_ws.kind() != SyntaxKind::Whitespace
|| !then_ws
.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
// then should be preceded by a newline
diagnostics.exceptable_add(
multiline_then_space(then_keyword.text_range().into()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
// else keyword should be preceded by a newline
let else_prior = else_keyword
.prev_sibling_or_token()
.expect("should have a prior sibling");
if else_prior.kind() != SyntaxKind::Whitespace
|| !else_prior
.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
// then should be preceded by a newline
diagnostics.exceptable_add(
multiline_else_space(else_keyword.text_range().into()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
// check the closing parenthesis
let next_tokens = expr.inner().siblings_with_tokens(Direction::Next).skip(1);
let mut newline = false;
let mut close_paren = false;
for t in next_tokens {
// else keyword in a multi-line if..then..else should only be followed by
// whitespace or a comment, then a closing parenthesis.
if !matches!(
t.kind(),
SyntaxKind::Whitespace | SyntaxKind::CloseParen | SyntaxKind::Comment
) {
// if should be preceded by an closing parenthesis and a newline
break;
} else if t.kind() == SyntaxKind::Whitespace
&& t.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
newline = true;
} else if t.kind() == SyntaxKind::CloseParen {
close_paren = true;
break;
}
}
if !close_paren || !newline {
diagnostics.exceptable_add(
multiline_if_close_paren(else_keyword.text_range().into()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
Expr::Index(_) => {
let open_bracket = expr
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::OpenBracket)
.expect("index expression node should have an opening bracket");
let close_bracket = expr
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::CloseBracket)
.expect("index expression node should have a closing bracket");
let checks = [
open_bracket
.prev_sibling_or_token()
.filter(|t| t.kind() == SyntaxKind::Whitespace),
open_bracket
.next_sibling_or_token()
.filter(|t| t.kind() == SyntaxKind::Whitespace),
close_bracket
.prev_sibling_or_token()
.filter(|t| t.kind() == SyntaxKind::Whitespace),
close_bracket
.next_sibling_or_token()
.filter(|t| t.kind() == SyntaxKind::Whitespace),
];
checks.iter().for_each(|f| {
if let Some(ws) = f {
diagnostics.exceptable_add(
disallowed_space(ws.text_range().into()),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
});
}
Expr::Access(acc) => {
let op = acc
.inner()
.children_with_tokens()
.find(|t| t.kind() == SyntaxKind::Dot)
.expect("access expression node should have a dot operator");
let before_ws = op
.prev_sibling_or_token()
.filter(|t| t.kind() == SyntaxKind::Whitespace);
let after_ws = op
.next_sibling_or_token()
.filter(|t| t.kind() == SyntaxKind::Whitespace);
if let Some(ws) = before_ws {
diagnostics.exceptable_add(
disallowed_space(ws.text_range().into()),
SyntaxElement::from(acc.inner().clone()),
&self.exceptable_nodes(),
);
}
if let Some(ws) = after_ws {
diagnostics.exceptable_add(
disallowed_space(ws.text_range().into()),
SyntaxElement::from(acc.inner().clone()),
&self.exceptable_nodes(),
);
}
}
Expr::Literal(l) => {
match l {
LiteralExpr::Array(_) | LiteralExpr::Map(_) | LiteralExpr::Object(_) => {
let newlines = l
.inner()
.descendants_with_tokens()
.filter(|t| {
if t.kind() == SyntaxKind::Whitespace
&& t.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
return true;
}
false
})
.count();
if newlines > 0 {
// Find the opening and closing brackets
let open_bracket = expr
.inner()
.children_with_tokens()
.find(|t| {
matches!(
t.kind(),
SyntaxKind::OpenBracket
| SyntaxKind::OpenBrace
| SyntaxKind::OpenParen
)
})
.expect("literal expression node should have an opening bracket");
let close_bracket = expr
.inner()
.children_with_tokens()
.find(|t| {
matches!(
t.kind(),
SyntaxKind::CloseBracket
| SyntaxKind::CloseBrace
| SyntaxKind::CloseParen
)
})
.expect("literal expression node should have a closing bracket");
let open_bracket_next = open_bracket
.as_token()
.expect("should be a token")
.siblings_with_tokens(Direction::Next)
.skip(1);
let mut newline = false;
for t in open_bracket_next {
// The opening bracket/brace/paren must be followed by a `\n`, with
// optional additional whitespace or comment tokens.
if !matches!(t.kind(), SyntaxKind::Whitespace | SyntaxKind::Comment)
{
break;
} else if t.kind() == SyntaxKind::Whitespace
&& t.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
newline = true;
break;
}
}
if !newline {
diagnostics.exceptable_add(
multiline_literal_open_newline(
open_bracket.text_range().into(),
),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
let close_bracket_prior = close_bracket
.as_token()
.expect("should be a token")
.siblings_with_tokens(Direction::Prev)
.skip(1);
let mut newline = false;
for t in close_bracket_prior {
// The closing bracket/brace/paren must be preceded by a `\n`, with
// optional additional whitespace or comment tokens.
if !matches!(t.kind(), SyntaxKind::Whitespace | SyntaxKind::Comment)
{
// if should be preceded by an opening parenthesis and a newline
break;
} else if t.kind() == SyntaxKind::Whitespace
&& t.as_token()
.expect("should be a token")
.text()
.contains('\n')
{
newline = true;
break;
}
}
if !newline {
diagnostics.exceptable_add(
multiline_literal_close_newline(
close_bracket.text_range().into(),
),
SyntaxElement::from(expr.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
_ => {}
}
}
// Expr::Literal, Expr::Name, Expr::Call
_ => {}
}
}
fn bound_decl(
&mut self,
diagnostics: &mut Diagnostics,
reason: VisitReason,
decl: &wdl_ast::v1::BoundDecl,
) {
if reason == VisitReason::Exit {
return;
}
if let Some(assign) = decl
.inner()
.descendants_with_tokens()
.find(|t| t.kind() == SyntaxKind::Assignment)
{
let before_ws =
assign.prev_sibling_or_token().map(|t| t.kind()) == Some(SyntaxKind::Whitespace);
let after_ws =
assign.next_sibling_or_token().map(|t| t.kind()) == Some(SyntaxKind::Whitespace);
if !before_ws && !after_ws {
// assignments must be surrounded by whitespace
diagnostics.exceptable_add(
assignment_missing_surrounding_whitespace(assign.text_range().into()),
SyntaxElement::from(decl.inner().clone()),
&self.exceptable_nodes(),
);
} else if !before_ws {
// assignments must be preceded by whitespace
diagnostics.exceptable_add(
assignment_missing_preceding_whitespace(assign.text_range().into()),
SyntaxElement::from(decl.inner().clone()),
&self.exceptable_nodes(),
);
} else if !after_ws {
// assignments must be followed by whitespace
diagnostics.exceptable_add(
assignment_missing_following_whitespace(assign.text_range().into()),
SyntaxElement::from(decl.inner().clone()),
&self.exceptable_nodes(),
);
}
}
}
}
/// Checks to ensure a token is surrounded by whitespace.
fn check_required_surrounding_ws(
diagnostics: &mut Diagnostics,
op: &SyntaxElement,
exceptable_nodes: &Option<&'static [SyntaxKind]>,
) {
let before_ws = op.prev_sibling_or_token().map(|t| t.kind()) == Some(SyntaxKind::Whitespace);
let after_ws = op.next_sibling_or_token().map(|t| t.kind()) == Some(SyntaxKind::Whitespace);
if !before_ws && !after_ws {
// must be surrounded by whitespace
diagnostics.exceptable_add(
missing_surrounding_whitespace(op.text_range().into()),
op.clone(),
exceptable_nodes,
);
} else if !before_ws {
// must be preceded by whitespace
diagnostics.exceptable_add(
missing_preceding_whitespace(op.text_range().into()),
op.clone(),
exceptable_nodes,
);
} else if !after_ws {
// must be followed by whitespace
diagnostics.exceptable_add(
missing_following_whitespace(op.text_range().into()),
op.clone(),
exceptable_nodes,
);
}
}