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

chore: ✨ strip span when parsing define's expression #1540

Merged
merged 2 commits into from
Aug 28, 2024
Merged
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
25 changes: 20 additions & 5 deletions crates/mako/src/visitors/env_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use anyhow::{anyhow, Result};
use serde_json::Value;
use swc_core::common::{Mark, DUMMY_SP};
use swc_core::common::{Mark, Span, DUMMY_SP};
use swc_core::ecma::ast::{
ArrayLit, Bool, ComputedPropName, Expr, ExprOrSpread, Ident, KeyValueProp, Lit, MemberExpr,
MemberProp, ModuleItem, Null, Number, ObjectLit, Prop, PropOrSpread, Stmt, Str,
Expand Down Expand Up @@ -135,10 +135,14 @@ fn get_env_expr(v: Value, context: &Arc<Context>) -> Result<Expr> {
v.clone()
};

// the string content is treat as expression, so it has to be parsed
let ast =
JsAst::build("_mako_internal/_define_.js", &safe_value, context.clone()).unwrap();
let module = ast.ast.body.first().unwrap();
let module = {
// the string content is treat as expression, so it has to be parsed
let mut ast =
JsAst::build("_mako_internal/_define_.js", &safe_value, context.clone())
.unwrap();
ast.ast.visit_mut_with(&mut strip_span());
ast.ast.body.pop().unwrap()
};

match module {
ModuleItem::Stmt(Stmt::Expr(stmt_expr)) => {
Expand Down Expand Up @@ -195,6 +199,17 @@ fn get_env_expr(v: Value, context: &Arc<Context>) -> Result<Expr> {
}
}

struct SpanStrip {}
impl VisitMut for SpanStrip {
fn visit_mut_span(&mut self, span: &mut Span) {
*span = DUMMY_SP;
}
}

fn strip_span() -> impl VisitMut {
SpanStrip {}
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;
Expand Down
Loading