Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reworks the concept of lint directives #162

Merged
merged 19 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions wdl-ast/src/validation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//! Validator for WDL documents.

use std::collections::HashSet;

use rowan::Direction;
use wdl_grammar::SyntaxElement;
use wdl_grammar::SyntaxKind;
use wdl_grammar::SyntaxNode;

use super::v1;
use super::Comment;
use super::Diagnostic;
Expand All @@ -19,6 +26,9 @@ mod requirements;
mod strings;
mod version;

/// The prefix of `except` comments.
pub const EXCEPT_COMMENT_PREFIX: &str = "#@ except:";

/// Represents a collection of validation diagnostics.
///
/// Validation visitors receive a diagnostics collection during
Expand All @@ -32,6 +42,62 @@ impl Diagnostics {
pub fn add(&mut self, diagnostic: Diagnostic) {
self.0.push(diagnostic);
}

/// Adds a diagnostic to the collection, unless the diagnostic is for an
/// element that has an exception for the given rule.
///
/// If the diagnostic does not have a rule, the diagnostic is always added.
pub fn exceptable_add(
a-frantz marked this conversation as resolved.
Show resolved Hide resolved
&mut self,
diagnostic: Diagnostic,
element: SyntaxElement,
exceptable_nodes: Option<Vec<SyntaxKind>>,
) {
if diagnostic.rule().is_none() {
self.add(diagnostic);
return;
}

for node in element.ancestors().filter(|node| {
exceptable_nodes
.as_ref()
.map_or(true, |nodes| nodes.contains(&node.kind()))
}) {
if self
.exceptions_for(&node)
.contains(diagnostic.rule().expect("rule id"))
{
return;
}
}

self.add(diagnostic);
a-frantz marked this conversation as resolved.
Show resolved Hide resolved
}

/// Gets the set of excepted rule ids for the given syntax node.
pub fn exceptions_for(&self, node: &SyntaxNode) -> HashSet<String> {
let siblings = node
.siblings_with_tokens(Direction::Prev)
.skip(1)
.take_while(|s| s.kind() == SyntaxKind::Whitespace || s.kind() == SyntaxKind::Comment)
.filter_map(SyntaxElement::into_token);

let mut set = HashSet::default();
for sibling in siblings {
if sibling.kind() == SyntaxKind::Whitespace {
continue;
}

if let Some(ids) = sibling.text().strip_prefix(EXCEPT_COMMENT_PREFIX) {
for id in ids.split(',') {
let id = id.trim();
set.insert(id.to_string());
}
}
}

set
}
}

/// Implements an AST validator.
Expand Down
7 changes: 7 additions & 0 deletions wdl-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#![warn(rustdoc::broken_intra_doc_links)]

use wdl_ast::Diagnostics;
use wdl_ast::SyntaxKind;
use wdl_ast::Visitor;

pub mod rules;
Expand Down Expand Up @@ -64,6 +65,11 @@ pub trait Rule: Visitor<State = Diagnostics> {
fn url(&self) -> Option<&'static str> {
None
}

/// Gets the nodes that are exceptable for this rule.
///
/// If `None` is returned, all nodes are exceptable.
fn exceptable_nodes(&self) -> Option<Vec<SyntaxKind>>;
a-frantz marked this conversation as resolved.
Show resolved Hide resolved
}

/// Gets the default rule set.
Expand Down Expand Up @@ -105,6 +111,7 @@ pub fn rules() -> Vec<Box<dyn Rule>> {
Box::<rules::DisallowedOutputNameRule>::default(),
Box::<rules::ContainerValue>::default(),
Box::<rules::MissingRequirementsRule>::default(),
Box::<rules::UnknownRule>::default(),
];

// Ensure all the rule ids are unique and pascal case
Expand Down
2 changes: 2 additions & 0 deletions wdl-lint/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod section_order;
mod snake_case;
mod todo;
mod trailing_comma;
mod unknown_rule;
mod whitespace;

