From 9e9db7fde09f7d4ab1cb2861c8392400e4b0b0a7 Mon Sep 17 00:00:00 2001 From: Stefanuk12 <42220813+Stefanuk12@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:40:23 +0000 Subject: [PATCH] ran `cargo fmt` --- src/rules/bundle/hybrid_require_mode/mod.rs | 12 +- src/rules/bundle/mod.rs | 2 +- src/rules/bundle/path_require_mode/mod.rs | 23 ++- src/rules/bundle/require_mode.rs | 11 +- src/rules/convert_require/mod.rs | 2 +- .../convert_require/roblox_require_mode.rs | 82 ++++++++--- src/rules/require/hybrid_require_mode.rs | 135 +++++++++++++----- src/rules/require/mod.rs | 2 +- src/rules/require/path_locator.rs | 8 +- src/rules/require/path_require_mode.rs | 14 +- .../require/require_path_locator_mode.rs | 2 +- 11 files changed, 206 insertions(+), 87 deletions(-) diff --git a/src/rules/bundle/hybrid_require_mode/mod.rs b/src/rules/bundle/hybrid_require_mode/mod.rs index ff6a9d5..cd94e3c 100644 --- a/src/rules/bundle/hybrid_require_mode/mod.rs +++ b/src/rules/bundle/hybrid_require_mode/mod.rs @@ -1,4 +1,12 @@ -use crate::{nodes::Block, process::{NodeVisitor, ScopeVisitor}, rules::{bundle::BundleOptions, require::HybridRequireMode, Context, FlawlessRule, ReplaceReferencedTokens}, utils::Timer}; +use crate::{ + nodes::Block, + process::{NodeVisitor, ScopeVisitor}, + rules::{ + bundle::BundleOptions, require::HybridRequireMode, Context, FlawlessRule, + ReplaceReferencedTokens, + }, + utils::Timer, +}; use super::path_require_mode::RequirePathProcessor; @@ -29,4 +37,4 @@ pub(crate) fn process_block( let mut processor = RequirePathProcessor::new(context, options, path_require_mode); ScopeVisitor::visit_block(block, &mut processor); processor.apply(block, context) -} \ No newline at end of file +} diff --git a/src/rules/bundle/mod.rs b/src/rules/bundle/mod.rs index aaae08c..ab4b503 100644 --- a/src/rules/bundle/mod.rs +++ b/src/rules/bundle/mod.rs @@ -1,5 +1,5 @@ -pub(crate) mod path_require_mode; pub(crate) mod hybrid_require_mode; +pub(crate) mod path_require_mode; mod require_mode; use std::path::Path; diff --git a/src/rules/bundle/path_require_mode/mod.rs b/src/rules/bundle/path_require_mode/mod.rs index 70755d9..0c3fdad 100644 --- a/src/rules/bundle/path_require_mode/mod.rs +++ b/src/rules/bundle/path_require_mode/mod.rs @@ -47,7 +47,9 @@ pub(crate) struct RequirePathProcessor<'a, 'b, 'resources, 'code, T: RequirePath errors: Vec, } -impl<'a, 'b, 'code, 'resources, T: RequirePathLocatorMode> RequirePathProcessor<'a, 'b, 'code, 'resources, T> { +impl<'a, 'b, 'code, 'resources, T: RequirePathLocatorMode> + RequirePathProcessor<'a, 'b, 'code, 'resources, T> +{ pub(crate) fn new<'context>( context: &'context Context<'b, 'resources, 'code>, options: &'a BundleOptions, @@ -87,11 +89,12 @@ impl<'a, 'b, 'code, 'resources, T: RequirePathLocatorMode> RequirePathProcessor< fn require_call(&self, call: &FunctionCall) -> Option { if let Some(x) = self.path_locator.require_call(call, &self.source) { - return Some(x) + return Some(x); } - + if is_require_call(call, self) { - self.path_locator.match_path_require_call(call, &self.source) + self.path_locator + .match_path_require_call(call, &self.source) } else { None } @@ -265,7 +268,9 @@ impl<'a, 'b, 'code, 'resources, T: RequirePathLocatorMode> RequirePathProcessor< } } -impl<'a, 'b, 'resources, 'code, T: RequirePathLocatorMode> Deref for RequirePathProcessor<'a, 'b, 'resources, 'code, T> { +impl<'a, 'b, 'resources, 'code, T: RequirePathLocatorMode> Deref + for RequirePathProcessor<'a, 'b, 'resources, 'code, T> +{ type Target = IdentifierTracker; fn deref(&self) -> &Self::Target { @@ -273,7 +278,9 @@ impl<'a, 'b, 'resources, 'code, T: RequirePathLocatorMode> Deref for RequirePath } } -impl<'a, 'b, 'resources, 'code, T: RequirePathLocatorMode> DerefMut for RequirePathProcessor<'a, 'b, 'resources, 'code, T> { +impl<'a, 'b, 'resources, 'code, T: RequirePathLocatorMode> DerefMut + for RequirePathProcessor<'a, 'b, 'resources, 'code, T> +{ fn deref_mut(&mut self) -> &mut Self::Target { &mut self.identifier_tracker } @@ -304,7 +311,9 @@ where expression } -impl<'a, 'b, 'resources, 'code, T: RequirePathLocatorMode> NodeProcessor for RequirePathProcessor<'a, 'b, 'resources, 'code, T> { +impl<'a, 'b, 'resources, 'code, T: RequirePathLocatorMode> NodeProcessor + for RequirePathProcessor<'a, 'b, 'resources, 'code, T> +{ fn process_expression(&mut self, expression: &mut Expression) { if let Expression::Call(call) = expression { if let Some(replace_with) = self.try_inline_call(call) { diff --git a/src/rules/bundle/require_mode.rs b/src/rules/bundle/require_mode.rs index 2d20c85..fb72c61 100644 --- a/src/rules/bundle/require_mode.rs +++ b/src/rules/bundle/require_mode.rs @@ -2,10 +2,13 @@ use std::str::FromStr; use serde::{Deserialize, Serialize}; -use crate::rules::{require::{HybridRequireMode, PathRequireMode}, RuleProcessResult}; +use crate::rules::{ + require::{HybridRequireMode, PathRequireMode}, + RuleProcessResult, +}; use crate::{nodes::Block, rules::Context}; -use super::{path_require_mode, hybrid_require_mode, BundleOptions}; +use super::{hybrid_require_mode, path_require_mode, BundleOptions}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(deny_unknown_fields, rename_all = "snake_case", tag = "name")] @@ -54,10 +57,10 @@ impl BundleRequireMode { match self { Self::Path(path_require_mode) => { path_require_mode::process_block(block, context, options, path_require_mode) - }, + } Self::Hybrid(hybrid_require_mode) => { hybrid_require_mode::process_block(block, context, options, hybrid_require_mode) - }, + } } } } diff --git a/src/rules/convert_require/mod.rs b/src/rules/convert_require/mod.rs index 3d60576..89d1085 100644 --- a/src/rules/convert_require/mod.rs +++ b/src/rules/convert_require/mod.rs @@ -13,7 +13,7 @@ use crate::rules::{Context, RuleConfiguration, RuleConfigurationError, RulePrope use instance_path::InstancePath; pub use roblox_index_style::RobloxIndexStyle; -pub use roblox_require_mode::{RobloxRequireMode, parse_roblox}; +pub use roblox_require_mode::{parse_roblox, RobloxRequireMode}; use super::{verify_required_properties, Rule, RuleProcessResult}; diff --git a/src/rules/convert_require/roblox_require_mode.rs b/src/rules/convert_require/roblox_require_mode.rs index dc00d44..d47e62b 100644 --- a/src/rules/convert_require/roblox_require_mode.rs +++ b/src/rules/convert_require/roblox_require_mode.rs @@ -7,7 +7,10 @@ use crate::{ utils, DarkluaError, }; -use std::{collections::VecDeque, path::{Component, Path, PathBuf}}; +use std::{ + collections::VecDeque, + path::{Component, Path, PathBuf}, +}; use super::{ instance_path::{get_parent_instance, script_identifier}, @@ -244,15 +247,17 @@ fn get_relative_parent_path(path: &Path) -> &Path { pub fn parse_roblox(call: &FunctionCall, current_path: &Path) -> DarkluaResult> { let Arguments::Tuple(args) = call.get_arguments() else { - Err(DarkluaError::custom("unexpected require call, only accepts tuples") - .context("while finding roblox requires") + Err( + DarkluaError::custom("unexpected require call, only accepts tuples") + .context("while finding roblox requires"), )? }; - + let mut path_builder = VecDeque::::new(); let Some(Expression::Field(field)) = args.iter_values().next() else { - Err(DarkluaError::custom("unexpected require argument, only accepts fields") - .context("while getting roblox path") + Err( + DarkluaError::custom("unexpected require argument, only accepts fields") + .context("while getting roblox path"), )? }; @@ -265,49 +270,86 @@ pub fn parse_roblox(call: &FunctionCall, current_path: &Path) -> DarkluaResult, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn parse_roblox_prefix( + prefix: &Prefix, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { match prefix { Prefix::Field(x) => parse_roblox_field(&x, path_builder, current_path)?, Prefix::Index(x) => parse_roblox_index(&x, path_builder, current_path)?, - Prefix::Identifier(x) => handle_roblox_script_parent(&x.get_name(), path_builder, current_path)?, - _ => Err(DarkluaError::custom("unexpected prefix, only constants accepted").context("while parsing roblox require"))? + Prefix::Identifier(x) => { + handle_roblox_script_parent(&x.get_name(), path_builder, current_path)? + } + _ => Err( + DarkluaError::custom("unexpected prefix, only constants accepted") + .context("while parsing roblox require"), + )?, }; Ok(()) } -fn parse_roblox_expression(expression: &Expression, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn parse_roblox_expression( + expression: &Expression, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { match expression { Expression::Field(x) => parse_roblox_field(x, path_builder, current_path)?, Expression::Index(x) => parse_roblox_index(x, path_builder, current_path)?, - Expression::Identifier(x) => handle_roblox_script_parent(&x.get_name(), path_builder, current_path)?, - Expression::String(x) => handle_roblox_script_parent(x.get_value(), path_builder, current_path)?, - _ => Err(DarkluaError::custom("unexpected expression, only constants accepted").context("while parsing roblox require"))?, + Expression::Identifier(x) => { + handle_roblox_script_parent(&x.get_name(), path_builder, current_path)? + } + Expression::String(x) => { + handle_roblox_script_parent(x.get_value(), path_builder, current_path)? + } + _ => Err( + DarkluaError::custom("unexpected expression, only constants accepted") + .context("while parsing roblox require"), + )?, }; Ok(()) } -fn parse_roblox_field(field: &Box, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn parse_roblox_field( + field: &Box, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { handle_roblox_script_parent(&field.get_field().get_name(), path_builder, current_path)?; parse_roblox_prefix(field.get_prefix(), path_builder, current_path) } -fn parse_roblox_index(index: &Box, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn parse_roblox_index( + index: &Box, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { parse_roblox_expression(index.get_index(), path_builder, current_path)?; parse_roblox_prefix(index.get_prefix(), path_builder, current_path) } -fn handle_roblox_script_parent(str: &str, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn handle_roblox_script_parent( + str: &str, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { match str { "script" => { - if path_builder.front().map(|x| x != "Parent").unwrap_or_default() { - Err(DarkluaError::custom("expected .Parent after script").context("while parsing roblox require"))? + if path_builder + .front() + .map(|x| x != "Parent") + .unwrap_or_default() + { + Err(DarkluaError::custom("expected .Parent after script") + .context("while parsing roblox require"))? } path_builder.pop_front(); - }, + } x @ "Parent" => { current_path.pop(); path_builder.push_front(x.to_string()); - }, + } x => path_builder.push_front(x.to_string()), }; Ok(()) diff --git a/src/rules/require/hybrid_require_mode.rs b/src/rules/require/hybrid_require_mode.rs index 42ae953..54ad29d 100644 --- a/src/rules/require/hybrid_require_mode.rs +++ b/src/rules/require/hybrid_require_mode.rs @@ -1,8 +1,21 @@ -use std::{collections::{HashMap, VecDeque}, path::{Path, PathBuf}}; - -use serde::{Serialize, Deserialize}; - -use crate::{frontend::DarkluaResult, nodes::{Arguments, Expression, FieldExpression, FunctionCall, Prefix}, rules::{parse_roblox, require::path_require_mode::{get_default_module_folder_name, is_default_module_folder_name}}, DarkluaError}; +use std::{ + collections::{HashMap, VecDeque}, + path::{Path, PathBuf}, +}; + +use serde::{Deserialize, Serialize}; + +use crate::{ + frontend::DarkluaResult, + nodes::{Arguments, Expression, FieldExpression, FunctionCall, Prefix}, + rules::{ + parse_roblox, + require::path_require_mode::{ + get_default_module_folder_name, is_default_module_folder_name, + }, + }, + DarkluaError, +}; use super::{match_path_require_call, RequirePathLocatorMode}; @@ -27,23 +40,30 @@ impl RequirePathLocatorMode for HybridRequireMode { &self.module_folder_name } fn match_path_require_call(&self, call: &FunctionCall, source: &Path) -> Option { - parse_roblox(call, source).ok().flatten().or(match_path_require_call(call)) + parse_roblox(call, source) + .ok() + .flatten() + .or(match_path_require_call(call)) } fn require_call(&self, call: &FunctionCall, source: &Path) -> Option { if !self.convert_ts_imports { - return None + return None; } - let Prefix::Field(field) = call.get_prefix() else { return None }; + let Prefix::Field(field) = call.get_prefix() else { + return None; + }; match field.get_prefix() { Prefix::Identifier(x) if x.get_name() == "TS" && x.get_token().is_none() => Some(()), - _ => None + _ => None, }?; if !(field.get_field().get_name() == "import" && field.get_field().get_token().is_none()) { - return None + return None; } - let Arguments::Tuple(values) = call.get_arguments() else { return None }; + let Arguments::Tuple(values) = call.get_arguments() else { + return None; + }; let mut current_path = source.to_path_buf(); if current_path.ends_with("init.lua") { @@ -52,7 +72,9 @@ impl RequirePathLocatorMode for HybridRequireMode { let mut path_builder = VecDeque::new(); - values.iter_values().for_each(|v| { parse_roblox_expression(v, &mut path_builder, &mut current_path).ok(); }); + values.iter_values().for_each(|v| { + parse_roblox_expression(v, &mut path_builder, &mut current_path).ok(); + }); while let Some(x) = path_builder.pop_back() { current_path.push(x); @@ -66,23 +88,34 @@ fn parse_roblox_call(call: &FunctionCall, current_path: &mut PathBuf) -> Darklua match call.get_prefix() { Prefix::Field(field) => { match field.get_prefix() { - Prefix::Identifier(x) if x.get_name() == "TS" && x.get_token().is_none() => {}, - _ => return Err(DarkluaError::custom("expected call to be apart of the TS module").context("while parsing roblox-ts require"))? + Prefix::Identifier(x) if x.get_name() == "TS" && x.get_token().is_none() => {} + _ => { + return Err( + DarkluaError::custom("expected call to be apart of the TS module") + .context("while parsing roblox-ts require"), + )? + } }; - if !(field.get_field().get_name() == "getModule" && field.get_field().get_token().is_none()) { - return Err(DarkluaError::custom("expected call to be TS.getModule").context("while parsing roblox-ts require")); + if !(field.get_field().get_name() == "getModule" + && field.get_field().get_token().is_none()) + { + return Err(DarkluaError::custom("expected call to be TS.getModule") + .context("while parsing roblox-ts require")); } - }, - _ => return Err(DarkluaError::custom("a"))? + } + _ => return Err(DarkluaError::custom("a"))?, }; - + let mut temp_path = PathBuf::from("node_modules"); - let Arguments::Tuple(args) = call.get_arguments() else { return Err(DarkluaError::custom("expected call arguments for TS.getModule to be a tuple").context("while parsing roblox-ts require"))? }; - args.iter_values().for_each(|arg| { - match arg { - Expression::String(x) => temp_path.push(x.get_value().to_string()), - _ => {}, - } + let Arguments::Tuple(args) = call.get_arguments() else { + return Err( + DarkluaError::custom("expected call arguments for TS.getModule to be a tuple") + .context("while parsing roblox-ts require"), + )?; + }; + args.iter_values().for_each(|arg| match arg { + Expression::String(x) => temp_path.push(x.get_value().to_string()), + _ => {} }); let _ = temp_path.join(¤t_path); @@ -91,36 +124,66 @@ fn parse_roblox_call(call: &FunctionCall, current_path: &mut PathBuf) -> Darklua Ok(()) } -fn parse_roblox_prefix(prefix: &Prefix, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn parse_roblox_prefix( + prefix: &Prefix, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { match prefix { Prefix::Field(x) => parse_roblox_field(&x, path_builder, current_path)?, - Prefix::Identifier(x) => handle_roblox_script_parent(&x.get_name(), path_builder, current_path)?, + Prefix::Identifier(x) => { + handle_roblox_script_parent(&x.get_name(), path_builder, current_path)? + } Prefix::Call(x) => parse_roblox_call(x, current_path)?, - _ => Err(DarkluaError::custom("unexpected prefix, only constants accepted").context("while parsing roblox require"))? + _ => Err( + DarkluaError::custom("unexpected prefix, only constants accepted") + .context("while parsing roblox require"), + )?, }; Ok(()) } -fn parse_roblox_expression(expression: &Expression, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn parse_roblox_expression( + expression: &Expression, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { match expression { Expression::Field(x) => parse_roblox_field(x, path_builder, current_path)?, - Expression::Identifier(x) => handle_roblox_script_parent(&x.get_name(), path_builder, current_path)?, - Expression::String(x) => handle_roblox_script_parent(x.get_value(), path_builder, current_path)?, + Expression::Identifier(x) => { + handle_roblox_script_parent(&x.get_name(), path_builder, current_path)? + } + Expression::String(x) => { + handle_roblox_script_parent(x.get_value(), path_builder, current_path)? + } Expression::Call(x) => parse_roblox_call(x, current_path)?, - _ => Err(DarkluaError::custom("unexpected expression, only constants accepted").context("while parsing roblox require"))?, + _ => Err( + DarkluaError::custom("unexpected expression, only constants accepted") + .context("while parsing roblox require"), + )?, }; Ok(()) } -fn parse_roblox_field(field: &Box, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn parse_roblox_field( + field: &Box, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { parse_roblox_prefix(field.get_prefix(), path_builder, current_path)?; handle_roblox_script_parent(&field.get_field().get_name(), path_builder, current_path) } -fn handle_roblox_script_parent(str: &str, path_builder: &mut VecDeque, current_path: &mut PathBuf) -> DarkluaResult<()> { +fn handle_roblox_script_parent( + str: &str, + path_builder: &mut VecDeque, + current_path: &mut PathBuf, +) -> DarkluaResult<()> { match str { - "script" => {}, - "Parent" => { current_path.pop(); }, + "script" => {} + "Parent" => { + current_path.pop(); + } x => path_builder.push_front(x.to_string()), }; Ok(()) diff --git a/src/rules/require/mod.rs b/src/rules/require/mod.rs index f44e6ed..aac792b 100644 --- a/src/rules/require/mod.rs +++ b/src/rules/require/mod.rs @@ -5,8 +5,8 @@ mod path_locator; mod path_require_mode; mod require_path_locator_mode; +pub(crate) use hybrid_require_mode::HybridRequireMode; pub(crate) use match_require::{is_require_call, match_path_require_call}; pub(crate) use path_locator::RequirePathLocator; pub(crate) use path_require_mode::PathRequireMode; -pub(crate) use hybrid_require_mode::HybridRequireMode; pub(crate) use require_path_locator_mode::RequirePathLocatorMode; diff --git a/src/rules/require/path_locator.rs b/src/rules/require/path_locator.rs index 22a6fc2..ed0a208 100644 --- a/src/rules/require/path_locator.rs +++ b/src/rules/require/path_locator.rs @@ -23,18 +23,14 @@ impl<'a, 'b, 'c, T: RequirePathLocatorMode> RequirePathLocator<'a, 'b, 'c, T> { } } - pub(crate) fn require_call( - &self, - call: &FunctionCall, - source: &Path - ) -> Option { + pub(crate) fn require_call(&self, call: &FunctionCall, source: &Path) -> Option { self.path_require_mode.require_call(call, source) } pub(crate) fn match_path_require_call( &self, call: &FunctionCall, - source: &Path + source: &Path, ) -> Option { self.path_require_mode.match_path_require_call(call, source) } diff --git a/src/rules/require/path_require_mode.rs b/src/rules/require/path_require_mode.rs index 5d1660b..0632476 100644 --- a/src/rules/require/path_require_mode.rs +++ b/src/rules/require/path_require_mode.rs @@ -83,25 +83,23 @@ impl PathRequireMode { let mut current_path = context.current_path().to_path_buf(); current_path.pop(); let diff = pathdiff::diff_paths(&path, ¤t_path).ok_or( - DarkluaError::custom("invalid path difference") - .context("path require mode cannot") + DarkluaError::custom("invalid path difference").context("path require mode cannot"), )?; - + let mut path_str = diff .to_str() .ok_or( DarkluaError::custom("invalid non-UTF8 characters") - .context("path require mode cannot") + .context("path require mode cannot"), )? .replace("\\", "/"); if !(path_str.starts_with("./")) { path_str = String::from("./") + path_str.as_str(); } - let string_expr = StringExpression::new(&format!("[[{path_str}]]")).map_err(|e| - DarkluaError::custom(format!("{e}")) - .context("path require mode cannot") - )?; + let string_expr = StringExpression::new(&format!("[[{path_str}]]")).map_err(|e| { + DarkluaError::custom(format!("{e}")).context("path require mode cannot") + })?; Ok(Some(crate::nodes::Arguments::String(string_expr))) } } diff --git a/src/rules/require/require_path_locator_mode.rs b/src/rules/require/require_path_locator_mode.rs index 3cee9d3..5eb5b3a 100644 --- a/src/rules/require/require_path_locator_mode.rs +++ b/src/rules/require/require_path_locator_mode.rs @@ -9,4 +9,4 @@ pub trait RequirePathLocatorMode { None } fn match_path_require_call(&self, call: &FunctionCall, source: &Path) -> Option; -} \ No newline at end of file +}