wdl-lint 0.16.0

Lint rules for Workflow Description Language (WDL) documents
Documentation
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
//! A lint rule that checks the formatting of the preamble.

use wdl_analysis::Diagnostics;
use wdl_analysis::EXCEPT_COMMENT_PREFIX;
use wdl_analysis::VisitReason;
use wdl_analysis::Visitor;
use wdl_ast::AstToken;
use wdl_ast::Comment;
use wdl_ast::Diagnostic;
use wdl_ast::Span;
use wdl_ast::SyntaxKind;
use wdl_ast::VersionStatement;
use wdl_ast::Whitespace;

use crate::Rule;
use crate::Tag;
use crate::TagSet;
use crate::util::lines_with_offset;

/// The identifier for the preamble formatting rule.
const ID: &str = "PreambleFormatted";

/// Creates an "invalid preamble comment" diagnostic.
fn invalid_preamble_comment(span: Span) -> Diagnostic {
    Diagnostic::note(
        "preamble comments must start with `##` and have at least one space between the `##` and \
         the comment text",
    )
    .with_rule(ID)
    .with_highlight(span)
    .with_fix(
        "either move this comment out of the preamble or change it to a preamble comment (i.e. a \
         comment that starts with `##`)",
    )
}

/// Creates a "directive after preamble comment" diagnostic.
fn directive_after_preamble_comment(span: Span) -> Diagnostic {
    Diagnostic::note("lint directives must come before preamble comments")
        .with_rule(ID)
        .with_highlight(span)
        .with_fix("move the lint directive to the beginning of the document")
}

/// Creates an "unnecessary whitespace" diagnostic.
fn leading_whitespace(span: Span) -> Diagnostic {
    Diagnostic::note("unnecessary whitespace in document preamble")
        .with_rule(ID)
        .with_highlight(span)
        .with_fix("remove the leading whitespace")
}

/// Creates an "expected a blank line before preamble comment" diagnostic.
fn expected_blank_line_before_preamble_comment(span: Span) -> Diagnostic {
    Diagnostic::note(
        "expected exactly one blank line between lint directives and preamble comments",
    )
    .with_rule(ID)
    .with_highlight(span)
    .with_fix("add a blank line between any lint directives and preamble comments")
}

/// Detects if a comment is a lint directive.
fn is_lint_directive(text: &str) -> bool {
    text.starts_with(EXCEPT_COMMENT_PREFIX)
}

/// Detects if a comment is a preamble comment.
fn is_preamble_comment(text: &str) -> bool {
    text == "##" || text.starts_with("## ")
}

/// The state of preamble processing.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
enum PreambleState {
    /// The preamble is not being processed.
    #[default]
    Start,
    /// We are processing the lint directive block.
    LintDirectiveBlock,
    /// We are processing the preamble comment block.
    PreambleCommentBlock,
    /// The preamble is finished
    Finished,
}

/// An enum that represents the type of diagnostic to extend.
enum ExtendDiagnostic {
    /// Extend a lint directive diagnostic.
    LintDirective,
    /// Extend an invalid comment diagnostic.
    InvalidComment,
}

/// Detects incorrect comments in a document preamble.
#[derive(Default, Debug, Clone, Copy)]
pub struct PreambleFormattedRule {
    /// The current state of preamble processing.
    state: PreambleState,
    /// The number of comment tokens to skip.
    ///
    /// This is used to skip comments that were consolidated in a prior
    /// diagnostic.
    skip_count: usize,
}