pub use blank_lines_between_elements::*;
Expand Down Expand Up @@ -72,4 +73,5 @@ pub use section_order::*;
pub use snake_case::*;
pub use todo::*;
pub use trailing_comma::*;
pub use unknown_rule::*;
pub use whitespace::*;
35 changes: 33 additions & 2 deletions wdl-lint/src/rules/blank_lines_between_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::SyntaxElement;
use wdl_ast::SyntaxKind;
use wdl_ast::SyntaxNode;
use wdl_ast::SyntaxToken;
Expand Down Expand Up @@ -102,6 +103,23 @@ impl Rule for BlankLinesBetweenElementsRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Spacing])
}

fn exceptable_nodes(&self) -> Option<Vec<SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::TaskDefinitionNode,
SyntaxKind::WorkflowDefinitionNode,
SyntaxKind::StructDefinitionNode,
SyntaxKind::InputSectionNode,
SyntaxKind::OutputSectionNode,
SyntaxKind::RuntimeSectionNode,
SyntaxKind::MetadataSectionNode,
SyntaxKind::ParameterMetadataSectionNode,
SyntaxKind::RequirementsSectionNode,
SyntaxKind::HintsSectionNode,
SyntaxKind::CommandSectionNode,
])
}
}

impl Visitor for BlankLinesBetweenElementsRule {
Expand Down Expand Up @@ -361,7 +379,11 @@ impl Visitor for BlankLinesBetweenElementsRule {
// one `\n` is allowed.
if self.state == State::InputSection || self.state == State::OutputSection {
if count > 1 {
state.add(excess_blank_line(p.text_range().to_span()));
state.exceptable_add(
a-frantz marked this conversation as resolved.
Show resolved Hide resolved
excess_blank_line(p.text_range().to_span()),
SyntaxElement::from(decl.syntax().clone()),
self.exceptable_nodes(),
);
}
} else {
let first = is_first_body(decl.syntax());
Expand Down Expand Up @@ -391,7 +413,11 @@ impl Visitor for BlankLinesBetweenElementsRule {
// one `\n` is allowed.
if self.state == State::InputSection || self.state == State::OutputSection {
if count > 1 {
state.add(excess_blank_line(p.text_range().to_span()));
state.exceptable_add(
excess_blank_line(p.text_range().to_span()),
SyntaxElement::from(decl.syntax().clone()),
self.exceptable_nodes(),
);
}
} else {
let first = is_first_body(decl.syntax());
Expand Down Expand Up @@ -452,6 +478,11 @@ fn flag_all_blank_lines_within(syntax: &SyntaxNode, state: &mut Diagnostics) {
let count = c.to_string().chars().filter(|c| *c == '\n').count();
if count > 1 {
state.add(excess_blank_line(c.text_range().to_span()));
// state.exceptable_add(
// excess_blank_line(c.text_range().to_span()),
// SyntaxElement::from(syntax.clone()),
// BlankLinesBetweenElementsRule::exceptable_nodes(),
a-frantz marked this conversation as resolved.
Show resolved Hide resolved
// );
}
}
});
Expand Down
8 changes: 8 additions & 0 deletions wdl-lint/src/rules/call_input_spacing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ impl Rule for CallInputSpacingRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Style, Tag::Clarity, Tag::Spacing])
}

fn exceptable_nodes(&self) -> Option<Vec<SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::CallStatementNode,
SyntaxKind::WorkflowDefinitionNode,
])
}
}

impl Visitor for CallInputSpacingRule {
Expand Down
8 changes: 8 additions & 0 deletions wdl-lint/src/rules/command_mixed_indentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ impl Rule for CommandSectionMixedIndentationRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Correctness, Tag::Spacing, Tag::Clarity])
}

fn exceptable_nodes(&self) -> Option<Vec<SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::TaskDefinitionNode,
SyntaxKind::CommandSectionNode,
])
}
}

impl Visitor for CommandSectionMixedIndentationRule {
Expand Down
4 changes: 4 additions & 0 deletions wdl-lint/src/rules/comment_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ impl Rule for CommentWhitespaceRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Spacing])
}

