-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmissing_metas.rs
195 lines (171 loc) · 5.37 KB
/
missing_metas.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
//! A lint rule for missing meta and parameter_meta sections.
use std::fmt;
use wdl_ast::v1::TaskDefinition;
use wdl_ast::v1::WorkflowDefinition;
use wdl_ast::AstToken;
use wdl_ast::Diagnostic;
use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::VisitReason;
use wdl_ast::Visitor;
use crate::Rule;
use crate::Tag;
use crate::TagSet;
/// Which section is missing.
enum Section {
/// The `meta` section is missing.
Meta,
/// The `parameter_meta` section is missing.
ParameterMeta,
}
impl fmt::Display for Section {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Meta => write!(f, "meta"),
Self::ParameterMeta => write!(f, "parameter_meta"),
}
}
}
/// The context for which section is missing.
enum Context {
/// A task.
Task,
/// A workflow.
Workflow,
}
impl fmt::Display for Context {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Task => write!(f, "task"),
Self::Workflow => write!(f, "workflow"),
}
}
}
/// The identifier for the missing meta sections rule.
const ID: &str = "MissingMetas";
/// Creates a "missing section" diagnostic.
fn missing_section(name: &str, section: Section, context: Context, span: Span) -> Diagnostic {
Diagnostic::note(format!(
"{context} `{name}` is missing a `{section}` section"
))
.with_rule(ID)
.with_label(
format!("this {context} is missing a `{section}` section"),
span,
)
.with_fix(format!("add a `{section}` section to the {context}"))
}
/// Creates a "missing sections" diagnostic.
fn missing_sections(name: &str, context: Context, span: Span) -> Diagnostic {
Diagnostic::note(format!(
"{context} `{name}` is missing both meta and parameter_meta sections"
))
.with_rule(ID)
.with_label(
format!("this {context} is missing both meta and parameter_meta sections"),
span,
)
.with_fix(format!(
"add meta and parameter_meta sections to the {context}"
))
}
/// A lint rule for missing meta and parameter_meta sections.
#[derive(Default, Debug, Clone, Copy)]
pub struct MissingMetasRule;
impl Rule for MissingMetasRule {
fn id(&self) -> &'static str {
ID
}
fn description(&self) -> &'static str {
"Ensures that tasks have both a meta and a parameter_meta section."
}
fn explanation(&self) -> &'static str {
"It is important that WDL code is well-documented. Every task and workflow should have \
both a meta and parameter_meta section. Tasks without an `input` section are permitted to \
skip the `parameter_meta` section."
}
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Completeness, Tag::Clarity])
}
}
impl Visitor for MissingMetasRule {
type State = Diagnostics;
fn document(&mut self, _: &mut Self::State, reason: VisitReason, _: &Document) {
if reason == VisitReason::Exit {
return;
}
// Reset the visitor upon document entry
*self = Default::default();
}
fn task_definition(
&mut self,
state: &mut Self::State,
reason: VisitReason,
task: &TaskDefinition,
) {
if reason == VisitReason::Exit {
return;
}
let inputs_present = task.inputs().next().is_some();
if inputs_present
&& task.metadata().next().is_none()
&& task.parameter_metadata().next().is_none()
{
state.add(missing_sections(
task.name().as_str(),
Context::Task,
task.name().span(),
));
} else if task.metadata().next().is_none() {
state.add(missing_section(
task.name().as_str(),
Section::Meta,
Context::Task,
task.name().span(),
));
} else if inputs_present && task.parameter_metadata().next().is_none() {
state.add(missing_section(
task.name().as_str(),
Section::ParameterMeta,
Context::Task,
task.name().span(),
));
}
}
fn workflow_definition(
&mut self,
state: &mut Self::State,
reason: VisitReason,
workflow: &WorkflowDefinition,
) {
if reason == VisitReason::Exit {
return;
}
let inputs_present = workflow.inputs().next().is_some();
if inputs_present
&& workflow.metadata().next().is_none()
&& workflow.parameter_metadata().next().is_none()
{
state.add(missing_sections(
workflow.name().as_str(),
Context::Workflow,
workflow.name().span(),
));
} else if workflow.metadata().next().is_none() {
state.add(missing_section(
workflow.name().as_str(),
Section::Meta,
Context::Workflow,
workflow.name().span(),
));
} else if inputs_present && workflow.parameter_metadata().next().is_none() {
state.add(missing_section(
workflow.name().as_str(),
Section::ParameterMeta,
Context::Workflow,
workflow.name().span(),
));
}
}
}