swc_ecma_transforms_compat/es2015/classes/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
use std::iter;
use serde::Deserialize;
use swc_common::{comments::Comments, util::take::Take, BytePos, Mark, Span, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{helper, native::is_native, perf::Check};
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
use swc_ecma_transforms_macros::fast_path;
use swc_ecma_utils::{
alias_if_required, contains_this_expr, default_constructor, is_valid_ident,
is_valid_prop_ident, prepend_stmt, private_ident, prop_name_to_expr, quote_expr, quote_ident,
quote_str, replace_ident, ExprFactory, IdentExt, IsDirective, ModuleItemLike, StmtLike,
};
use swc_ecma_visit::{
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith,
};
use swc_trace_macro::swc_trace;
use tracing::debug;
use self::{
constructor::{
constructor_fn, make_possible_return_value, replace_this_in_constructor, ConstructorFolder,
ReturningMode, SuperCallFinder, SuperFoldingMode,
},
prop_name::{is_pure_prop_name, should_extract_class_prop_key, HashKey},
};
mod constructor;
mod prop_name;
#[tracing::instrument(level = "info", skip_all)]
pub fn classes<C>(comments: Option<C>, config: Config) -> impl Fold + VisitMut
where
C: Comments,
{
as_folder(Classes {
in_strict: false,
comments,
config,
params: Default::default(),
args: Default::default(),
})
}
type IndexMap<K, V> = indexmap::IndexMap<K, V, ahash::RandomState>;
/// `@babel/plugin-transform-classes`
///
/// # In
/// ```js
/// class Test {
/// constructor(name) {
/// this.name = name;
/// }
///
/// logger () {
/// console.log("Hello", this.name);
/// }
/// }
/// ```
///
/// # Out
/// ```js
/// var Test = function () {
/// function Test(name) {
/// _classCallCheck(this, Test);
///
/// this.name = name;
/// }
///
/// Test.prototype.logger = function logger() {
/// console.log("Hello", this.name);
/// };
///
/// return Test;
/// }();
/// ```
#[derive(Default, Clone)]
struct Classes<C>
where
C: Comments,
{
in_strict: bool,
comments: Option<C>,
config: Config,
params: Vec<Param>,
args: Vec<ExprOrSpread>,
}
#[derive(Debug, Clone, Copy, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
#[serde(default)]
pub constant_super: bool,
#[serde(default)]
pub no_class_calls: bool,
#[serde(default)]
pub set_class_methods: bool,
#[serde(default)]
pub super_is_callable_constructor: bool,
}
struct Data {
key_prop: Box<PropName>,
method: Option<Box<Expr>>,
set: Option<Box<Expr>>,
get: Option<Box<Expr>>,
}
#[swc_trace]
impl<C> Classes<C>
where
C: Comments,
{
fn visit_mut_stmt_like<T>(&mut self, stmts: &mut Vec<T>)
where
T: StmtLike + ModuleItemLike + VisitMutWith<Self> + Take,
{
let mut buf = Vec::with_capacity(stmts.len());
let mut first = true;
let old = self.in_strict;
for stmt in stmts.iter_mut() {
match T::try_into_stmt(stmt.take()) {
Err(node) => match node.try_into_module_decl() {
Ok(mut decl) => {
match decl {
ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
decl: DefaultDecl::Class(ClassExpr { ident, class }),
..
}) => {
let ident = ident.unwrap_or_else(|| quote_ident!("_default"));
let mut decl = self.fold_class_as_var_decl(ident.clone(), class);
decl.visit_mut_children_with(self);
buf.push(T::from_stmt(decl.into()));
buf.push(
match T::try_from_module_decl(ModuleDecl::ExportNamed(
NamedExport {
span: DUMMY_SP,
specifiers: vec![ExportNamedSpecifier {
span: DUMMY_SP,
orig: ModuleExportName::Ident(ident),
exported: Some(ModuleExportName::Ident(
quote_ident!("default"),
)),
is_type_only: false,
}
.into()],
src: None,
type_only: false,
asserts: None,
},
)) {
Ok(t) => t,
Err(..) => unreachable!(),
},
);
}
ModuleDecl::ExportDecl(ExportDecl {
span,
decl:
Decl::Class(ClassDecl {
ident,
declare: false,
class,
}),
..
}) => {
let mut decl = self.fold_class_as_var_decl(ident, class);
decl.visit_mut_children_with(self);
buf.push(
match T::try_from_module_decl(ModuleDecl::ExportDecl(
ExportDecl {
span,
decl: decl.into(),
},
)) {
Ok(t) => t,
Err(..) => unreachable!(),
},
);
}
_ => buf.push({
decl.visit_mut_children_with(self);
match T::try_from_module_decl(decl) {
Ok(t) => t,
Err(..) => unreachable!(),
}
}),
};
}
Err(..) => unreachable!(),
},
Ok(mut stmt) => {
if first {
self.in_strict |= stmt.is_use_strict();
}
stmt.visit_mut_children_with(self);
buf.push(T::from_stmt(stmt));
}
}
first = false;
}
self.in_strict = old;
*stmts = buf;
}
}
#[swc_trace]
#[fast_path(ClassFinder)]
impl<C> VisitMut for Classes<C>
where
C: Comments,
{
noop_visit_mut_type!();
fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
self.visit_mut_stmt_like(items)
}
fn visit_mut_stmts(&mut self, items: &mut Vec<Stmt>) {
self.visit_mut_stmt_like(items)
}
fn visit_mut_decl(&mut self, n: &mut Decl) {
if let Decl::Class(decl) = n {
*n = self
.fold_class_as_var_decl(decl.ident.take(), decl.class.take())
.into()
};
n.visit_mut_children_with(self);
}
fn visit_mut_expr(&mut self, n: &mut Expr) {
match n {
Expr::Class(e) => {
let class = self.fold_class(e.ident.take(), e.class.take());
if let Expr::Call(call) = &class {
self.add_pure_comments(call.span.lo)
}
*n = class;
n.visit_mut_children_with(self)
}
_ => n.visit_mut_children_with(self),
}
}
fn visit_mut_var_declarator(&mut self, d: &mut VarDeclarator) {
// will there be anything else in var name at this point?
if let VarDeclarator {
name: Pat::Ident(i),
init: Some(init),
..
} = d
{
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut **init {
c.ident = Some(i.id.clone().private())
}
}
d.visit_mut_children_with(self)
}
/// `let { f = class /* f */ {} } = {};`
fn visit_mut_assign_pat_prop(&mut self, n: &mut AssignPatProp) {
if let Some(value) = &mut n.value {
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut **value {
c.ident = Some(n.key.clone().private());
}
}
n.visit_mut_children_with(self);
}
/// `let [c = class /* c */ {}] = [];`
/// `function foo(bar = class /* bar */ {}) {}`
fn visit_mut_assign_pat(&mut self, n: &mut AssignPat) {
if let (
Pat::Ident(BindingIdent { id, .. }),
Expr::Class(c @ ClassExpr { ident: None, .. }),
) = (&*n.left, &mut *n.right)
{
c.ident = Some(id.clone().private())
}
n.visit_mut_children_with(self);
}
/// {
/// hello: class {},
/// "foo": class {},
/// ["x"]: class {}
/// }
fn visit_mut_key_value_prop(&mut self, n: &mut KeyValueProp) {
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut *n.value {
match &n.key {
PropName::Ident(ident) => {
c.ident = Some(ident.clone().private());
}
PropName::Str(Str { value, span, .. }) => {
if is_valid_prop_ident(value) {
c.ident = Some(private_ident!(*span, value));
}
}
PropName::Computed(ComputedPropName { expr, .. }) => {
if let Expr::Lit(Lit::Str(Str { value, span, .. })) = &**expr {
if is_valid_prop_ident(value) {
c.ident = Some(private_ident!(*span, value));
}
}
}
_ => {}
}
}
n.visit_mut_children_with(self)
}
fn visit_mut_assign_expr(&mut self, a: &mut AssignExpr) {
if let AssignExpr {
op: op!("=") | op!("||=") | op!("??="),
left,
right,
..
} = a
{
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut **right {
match left {
PatOrExpr::Pat(pat) => {
if let Pat::Ident(i) = &**pat {
c.ident = Some(i.id.clone().private())
}
}
PatOrExpr::Expr(expr) => {
if let Expr::Ident(ident) = &**expr {
c.ident = Some(ident.clone().private())
}
}
}
}
}
a.visit_mut_children_with(self)
}
}
#[swc_trace]
impl<C> Classes<C>
where
C: Comments,
{
fn add_pure_comments(&mut self, start: BytePos) {
if let Some(comments) = &self.comments {
comments.add_pure_comment(start);
}
}
fn fold_class_as_var_decl(&mut self, ident: Ident, class: Box<Class>) -> VarDecl {
let span = class.span;
let mut rhs = self.fold_class(Some(ident.clone()), class);
let mut new_name = ident.clone();
new_name.span = new_name.span.apply_mark(Mark::new());
replace_ident(&mut rhs, ident.to_id(), &new_name);
// let VarDecl take every comments except pure
if let Expr::Call(call) = &mut rhs {
let span = Span {
// after class
lo: span.hi + BytePos(5),
..span
};
self.add_pure_comments(span.lo);
call.span = span;
}
VarDecl {
span,
kind: VarDeclKind::Let,
decls: vec![VarDeclarator {
span,
init: Some(Box::new(rhs)),
// Foo in var Foo =
name: ident.into(),
definite: false,
}],
declare: false,
}
}
/// Turns class expression into iife.
///
/// ```js
/// class Foo {}
/// ```
///
/// ```js
/// function() {
/// var Foo = function Foo(){
/// };
/// }()
/// ```
fn fold_class(&mut self, class_name: Option<Ident>, class: Box<Class>) -> Expr {
let span = class.span;
// Ident of the super class *inside* function.
let super_ident = class
.super_class
.as_ref()
.map(|e| alias_if_required(e, "_superClass").0);
let has_super = super_ident.is_some();
let (mut params, mut args, super_ident) = if let Some(ref super_ident) = super_ident {
// Param should have a separate syntax context from arg.
let super_param = private_ident!(super_ident.sym.clone());
let params = vec![Param {
span: DUMMY_SP,
decorators: Default::default(),
pat: super_param.clone().into(),
}];
let super_class = class.super_class.clone().unwrap();
let is_super_native = match *super_class {
Expr::Ident(Ident { ref sym, .. }) => is_native(sym),
_ => false,
};
if is_super_native {
(
params,
vec![CallExpr {
span: DUMMY_SP,
callee: helper!(wrap_native_super, "wrapNativeSuper"),
args: vec![super_class.as_arg()],
type_args: Default::default(),
}
.as_arg()],
Some(super_param),
)
} else {
(params, vec![super_class.as_arg()], Some(super_param))
}
} else {
(vec![], vec![], None)
};
let mut stmts = self.class_to_stmts(class_name, super_ident, class);
params.extend(self.params.take());
args.extend(self.args.take());
let cnt_of_non_directive = stmts
.iter()
.filter(|stmt| match stmt {
Stmt::Expr(ExprStmt { expr, .. }) => !matches!(&**expr, Expr::Lit(Lit::Str(..))),
_ => true,
})
.count();
if !has_super && cnt_of_non_directive == 1 {
// class Foo {}
//
// should be
//
// var Foo = function Foo() {
// _classCallCheck(this, Foo);
// };
//
// instead of
//
// var Foo = function(){
// function Foo() {
// _classCallCheck(this, Foo);
// }
//
// return Foo;
// }();
let stmt = stmts.pop().unwrap();
match stmt {
Stmt::Decl(Decl::Fn(FnDecl {
ident,
mut function,
..
})) => {
if let Some(use_strict) = stmts.pop() {
prepend_stmt(&mut function.body.as_mut().unwrap().stmts, use_strict);
}
function.span = span;
return Expr::Fn(FnExpr {
ident: Some(ident),
function,
});
}
_ => unreachable!(),
}
}
let body = BlockStmt {
span: DUMMY_SP,
stmts,
};
let call = CallExpr {
span,
callee: Function {
span,
is_async: false,
is_generator: false,
params,
body: Some(body),
decorators: Default::default(),
type_params: Default::default(),
return_type: Default::default(),
}
.as_callee(),
args,
type_args: Default::default(),
};
Expr::Call(call)
}
/// Returned `stmts` contains `return Foo`
fn class_to_stmts(
&mut self,
class_name: Option<Ident>,
super_class_ident: Option<Ident>,
class: Box<Class>,
) -> Vec<Stmt> {
let class_name = class_name.unwrap_or_else(|| quote_ident!("_class"));
let mut stmts = vec![];
let mut methods = vec![];
let mut constructor = None;
for member in class.body {
match member {
ClassMember::Constructor(c) => {
if constructor.is_some() {
unimplemented!("multiple constructor")
} else {
constructor = Some(c)
}
}
ClassMember::PrivateMethod(_) => unreachable!(
"classes pass: private method\nclass_properties pass should remove this"
),
ClassMember::Method(m) => methods.push(m),
ClassMember::ClassProp(..) => {
unreachable!("classes pass: property\nclass_properties pass should remove this")
}
ClassMember::PrivateProp(..) => unreachable!(
"classes pass: private property\nclass_properties pass should remove this"
),
ClassMember::TsIndexSignature(..) => {
// We just strip this.
}
ClassMember::Empty(..) => {}
ClassMember::StaticBlock(..) => unreachable!(
"classes pass: static blocks\nstatic_blocks pass should remove this"
),
}
}
if let Some(ref super_class_ident) = super_class_ident {
// inject helper methods
let mut class_name_sym = class_name.clone();
class_name_sym.span = DUMMY_SP;
class_name_sym.span.ctxt = class_name.span.ctxt;
let mut super_class_name_sym = super_class_ident.clone();
super_class_name_sym.span = DUMMY_SP;
super_class_name_sym.span.ctxt = super_class_ident.span.ctxt;
stmts.push(
CallExpr {
span: DUMMY_SP,
callee: helper!(inherits, "inherits"),
args: vec![class_name_sym.as_arg(), super_class_name_sym.as_arg()],
type_args: Default::default(),
}
.into_stmt(),
);
}
let super_var = super_class_ident.as_ref().map(|super_class| {
let var = private_ident!("_super");
let mut class_name_sym = class_name.clone();
class_name_sym.span = DUMMY_SP;
class_name_sym.span.ctxt = class_name.span.ctxt;
if !self.config.super_is_callable_constructor {
stmts.push(
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: Default::default(),
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: var.clone().into(),
init: Some(Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: helper!(create_super, "createSuper"),
args: vec![class_name_sym.as_arg()],
type_args: Default::default(),
}))),
definite: Default::default(),
}],
}
.into(),
);
var
} else {
super_class.clone()
}
});
// Marker for `_this`
let this_mark = Mark::fresh(Mark::root());
{
// Process constructor
let mut constructor =
constructor.unwrap_or_else(|| default_constructor(super_class_ident.is_some()));
// Rename variables to avoid conflicting with class name
// TODO: bring it back once we have a proper private ident
// constructor.body.visit_mut_with(&mut VarRenamer {
// mark: Mark::fresh(Mark::root()),
// class_name: &class_name.sym,
// });
// Black magic to detect injected constructor.
let is_constructor_default = constructor.span.is_dummy();
if is_constructor_default {
debug!("Dropping constructor parameters because the constructor is injected");
constructor.params = vec![];
}
let mut insert_this = false;
if super_class_ident.is_some() {
let inserted_this = replace_this_in_constructor(this_mark, &mut constructor);
insert_this |= inserted_this;
}
let mut vars = vec![];
let mut body = constructor.body.unwrap().stmts;
// should we insert `var _this`?
let is_always_initialized = is_always_initialized(&body);
// We should handle branching
if !is_always_initialized {
insert_this = true;
}
// inject possibleReturnCheck
let found_mode = SuperCallFinder::find(&body);
let mode = match found_mode {
None => None,
_ => {
if insert_this {
Some(SuperFoldingMode::Assign)
} else {
found_mode
}
}
};
if super_class_ident.is_some() {
let this = quote_ident!(DUMMY_SP.apply_mark(this_mark), "_this");
// We should fold body instead of constructor itself.
// Handle `super()`
body.visit_mut_with(&mut ConstructorFolder {
class_name: &class_name,
mode: if insert_this {
Some(SuperFoldingMode::Assign)
} else {
mode
},
mark: this_mark,
is_constructor_default,
super_var,
ignore_return: false,
super_is_callable_constructor: self.config.super_is_callable_constructor,
});
insert_this |= (mode.is_none() && !is_always_initialized)
|| mode == Some(SuperFoldingMode::Assign);
if insert_this {
vars.push(VarDeclarator {
span: DUMMY_SP,
name: this.clone().into(),
init: None,
definite: false,
});
}
if !vars.is_empty() {
prepend_stmt(
&mut body,
VarDecl {
span: DUMMY_SP,
declare: false,
kind: VarDeclKind::Var,
decls: vars,
}
.into(),
);
}
let is_last_return = matches!(body.last(), Some(Stmt::Return(..)));
if !is_last_return {
if is_always_initialized {
body.push(Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(Box::new(Expr::Ident(this))),
}));
} else {
let possible_return_value =
Box::new(make_possible_return_value(ReturningMode::Returning {
mark: this_mark,
arg: None,
}));
body.push(Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(possible_return_value),
}));
}
}
}
let is_this_declared = (insert_this && super_class_ident.is_some())
|| (mode == Some(SuperFoldingMode::Var));
// Handle `super.XX`
body = self.handle_super_access(
&class_name,
&super_class_ident,
body,
if is_this_declared {
Some(this_mark)
} else {
None
},
);
// inject _classCallCheck(this, Bar);
if !self.config.no_class_calls {
inject_class_call_check(&mut body, class_name.clone());
}
stmts.push(Stmt::Decl(Decl::Fn(FnDecl {
ident: class_name.clone(),
function: constructor_fn(Constructor {
body: Some(BlockStmt {
span: DUMMY_SP,
stmts: body,
}),
..constructor
}),
declare: false,
})));
}
// convert class methods
stmts.extend(self.fold_class_methods(&class_name, &super_class_ident, methods));
if stmts.first().map(|v| !v.is_use_strict()).unwrap_or(false) && !self.in_strict {
prepend_stmt(
&mut stmts,
Lit::Str(Str {
span: DUMMY_SP,
value: "use strict".into(),
raw: Some("\"use strict\"".into()),
})
.into_stmt(),
);
if stmts.len() == 2 {
return stmts;
}
}
if super_class_ident.is_none()
&& stmts
.iter()
.filter(|stmt| match stmt {
Stmt::Expr(ExprStmt { expr, .. }) => {
!matches!(&**expr, Expr::Lit(Lit::Str(..)))
}
_ => true,
})
.count()
== 1
{
return stmts;
}
let mut class_name_sym = class_name.clone();
class_name_sym.span = DUMMY_SP;
class_name_sym.span.ctxt = class_name.span.ctxt;
// `return Foo`
stmts.push(Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(Box::new(Expr::Ident(class_name_sym))),
}));
stmts
}
///
/// - `this_mark`: `Some(mark)` if we injected `var _this;`; otherwise
/// `None`
fn handle_super_access(
&mut self,
class_name: &Ident,
super_class_ident: &Option<Ident>,
mut body: Vec<Stmt>,
this_mark: Option<Mark>,
) -> Vec<Stmt> {
let mut vars = vec![];
let mut folder = SuperFieldAccessFolder {
class_name,
vars: &mut vars,
constructor_this_mark: this_mark,
// constructor cannot be static
is_static: false,
folding_constructor: true,
in_nested_scope: false,
in_injected_define_property_call: false,
this_alias_mark: None,
constant_super: self.config.constant_super,
super_class: super_class_ident,
in_pat: false,
};
body.visit_mut_with(&mut folder);
if let Some(mark) = folder.this_alias_mark {
prepend_stmt(
&mut body,
VarDecl {
span: DUMMY_SP,
declare: false,
kind: VarDeclKind::Var,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: quote_ident!(DUMMY_SP.apply_mark(mark), "_this").into(),
init: Some(Box::new(Expr::This(ThisExpr { span: DUMMY_SP }))),
definite: false,
}],
}
.into(),
);
}
if !vars.is_empty() {
prepend_stmt(
&mut body,
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: vars,
}
.into(),
);
}
body
}
fn fold_class_methods(
&mut self,
class_name: &Ident,
super_class_ident: &Option<Ident>,
methods: Vec<ClassMethod>,
) -> Vec<Stmt> {
if methods.is_empty() {
return vec![];
}
/// { key: "prop" }
fn mk_key_prop(key: PropName) -> Box<Prop> {
Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!(key.span(), "key")),
value: match key {
PropName::Ident(i) => Box::new(Expr::Lit(Lit::Str(quote_str!(i.span, i.sym)))),
PropName::Str(s) => Box::new(Expr::from(s)),
PropName::Num(n) => Box::new(Expr::from(n)),
PropName::BigInt(b) => Box::new(Expr::Lit(
Str {
span: b.span,
raw: None,
value: b.value.to_string().into(),
}
.into(),
)),
PropName::Computed(c) => c.expr,
},
}))
}
fn mk_key_prop_member(key: PropName) -> MemberProp {
match key {
PropName::Ident(i) => MemberProp::Ident(i),
PropName::Str(s) => MemberProp::Computed(ComputedPropName {
span: s.span,
expr: Box::new(Expr::Lit(Lit::Str(s))),
}),
PropName::Num(n) => MemberProp::Computed(ComputedPropName {
span: n.span,
expr: Box::new(Expr::Lit(Lit::Num(n))),
}),
PropName::BigInt(b) => MemberProp::Computed(ComputedPropName {
span: b.span,
expr: Box::new(Expr::Lit(
Str {
span: b.span,
raw: None,
value: b.value.to_string().into(),
}
.into(),
)),
}),
PropName::Computed(c) => MemberProp::Computed(c),
}
}
fn mk_arg_obj_for_create_class(props: IndexMap<HashKey, Data>) -> ExprOrSpread {
if props.is_empty() {
return quote_expr!(DUMMY_SP, null).as_arg();
}
Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: props
.into_iter()
.map(|(_, data)| {
let mut props = vec![PropOrSpread::Prop(mk_key_prop(*data.key_prop))];
macro_rules! add {
($field:expr, $kind:expr, $s:literal) => {{
if let Some(value) = $field {
let value = escape_keywords(value);
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!($s)),
value,
},
))));
}
}};
}
add!(data.get, MethodKind::Getter, "get");
add!(data.set, MethodKind::Setter, "set");
add!(data.method, MethodKind::Method, "value");
ObjectLit {
span: DUMMY_SP,
props,
}
.as_arg()
})
.map(Some)
.collect(),
})
.as_arg()
}
/// _createClass(Foo, [{}], [{}]);
fn mk_create_class_call(
class_name: Ident,
methods: ExprOrSpread,
static_methods: Option<ExprOrSpread>,
) -> Stmt {
let mut class_name_sym = class_name.clone();
class_name_sym.span = DUMMY_SP;
class_name_sym.span.ctxt = class_name.span.ctxt;
CallExpr {
span: DUMMY_SP,
callee: helper!(create_class, "createClass"),
args: iter::once(class_name_sym.as_arg())
.chain(iter::once(methods))
.chain(static_methods)
.collect(),
type_args: Default::default(),
}
.into_stmt()
}
let (mut props, mut static_props) = (IndexMap::default(), IndexMap::default());
let should_extract = should_extract_class_prop_key(&methods);
for mut m in methods {
let key = HashKey::from(&m.key);
let key_is_pure = is_pure_prop_name(&m.key);
// class is always strict, however computed key is not part of class
let key_contain_this = !self.in_strict && contains_this_expr(&m.key);
let key_prop = Box::new(m.key.clone());
let computed = matches!(m.key, PropName::Computed(..));
let prop_name = prop_name_to_expr(m.key);
let key_prop = if should_extract && !key_is_pure || key_contain_this {
let ident = private_ident!("_prop");
self.params.push(ident.clone().into());
self.args.push(prop_name.clone().into());
Box::new(PropName::Computed(ComputedPropName {
span: DUMMY_SP,
expr: Box::new(ident.into()),
}))
} else {
key_prop
};
let append_to: &mut IndexMap<_, _> = if m.is_static {
&mut static_props
} else {
&mut props
};
let mut vars = vec![];
let mut folder = SuperFieldAccessFolder {
class_name,
vars: &mut vars,
constructor_this_mark: None,
is_static: m.is_static,
folding_constructor: false,
in_nested_scope: false,
in_injected_define_property_call: false,
this_alias_mark: None,
constant_super: self.config.constant_super,
super_class: super_class_ident,
in_pat: false,
};
m.function.visit_mut_with(&mut folder);
if let Some(mark) = folder.this_alias_mark {
prepend_stmt(
&mut m.function.body.as_mut().unwrap().stmts,
VarDecl {
span: DUMMY_SP,
declare: false,
kind: VarDeclKind::Var,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: quote_ident!(DUMMY_SP.apply_mark(mark), "_this").into(),
init: Some(Box::new(Expr::This(ThisExpr { span: DUMMY_SP }))),
definite: false,
}],
}
.into(),
);
}
if !vars.is_empty() {
prepend_stmt(
&mut m.function.body.as_mut().unwrap().stmts,
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: vars,
}
.into(),
);
}
let value = Box::new(Expr::Fn(FnExpr {
ident: if m.kind == MethodKind::Method && !computed {
match prop_name {
Expr::Ident(ident) => Some(private_ident!(ident.span, ident.sym)),
Expr::Lit(Lit::Str(Str { span, value, .. })) if is_valid_ident(&value) => {
Some(Ident::new(value, span.private()))
}
_ => None,
}
} else {
None
},
function: m.function,
}));
let data = append_to.entry(key).or_insert_with(|| Data {
key_prop,
get: None,
set: None,
method: None,
});
match m.kind {
// https://github.com/swc-project/swc/issues/5029
MethodKind::Getter => {
data.method = None;
data.get = Some(value)
}
MethodKind::Setter => {
data.method = None;
data.set = Some(value)
}
MethodKind::Method => {
data.get = None;
data.set = None;
data.method = Some(value)
}
}
}
let mut res = Vec::new();
if self.config.set_class_methods {
let proto = private_ident!("_proto");
props.retain(|_, v| {
if let Some(method) = v.method.take() {
if res.is_empty() {
res.push(
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: proto.clone().into(),
init: Some(Box::new(
class_name.clone().make_member(quote_ident!("prototype")),
)),
definite: false,
}],
}
.into(),
);
}
let span = method.span();
let prop = *v.key_prop.clone();
res.push(Stmt::Expr(ExprStmt {
span,
expr: Box::new(Expr::Assign(AssignExpr {
span,
op: op!("="),
left: PatOrExpr::Expr(Box::new(Expr::Member(MemberExpr {
span,
obj: Box::new(proto.clone().into()),
prop: mk_key_prop_member(prop),
}))),
right: escape_keywords(method),
})),
}));
!(v.get.is_none() && v.set.is_none())
} else {
true
}
});
static_props.retain(|_, v| {
if let Some(method) = v.method.take() {
let span = method.span();
let prop = *v.key_prop.clone();
res.push(Stmt::Expr(ExprStmt {
span,
expr: Box::new(Expr::Assign(AssignExpr {
span,
op: op!("="),
left: PatOrExpr::Expr(Box::new(Expr::Member(MemberExpr {
span,
obj: Box::new(class_name.clone().into()),
prop: mk_key_prop_member(prop),
}))),
right: escape_keywords(method),
})),
}));
!(v.get.is_none() && v.set.is_none())
} else {
true
}
})
}
if props.is_empty() && static_props.is_empty() {
return res;
}
res.push(mk_create_class_call(
class_name.clone(),
mk_arg_obj_for_create_class(props),
if static_props.is_empty() {
None
} else {
Some(mk_arg_obj_for_create_class(static_props))
},
));
res
}
}
#[tracing::instrument(level = "info", skip_all)]
fn inject_class_call_check(c: &mut Vec<Stmt>, name: Ident) {
let mut class_name_sym = name.clone();
class_name_sym.span = DUMMY_SP;
class_name_sym.span.ctxt = name.span.ctxt;
let class_call_check = CallExpr {
span: DUMMY_SP,
callee: helper!(class_call_check, "classCallCheck"),
args: vec![
Expr::This(ThisExpr { span: DUMMY_SP }).as_arg(),
class_name_sym.as_arg(),
],
type_args: Default::default(),
}
.into_stmt();
prepend_stmt(c, class_call_check)
}
/// Returns true if no `super` is used before `super()` call.
#[tracing::instrument(level = "info", skip_all)]
fn is_always_initialized(body: &[Stmt]) -> bool {
struct SuperFinder {
found: bool,
}
impl Visit for SuperFinder {
noop_visit_type!();
fn visit_callee(&mut self, node: &Callee) {
match *node {
Callee::Super(..) => self.found = true,
_ => node.visit_children_with(self),
}
}
fn visit_super_prop_expr(&mut self, _: &SuperPropExpr) {
self.found = true
}
}
let pos = match body.iter().position(|s| match s {
Stmt::Expr(ExprStmt { expr, .. }) => matches!(
&**expr,
Expr::Call(CallExpr {
callee: Callee::Super(..),
..
})
),
_ => false,
}) {
Some(pos) => pos,
_ => return false,
};
let mut v = SuperFinder { found: false };
let body = &body[..pos];
v.visit_stmts(body);
!v.found
}
fn escape_keywords(mut e: Box<Expr>) -> Box<Expr> {
if let Expr::Fn(f) = &mut *e {
if let Some(i) = &mut f.ident {
let sym = Ident::verify_symbol(&i.sym);
if let Err(new) = sym {
i.sym = new.into();
}
}
}
e
}
#[derive(Default)]
struct ClassFinder {
found: bool,
}
impl Visit for ClassFinder {
noop_visit_type!();
fn visit_class(&mut self, _: &Class) {
self.found = true
}
}
impl Check for ClassFinder {
fn should_handle(&self) -> bool {
self.found
}
}