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

This commit is contained in:
Donny/강동윤 2022-09-27 20:48:42 +09:00 committed by GitHub
parent 3eecf954b3
commit e410102633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 2 deletions

View File

@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2020"
},
"module": {
"type": "commonjs"
}
}

View File

@ -0,0 +1,6 @@
const sym = Symbol("sym");
class Cls {
@Memoize()
[sym]() { }
}

View 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);

View File

@ -1,8 +1,8 @@
var _key;
_key = foo;
class Foo {
[_key]() {}
}
_key = foo;
__decorate([
dec
], Foo.prototype, _key, null);

View File

@ -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()),