wdl-lint 0.26.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
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
//! A lint rule to ensure each output is documented in `meta`.

use indexmap::IndexMap;
use wdl_analysis::Diagnostics;
use wdl_analysis::Example;
use wdl_analysis::LabeledSnippet;
use wdl_analysis::VisitReason;
use wdl_analysis::Visitor;
use wdl_ast::AstNode;
use wdl_ast::AstToken;
use wdl_ast::Diagnostic;
use wdl_ast::Span;
use wdl_ast::SyntaxKind;
use wdl_ast::SyntaxNode;
use wdl_ast::v1::MetadataSection;
use wdl_ast::v1::MetadataValue;
use wdl_ast::v1::OutputSection;
use wdl_ast::v1::TaskDefinition;
use wdl_ast::v1::WorkflowDefinition;

use crate::Rule;
use crate::Tag;
use crate::TagSet;

/// The identifier for the non-matching output rule.
const ID: &str = "MatchingOutputMeta";

/// Creates a "non-matching output" diagnostic.
fn nonmatching_output(span: Span, name: &str, item_name: &str, ty: &str) -> Diagnostic {
    Diagnostic::warning(format!(
        "output `{name}` is missing from `meta.outputs` section in {ty} `{item_name}`"
    ))
    .with_rule(ID)
    .with_highlight(span)
    .with_fix(format!(
        "add a description of output `{name}` to documentation in `meta.outputs`"
    ))
}

/// Creates a missing outputs in meta diagnostic.
fn missing_outputs_in_meta(span: Span, item_name: &str, ty: &str) -> Diagnostic {
    Diagnostic::warning(format!(
        "`outputs` key missing in `meta` section for the {ty} `{item_name}`"
    ))
    .with_rule(ID)
    .with_highlight(span)
    .with_fix("add an `outputs` key to `meta` section describing the outputs")
}

/// Creates a diagnostic for extra `meta.outputs` entries.
fn extra_output_in_meta(span: Span, name: &str, item_name: &str, ty: &str) -> Diagnostic {
    Diagnostic::warning(format!(
        "`{name}` appears in `outputs` section of the {ty} `{item_name}` but is not a declared \
         `output`"
    ))
    .with_rule(ID)
    .with_highlight(span)
    .with_fix(format!(
        "ensure the output exists or remove the `{name}` key from `meta.outputs`"
    ))
}

/// Creates a diagnostic for out-of-order entries.
fn out_of_order(span: Span, output_span: Span, item_name: &str, ty: &str) -> Diagnostic {
    Diagnostic::note(format!(
        "`outputs` section of `meta` for the {ty} `{item_name}` is out of order"
    ))
    .with_rule(ID)
    .with_highlight(span)
    .with_highlight(output_span)
    .with_fix(
        "ensure the keys within `meta.outputs` have the same order as they appear in `output`",
    )
}

/// Creates a diagnostic for non-object `meta.outputs` entries.
fn non_object_meta_outputs(span: Span, item_name: &str, ty: &str) -> Diagnostic {
    Diagnostic::warning(format!(
        "{ty} `{item_name}` has a `meta.outputs` key that is not an object containing output \
         descriptions"
    ))
    .with_rule(ID)
    .with_highlight(span)
    .with_fix("ensure `meta.outputs` is an object containing descriptions for each output")
}