impl Rule for PreambleFormattedRule {
    fn id(&self) -> &'static str {
        ID
    }

    fn description(&self) -> &'static str {
        "Ensures that documents have correct formatting in the preamble."
    }

    fn explanation(&self) -> &'static str {
        "The document preamble is defined as anything before the version declaration statement and \
         the version declaration statement itself. Only comments and whitespace are permitted \
         before the version declaration.

         All comments in the preamble should conform to one of two special formats: lint \
         directives or preamble comments.

         This rule enforces the following formatting requirements:

         1. Comments in the preamble should be full line comments (no whitespace before the \
         comment).
         2. If lint directives are present, they should be at the absolute beginning of the \
         document.
         3. Multiple lint directives are permitted, but they should not be interleaved with \
         preamble comments or blank lines.
         4. A space should follow the double-pound-sign (`##`) if there is any text within the \
         preamble comment.
         5. \"Empty\" preamble comments (`##`) are permitted and should not have any whitespace \
         following the `##`.
         6. Comments beginning with 3 or more pound signs before the version declaration are not \
         permitted.
         7. All preamble comments should be in a single block without blank lines.
         8. Following the preamble comment block, there should always be a blank line before the \
         version statement.
         9. When transitioning from lint directives to preamble comments, there should be exactly \
         one blank line.
         10. Both lint directives and preamble comments are optional, and if they are not present, \
         there should be no comments or whitespace before the version declaration."
    }

    fn tags(&self) -> TagSet {
        TagSet::new(&[Tag::Spacing, Tag::Style])
    }

    fn exceptable_nodes(&self) -> Option<&'static [SyntaxKind]> {
        Some(&[SyntaxKind::VersionStatementNode])
    }

    fn related_rules(&self) -> &[&'static str] {
        &[]
    }
}

impl Visitor for PreambleFormattedRule {
    fn reset(&mut self) {
        *self = Self::default();
    }

    fn whitespace(&mut self, diagnostics: &mut Diagnostics, whitespace: &Whitespace) {
        // Since this rule can only be excepted in a document-wide fashion,
        // if the rule is running we can directly add the diagnostic
        // without checking for the exceptable nodes

        if self.state == PreambleState::Finished {
            return;
        }

        // If the next sibling is the version statement, let the
        // VersionStatementFormatted rule handle this particular whitespace
        if whitespace
            .inner()
            .next_sibling_or_token()
            .map(|s| s.kind() == SyntaxKind::VersionStatementNode)
            .unwrap_or(false)
        {
            return;
        }

        let s = whitespace.text();
        // If there is a previous token, it must be a comment
        match whitespace.inner().prev_token() {
            Some(prev_comment) => {
                let prev_text = prev_comment.text();
                let prev_is_lint_directive = is_lint_directive(prev_text);
                let prev_is_preamble_comment = is_preamble_comment(prev_text);

                let next_token = whitespace
                    .inner()
                    .next_token()
                    .expect("should have a next token");
                assert!(
                    next_token.kind() == SyntaxKind::Comment,
                    "next token should be a comment"
                );

                let next_text = next_token.text();
                let next_is_lint_directive = is_lint_directive(next_text);
                let next_is_preamble_comment = is_preamble_comment(next_text);

                let expect_single_blank = match (
                    prev_is_lint_directive,
                    prev_is_preamble_comment,
                    next_is_lint_directive,
                    next_is_preamble_comment,
                ) {
                    (true, false, true, false) => {
                        // Lint directive followed by lint directive
                        false
                    }
                    (true, false, false, true) => {
                        // Lint directive followed by preamble comment
                        true
                    }
                    (false, true, false, true) => {
                        // Preamble comment followed by preamble comment
                        false
                    }
                    (false, true, true, false) => {
                        // Preamble comment followed by lint directive
                        // Handled by comment visitor
                        return;
                    }
                    (_, _, false, false) => {
                        // Anything followed by invalid comment
                        // Handled by comment visitor
                        return;
                    }
                    (false, false, ..) => {
                        // Invalid comment followed by anything
                        // Handled by comment visitor
                        return;
                    }
                    _ => {
                        unreachable!()
                    }
                };

                let span = whitespace.span();
                if expect_single_blank {
                    if s != "\r\n\r\n" && s != "\n\n" {
                        // There's a special case where the blank line has extra whitespace
                        // but that doesn't appear in the printed diagnostic.
                        let mut diagnostic = expected_blank_line_before_preamble_comment(span);

                        if s.chars().filter(|&c| c == '\n').count() == 2 {
                            for (line, start, end) in lines_with_offset(s) {
                                if !line.is_empty() {
                                    let end_offset = if s.ends_with("\r\n") {
                                        2
                                    } else if s.ends_with('\n') {
                                        1
                                    } else {
                                        0
                                    };

                                    diagnostic = diagnostic.with_highlight(Span::new(
                                        span.start() + start,
                                        end - start - end_offset,
                                    ));
                                }
                            }
                        }
                        diagnostics.add(diagnostic);
                    }
                } else if s != "\r\n" && s != "\n" {
                    // Don't include the newline separating the previous comment from the
                    // leading whitespace
                    let offset = if s.starts_with("\r\n") {
                        2
                    } else if s.starts_with('\n') {
                        1
                    } else {
                        0
                    };

                    diagnostics.add(leading_whitespace(Span::new(
                        span.start() + offset,
                        span.len() - offset,
                    )));
                } else {
                    // NOTE: this return should be kept in case future
                    // iterations of the code add something after this `if`
                    // statement.
                    #[allow(clippy::needless_return)]
                    return;
                }
            }
            _ => {
                // Whitespace is not allowed to start the document.
                diagnostics.add(leading_whitespace(whitespace.span()));
            }
        }
    }

