mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 22:22:34 +03:00
fix(es/compat): Fix the order of initialization for decorators on computed keys (#5964)
This commit is contained in:
parent
3eecf954b3
commit
e410102633
12
crates/swc/tests/fixture/issues-5xxx/5189/1/input/.swcrc
Normal file
12
crates/swc/tests/fixture/issues-5xxx/5189/1/input/.swcrc
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"decorators": true
|
||||
},
|
||||
"target": "es2020"
|
||||
},
|
||||
"module": {
|
||||
"type": "commonjs"
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
const sym = Symbol("sym");
|
||||
|
||||
class Cls {
|
||||
@Memoize()
|
||||
[sym]() { }
|
||||
}
|
14
crates/swc/tests/fixture/issues-5xxx/5189/1/output/index.ts
Normal file
14
crates/swc/tests/fixture/issues-5xxx/5189/1/output/index.ts
Normal file
@ -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);
|
@ -1,8 +1,8 @@
|
||||
var _key;
|
||||
_key = foo;
|
||||
class Foo {
|
||||
[_key]() {}
|
||||
}
|
||||
_key = foo;
|
||||
__decorate([
|
||||
dec
|
||||
], Foo.prototype, _key, null);
|
||||
|
@ -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(),
|
||||
@ -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>,
|
||||
|
||||
@ -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![];
|
||||
|
||||
@ -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(
|
||||
@ -96,6 +112,7 @@ impl TscDecorator {
|
||||
|
||||
*stmts = new;
|
||||
|
||||
self.prepended_exprs = old_prepended_exprs;
|
||||
self.appended_exprs = old_appended_exprs;
|
||||
self.vars = old_vars;
|
||||
}
|
||||
@ -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()),
|
||||
|
Loading…
Reference in New Issue
Block a user