Skip to content

Commit

Permalink
fix: hoist statements. Fixes #418
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Feb 3, 2024
1 parent 6b3c764 commit 276bbae
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {

let source_type =
determine_oxc_source_type(self.resolved_path.path.as_path(), self.module_type);
let program = OxcCompiler::parse(Arc::clone(source), source_type);
let mut program = OxcCompiler::parse(Arc::clone(source), source_type);

let semantic = program.make_semantic(source_type);
let (mut symbol_table, scope) = semantic.into_symbol_table_and_scope_tree();
Expand All @@ -161,6 +161,7 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
&self.resolved_path.path,
);
let namespace_symbol = scanner.namespace_symbol;
program.hoist_import_export_from_stmts();
let scan_result = scanner.scan(program.program());
if !self.ctx.input_options.treeshake {
// TODO: tree shaking is not supported yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl RuntimeNormalModuleTask {
source: &Arc<str>,
) -> (OxcProgram, AstScope, ScanResult, AstSymbol, SymbolRef) {
let source_type = SourceType::default();
let program = OxcCompiler::parse(Arc::clone(source), source_type);
let mut program = OxcCompiler::parse(Arc::clone(source), source_type);

let semantic = program.make_semantic(source_type);
let (mut symbol_table, scope) = semantic.into_symbol_table_and_scope_tree();
Expand All @@ -109,6 +109,7 @@ impl RuntimeNormalModuleTask {
&facade_path,
);
let namespace_symbol = scanner.namespace_symbol;
program.hoist_import_export_from_stmts();
let scan_result = scanner.scan(program.program());

(program, ast_scope, scan_result, symbol_for_module, namespace_symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ var require_bar = __commonJS((exports, module) => {
// entry.js
var import_foo = require_foo();
console.log((0,import_foo.foo)(), (0,import_bar.bar)());
var import_bar = require_bar();
console.log((0,import_foo.foo)(), (0,import_bar.bar)());
```
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ var init_b = __esm(() => {
function Fn() {
}
var init_commonjs = __esm(() => {
init_b();
var commonjs_default = 123;
var v = 234;
let l = 234;
const c = 234;
class Class {
}
init_b();
});
// c.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const value = 1;
var import_commonjs$2 = require_commonjs();
// main.js
console.log(foo_ns, import_commonjs$2.bar, value, foo_ns.foo);
var import_commonjs = require_commonjs();
console.log(foo_ns, import_commonjs$2.bar, value, foo_ns.foo);
var bar$1 = import_commonjs.bar;
export { bar$1 as bar };
Expand Down
19 changes: 18 additions & 1 deletion crates/rolldown_oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc::{
span::SourceType,
};

use crate::Dummy;
use crate::{Dummy, StatementExt, TakeIn};
pub struct OxcCompiler;

#[allow(clippy::box_collection, clippy::non_send_fields_in_send_ty, unused)]
Expand Down Expand Up @@ -67,6 +67,23 @@ impl OxcProgram {
let semantic = SemanticBuilder::new(&self.source, ty).build(self.program()).semantic;
semantic
}

// TODO: should move this to `rolldown` crate
pub fn hoist_import_export_from_stmts(&mut self) {
let (program, allocator) = self.program_mut_and_allocator();
let old_body = program.body.take_in(allocator);
program.body.reserve_exact(old_body.len());
let mut non_hoisted = oxc::allocator::Vec::new_in(allocator);

old_body.into_iter().for_each(|top_stmt| {
if top_stmt.is_module_declaration_with_source() {
program.body.push(top_stmt);
} else {
non_hoisted.push(top_stmt);
}
});
program.body.extend(non_hoisted);
}
}

impl OxcCompiler {
Expand Down
15 changes: 15 additions & 0 deletions crates/rolldown_oxc/src/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub trait StatementExt<'me, 'ast> {

fn is_function_declaration(&self) -> bool;
fn as_function_declaration(&self) -> Option<&ast::Function<'ast>>;

fn as_module_declaration(&self) -> Option<&ast::ModuleDeclaration<'ast>>;
fn is_module_declaration_with_source(&self) -> bool;
}

impl<'me, 'ast> StatementExt<'me, 'ast> for ast::Statement<'ast> {
Expand Down Expand Up @@ -129,6 +132,18 @@ impl<'me, 'ast> StatementExt<'me, 'ast> for ast::Statement<'ast> {
fn is_function_declaration(&self) -> bool {
self.as_function_declaration().is_some()
}

fn as_module_declaration(&self) -> Option<&ast::ModuleDeclaration<'ast>> {
if let ast::Statement::ModuleDeclaration(module_decl) = self {
Some(module_decl)
} else {
None
}
}

fn is_module_declaration_with_source(&self) -> bool {
matches!(self.as_module_declaration(), Some(decl) if decl.source().is_some())
}
}

pub trait ExpressionExt<'ast> {
Expand Down

0 comments on commit 276bbae

Please sign in to comment.