forked from stjude-rust-labs/wdl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move import validation from
wdl-analysis
to wdl-ast
.
This commit moves the validation of imports from `wdl-anlaysis` to `wdl-ast` where the rest of the validation visits occur. Fixes stjude-rust-labs#152.
- Loading branch information
1 parent
da8b7e6
commit e65deaa
Showing
11 changed files
with
118 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
wdl-analysis/tests/analysis/import-namespace-invalid/invalid-namespace.wdl
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//! Validation of imports. | ||
use crate::v1; | ||
use crate::v1::StringPart; | ||
use crate::AstNode; | ||
use crate::Diagnostic; | ||
use crate::Diagnostics; | ||
use crate::Document; | ||
use crate::Span; | ||
use crate::SupportedVersion; | ||
use crate::ToSpan; | ||
use crate::VisitReason; | ||
use crate::Visitor; | ||
|
||
/// Creates an "empty import" diagnostic | ||
fn empty_import(span: Span) -> Diagnostic { | ||
Diagnostic::error("import URI cannot be empty").with_highlight(span) | ||
} | ||
|
||
/// Creates a "placeholder in import" diagnostic | ||
fn placeholder_in_import(span: Span) -> Diagnostic { | ||
Diagnostic::error("import URI cannot contain placeholders") | ||
.with_label("remove this placeholder", span) | ||
} | ||
|
||
/// Creates an "invalid import namespace" diagnostic | ||
fn invalid_import_namespace(span: Span) -> Diagnostic { | ||
Diagnostic::error("import namespace is not a valid WDL identifier") | ||
.with_label("a namespace cannot be derived from this import path", span) | ||
.with_fix("add an `as` clause to the import to specify a namespace") | ||
} | ||
|
||
/// An AST visitor that ensures that imports are valid. | ||
#[derive(Debug, Default)] | ||
pub struct ImportsVisitor; | ||
|
||
impl Visitor for ImportsVisitor { | ||
type State = Diagnostics; | ||
|
||
fn document( | ||
&mut self, | ||
_: &mut Self::State, | ||
reason: VisitReason, | ||
_: &Document, | ||
_: SupportedVersion, | ||
) { | ||
if reason == VisitReason::Exit { | ||
return; | ||
} | ||
|
||
*self = Default::default(); | ||
} | ||
|
||
fn import_statement( | ||
&mut self, | ||
state: &mut Self::State, | ||
reason: VisitReason, | ||
stmt: &v1::ImportStatement, | ||
) { | ||
if reason == VisitReason::Exit { | ||
return; | ||
} | ||
|
||
let uri = stmt.uri(); | ||
if uri.is_empty() { | ||
state.add(empty_import(uri.syntax().text_range().to_span())); | ||
return; | ||
} | ||
|
||
if uri.text().is_none() { | ||
let span = uri | ||
.parts() | ||
.find_map(|p| match p { | ||
StringPart::Text(_) => None, | ||
StringPart::Placeholder(p) => Some(p.syntax().text_range().to_span()), | ||
}) | ||
.expect("should have a placeholder span"); | ||
|
||
state.add(placeholder_in_import(span)); | ||
return; | ||
} | ||
|
||
if stmt.namespace().is_none() { | ||
state.add(invalid_import_namespace( | ||
uri.syntax().text_range().to_span(), | ||
)); | ||
return; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...port-namespace-invalid/source.diagnostics → ...on/import-namespace-invalid/source.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
.../placeholder-in-import/source.diagnostics → ...ation/placeholder-in-import/source.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.