/// Detects non-matching outputs.
#[derive(Default, Debug, Clone)]
pub struct MatchingOutputMetaRule<'a> {
    /// The span of the `meta` section.
    current_meta_span: Option<Span>,
    /// Are we currently within a `meta` section?
    in_meta: bool,
    /// The span of the `meta.outputs` section.
    current_meta_outputs_span: Option<Span>,
    /// The span of the `output` section.
    current_output_span: Option<Span>,
    /// Are we currently within an `output` section?
    in_output: bool,
    /// The keys seen in `meta.outputs`.
    meta_outputs_keys: IndexMap<String, Span>,
    /// The keys seen in `output`.
    output_keys: IndexMap<String, Span>,
    /// The context type.
    ty: Option<&'a str>,
    /// The item name.
    name: Option<String>,
    /// Prior objects
    prior_objects: Vec<String>,
}

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

    fn description(&self) -> &'static str {
        "Ensures that each output field is documented in the meta section under `meta.outputs`."
    }

    fn explanation(&self) -> &'static str {
        "The meta section should have an `outputs` key that is an object and contains keys with \
         descriptions for each output of the task/workflow. These must match exactly. i.e. for \
         each named output of a task or workflow, there should be an entry under `meta.outputs` \
         with that same name. Additionally, these entries should be in the same order (that order \
         is up to the developer to decide). No extraneous `meta.outputs` entries are allowed."
    }

    fn examples(&self) -> &'static [Example] {
        &[Example {
            negative: LabeledSnippet {
                label: None,
                snippet: r#"version 1.2

task generate_greeting {
    meta {
        outputs: {
        # Missing `greeting`
        }
    }

    input {
        String name
    }

    output {
        String greeting = "Hello, ~{name}!"
    }
}
"#,
            },
            revised: Some(LabeledSnippet {
                label: None,
                snippet: r#"version 1.2

task generate_greeting {
    meta {
        outputs: {
            greeting: "The generated greeting for the provided name",
        }
    }

    input {
        String name
    }

    output {
        String greeting = "Hello, ~{name}!"
    }
}
"#,
            }),
        }]
    }

    fn tags(&self) -> TagSet {
        TagSet::new(&[
            Tag::Completeness,
            Tag::Documentation,
            Tag::SprocketCompatibility,
        ])
    }

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

    fn related_rules(&self) -> &'static [&'static str] {
        &[
            "MetaDescription",
            "ParameterMetaMatched",
            "OutputSection",
            "RequirementsSection",
            "RuntimeSection",
            "DescriptionLength",
        ]
    }
}

/// Check each output key exists in the `outputs` key within the `meta` section.
fn check_matching(
    diagnostics: &mut Diagnostics,
    rule: &mut MatchingOutputMetaRule<'_>,
    node: &SyntaxNode,
) {
    let mut exact_match = true;
    // Check for expected entries missing from `meta.outputs`.
    for (name, span) in &rule.output_keys {
        if !rule.meta_outputs_keys.contains_key(name) {
            exact_match = false;
            if rule.current_meta_span.is_some() {
                diagnostics.exceptable_add(
                    nonmatching_output(
                        *span,
                        name,
                        rule.name.as_deref().expect("should have a name"),
                        rule.ty.expect("should have a type"),
                    ),
                    node,
                    &rule.exceptable_nodes(),
                );
            }
        }
    }

    // Check for extra entries in `meta.outputs`.
    // This should flag any meta.outputs entry that doesn't have a corresponding
    // declared output, even if the output section is entirely missing.
    for (name, span) in &rule.meta_outputs_keys {
        if !rule.output_keys.contains_key(name) {
            exact_match = false;
            diagnostics.exceptable_add(
                extra_output_in_meta(
                    *span,
                    name,
                    rule.name.as_deref().expect("should have a name"),
                    rule.ty.expect("should have a type"),
                ),
                node,
                &rule.exceptable_nodes(),
            );
        }
    }

    // Check for out-of-order entries.
    if exact_match && !rule.meta_outputs_keys.keys().eq(rule.output_keys.keys()) {
        diagnostics.exceptable_add(
            out_of_order(
                rule.current_meta_outputs_span
                    .expect("should have a `meta.outputs` span"),
                rule.current_output_span
                    .expect("should have an `output` span"),
                rule.name.as_deref().expect("should have a name"),
                rule.ty.expect("should have a type"),
            ),
            node,
            &rule.exceptable_nodes(),
        );
    }
}

/// Handle missing `meta.outputs` and reset the visitor.
fn handle_meta_outputs_and_reset(
    diagnostics: &mut Diagnostics,
    rule: &mut MatchingOutputMetaRule<'_>,
    node: &SyntaxNode,
) {
    if let Some(current_meta_span) = rule.current_meta_span
        && rule.current_meta_outputs_span.is_none()
        && !rule.output_keys.is_empty()
    {
        diagnostics.exceptable_add(
            missing_outputs_in_meta(
                current_meta_span,
                rule.name.as_deref().expect("should have a name"),
                rule.ty.expect("should have a type"),
            ),
            node,
            &rule.exceptable_nodes(),
        );
    } else {
        check_matching(diagnostics, rule, node);
    }

    rule.name = None;
    rule.current_meta_outputs_span = None;
    rule.current_meta_span = None;
    rule.current_output_span = None;
    rule.output_keys.clear();
    rule.meta_outputs_keys.clear();
}

