Skip to content

Commit

Permalink
Refactor bundling to use cached function calls to load modules (#147)
Browse files Browse the repository at this point in the history
The bundler will generate functions to load each
modules instead of generating simple do blocks
  • Loading branch information
jeparlefrancais committed Nov 3, 2023
1 parent 57e515d commit 9ae3158
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 160 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* fix bundling to handle modules with early return calls. This change also makes the bundled code preserve the module require ordering ([#147](https://github.com/seaofvoices/darklua/pull/147))
* fix bundling to avoid token reference removal errors ([#146](https://github.com/seaofvoices/darklua/pull/146))
* fix `remove_types` rule to handle type cast of expressions that could return multiple values ([#142](https://github.com/seaofvoices/darklua/pull/142))

Expand Down
27 changes: 26 additions & 1 deletion src/nodes/expressions/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::nodes::{Expression, Identifier, Token};
use crate::{
nodes::{Expression, Identifier, Token},
process::utils::is_valid_identifier,
};

use super::StringExpression;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableFieldEntry {
Expand Down Expand Up @@ -198,6 +203,26 @@ pub enum TableEntry {
}

impl TableEntry {
/// Creates a field entry if the provided key is a valid identifier, otherwise it
/// creates an index entry.
pub fn from_string_key_and_value(key: impl Into<String>, value: impl Into<Expression>) -> Self {
let key = key.into();
let value = value.into();
if is_valid_identifier(&key) {
Self::Field(TableFieldEntry {
field: Identifier::new(key),
value,
token: None,
})
} else {
Self::Index(TableIndexEntry {
key: Expression::String(StringExpression::from_value(key)),
value,
tokens: None,
})
}
}

pub fn clear_comments(&mut self) {
match self {
TableEntry::Field(entry) => entry.clear_comments(),
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/statements/if_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl IfStatement {
}
}

pub fn create<E: Into<Expression>, B: Into<Block>>(condition: E, block: B) -> Self {
pub fn create(condition: impl Into<Expression>, block: impl Into<Block>) -> Self {
Self {
branches: vec![IfBranch::new(condition, block)],
else_block: None,
Expand Down Expand Up @@ -209,10 +209,10 @@ impl IfStatement {
self
}

pub fn with_new_branch<E: Into<Expression>, B: Into<Block>>(
pub fn with_new_branch(
mut self,
condition: E,
block: B,
condition: impl Into<Expression>,
block: impl Into<Block>,
) -> Self {
self.branches.push(IfBranch::new(condition, block));
self
Expand Down Expand Up @@ -258,7 +258,7 @@ impl IfStatement {
}

#[inline]
pub fn push_new_branch<E: Into<Expression>, B: Into<Block>>(&mut self, condition: E, block: B) {
pub fn push_new_branch(&mut self, condition: impl Into<Expression>, block: impl Into<Block>) {
self.branches
.push(IfBranch::new(condition.into(), block.into()));
}
Expand All @@ -279,7 +279,7 @@ impl IfStatement {
}

#[inline]
pub fn set_else_block<B: Into<Block>>(&mut self, block: B) {
pub fn set_else_block(&mut self, block: impl Into<Block>) {
self.else_block = Some(block.into());
}

Expand Down
4 changes: 0 additions & 4 deletions src/nodes/types/type_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,27 @@ impl TypeField {

pub fn clear_comments(&mut self) {
self.namespace.clear_comments();
self.name.clear_comments();
if let Some(token) = &mut self.token {
token.clear_comments();
}
}

pub fn clear_whitespaces(&mut self) {
self.namespace.clear_whitespaces();
self.name.clear_whitespaces();
if let Some(token) = &mut self.token {
token.clear_whitespaces();
}
}

pub(crate) fn replace_referenced_tokens(&mut self, code: &str) {
self.namespace.replace_referenced_tokens(code);
self.name.replace_referenced_tokens(code);
if let Some(token) = &mut self.token {
token.replace_referenced_tokens(code);
}
}

pub(crate) fn shift_token_line(&mut self, amount: usize) {
self.namespace.shift_token_line(amount);
self.name.shift_token_line(amount);
if let Some(token) = &mut self.token {
token.shift_token_line(amount);
}
Expand Down
18 changes: 18 additions & 0 deletions src/rules/bundle/path_require_mode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,24 @@ pub(crate) fn process_block(
options: &BundleOptions,
path_require_mode: &PathRequireMode,
) -> Result<(), String> {
if options.parser().is_preserving_tokens() {
log::trace!(
"replacing token references of {}",
context.current_path().display()
);
let replace_tokens = ReplaceReferencedTokens::default();

let apply_replace_tokens_timer = Timer::now();

replace_tokens.flawless_process(block, context);

log::trace!(
"replaced token references for `{}` in {}",
context.current_path().display(),
apply_replace_tokens_timer.duration_label()
);
}

let mut processor = RequirePathProcessor::new(context, options, path_require_mode);
ScopeVisitor::visit_block(block, &mut processor);
processor.apply(block, context)
Expand Down
Loading

0 comments on commit 9ae3158

Please sign in to comment.