From ee95553f0e0386189de97040b37294c5ca23ae44 Mon Sep 17 00:00:00 2001 From: Jamie Mason Date: Sun, 9 Feb 2025 15:07:57 +0000 Subject: [PATCH] refactor(core): remove unused code --- src/dependency.rs | 27 +-------------------------- src/effects/fix.rs | 2 +- src/effects/lint.rs | 1 - src/effects/ui.rs | 15 --------------- src/instance.rs | 6 +----- src/packages.rs | 18 ------------------ src/rcfile.rs | 19 ------------------- src/semver_group.rs | 24 ++---------------------- src/specifier/parser.rs | 12 +----------- src/specifier/regexes.rs | 5 ----- src/version_group.rs | 7 ------- 11 files changed, 6 insertions(+), 130 deletions(-) diff --git a/src/dependency.rs b/src/dependency.rs index 45bb8547..033814eb 100644 --- a/src/dependency.rs +++ b/src/dependency.rs @@ -7,13 +7,7 @@ use { version_group::VersionGroupVariant, }, itertools::Itertools, - std::{ - cell::RefCell, - cmp::Ordering, - collections::{BTreeMap, HashSet}, - rc::Rc, - vec, - }, + std::{cell::RefCell, cmp::Ordering, rc::Rc, vec}, }; #[derive(Debug)] @@ -63,16 +57,6 @@ impl Dependency { } } - pub fn get_unique_specifiers(&self) -> Vec { - let set: HashSet = self - .instances - .borrow() - .iter() - .map(|instance| instance.descriptor.specifier.clone()) - .collect(); - set.into_iter().collect() - } - /// Return the most severe state of all instances in this group pub fn get_state(&self) -> InstanceState { self @@ -92,15 +76,6 @@ impl Dependency { .collect::>() } - pub fn get_instances_by_specifier(&self) -> BTreeMap>> { - let mut map = BTreeMap::new(); - for instance in self.instances.borrow().iter() { - let raw = instance.descriptor.specifier.get_raw(); - map.entry(raw).or_insert_with(Vec::new).push(Rc::clone(instance)); - } - map - } - pub fn set_expected_specifier(&self, specifier: &Specifier) -> &Self { *self.expected.borrow_mut() = Some(specifier.clone()); self diff --git a/src/effects/fix.rs b/src/effects/fix.rs index e5c9f938..67d9600a 100644 --- a/src/effects/fix.rs +++ b/src/effects/fix.rs @@ -37,7 +37,7 @@ pub fn run(ctx: Context) -> Context { match state { InstanceState::Unknown => {} - InstanceState::Valid(variant) => { + InstanceState::Valid(_) => { valid += 1; } InstanceState::Invalid(variant) => match variant { diff --git a/src/effects/lint.rs b/src/effects/lint.rs index 90f40543..474fa52f 100644 --- a/src/effects/lint.rs +++ b/src/effects/lint.rs @@ -15,7 +15,6 @@ pub fn run(ctx: Context) -> Context { } ui.print_group_header(group); if group.dependencies.borrow().len() == 0 { - let label = &group.selector.label; ui.print_empty_group(); return; } diff --git a/src/effects/ui.rs b/src/effects/ui.rs index 38d0e719..9b003f25 100644 --- a/src/effects/ui.rs +++ b/src/effects/ui.rs @@ -419,21 +419,6 @@ impl Ui<'_> { format!("in {file_link} at {path_to_prop}").normal() } - /// Issues related to whether a specifier is the highest or lowest semver are - /// all the same logic internally, so we have combined enum branches for them. - /// - /// From an end user point of view though it is clearer to have a specific - /// status code related to what has happened. - fn to_public_status_code(group_variant: &VersionGroupVariant, code: &str) -> ColoredString { - if matches!(group_variant, VersionGroupVariant::HighestSemver) { - code.replace("HighestOrLowestSemver", "HighestSemver").normal() - } else if matches!(group_variant, VersionGroupVariant::LowestSemver) { - code.replace("HighestOrLowestSemver", "LowestSemver").normal() - } else { - code.normal() - } - } - pub fn print_empty_group(&self) { warn!("Version Group is empty"); } diff --git a/src/instance.rs b/src/instance.rs index dfef2f0a..2cc7f214 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -10,7 +10,7 @@ use { }, log::debug, serde_json::Value, - std::{cell::RefCell, path::PathBuf, rc::Rc}, + std::{cell::RefCell, rc::Rc}, }; pub type InstanceId = String; @@ -58,8 +58,6 @@ pub struct Instance { /// be set to, if it was not possible to determine without user intervention, /// this will be a `None`. pub expected_specifier: RefCell>, - /// The file path of the package.json file this instance belongs to - pub file_path: PathBuf, /// A unique identifier for this instance pub id: InstanceId, /// Whether this is a package developed in this repo @@ -79,7 +77,6 @@ impl Instance { let package_name = descriptor.package.borrow().name.clone(); let id = format!("{} in {} of {}", &descriptor.name, dependency_type_name, package_name); let is_local = dependency_type_name == "/version"; - let file_path = descriptor.package.borrow().file_path.clone(); Instance { // deprecated actual_specifier: descriptor.specifier.clone(), @@ -91,7 +88,6 @@ impl Instance { descriptor, expected_specifier: RefCell::new(None), - file_path, id, is_local, preferred_semver_range: RefCell::new(None), diff --git a/src/packages.rs b/src/packages.rs index ef9cf850..657d8e6c 100644 --- a/src/packages.rs +++ b/src/packages.rs @@ -9,18 +9,15 @@ use { specifier::{basic_semver::BasicSemver, Specifier}, }, glob::glob, - itertools::Itertools, log::debug, serde::Deserialize, serde_json::Value, std::{ cell::RefCell, - cmp::Ordering, collections::HashMap, fs, path::{Path, PathBuf}, rc::Rc, - vec::IntoIter, }, }; @@ -53,21 +50,6 @@ impl Packages { self } - /// Get all packages sorted by their file path - pub fn sorted_by_path(&self, cwd: &PathBuf) -> IntoIter<&Rc>> { - self.all.iter().sorted_by(|a, b| { - let a = a.borrow().get_relative_file_path(cwd); - let b = b.borrow().get_relative_file_path(cwd); - if a == "package.json" { - return Ordering::Less; - } - if b == "package.json" { - return Ordering::Greater; - } - Ord::cmp(&a, &b) - }) - } - /// Get a package.json file by its name pub fn get_by_name(&self, name: &str) -> Option>> { self.all.iter().find(|package| package.borrow().name == name).map(Rc::clone) diff --git a/src/rcfile.rs b/src/rcfile.rs index ba4eb863..bc387682 100644 --- a/src/rcfile.rs +++ b/src/rcfile.rs @@ -129,25 +129,6 @@ pub struct Rcfile { } impl Rcfile { - /// Create a new rcfile containing only default values. - pub fn new() -> Rcfile { - Rcfile { - custom_types: empty_custom_types(), - dependency_groups: vec![], - format_bugs: default_true(), - format_repository: default_true(), - indent: default_indent(), - semver_groups: vec![], - sort_az: default_sort_az(), - sort_exports: default_sort_exports(), - sort_first: sort_first(), - sort_packages: default_true(), - source: default_source(), - strict: default_false(), - version_groups: vec![], - } - } - /// Until we can port cosmiconfig to Rust, call out to Node.js to get the /// rcfile from the filesystem pub fn from_cosmiconfig(cli: &Cli) -> Rcfile { diff --git a/src/semver_group.rs b/src/semver_group.rs index c272d5d6..3f1cd9d3 100644 --- a/src/semver_group.rs +++ b/src/semver_group.rs @@ -3,17 +3,8 @@ use { serde::Deserialize, }; -#[derive(Debug)] -pub enum SemverGroupVariant { - Disabled, - Ignored, - WithRange, -} - #[derive(Debug)] pub struct SemverGroup { - /// What behaviour has this group been configured to exhibit? - pub variant: SemverGroupVariant, /// Data to determine which instances should be added to this group pub selector: GroupSelector, /// The Semver Range which all instances in this group should use @@ -24,7 +15,6 @@ impl SemverGroup { /// Create a default group which ensures local packages are an exact version pub fn get_exact_local_specifiers() -> SemverGroup { SemverGroup { - variant: SemverGroupVariant::WithRange, selector: GroupSelector::new( /* all_packages: */ &Packages::new(), /* include_dependencies: */ vec![], @@ -40,7 +30,6 @@ impl SemverGroup { /// Create a default/catch-all group which would apply to any instance pub fn get_catch_all() -> SemverGroup { SemverGroup { - variant: SemverGroupVariant::Disabled, selector: GroupSelector::new( /* all_packages: */ &Packages::new(), /* include_dependencies: */ vec![], @@ -65,20 +54,11 @@ impl SemverGroup { ); if let Some(true) = group.is_disabled { - SemverGroup { - variant: SemverGroupVariant::Disabled, - selector, - range: None, - } + SemverGroup { selector, range: None } } else if let Some(true) = group.is_ignored { - SemverGroup { - variant: SemverGroupVariant::Ignored, - selector, - range: None, - } + SemverGroup { selector, range: None } } else if let Some(range) = &group.range { SemverGroup { - variant: SemverGroupVariant::WithRange, selector, range: SemverRange::new(range), } diff --git a/src/specifier/parser.rs b/src/specifier/parser.rs index 0379562f..1edb5d62 100644 --- a/src/specifier/parser.rs +++ b/src/specifier/parser.rs @@ -1,14 +1,4 @@ -use {super::regexes, log::debug}; - -/// Convert non-semver specifiers to semver when behaviour is identical -pub fn sanitise(specifier: &str) -> &str { - if specifier == "latest" || specifier == "x" { - debug!("Sanitising specifier: {} → *", specifier); - "*" - } else { - specifier - } -} +use super::regexes; pub fn is_simple_semver(str: &str) -> bool { is_exact(str) || is_latest(str) || is_major(str) || is_minor(str) || is_range(str) || is_range_major(str) || is_range_minor(str) diff --git a/src/specifier/regexes.rs b/src/specifier/regexes.rs index 847d6dba..cefeac4b 100644 --- a/src/specifier/regexes.rs +++ b/src/specifier/regexes.rs @@ -80,8 +80,3 @@ lazy_static! { /// "<1.0.0 >2.0.0" pub static ref INFIX_OPERATORS:Regex = Regex::new(r" ?(-|\|\|) ?| ").unwrap(); } - -/// Check if a string matches any of the regexes -pub fn matches_any(regexes: Vec<&Regex>, string: &str) -> bool { - regexes.iter().any(|re| re.is_match(string)) -} diff --git a/src/version_group.rs b/src/version_group.rs index f698694a..d37ccfe9 100644 --- a/src/version_group.rs +++ b/src/version_group.rs @@ -198,13 +198,6 @@ impl VersionGroup { } } -struct SnapToMismatches { - pub instance_ids: Vec, - pub actual_specifier: Specifier, - pub expected_specifier: Specifier, - pub snap_to_instance_id: String, -} - #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct AnyVersionGroup {