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

fix(es/compat): Fix the order of initialization for decorators on computed keys #5964

Merged
merged 4 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/issues-5xxx/5189/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2020"
},
"module": {
"type": "commonjs"
}
}
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-5xxx/5189/1/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const sym = Symbol("sym");

class Cls {
@Memoize()
[sym]() { }
}
14 changes: 14 additions & 0 deletions crates/swc/tests/fixture/issues-5xxx/5189/1/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _tsDecorate = require("@swc/helpers/lib/_ts_decorate.js").default;
const sym = Symbol("sym");
var _key;
_key = sym;
class Cls {
[_key]() {}
}
_tsDecorate([
Memoize()
], Cls.prototype, _key, null);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var _key;
_key = foo;
class Foo {
[_key]() {}
}
_key = foo;
__decorate([
dec
], Foo.prototype, _key, null);
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(super) fn new(metadata: bool, use_define_for_class_fields: bool) -> TscDecor
enums: Default::default(),
vars: Default::default(),
appended_exprs: Default::default(),
prepended_exprs: Default::default(),
class_name: Default::default(),
constructor_exprs: Default::default(),
exports: Default::default(),
Expand All @@ -42,6 +43,7 @@ pub(super) struct TscDecorator {
/// Used for computed keys, and this variables are not initialized.
vars: Vec<VarDeclarator>,
appended_exprs: Vec<Box<Expr>>,
prepended_exprs: Vec<Box<Expr>>,

class_name: Option<Ident>,

Expand All @@ -58,6 +60,7 @@ impl TscDecorator {
{
let old_vars = self.vars.take();
let old_appended_exprs = self.appended_exprs.take();
let old_prepended_exprs = self.prepended_exprs.take();

let mut new = vec![];

Expand All @@ -78,6 +81,19 @@ impl TscDecorator {
));
}

new.extend(
self.prepended_exprs
.drain(..)
.into_iter()
.map(|expr| {
Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr,
})
})
.map(T::from_stmt),
);

new.push(s);

new.extend(
Expand All @@ -96,6 +112,7 @@ impl TscDecorator {

*stmts = new;

self.prepended_exprs = old_prepended_exprs;
self.appended_exprs = old_appended_exprs;
self.vars = old_vars;
}
Expand All @@ -114,7 +131,7 @@ impl TscDecorator {
});

// Initialize var
self.appended_exprs.push(Box::new(Expr::Assign(AssignExpr {
self.prepended_exprs.push(Box::new(Expr::Assign(AssignExpr {
span: DUMMY_SP,
op: op!("="),
left: PatOrExpr::Pat(var_name.clone().into()),
Expand Down