Skip to content

Commit

Permalink
remove some span
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jun 18, 2024
1 parent a29e514 commit 64e7b82
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ impl VisitMut for ParamMetadata {
}

impl ParamMetadata {
fn create_param_decorator(&self, param_index: usize, decorator_expr: Box<Expr>) -> Decorator {
fn create_param_decorator(
&self,
param_index: usize,
mut decorator_expr: Box<Expr>,
) -> Decorator {
remove_span(&mut decorator_expr);

Decorator {
span: DUMMY_SP,
expr: Box::new(Expr::Call(CallExpr {
span: decorator_expr.span(),
span: DUMMY_SP,
callee: helper!(ts, ts_param),
args: vec![param_index.as_arg(), decorator_expr.as_arg()],
type_args: Default::default(),
Expand All @@ -73,6 +79,27 @@ impl ParamMetadata {
}
}

pub(super) fn remove_span(e: &mut Expr) {
match e {
Expr::Member(m) => {
m.span = DUMMY_SP;
remove_span(&mut m.obj);
}
Expr::Call(c) => {
c.span = DUMMY_SP;
if let Callee::Expr(e) = &mut c.callee {
remove_span(e);
}
for arg in &mut c.args {
remove_span(&mut arg.expr);
}
}
_ => {
e.set_span(DUMMY_SP);
}
}
}

type EnumMapType = AHashMap<JsWord, EnumKind>;

pub(super) struct EnumMap<'a>(&'a EnumMapType);
Expand Down
32 changes: 25 additions & 7 deletions crates/swc_ecma_transforms_proposal/src/decorators/legacy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{iter, mem};

use metadata::remove_span;
use swc_atoms::JsWord;
use swc_common::{collections::AHashMap, util::take::Take, DUMMY_SP};
use swc_common::{collections::AHashMap, util::take::Take, BytePos, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
use swc_ecma_utils::{private_ident, prop_name_to_expr_value, quote_ident, ExprFactory, StmtLike};
Expand Down Expand Up @@ -151,20 +152,27 @@ impl TscDecorator {
fn add_decorate_call(
&mut self,
decorators: impl IntoIterator<Item = Box<Expr>>,
target: ExprOrSpread,
mut target: ExprOrSpread,
key: ExprOrSpread,
desc: ExprOrSpread,
mut desc: ExprOrSpread,
) {
let decorators = ArrayLit {
span: DUMMY_SP,
elems: decorators
.into_iter()
.map(|v| v.as_arg())
.map(|mut v| {
remove_span(&mut v);

v.as_arg()
})
.map(Some)
.collect(),
}
.as_arg();

remove_span(&mut target.expr);
remove_span(&mut desc.expr);

self.appended_exprs.push(Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: helper!(ts, ts_decorate),
Expand Down Expand Up @@ -235,7 +243,11 @@ impl VisitMut for TscDecorator {
.decorators
.take()
.into_iter()
.map(|v| v.expr.as_arg())
.map(|mut v| {
remove_span(&mut v.expr);

v.expr.as_arg()
})
.map(Some)
.collect(),
}
Expand All @@ -244,13 +256,19 @@ impl VisitMut for TscDecorator {
let decorated = Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: helper!(ts, ts_decorate),
args: vec![decorators, class_name.clone().as_arg()],
args: vec![
decorators,
class_name
.clone()
.with_pos(BytePos::DUMMY, BytePos::DUMMY)
.as_arg(),
],
type_args: Default::default(),
}));
self.appended_exprs.push(Box::new(Expr::Assign(AssignExpr {
span: DUMMY_SP,
op: op!("="),
left: class_name.into(),
left: class_name.with_pos(BytePos::DUMMY, BytePos::DUMMY).into(),
right: decorated,
})));
}
Expand Down

0 comments on commit 64e7b82

Please sign in to comment.