2020-03-22 08:45:59 +00:00
|
|
|
use crate::visitor::{Visitor, VisitorContext};
|
2020-03-09 12:39:46 +00:00
|
|
|
use graphql_parser::query::{OperationDefinition, VariableDefinition};
|
|
|
|
use graphql_parser::schema::Value;
|
|
|
|
use graphql_parser::Pos;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct NoUnusedVariables<'a> {
|
|
|
|
vars: HashSet<(&'a str, Pos)>,
|
|
|
|
used_vars: HashSet<&'a str>,
|
|
|
|
}
|
|
|
|
|
2020-03-10 06:14:09 +00:00
|
|
|
impl<'a> NoUnusedVariables<'a> {
|
|
|
|
fn travel_value(&mut self, value: &'a Value) {
|
|
|
|
match value {
|
|
|
|
Value::Variable(var_name) => {
|
|
|
|
self.used_vars.insert(var_name.as_str());
|
|
|
|
}
|
|
|
|
Value::List(values) => {
|
|
|
|
for value in values {
|
|
|
|
self.travel_value(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Object(obj) => {
|
|
|
|
for value in obj.values() {
|
|
|
|
self.travel_value(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:39:46 +00:00
|
|
|
impl<'a> Visitor<'a> for NoUnusedVariables<'a> {
|
|
|
|
fn enter_operation_definition(
|
|
|
|
&mut self,
|
2020-03-22 08:45:59 +00:00
|
|
|
_ctx: &mut VisitorContext<'a>,
|
2020-03-09 12:39:46 +00:00
|
|
|
_operation_definition: &'a OperationDefinition,
|
|
|
|
) {
|
|
|
|
self.used_vars.clear();
|
|
|
|
self.vars.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_operation_definition(
|
|
|
|
&mut self,
|
2020-03-22 08:45:59 +00:00
|
|
|
ctx: &mut VisitorContext<'a>,
|
2020-03-09 12:39:46 +00:00
|
|
|
_operation_definition: &'a OperationDefinition,
|
|
|
|
) {
|
|
|
|
for (name, pos) in &self.vars {
|
|
|
|
if !self.used_vars.contains(name) {
|
|
|
|
ctx.report_error(vec![*pos], format!("Variable \"${}\" is not used", name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enter_variable_definition(
|
|
|
|
&mut self,
|
2020-03-22 08:45:59 +00:00
|
|
|
_ctx: &mut VisitorContext<'a>,
|
2020-03-09 12:39:46 +00:00
|
|
|
variable_definition: &'a VariableDefinition,
|
|
|
|
) {
|
|
|
|
self.vars
|
|
|
|
.insert((&variable_definition.name, variable_definition.position));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enter_argument(
|
|
|
|
&mut self,
|
2020-03-22 08:45:59 +00:00
|
|
|
_ctx: &mut VisitorContext<'a>,
|
2020-03-09 12:39:46 +00:00
|
|
|
_pos: Pos,
|
|
|
|
_name: &'a str,
|
|
|
|
value: &'a Value,
|
|
|
|
) {
|
2020-03-10 06:14:09 +00:00
|
|
|
self.travel_value(value);
|
2020-03-09 12:39:46 +00:00
|
|
|
}
|
|
|
|
}
|