fn exceptable_nodes(&self) -> Option<Vec<SyntaxKind>> {
None
}
}

impl Visitor for CommentWhitespaceRule {
Expand Down
9 changes: 9 additions & 0 deletions wdl-lint/src/rules/container_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::SyntaxKind;
use wdl_ast::VisitReason;
use wdl_ast::Visitor;

Expand Down Expand Up @@ -127,6 +128,14 @@ impl Rule for ContainerValue {
// use a older, cached version until the user prompts it to upgrade).
TagSet::new(&[Tag::Clarity, Tag::Portability])
}

fn exceptable_nodes(&self) -> Option<Vec<wdl_ast::SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::RuntimeSectionNode,
SyntaxKind::RequirementsSectionNode,
])
}
}

impl Visitor for ContainerValue {
Expand Down
11 changes: 11 additions & 0 deletions wdl-lint/src/rules/deprecated_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::SyntaxKind;
use wdl_ast::VisitReason;
use wdl_ast::Visitor;

Expand Down Expand Up @@ -52,6 +53,16 @@ impl Rule for DeprecatedObjectRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Deprecated])
}

fn exceptable_nodes(&self) -> Option<Vec<wdl_ast::SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::TaskDefinitionNode,
SyntaxKind::WorkflowDefinitionNode,
SyntaxKind::BoundDeclNode,
SyntaxKind::UnboundDeclNode,
])
}
}

impl Visitor for DeprecatedObjectRule {
Expand Down
10 changes: 10 additions & 0 deletions wdl-lint/src/rules/deprecated_placeholder_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::SyntaxKind;
use wdl_ast::VisitReason;
use wdl_ast::Visitor;

Expand Down Expand Up @@ -85,6 +86,15 @@ impl Rule for DeprecatedPlaceholderOptionRule {
was the version where the deprecation was introduced."
}

fn exceptable_nodes(&self) -> Option<Vec<wdl_ast::SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::TaskDefinitionNode,
SyntaxKind::WorkflowDefinitionNode,
SyntaxKind::PlaceholderNode,
])
}

fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Deprecated])
}
Expand Down
8 changes: 8 additions & 0 deletions wdl-lint/src/rules/description_missing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::SyntaxKind;
use wdl_ast::ToSpan;
use wdl_ast::VisitReason;
use wdl_ast::Visitor;
Expand Down Expand Up @@ -66,6 +67,13 @@ impl Rule for DescriptionMissingRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Completeness])
}

fn exceptable_nodes(&self) -> Option<Vec<wdl_ast::SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::MetadataSectionNode,
])
}
}

impl Visitor for DescriptionMissingRule {
Expand Down
10 changes: 10 additions & 0 deletions wdl-lint/src/rules/disallowed_input_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::SyntaxKind;
use wdl_ast::VisitReason;
use wdl_ast::Visitor;

Expand Down Expand Up @@ -72,6 +73,15 @@ impl Rule for DisallowedInputNameRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Naming])
}

fn exceptable_nodes(&self) -> Option<Vec<wdl_ast::SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::InputSectionNode,
SyntaxKind::BoundDeclNode,
SyntaxKind::UnboundDeclNode,
])
}
}

impl Visitor for DisallowedInputNameRule {
Expand Down
9 changes: 9 additions & 0 deletions wdl-lint/src/rules/disallowed_output_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use wdl_ast::Diagnostics;
use wdl_ast::Document;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::SyntaxKind;
use wdl_ast::VisitReason;
use wdl_ast::Visitor;

Expand Down Expand Up @@ -72,6 +73,14 @@ impl Rule for DisallowedOutputNameRule {
fn tags(&self) -> TagSet {
TagSet::new(&[Tag::Naming])
}

fn exceptable_nodes(&self) -> Option<Vec<wdl_ast::SyntaxKind>> {
Some(vec![
SyntaxKind::VersionStatementNode,
SyntaxKind::OutputSectionNode,
SyntaxKind::BoundDeclNode,
])
}
}

impl Visitor for DisallowedOutputNameRule {
Expand Down
Loading
Loading