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

chore(compiler): make unused variable validation more efficient #887

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Changes from all 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
30 changes: 19 additions & 11 deletions crates/apollo-compiler/src/validation/variable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::ast;
use crate::collections::HashMap;
use crate::collections::HashSet;
use crate::executable;
use crate::validation::diagnostics::DiagnosticData;
use crate::validation::DiagnosticList;
Expand Down Expand Up @@ -167,8 +166,8 @@ pub(crate) fn validate_unused_variables(
document: &ExecutableDocument,
operation: &executable::Operation,
) {
let defined_vars: HashSet<_> = operation.variables.iter().map(|var| &var.name).collect();
let locations: HashMap<_, _> = operation
// Start off by considering all variables unused: names are removed from this as we find them.
let mut unused_vars: HashMap<_, _> = operation
.variables
.iter()
.map(|var| {
Expand All @@ -178,23 +177,32 @@ pub(crate) fn validate_unused_variables(
)
})
.collect();
let mut used_vars = HashSet::default();
let walked = walk_selections(
document,
&operation.selection_set,
|selection| match selection {
executable::Selection::Field(field) => {
used_vars.extend(variables_in_directives(&field.directives));
used_vars.extend(variables_in_arguments(&field.arguments));
for used in variables_in_directives(&field.directives) {
unused_vars.remove(used);
}
for used in variables_in_arguments(&field.arguments) {
unused_vars.remove(used);
}
}
executable::Selection::FragmentSpread(fragment) => {
if let Some(fragment_def) = document.fragments.get(&fragment.fragment_name) {
used_vars.extend(variables_in_directives(&fragment_def.directives));
for used in variables_in_directives(&fragment_def.directives) {
unused_vars.remove(used);
}
}
for used in variables_in_directives(&fragment.directives) {
unused_vars.remove(used);
}
used_vars.extend(variables_in_directives(&fragment.directives));
}
executable::Selection::InlineFragment(fragment) => {
used_vars.extend(variables_in_directives(&fragment.directives));
for used in variables_in_directives(&fragment.directives) {
unused_vars.remove(used);
}
}
},
);
Expand All @@ -203,9 +211,9 @@ pub(crate) fn validate_unused_variables(
return;
}

for &unused_var in defined_vars.difference(&used_vars) {
for (unused_var, location) in unused_vars {
diagnostics.push(
locations[unused_var],
location,
DiagnosticData::UnusedVariable {
name: unused_var.clone(),
},
Expand Down