Skip to content

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Aug 9, 2020
1 parent 64dc092 commit fce5f5f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 161 deletions.
92 changes: 60 additions & 32 deletions ecmascript/transforms/src/compat/es2020/class_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use self::{
class_name_tdz::ClassNameTdzFolder,
private_field::FieldAccessFolder,
this_in_static::ThisInStaticFolder,
typescript::HygieneFixer,
used_name::{UsedNameCollector, UsedNameRenamer},
};
use crate::{
Expand All @@ -14,14 +13,13 @@ use crate::{
};
use std::collections::HashSet;
use swc_atoms::JsWord;
use swc_common::{chain, Mark, Spanned, DUMMY_SP};
use swc_common::{Mark, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_visit::{Fold, FoldWith, VisitWith};

mod class_name_tdz;
mod private_field;
mod this_in_static;
mod typescript;
mod used_name;

///
Expand All @@ -40,13 +38,10 @@ pub fn class_properties() -> impl Fold {

/// Class properties pass for the typescript.
pub fn typescript_class_properties() -> impl Fold {
chain!(
HygieneFixer::default(),
ClassProperties {
typescript: true,
mark: Mark::root(),
}
)
ClassProperties {
typescript: true,
mark: Mark::root(),
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -599,6 +594,35 @@ impl ClassProperties {
)
}

/// # Legacy support.
///
/// ## Why is this required?
///
/// Hygiene data of
///
///```ts
/// class A {
/// b = this.a;
/// constructor(a){
/// this.a = a;
/// }
/// }
/// ```
///
/// is
///
///```ts
/// class A0 {
/// constructor(a1){
/// this.a0 = a0;
/// this.b0 = this.a0;
/// }
/// }
/// ```
///
/// which is valid only for es2020 properties.
///
/// Legacy proposal which is used by typescript requires different hygiene.
#[allow(clippy::vec_box)]
fn process_constructor(
&mut self,
Expand All @@ -609,28 +633,32 @@ impl ClassProperties {
) -> Option<Constructor> {
let constructor = constructor
.map(|c| {
let mut folder = UsedNameRenamer {
mark: Mark::fresh(Mark::root()),
used_names,
};

// Handle collisions like
//
// var foo = "bar";
//
// class Foo {
// bar = foo;
// static bar = baz;
//
// constructor() {
// var foo = "foo";
// var baz = "baz";
// }
// }
let body = c.body.fold_with(&mut folder);

let params = c.params.fold_with(&mut folder);
Constructor { body, params, ..c }
if self.typescript {
c
} else {
let mut folder = UsedNameRenamer {
mark: Mark::fresh(Mark::root()),
used_names,
};

// Handle collisions like
//
// var foo = "bar";
//
// class Foo {
// bar = foo;
// static bar = baz;
//
// constructor() {
// var foo = "foo";
// var baz = "baz";
// }
// }
let body = c.body.fold_with(&mut folder);

let params = c.params.fold_with(&mut folder);
Constructor { body, params, ..c }
}
})
.or_else(|| {
if constructor_exprs.is_empty() {
Expand Down
129 changes: 0 additions & 129 deletions ecmascript/transforms/src/compat/es2020/class_properties/typescript.rs

This file was deleted.

0 comments on commit fce5f5f

Please sign in to comment.