impl Visitor for MatchingOutputMetaRule<'_> {
    fn reset(&mut self) {
        self.current_meta_span = None;
        self.in_meta = false;
        self.current_meta_outputs_span = None;
        self.current_output_span = None;
        self.in_output = false;
        self.meta_outputs_keys.clear();
        self.output_keys.clear();
        self.name = None;
        self.ty = None;
        self.prior_objects.clear();
    }

    fn workflow_definition(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        workflow: &WorkflowDefinition,
    ) {
        match reason {
            VisitReason::Enter => {
                self.name = Some(workflow.name().text().to_string());
                self.ty = Some("workflow");
            }
            VisitReason::Exit => {
                handle_meta_outputs_and_reset(diagnostics, self, workflow.inner());
            }
        }
    }

    fn task_definition(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        task: &TaskDefinition,
    ) {
        match reason {
            VisitReason::Enter => {
                self.name = Some(task.name().text().to_string());
                self.ty = Some("task");
            }
            VisitReason::Exit => {
                handle_meta_outputs_and_reset(diagnostics, self, task.inner());
            }
        }
    }

    fn metadata_section(
        &mut self,
        _diagnostics: &mut Diagnostics,
        reason: VisitReason,
        section: &MetadataSection,
    ) {
        match reason {
            VisitReason::Enter => {
                self.current_meta_span = Some(
                    section
                        .inner()
                        .first_token()
                        .expect("metadata section should have tokens")
                        .text_range()
                        .into(),
                );
                self.in_meta = true;
            }
            VisitReason::Exit => {
                self.in_meta = false;
            }
        }
    }

    fn output_section(
        &mut self,
        _diagnostics: &mut Diagnostics,
        reason: VisitReason,
        section: &OutputSection,
    ) {
        match reason {
            VisitReason::Enter => {
                self.current_output_span = Some(
                    section
                        .inner()
                        .first_token()
                        .expect("output section should have tokens")
                        .text_range()
                        .into(),
                );
                self.in_output = true;
            }
            VisitReason::Exit => {
                self.in_output = false;
            }
        }
    }

    fn bound_decl(
        &mut self,
        _diagnostics: &mut Diagnostics,
        reason: VisitReason,
        decl: &wdl_ast::v1::BoundDecl,
    ) {
        if reason == VisitReason::Enter && self.in_output {
            self.output_keys
                .insert(decl.name().text().to_string(), decl.name().span());
        }
    }

    fn metadata_object_item(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        item: &wdl_ast::v1::MetadataObjectItem,
    ) {
        if !self.in_meta {
            return;
        }

        match reason {
            VisitReason::Exit => {
                if let MetadataValue::Object(_) = item.value() {
                    self.prior_objects.pop();
                }
            }
            VisitReason::Enter => {
                if let Some(_meta_span) = self.current_meta_span {
                    if item.name().text() == "outputs" {
                        self.current_meta_outputs_span = Some(item.span());
                        match item.value() {
                            MetadataValue::Object(_) => {}
                            _ => {
                                diagnostics.exceptable_add(
                                    non_object_meta_outputs(
                                        item.span(),
                                        self.name.as_deref().expect("should have a name"),
                                        self.ty.expect("should have a type"),
                                    ),
                                    item.inner(),
                                    &self.exceptable_nodes(),
                                );
                            }
                        }
                    } else if let Some(meta_outputs_span) = self.current_meta_outputs_span {
                        let span = item.span();
                        if span.start() > meta_outputs_span.start()
                            && span.end() < meta_outputs_span.end()
                            && self
                                .prior_objects
                                .last()
                                .expect("should have seen `meta.outputs`")
                                == "outputs"
                        {
                            self.meta_outputs_keys
                                .insert(item.name().text().to_string(), item.span());
                        }
                    }
                }
                if let MetadataValue::Object(_) = item.value() {
                    self.prior_objects.push(item.name().text().to_string());
                }
            }
        }
    }
}