Skip to content

Commit

Permalink
fix(es): Source map super(...args) calls in injected constructors (#…
Browse files Browse the repository at this point in the history
…9745)

**Related issue:**

 - Closes #9742
  • Loading branch information
kdy1 authored Nov 19, 2024
1 parent c55e524 commit 35b0ca0
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 31 deletions.
9 changes: 9 additions & 0 deletions .changeset/smart-horses-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
swc_ecma_compat_es2015: patch
swc_ecma_compat_es2022: patch
swc_ecma_transforms_compat: patch
swc_ecma_transforms_proposal: patch
swc_ecma_utils: patch
---

fix(es/transforms): Source map `super(...args)` calls in injected constructors
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.map
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": ";;;+BACaA;;;eAAAA;;;;;;;oBADQ;AACd,IAAA,AAAMA,oBAAN;;gBAAMA;aAAAA;kCAAAA;mCAAAA;;oBAAAA;;YACTC,KAAAA;mBAAAA,SAAAA;gBACI,OAAO,IAAI,IAAI,CAACC,GAAG;YACvB;;;WAHSF;EAAYG,UAAI",
"mappings": ";;;+BACaA;;;eAAAA;;;;;;;oBADQ;AACd,IAAA,AAAMA,oBAAN;;gBAAMA;aAAAA;kCAAAA;eAAN,oBAAMA;;oBAAAA;;YACTC,KAAAA;mBAAAA,SAAAA;gBACI,OAAO,IAAI,IAAI,CAACC,GAAG;YACvB;;;WAHSF;EAAYG,UAAI",
"names": [
"Foo",
"bar",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": ";;;;;;;;;;;IA6BaA,MAAM;eAANA;;IAMAC,MAAM;eAANA;;IARDC,IAAI;;;IAKHC,MAAM;eAANA;;IAaAC,GAAG;eAAHA;;IARAC,MAAM;eAANA;;IAKAC,OAAO;eAAPA;;IATAC,KAAK;eAALA;;IAHAC,QAAQ;eAARA;;;;;;;;kCA9BuC;8BACJ;gCAC1B;2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I;AADJ,MAAMR,gCAAgCY,oCAAkB;IAGpDhB,MAAMA,KAA2C,EAAQ;yCAChD,SAASA,MAAMO,GAAG,CAACU,0BAAY;QACpC,OAAO,IAAI;IACf;IAEAG,cAAcC,cAAuC,EAAiB;QAClE,MAAMC,KAAKC,IAAAA,kBAAI,EAAC,6CAAkB,IAAI,EAAC;QACvC,OAAO;YACHD;YACAtB,OAAOqB,eAAeG,KAAK,4BAAC,IAAI,EAAC;QACrC;IACJ;;QAdJ,gBACI,kCAAA;;mBAAsC,EAAE;;;AAc5C;IAGI;AADJ,MAAMvB,6BAA6BI,iCAAe;IAQ9CL,MAAM,GAAGA,KAA+B,EAAQ;QAC5C,2BAAA,IAAI,EAAC,cAAYA,KAAK,CAAC;YAACA;SAAM;QAC9B,OAAO,IAAI;IACf;IAEAW,QAAuB;QACnB,OAAO,2BAAA,IAAI,EAAC,cAAYA,KAAK;IACjC;IAZAgB,YAAY,GAAG3B,KAA+B,CAAE;QAC5C,KAAK,IAHT,kCAAA;;mBAAuC,IAAII;;QAIvC,IAAI,CAACJ,KAAK,IAAIA;IAClB;AAUJ;IAGI,uCACA;AAFJ,MAAME,8BAA8BG,iCAAe;IAI/CL,MAAM,GAAGA,KAA+B,EAAQ;yCACvC,SAASA,MAAMO,GAAG,CAACC,yBAAW;QACnC,OAAO,IAAI;IACf;IAEAK,OAAO,GAAGA,MAAgC,EAAQ;yCACzC,UAAUA,OAAON,GAAG,CAACC,yBAAW;QACrC,OAAO,IAAI;IACf;IAEAG,QAAwB;QACpB,OAAO;YACHX,KAAK,6BAAE,IAAI,EAAC;YACZa,MAAM,6BAAE,IAAI,EAAC;QACjB;IACJ;;QAnBJ,gBACI,kCAAA;;mBAA0B,EAAE;YAC5B,kCAAA;;mBAA2B,EAAE;;;AAkBjC",
"names": [
"create",
"header",
Expand Down

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions crates/swc/tests/stacktrace/issue-9742/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"loose": true,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true,
"env": {
"targets": "node >= 8"
}
}
11 changes: 11 additions & 0 deletions crates/swc/tests/stacktrace/issue-9742/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Parent {
constructor() {
throw new Error('foo')
}
}

class Child extends Parent {
handleScroll = () => { }
}

new Child()
9 changes: 9 additions & 0 deletions crates/swc/tests/stacktrace/issue-9742/output/stacks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$DIR/tests/stacktrace/issue-9742/input/index.js:3
throw new Error('foo')
^


Error: foo
at constructor ($DIR/tests/stacktrace/issue-9742/input/index.js:3:15)
at Parent ($DIR/tests/stacktrace/issue-9742/input/index.js:7:1)

This comment has been minimized.

Copy link
@eps1lon

eps1lon Nov 19, 2024

Is this the sourcemapped stack? Explicitly enabling sourcemaps doesn't fix it. The stack should read

at new Parent
at new Child

Filed #9748

at Object.<anonymous> ($DIR/tests/stacktrace/issue-9742/input/index.js:11:1)
8 changes: 5 additions & 3 deletions crates/swc_ecma_compat_es2015/src/classes/constructor.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use std::mem;

use swc_common::{util::take::Take, Spanned, SyntaxContext, DUMMY_SP};
use swc_common::{util::take::Take, Span, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{helper, helper_expr};
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
use swc_ecma_utils::{default_constructor, private_ident, quote_ident, ExprFactory};
use swc_ecma_utils::{default_constructor_with_span, private_ident, quote_ident, ExprFactory};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;
use tracing::debug;

use super::Config;

pub(super) fn fold_constructor(
class_span: Span,
constructor: Option<Constructor>,
class_name: &Ident,
class_super_name: &Option<Ident>,
config: Config,
) -> FnDecl {
let is_derived = class_super_name.is_some();
let mut constructor = constructor.unwrap_or_else(|| default_constructor(is_derived));
let mut constructor =
constructor.unwrap_or_else(|| default_constructor_with_span(is_derived, class_span));

// Black magic to detect injected constructor.
let is_constructor_default = constructor.span.is_dummy();
Expand Down
9 changes: 8 additions & 1 deletion crates/swc_ecma_compat_es2015/src/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,14 @@ impl Classes {

// constructor
stmts.push(
fold_constructor(constructor, &class_name, &super_class_ident, self.config).into(),
fold_constructor(
class.span,
constructor,
&class_name,
&super_class_ident,
self.config,
)
.into(),
);

// convert class methods
Expand Down
16 changes: 9 additions & 7 deletions crates/swc_ecma_compat_es2022/src/class_properties/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use swc_common::{
collections::AHashMap, errors::HANDLER, source_map::PURE_SP, util::take::Take, Mark, Spanned,
SyntaxContext, DUMMY_SP,
collections::AHashMap, errors::HANDLER, source_map::PURE_SP, util::take::Take, Mark, Span,
Spanned, SyntaxContext, DUMMY_SP,
};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{helper, perf::Check};
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
use swc_ecma_transforms_macros::fast_path;
use swc_ecma_utils::{
alias_ident_for, alias_if_required, constructor::inject_after_super, default_constructor,
is_literal, prepend_stmt, private_ident, quote_ident, replace_ident, ExprFactory,
ModuleItemLike, StmtLike,
alias_ident_for, alias_if_required, constructor::inject_after_super,
default_constructor_with_span, is_literal, prepend_stmt, private_ident, quote_ident,
replace_ident, ExprFactory, ModuleItemLike, StmtLike,
};
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith,
Expand Down Expand Up @@ -938,7 +938,8 @@ impl ClassProperties {
}
}

let constructor = self.process_constructor(constructor, has_super, constructor_inits);
let constructor =
self.process_constructor(class.span, constructor, has_super, constructor_inits);
if let Some(c) = constructor {
members.push(ClassMember::Constructor(c));
}
Expand Down Expand Up @@ -1015,6 +1016,7 @@ impl ClassProperties {
#[allow(clippy::vec_box)]
fn process_constructor(
&mut self,
class_span: Span,
constructor: Option<Constructor>,
has_super: bool,
constructor_exprs: MemberInitRecord,
Expand All @@ -1023,7 +1025,7 @@ impl ClassProperties {
if constructor_exprs.record.is_empty() {
None
} else {
Some(default_constructor(has_super))
Some(default_constructor_with_span(has_super, class_span))
}
});

Expand Down
8 changes: 6 additions & 2 deletions crates/swc_ecma_compat_es2022/src/private_in_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use swc_common::{
DUMMY_SP,
};
use swc_ecma_ast::*;
use swc_ecma_utils::{default_constructor, prepend_stmt, private_ident, quote_ident, ExprFactory};
use swc_ecma_utils::{
default_constructor_with_span, prepend_stmt, private_ident, quote_ident, ExprFactory,
};
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith, VisitWith,
};
Expand Down Expand Up @@ -174,7 +176,9 @@ impl VisitMut for PrivateInObject {
if !has_constructor {
let has_super = n.super_class.is_some();
n.body
.push(ClassMember::Constructor(default_constructor(has_super)));
.push(ClassMember::Constructor(default_constructor_with_span(
has_super, n.span,
)));
}

for m in &mut n.body {
Expand Down
18 changes: 11 additions & 7 deletions crates/swc_ecma_transforms_compat/src/class_fields_use_set.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::mem;

use swc_common::{util::take::Take, DUMMY_SP};
use swc_common::{util::take::Take, Span, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{
constructor::inject_after_super, default_constructor, is_literal, is_simple_pure_expr,
private_ident, prop_name_to_member_prop, ExprFactory, ModuleItemLike, StmtLike,
constructor::inject_after_super, default_constructor_with_span, is_literal,
is_simple_pure_expr, private_ident, prop_name_to_member_prop, ExprFactory, ModuleItemLike,
StmtLike,
};
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};

Expand Down Expand Up @@ -107,8 +108,8 @@ impl VisitMut for ClassFieldsUseSet {
// visit inner classes first
n.visit_mut_children_with(self);

let mut fields_handler = FieldsHandler {
has_super: n.super_class.is_some(),
let mut fields_handler: FieldsHandler = FieldsHandler {
super_call_span: n.super_class.as_ref().map(|_| n.span),
};

n.body.visit_mut_with(&mut fields_handler);
Expand Down Expand Up @@ -160,7 +161,7 @@ impl ClassFieldsUseSet {

#[derive(Debug)]
struct FieldsHandler {
has_super: bool,
super_call_span: Option<Span>,
}

impl VisitMut for FieldsHandler {
Expand Down Expand Up @@ -253,7 +254,10 @@ impl VisitMut for FieldsHandler {
if let Some(c) = n.iter_mut().find_map(|m| m.as_mut_constructor()) {
inject_after_super(c, constructor_inits.take());
} else {
let mut c = default_constructor(self.has_super);
let mut c = default_constructor_with_span(
self.super_call_span.is_some(),
self.super_call_span.span(),
);
inject_after_super(&mut c, constructor_inits.take());
n.push(c.into());
}
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_proposal/src/decorator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use swc_common::{util::take::Take, Mark, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{helper, helper_expr};
use swc_ecma_utils::{
alias_ident_for, constructor::inject_after_super, default_constructor,
alias_ident_for, constructor::inject_after_super, default_constructor_with_span,
is_maybe_branch_directive, private_ident, prop_name_to_expr_value, quote_ident, replace_ident,
stack_size::maybe_grow_default, ExprFactory, IdentRenamer,
};
Expand Down Expand Up @@ -330,7 +330,7 @@ impl DecoratorPass {

c.body.insert(
insert_index,
default_constructor(c.super_class.is_some()).into(),
default_constructor_with_span(c.super_class.is_some(), c.span).into(),
);

if let Some(ClassMember::Constructor(c)) = c.body.get_mut(insert_index) {
Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_transforms_proposal/src/decorators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use swc_ecma_ast::{Pass, *};
use swc_ecma_transforms_base::helper;
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
use swc_ecma_utils::{
alias_ident_for, constructor::inject_after_super, default_constructor, prepend_stmt,
alias_ident_for, constructor::inject_after_super, default_constructor_with_span, prepend_stmt,
private_ident, prop_name_to_expr, prop_name_to_expr_value, quote_ident, quote_str, ExprFactory,
};
use swc_ecma_visit::{
Expand Down Expand Up @@ -316,7 +316,8 @@ impl Decorators {
ClassMember::Constructor(c)
}
None => {
let mut c = default_constructor(super_class_ident.is_some());
let mut c =
default_constructor_with_span(super_class_ident.is_some(), class.span);

c.body
.as_mut()
Expand Down
14 changes: 10 additions & 4 deletions crates/swc_ecma_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2226,18 +2226,24 @@ pub fn prop_name_to_member_prop(prop_name: PropName) -> MemberProp {
}
}

#[deprecated(note = "Use default_constructor_with_span instead")]
pub fn default_constructor(has_super: bool) -> Constructor {
trace!(has_super = has_super, "Creating a default constructor");
default_constructor_with_span(has_super, DUMMY_SP)
}

let span = DUMMY_SP;
/// `super_call_span` should be the span of the class definition
/// Use value of [`Class::span`].
pub fn default_constructor_with_span(has_super: bool, super_call_span: Span) -> Constructor {
trace!(has_super = has_super, "Creating a default constructor");
let super_call_span = super_call_span.with_hi(super_call_span.lo);

Constructor {
span: DUMMY_SP,
key: PropName::Ident("constructor".into()),
is_optional: false,
params: if has_super {
vec![ParamOrTsParamProp::Param(Param {
span,
span: DUMMY_SP,
decorators: Vec::new(),
pat: Pat::Rest(RestPat {
span: DUMMY_SP,
Expand All @@ -2252,7 +2258,7 @@ pub fn default_constructor(has_super: bool) -> Constructor {
body: Some(BlockStmt {
stmts: if has_super {
vec![CallExpr {
span: DUMMY_SP,
span: super_call_span,
callee: Callee::Super(Super { span: DUMMY_SP }),
args: vec![ExprOrSpread {
spread: Some(DUMMY_SP),
Expand Down

0 comments on commit 35b0ca0

Please sign in to comment.