    fn comment(&mut self, diagnostics: &mut Diagnostics, comment: &Comment) {
        if self.state == PreambleState::Finished {
            return;
        }

        // Skip this comment if necessary; this occurs if we've consolidated multiple
        // comments in a row into a single diagnostic
        if self.skip_count > 0 {
            self.skip_count -= 1;
            return;
        }

        let text = comment.text();
        let lint_directive = is_lint_directive(text);
        let preamble_comment = is_preamble_comment(text);

        let mut extend = None;

        if !lint_directive && !preamble_comment {
            extend = Some(ExtendDiagnostic::InvalidComment);
        } else if self.state == PreambleState::Start {
            if lint_directive {
                self.state = PreambleState::LintDirectiveBlock;
            }
            if preamble_comment {
                self.state = PreambleState::PreambleCommentBlock;
            }
            return;
        } else if self.state == PreambleState::LintDirectiveBlock {
            if lint_directive {
                return;
            }
            if preamble_comment {
                self.state = PreambleState::PreambleCommentBlock;
                return;
            }
        } else if self.state == PreambleState::PreambleCommentBlock {
            if preamble_comment {
                return;
            }
            if lint_directive {
                extend = Some(ExtendDiagnostic::LintDirective);
            }
        }

        // Otherwise, look for the next siblings that might also be problematic;
        // if so, consolidate them into a single diagnostic
        let mut span = comment.span();
        let mut current = comment.inner().next_sibling_or_token();
        while let Some(sibling) = current {
            match sibling.kind() {
                SyntaxKind::Comment => {
                    let sibling_text = sibling.as_token().expect("should be a token").text();
                    let sibling_is_lint_directive = is_lint_directive(sibling_text);
                    let sibling_is_preamble_comment = is_preamble_comment(sibling_text);

                    match extend {
                        Some(ExtendDiagnostic::LintDirective) => {
                            if sibling_is_lint_directive {
                                // As we're processing this sibling comment here, increment the skip
                                // count
                                self.skip_count += 1;

                                span = Span::new(
                                    span.start(),
                                    usize::from(sibling.text_range().end()) - span.start(),
                                );
                            } else {
                                // Sibling should not be part of this diagnostic
                                break;
                            }
                        }
                        Some(ExtendDiagnostic::InvalidComment) => {
                            if !sibling_is_lint_directive && !sibling_is_preamble_comment {
                                // As we're processing this sibling comment here, increment the skip
                                // count
                                self.skip_count += 1;

                                span = Span::new(
                                    span.start(),
                                    usize::from(sibling.text_range().end()) - span.start(),
                                );
                            } else {
                                // Sibling should not be part of this diagnostic
                                break;
                            }
                        }
                        None => {
                            unreachable!();
                        }
                    }
                }
                SyntaxKind::Whitespace => {
                    // Skip whitespace
                }
                _ => break,
            }

            current = sibling.next_sibling_or_token();
        }

        // Since this rule can only be excepted in a document-wide fashion,
        // if the rule is running we can directly add the diagnostic
        // without checking for the exceptable nodes
        match extend {
            Some(ExtendDiagnostic::LintDirective) => {
                diagnostics.add(directive_after_preamble_comment(span));
            }
            Some(ExtendDiagnostic::InvalidComment) => {
                diagnostics.add(invalid_preamble_comment(span));
            }
            None => {
                unreachable!()
            }
        }
    }

    fn version_statement(
        &mut self,
        _diagnostics: &mut Diagnostics,
        reason: VisitReason,
        _stmt: &VersionStatement,
    ) {
        if reason == VisitReason::Exit {
            return;
        }
        self.state = PreambleState::Finished;
    }
}