Skip to content

Commit

Permalink
fix: clippy (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Sep 19, 2023
1 parent ac972df commit f028cff
Show file tree
Hide file tree
Showing 27 changed files with 166 additions and 154 deletions.
14 changes: 5 additions & 9 deletions crates/rolldown/src/bundler/bundle/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ impl<'a> Bundle<'a> {

pub fn generate_chunks(graph: &Graph) -> ChunksVec {
let mut chunks = ChunksVec::with_capacity(graph.entries.len());
let mut chunk = Chunk::default();
chunk.name = Some("main".to_string());
chunk.is_entry = true;
chunk.modules = graph.modules.iter().map(|m| m.id()).collect();
chunk
.modules
.sort_by_key(|id| graph.modules[*id].exec_order());
let mut modules = graph.modules.iter().map(|m| m.id()).collect::<Vec<_>>();
modules.sort_by_key(|id| graph.modules[*id].exec_order());
let chunk = Chunk::new(Some("main".to_string()), true, modules);
chunks.push(chunk);
chunks
}
Expand All @@ -50,7 +46,7 @@ impl<'a> Bundle<'a> {
.for_each(|chunk| chunk.render_file_name(self.output_options));

chunks.iter_mut().par_bridge().for_each(|chunk| {
chunk.de_conflict(&self.graph);
chunk.de_conflict(self.graph);
});

chunks.iter_mut().for_each(|chunk| {
Expand All @@ -74,7 +70,7 @@ impl<'a> Bundle<'a> {
let assets = chunks
.iter()
.map(|c| {
let content = c.render(&self.graph, input_options).unwrap();
let content = c.render(self.graph, input_options).unwrap();

Asset {
file_name: c.file_name.clone().unwrap(),
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/bundle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod asset;
#[allow(clippy::module_inception)]
pub mod bundle;
11 changes: 10 additions & 1 deletion crates/rolldown/src/bundler/chunk/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ pub struct Chunk {
}

impl Chunk {
pub fn new(name: Option<String>, is_entry: bool, modules: Vec<ModuleId>) -> Self {
Self {
name,
is_entry,
modules,
..Default::default()
}
}

pub fn render_file_name(&mut self, output_options: &NormalizedOutputOptions) {
self.file_name = Some(
output_options
Expand All @@ -34,7 +43,7 @@ impl Chunk {
}

/// - import symbols from other chunks and external modules
pub fn generate_cross_chunk_links(&mut self) {}
// pub fn generate_cross_chunk_links(&mut self) {}

pub fn initialize_exports(&mut self, modules: &mut ModuleVec, symbols: &Symbols) {
let entry = &mut modules[*self.modules.last().unwrap()];
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/chunk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(clippy::module_inception)]
pub mod chunk;
mod de_conflict;

Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Graph {
&mut self,
input_options: &NormalizedInputOptions,
) -> anyhow::Result<()> {
ModuleLoader::new(&input_options, self)
ModuleLoader::new(input_options, self)
.fetch_all_modules()
.await?;

Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(clippy::module_inception)]
pub mod graph;
pub mod linker;
pub mod symbols;
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod module;
pub mod options;
mod visitors;

#[allow(clippy::module_inception)]
pub mod bundler;
mod chunk;
mod module_loader;
Expand Down
7 changes: 4 additions & 3 deletions crates/rolldown/src/bundler/module/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rolldown_common::{ImportRecord, ImportRecordId, ModuleId, ResourceId, Symbol
use rustc_hash::FxHashMap;
use string_wizard::MagicString;

use crate::bundler::{graph::symbols::Symbols, visitors::FinalizeContext};
use crate::bundler::graph::symbols::Symbols;

use super::{module::ModuleFinalizeContext, render::RenderModuleContext};

Expand All @@ -30,16 +30,17 @@ impl ExternalModule {
}
}

pub fn finalize(&mut self, ctx: ModuleFinalizeContext) {}
pub fn finalize(&mut self, _ctx: ModuleFinalizeContext) {}

pub fn render(&self, ctx: RenderModuleContext) -> Option<MagicString<'static>> {
pub fn render(&self, _ctx: RenderModuleContext) -> Option<MagicString<'static>> {
let mut rendered = MagicString::new(format!("import \"{}\"", self.resource_id.as_ref()));

rendered.prepend(format!("// {}\n", self.resource_id.prettify()));
rendered.append("\n");
Some(rendered)
}

#[allow(dead_code)]
pub fn resolve_export(&mut self, symbols: &mut Symbols, exported: &Atom) -> SymbolRef {
*self
.symbols_imported_by_others
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod external_module;
#[allow(clippy::module_inception)]
pub mod module;
pub mod module_builder;
pub mod module_id;
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler/module/module.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use index_vec::IndexVec;
use oxc::{semantic::SymbolId, span::Atom};
use oxc::span::Atom;
use rolldown_common::{ImportRecord, ImportRecordId, ModuleId, SymbolRef};
use rustc_hash::FxHashMap;
use string_wizard::MagicString;

use crate::bundler::{graph::symbols::Symbols, visitors::FinalizeContext};
use crate::bundler::graph::symbols::Symbols;

use super::{external_module::ExternalModule, render::RenderModuleContext, NormalModule};

Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ impl NormalModule {
}

// https://tc39.es/ecma262/#sec-resolveexport
pub fn resolve_export<'modules, 'symbols>(
pub fn resolve_export<'modules>(
&'modules self,
export_name: &'modules Atom,
resolve_set: &mut Vec<(ModuleId, &'modules Atom)>,
modules: &'modules ModuleVec,
symbols: &'symbols mut Symbols,
symbols: &mut Symbols,
) -> Resolution {
let record = (self.id, export_name);
if resolve_set.iter().rev().any(|prev| prev == &record) {
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/module_loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(clippy::module_inception)]
mod module_loader;
mod module_task;
mod task_result;
Expand Down
6 changes: 2 additions & 4 deletions crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use index_vec::IndexVec;
use rolldown_common::{ImportRecord, ModuleId, RawPath, ResourceId};
use rolldown_common::{ModuleId, RawPath, ResourceId};
use rolldown_resolver::Resolver;
use rolldown_utils::block_on_spawn_all;
use rustc_hash::FxHashMap;
Expand All @@ -11,7 +11,6 @@ use crate::bundler::graph::graph::Graph;
use crate::bundler::graph::symbols::{SymbolMap, Symbols};
use crate::bundler::module::external_module::ExternalModule;
use crate::bundler::module::module::Module;
use crate::bundler::module::NormalModule;
use crate::bundler::options::normalized_input_options::NormalizedInputOptions;
use crate::bundler::resolve_id::{resolve_id, ResolvedRequestInfo};
use crate::BuildError;
Expand Down Expand Up @@ -68,9 +67,8 @@ impl<'a> ModuleLoader<'a> {
module_id,
symbol_map: symbol_table,
resolved_deps,
errors,
warnings,
mut builder,
..
} = task_result;

let import_records = builder.import_records.as_mut().unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/options/file_name_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub struct FileNameTemplate {
}

impl FileNameTemplate {
#[allow(dead_code)]
pub fn new(template: String) -> Self {
Self { template }
}
Expand Down
99 changes: 50 additions & 49 deletions crates/rolldown/src/bundler/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<'ast, 'p> VisitMut<'ast, 'p> for Finalizer<'ast> {
let symbol_ref = (self.ctx.id, ident.expect_symbol_id()).into();
let final_ref = self.ctx.symbols.par_get_canonical_ref(symbol_ref);
if let Some(name) = self.ctx.final_names.get(&final_ref) {
if &ident.name != name {
if ident.name != name {
ident.name = name.clone()
}
}
Expand All @@ -80,7 +80,7 @@ impl<'ast, 'p> VisitMut<'ast, 'p> for Finalizer<'ast> {
let symbol_ref = (self.ctx.id, symbol_id).into();
let final_ref = self.ctx.symbols.par_get_canonical_ref(symbol_ref);
if let Some(name) = self.ctx.final_names.get(&final_ref) {
if &ident.name != name {
if ident.name != name {
ident.name = name.clone()
}
}
Expand All @@ -92,59 +92,60 @@ impl<'ast, 'p> VisitMut<'ast, 'p> for Finalizer<'ast> {
program
.body
.retain(|stmt| self.should_keep_this_top_level_stmt(stmt));
program.body.iter_mut().for_each(|stmt| match stmt {
Statement::ModuleDeclaration(decl) => match decl.0 {
oxc::ast::ast::ModuleDeclaration::ExportDefaultDeclaration(decl) => {
match &mut decl.declaration {
oxc::ast::ast::ExportDefaultDeclarationKind::Expression(exp) => {
let mut declarations = Vec::new_in(self.ctx.allocator);
declarations.push(VariableDeclarator {
span: Default::default(),
kind: oxc::ast::ast::VariableDeclarationKind::Var,
id: BindingPattern {
kind: oxc::ast::ast::BindingPatternKind::BindingIdentifier(Box(
self.ctx.allocator.alloc(BindingIdentifier {
span: Default::default(),
name: "".into(),
symbol_id: Cell::new(self.ctx.default_export_symbol),
}),
)),
type_annotation: None,
optional: false,
},
init: Some(exp.take_in(self.ctx.allocator)),
definite: false,
});
*stmt = Statement::Declaration(Declaration::VariableDeclaration(Box(alloc.alloc(
VariableDeclaration {
program.body.iter_mut().for_each(|stmt| {
if let Statement::ModuleDeclaration(decl) = stmt {
match decl.0 {
oxc::ast::ast::ModuleDeclaration::ExportDefaultDeclaration(decl) => {
match &mut decl.declaration {
oxc::ast::ast::ExportDefaultDeclarationKind::Expression(exp) => {
let mut declarations = Vec::new_in(self.ctx.allocator);
declarations.push(VariableDeclarator {
span: Default::default(),
kind: oxc::ast::ast::VariableDeclarationKind::Var,
declarations,
modifiers: Default::default(),
},
))))
id: BindingPattern {
kind: oxc::ast::ast::BindingPatternKind::BindingIdentifier(Box(
self.ctx.allocator.alloc(BindingIdentifier {
span: Default::default(),
name: "".into(),
symbol_id: Cell::new(self.ctx.default_export_symbol),
}),
)),
type_annotation: None,
optional: false,
},
init: Some(exp.take_in(self.ctx.allocator)),
definite: false,
});
*stmt = Statement::Declaration(Declaration::VariableDeclaration(Box(alloc.alloc(
VariableDeclaration {
span: Default::default(),
kind: oxc::ast::ast::VariableDeclarationKind::Var,
declarations,
modifiers: Default::default(),
},
))))
}
oxc::ast::ast::ExportDefaultDeclarationKind::FunctionDeclaration(decl) => {
*stmt = Statement::Declaration(oxc::ast::ast::Declaration::FunctionDeclaration(
decl.take_in(alloc),
))
}
oxc::ast::ast::ExportDefaultDeclarationKind::ClassDeclaration(decl) => {
*stmt = Statement::Declaration(oxc::ast::ast::Declaration::ClassDeclaration(
decl.take_in(alloc),
))
}
_ => {}
}
oxc::ast::ast::ExportDefaultDeclarationKind::FunctionDeclaration(decl) => {
*stmt = Statement::Declaration(oxc::ast::ast::Declaration::FunctionDeclaration(
decl.take_in(alloc),
))
}
oxc::ast::ast::ExportDefaultDeclarationKind::ClassDeclaration(decl) => {
*stmt = Statement::Declaration(oxc::ast::ast::Declaration::ClassDeclaration(
decl.take_in(alloc),
))
}
_ => {}
}
}
oxc::ast::ast::ModuleDeclaration::ExportNamedDeclaration(named_decl) => {
if let Some(decl) = &mut named_decl.declaration {
*stmt = Statement::Declaration(decl.take_in(alloc))
oxc::ast::ast::ModuleDeclaration::ExportNamedDeclaration(named_decl) => {
if let Some(decl) = &mut named_decl.declaration {
*stmt = Statement::Declaration(decl.take_in(alloc))
}
}
_ => {}
}
_ => {}
},
_ => {}
}
});

self.visit_statements(&mut program.body);
Expand Down
8 changes: 3 additions & 5 deletions crates/rolldown/src/bundler/visitors/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use index_vec::IndexVec;
use oxc::{
allocator::Box,
ast::{
ast::{
ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, IdentifierReference,
ImportDeclaration, ModuleDeclaration,
},
Visit, VisitMut,
VisitMut,
},
semantic::{ScopeTree, Semantic, SymbolFlags, SymbolId, SymbolTable},
semantic::{ScopeTree, SymbolFlags, SymbolId, SymbolTable},
span::Atom,
};
use rolldown_common::{
Expand Down Expand Up @@ -64,8 +63,7 @@ impl<'a> Scanner<'a> {

fn add_import_record(&mut self, module_request: &Atom) -> ImportRecordId {
let rec = ImportRecord::new(module_request.clone());
let idx = self.result.import_records.push(rec);
idx
self.result.import_records.push(rec)
}

fn add_named_import(&mut self, local: SymbolId, imported: &Atom, record_id: ImportRecordId) {
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rolldown_resolver::Resolver;

pub(crate) type SharedResolver = Arc<Resolver>;
pub type BuildError = rolldown_error::Error;
pub type BuildResult<T> = Result<T, BuildError>;
pub type BuildResult<T> = Result<T, Box<BuildError>>;

pub use crate::bundler::{
bundle::asset::Asset,
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_common/src/module_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl ResourceId {
&self.0.pretty
}

#[allow(clippy::needless_return)]
pub fn generate_unique_name(&self) -> String {
let path = Path::new(self.0.path.as_str());
let unique_name = path.file_stem().unwrap().to_str().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/rolldown_error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::error_kind::ErrorKind;

type StaticStr = Cow<'static, str>;

// TODO reduce struct size
#[allow(unused)]
#[derive(Debug)]
pub struct Error {
contexts: Vec<Cow<'static, str>>,
Expand Down

0 comments on commit f028cff

Please sign in to comment.