Skip to content

Commit

Permalink
write failing test about assignments in constant definitions and make
Browse files Browse the repository at this point in the history
test pass
  • Loading branch information
Alex Evanczuk committed Jun 2, 2023
1 parent 461ee3b commit ba09aa3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion scripts/packwerk_parity_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_pretty_printed_string(object)
end
end

all_files = Dir['packs/**/*.rb']
all_files = Dir['app/**/*.rb']
# Shuffle can be used to find a simpler error to fix
all_files.shuffle! if ENV['SHUFFLE']

Expand Down
2 changes: 1 addition & 1 deletion src/packs/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn run() {
write_cache(absolute_root.as_path(), path.as_path())
})
} else {
let pattern = absolute_root.join("packs/**/*.rb");
let pattern = absolute_root.join("app/**/*.rb");
let paths = glob(pattern.to_str().unwrap())
.expect("Failed to read glob pattern");
paths.par_bridge().for_each(|path| match path {
Expand Down
39 changes: 36 additions & 3 deletions src/packs/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,13 @@ impl<'a> Visitor for ReferenceCollector<'a> {
location: loc_to_range(node.expression_l, &self.line_col_lookup),
});

// TODO: Write a failing test for references to constants within a constant assignment
// self.visit(node.value)
if let Some(v) = node.value.to_owned() {
self.visit(&v);
} else {
// We don't handle constant assignments as part of a multi-assignment yet,
// e.g. A, B = 1, 2
// See the documentation for nodes::Casgn#value for more info.
}
}

// TODO: extract the common stuff from on_class
Expand Down Expand Up @@ -302,7 +307,7 @@ fn loc_to_range(loc: Loc, lookup: &LineColLookup) -> Range {

pub fn get_references(absolute_root: &Path) -> Vec<Reference> {
// Later this can come from config
let pattern = absolute_root.join("packs/**/*.rb");
let pattern = absolute_root.join("app/**/*.rb");

glob(pattern.to_str().unwrap())
.expect("Failed to read glob pattern")
Expand Down Expand Up @@ -1052,4 +1057,32 @@ end
]
);
}

#[test]
fn test_const_assignments_are_references() {
let contents: String = String::from(
"\
FOO = BAR
",
);

let references = extract_from_contents(contents);
assert_eq!(references.len(), 1);
let first_reference = references
.get(0)
.expect("There should be a reference at index 0");
assert_eq!(
Reference {
name: String::from("BAR"),
namespace_path: vec![],
location: Range {
start_row: 1,
start_col: 6,
end_row: 1,
end_col: 10
}
},
*first_reference,
);
}
}

0 comments on commit ba09aa3

Please sign in to comment.