-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add no_unused_variables and mark missing defer ones
- Loading branch information
1 parent
d16aab7
commit b0cdf31
Showing
2 changed files
with
81 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::super::{ValidationContext, ValidationRule}; | ||
use crate::{ast::*, visit::*}; | ||
|
||
/// Validate that a document uses all the variables it defines at least once. | ||
/// | ||
/// See [`ValidationRule`] | ||
/// [Reference](https://spec.graphql.org/draft/#sec-All-Variables-Used) | ||
#[derive(Default)] | ||
pub struct NoUnusedVariables<'a> { | ||
variables: Vec<&'a str>, | ||
used_variables: Vec<&'a str>, | ||
} | ||
|
||
impl<'a> ValidationRule<'a> for NoUnusedVariables<'a> {} | ||
|
||
impl<'a> Visitor<'a, ValidationContext<'a>> for NoUnusedVariables<'a> { | ||
fn enter_operation( | ||
&mut self, | ||
_ctx: &mut ValidationContext<'a>, | ||
operation: &'a OperationDefinition<'a>, | ||
_info: &VisitInfo | ||
) -> VisitFlow { | ||
operation.variable_definitions.children.iter().for_each(|def| { | ||
self.variables.push(def.variable.name); | ||
}); | ||
VisitFlow::Next | ||
} | ||
|
||
fn enter_field(&mut self, _ctx: &mut ValidationContext<'a>, field: &'a Field<'a>, _info: &VisitInfo) -> VisitFlow { | ||
field.arguments.children.iter().for_each(|arg| { | ||
if let Value::Variable(var) = arg.value { | ||
self.used_variables.push(var.name); | ||
} | ||
}); | ||
VisitFlow::Next | ||
} | ||
|
||
fn leave_document( | ||
&mut self, | ||
ctx: &mut ValidationContext<'a>, | ||
_document: &'a Document<'a>, | ||
_info: &VisitInfo | ||
) -> VisitFlow { | ||
self.variables.iter().for_each(|defined_variable| { | ||
if !self.used_variables.contains(defined_variable) { | ||
ctx.add_error("All defined variables must be at least used once"); | ||
} | ||
}); | ||
VisitFlow::Next | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn used_all_variables() { | ||
let ctx = ASTContext::new(); | ||
let document = | ||
Document::parse(&ctx, "query ($x: Int!) { todos(from: $x) { id } }").unwrap(); | ||
NoUnusedVariables::validate(&ctx, document).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn has_unused_variable() { | ||
let ctx = ASTContext::new(); | ||
let document = Document::parse( | ||
&ctx, | ||
"query ($x: Int!, $unused: String!) { todos(from: $x) { id } }", | ||
) | ||
.unwrap(); | ||
NoUnusedVariables::validate(&ctx, document).unwrap_err(); | ||
} | ||
} |