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

style(*): apply some lint suggestions #474

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crates/compiler/src/update/patch_module_group_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ mod tests {
);

let affected_groups = patch_module_group_graph(
start_points.clone(),
start_points,
&diff_result,
&removed_modules,
&mut module_graph,
Expand Down Expand Up @@ -535,7 +535,7 @@ mod tests {
);

let affected_groups = patch_module_group_graph(
start_points.clone(),
start_points,
&diff_result,
&removed_modules,
&mut module_graph,
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ pub fn get_compiler_result(compiler: &Compiler, entry_name: Option<&String>) ->
}

pub fn load_expected_result(cwd: PathBuf) -> String {
let expected_result = std::fs::read_to_string(cwd.join("output.js")).unwrap_or("".to_string());
expected_result

std::fs::read_to_string(cwd.join("output.js")).unwrap_or("".to_string())
}

pub fn assert_compiler_result(compiler: &Compiler, entry_name: Option<&String>) {
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/tests/tree_shake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn tree_shake_html_entry() {

let entry_name = "index".to_string();
let compiler = create_compiler(
HashMap::from([(entry_name.clone(), "./index.html".to_string())]),
HashMap::from([(entry_name, "./index.html".to_string())]),
cwd.to_path_buf(),
crate_path,
false,
Expand Down
10 changes: 5 additions & 5 deletions crates/compiler/tests/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn update_without_dependencies_change() {
let cwd = file.parent().unwrap().to_path_buf();
let compiler = create_update_compiler(
HashMap::from([("index".to_string(), "./index.html".to_string())]),
cwd.clone(),
cwd,
crate_path,
false,
);
Expand All @@ -69,7 +69,7 @@ fn update_without_dependencies_change() {
.to_string();
let result = compiler
.update(
vec![(update_file.clone(), UpdateType::Updated)],
vec![(update_file, UpdateType::Updated)],
|| {},
true,
)
Expand All @@ -92,7 +92,7 @@ fn update_without_dependencies_change_css() {
let cwd = file.parent().unwrap().to_path_buf();
let compiler = create_update_compiler(
HashMap::from([("index".to_string(), "./index.html".to_string())]),
cwd.clone(),
cwd,
crate_path,
false,
);
Expand Down Expand Up @@ -121,7 +121,7 @@ fn update_without_dependencies_change_css() {

let result = compiler
.update(
vec![(update_file.clone(), UpdateType::Updated)],
vec![(update_file, UpdateType::Updated)],
|| {},
false,
)
Expand Down Expand Up @@ -228,7 +228,7 @@ fn update_with_dependencies_change_css_modules() {
original_ts_file.write_all(original_ts.as_bytes()).unwrap();
let result = compiler
.update(
vec![(update_file.clone(), UpdateType::Updated)],
vec![(update_file, UpdateType::Updated)],
|| {},
false,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/macro_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syn::{parse_macro_input, ItemStruct};
#[proc_macro_attribute]
pub fn farm_plugin(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item_struct: ItemStruct = parse_macro_input!(item);
let struct_name = item_struct.ident.clone();
let struct_name = &item_struct.ident;

let ts = quote! {
#[no_mangle]
Expand Down
4 changes: 2 additions & 2 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ impl FsWatcher {
return false;
}

return item_comps.nth(index).unwrap() == comp;
item_comps.nth(index).unwrap() == comp
}) {
prefix_comps.push(comp.clone());
prefix_comps.push(comp);
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/plugin_css/src/dep_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Visit for DepAnalyzer {

pub fn is_source_ignored(source: &str) -> bool {
source.starts_with("http")
|| source.starts_with("/")
|| source.starts_with('/')
|| source.starts_with("data:")
|| source.starts_with("#")
|| source.starts_with('#')
}
56 changes: 26 additions & 30 deletions crates/plugin_css/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct FarmPluginCss {
ast_map: Mutex<HashMap<String, Stylesheet>>,
}

pub fn wrapper_style_load(code: &String, id: String, css_deps: &String) -> String {
pub fn wrapper_style_load(code: &str, id: String, css_deps: &String) -> String {
format!(
r#"
const cssCode = `{}`;
Expand All @@ -67,8 +67,8 @@ module.onDispose(() => {{
style.remove();
}});
"#,
code.replace("`", "'").replace("\\", "\\\\"),
id.replace("\\", "\\\\"),
code.replace('`', "'").replace('\\', "\\\\"),
id.replace('\\', "\\\\"),
css_deps
)
}
Expand Down Expand Up @@ -227,19 +227,16 @@ impl Plugin for FarmPluginCss {

let code = format!(
r#"
import "{}?{}";
import "{}{}?{}";
{}
export default {{{}}}
"#,
format!(
"{}{}",
if cfg!(windows) {
param.resolved_path.replace("\\", "\\\\")
} else {
param.resolved_path.to_string()
},
FARM_CSS_MODULES_SUFFIX
),
if cfg!(windows) {
param.resolved_path.replace('\\', "\\\\")
} else {
param.resolved_path.to_string()
},
FARM_CSS_MODULES_SUFFIX,
// add hash to avoid cache, make sure hmr works
sha256(param.content.replace("\r\n", "\n").as_bytes(), 8),
dynamic_import_of_composes
Expand All @@ -256,11 +253,11 @@ impl Plugin for FarmPluginCss {
.join(",")
);

return Ok(Some(PluginTransformHookResult {
Ok(Some(PluginTransformHookResult {
content: code,
module_type: Some(ModuleType::Js),
source_map: None,
}));
}))
// } else if matches!(context.config.mode, farmfe_core::config::Mode::Development) {
// let css_js_code = wrapper_style_load(&param.content, module_id.to_string());

Expand Down Expand Up @@ -363,7 +360,7 @@ impl Plugin for FarmPluginCss {

// transform css resource pot to js resource pot in development mode
for resource_pot in resource_pots {
transform_css_resource_pot(*resource_pot, &mut *module_graph, context)?;
transform_css_resource_pot(resource_pot, &mut module_graph, context)?;
}

Ok(Some(()))
Expand Down Expand Up @@ -400,8 +397,8 @@ impl Plugin for FarmPluginCss {
source_replace(
&mut module_css_ast,
&module.id,
&*module_graph,
&*resources_map,
&module_graph,
&resources_map,
);

merged_css_ast.rules.extend(module_css_ast.rules);
Expand Down Expand Up @@ -429,7 +426,7 @@ impl Plugin for FarmPluginCss {
let source_map_enabled = context.config.sourcemap.enabled();

let (css_code, src_map) = codegen_css_stylesheet(
&stylesheet,
stylesheet,
if source_map_enabled {
Some(context.meta.css.cm.clone())
} else {
Expand All @@ -442,17 +439,17 @@ impl Plugin for FarmPluginCss {

if context.config.sourcemap.enabled()
&& (context.config.sourcemap.is_all() || !resource_pot.immutable)
&& src_map.is_some()
{
// css_code.push_str(format!("\n/*# sourceMappingURL={} */", sourcemap_filename).as_str());

resources.push(Resource {
name: format!("{}.map", resource_pot.id.to_string()),
bytes: src_map.unwrap(),
emitted: false,
resource_type: ResourceType::SourceMap(resource_pot.id.to_string()),
origin: ResourceOrigin::ResourcePot(resource_pot.id.clone()),
})
if let Some(bytes) = src_map {
resources.push(Resource {
name: format!("{}.map", resource_pot.id.to_string()),
bytes,
emitted: false,
resource_type: ResourceType::SourceMap(resource_pot.id.to_string()),
origin: ResourceOrigin::ResourcePot(resource_pot.id.clone()),
})
}
}

resources.push(Resource {
Expand Down Expand Up @@ -523,8 +520,7 @@ fn transform_css_module_indent_name(

fn is_farm_css_modules(path: &str) -> bool {
path
.split("?")
.into_iter()
.split('?')
.next()
.unwrap()
.ends_with(FARM_CSS_MODULES_SUFFIX)
Expand Down
40 changes: 16 additions & 24 deletions crates/plugin_css/src/source_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,23 @@ impl<'a> VisitMut for SourceReplacer<'a> {
// remove all at rule that resolves to a module
for (i, rule) in stylesheet.rules.iter().enumerate() {
if let Rule::AtRule(box at_rule) = rule {
if let Some(box prelude) = &at_rule.prelude {
if let AtRulePrelude::ImportPrelude(import) = prelude {
let source = match &import.href {
box ImportHref::Url(url) => {
if let Some(value) = &url.value {
Some(match &**value {
UrlValue::Str(str) => str.value.to_string(),
UrlValue::Raw(raw) => raw.value.to_string(),
})
} else {
None
}
}
box ImportHref::Str(str) => Some(str.value.to_string()),
};
if let Some(box AtRulePrelude::ImportPrelude(import)) = &at_rule.prelude {
let source = match &import.href {
box ImportHref::Url(url) => url.value.as_ref().map(|value| match &**value {
UrlValue::Str(str) => str.value.to_string(),
UrlValue::Raw(raw) => raw.value.to_string(),
}),
box ImportHref::Str(str) => Some(str.value.to_string()),
};

if let Some(source) = source {
if !is_source_ignored(&source)
&& self
.module_graph
.get_dep_by_source_optional(&self.module_id, &source)
.is_some()
{
rules_to_remove.push(i);
}
if let Some(source) = source {
if !is_source_ignored(&source)
&& self
.module_graph
.get_dep_by_source_optional(&self.module_id, &source)
.is_some()
{
rules_to_remove.push(i);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/plugin_css/src/transform_resource_pot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn transform_css_stylesheet(
};

let resources_map = context.resources_map.lock();
source_replace(&mut stylesheet, module_id, module_graph, &*resources_map);
source_replace(&mut stylesheet, module_id, module_graph, &resources_map);

stylesheet
}
Expand All @@ -116,7 +116,7 @@ pub fn transform_css_deps(
let load_statement = format!(
"farmRequire(\"{}\");",
if cfg!(windows) {
relative_path.replace("\\", "\\\\")
relative_path.replace('\\', "\\\\")
} else {
relative_path.to_string()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/plugin_css/tests/analyze_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use farmfe_testing_helpers::fixture;

#[test]
fn analyze_deps() {
fixture!("tests/fixtures/analyze_deps/basic.css", |file, base| {
fixture!("tests/fixtures/analyze_deps/basic.css", |file, _base| {
let context = Arc::new(CompilationContext::new(Config::default(), vec![]).unwrap());
let css_plugin = FarmPluginCss::new(&context.config);
let load_result = css_plugin
.load(
&PluginLoadHookParam {
resolved_path: &file.to_string_lossy().to_string(),
resolved_path: &file.to_string_lossy(),
query: vec![],
meta: HashMap::new(),
},
Expand Down
4 changes: 2 additions & 2 deletions crates/plugin_html/src/deps_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn get_script_src_value(element: &Element) -> Option<String> {
if let Some(value) = &src_attr.value {
let value = value.to_string();
// the dependencies of html should be relative path and should not start with http or /
if value.starts_with("http") || value.starts_with("/") {
if value.starts_with("http") || value.starts_with('/') {
return None;
}

Expand All @@ -84,7 +84,7 @@ pub fn get_href_link_value(element: &Element) -> Option<String> {
if let Some(value) = &src_attr.value {
let value = value.to_string();
// the dependencies of html should be relative path and should not start with http or /
if value.starts_with("http") || value.starts_with("/") {
if value.starts_with("http") || value.starts_with('/') {
return None;
}

Expand Down
10 changes: 5 additions & 5 deletions crates/plugin_html/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ impl Plugin for FarmPluginHtml {

resource_pot.meta = ResourcePotMetaData::Html(HtmlResourcePotMetaData {
ast: Document {
span: html_module_document.span.clone(),
mode: html_module_document.mode.clone(),
span: html_module_document.span,
mode: html_module_document.mode,
children: html_module_document.children.to_vec(),
},
});
Expand Down Expand Up @@ -286,9 +286,9 @@ impl Plugin for FarmPluginHtml {
}

let dynamic_resources_map = get_dynamic_resources_map(
&*module_group_graph,
&module_group_graph,
&module_group_id,
&*resource_pot_map,
&resource_pot_map,
resources_map,
);

Expand Down Expand Up @@ -350,7 +350,7 @@ impl Plugin for FarmPluginHtml {
);

let resource_pot = resource_pot_map
.resource_pot_mut(&html_resource.origin.as_resource_pot())
.resource_pot_mut(html_resource.origin.as_resource_pot())
.unwrap();
let html_ast = &mut resource_pot.meta.as_html_mut().ast;
resources_injector.inject(html_ast);
Expand Down
5 changes: 2 additions & 3 deletions crates/plugin_html/src/resources_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ impl ResourcesInjector {
element.children.push(Child::Element(create_element(
"script",
Some(&format!(
r#"{FARM_GLOBAL_THIS}.{}.setInitialLoadedResources({});"#,
FARM_MODULE_SYSTEM,
format!("[{}]", initial_resources_code)
r#"{FARM_GLOBAL_THIS}.{}.setInitialLoadedResources([{}]);"#,
FARM_MODULE_SYSTEM, initial_resources_code
)),
vec![(FARM_ENTRY, "true")],
)));
Expand Down
Loading
Loading