diff --git a/ecmascript/transforms/base/Cargo.toml b/ecmascript/transforms/base/Cargo.toml index 0c20abbf276..75c644bde26 100644 --- a/ecmascript/transforms/base/Cargo.toml +++ b/ecmascript/transforms/base/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_ecma_transforms_base" repository = "https://github.com/swc-project/swc.git" -version = "0.21.0" +version = "0.21.1" [dependencies] fxhash = "0.2.1" diff --git a/ecmascript/transforms/base/src/hygiene/mod.rs b/ecmascript/transforms/base/src/hygiene/mod.rs index 5d1966f2489..1d86dfc2ccc 100644 --- a/ecmascript/transforms/base/src/hygiene/mod.rs +++ b/ecmascript/transforms/base/src/hygiene/mod.rs @@ -540,15 +540,6 @@ macro_rules! track_ident_mut { self.ident_type = old; } - fn visit_mut_class_decl(&mut self, n: &mut ClassDecl) { - let old = self.ident_type; - self.ident_type = IdentType::Binding; - n.ident.visit_mut_with(self); - self.ident_type = old; - - n.class.visit_mut_with(self); - } - fn visit_mut_key_value_pat_prop(&mut self, n: &mut KeyValuePatProp) { n.key.visit_mut_with(self); n.value.visit_mut_with(self); @@ -591,6 +582,15 @@ impl<'a> VisitMut for Hygiene<'a> { track_ident_mut!(); + fn visit_mut_class_decl(&mut self, n: &mut ClassDecl) { + let old = self.ident_type; + self.ident_type = IdentType::Binding; + n.ident.visit_mut_with(self); + self.ident_type = old; + + n.class.visit_mut_with(self); + } + fn visit_mut_setter_prop(&mut self, f: &mut SetterProp) { let old = self.ident_type; self.ident_type = IdentType::Ref; diff --git a/ecmascript/transforms/base/src/resolver/mod.rs b/ecmascript/transforms/base/src/resolver/mod.rs index 3c1a59c0aa4..b0903fe7cc0 100644 --- a/ecmascript/transforms/base/src/resolver/mod.rs +++ b/ecmascript/transforms/base/src/resolver/mod.rs @@ -510,6 +510,23 @@ impl<'a> VisitMut for Resolver<'a> { c.body.visit_mut_children_with(&mut folder); } + fn visit_mut_class_decl(&mut self, n: &mut ClassDecl) { + self.modify(&mut n.ident, None); + + // Create a child scope. The class name is only accessible within the class. + let child_mark = Mark::fresh(self.mark); + + let mut folder = Resolver::new( + child_mark, + Scope::new(ScopeKind::Fn, Some(&self.current)), + self.handle_types, + ); + + folder.ident_type = IdentType::Ref; + + n.class.visit_mut_with(&mut folder); + } + fn visit_mut_class_expr(&mut self, n: &mut ClassExpr) { // Create a child scope. The class name is only accessible within the class. let child_mark = Mark::fresh(self.mark); diff --git a/ecmascript/transforms/base/src/resolver/tests.rs b/ecmascript/transforms/base/src/resolver/tests.rs index dca048231ff..ca45854ab64 100644 --- a/ecmascript/transforms/base/src/resolver/tests.rs +++ b/ecmascript/transforms/base/src/resolver/tests.rs @@ -1263,73 +1263,6 @@ identical_ts!( " ); -identical_ts!( - ts_resolver_003, - " - class Foo {} - class A {} - class B {} - new Foo(); - new Foo(); - " -); - -to_ts!( - ts_resolver_class_constructor, - " -class G {} -class Foo { - constructor() { - class Foo { - - } - - new G(); - } -} -new G(); -", - " -class G { -} -class Foo { - constructor(){ - class Foo__2 { - } - new G(); - } -} -new G(); - " -); - -to_ts!( - ts_resolver_class_getter, - " -class G {} -class Foo { - get foo() { - class Foo { - - } - - new G(); - } -} -", - " -class G { -} -class Foo { - get foo() { - class Foo__2 { - } - new G(); - } -} - " -); - to_ts!( ts_resolver_nested_interface, " @@ -2136,84 +2069,6 @@ to_ts!( " ); -to_ts!( - generic_call_type_argument_inference, - " - class C { - constructor(public t: T, public u: U) { - } - - foo(t: T, u: U) { - return t; - } - - foo2(t: T, u: U) { - return u; - } - - foo3(t: T, u: U) { - return t; - } - - foo4(t: T, u: U) { - return t; - } - - foo5(t: T, u: U) { - return t; - } - - foo6() { - var x: T; - return x; - } - - foo7(u: U) { - var x: T; - return x; - } - - foo8() { - var x: T; - return x; - } - } - ", - " - class C { - constructor(public t__2: T, public u__2: U){ - } - foo(t__3: T, u__3: U) { - return t__3; - } - foo2(t__4: T, u__4: U) { - return u__4; - } - foo3(t__5: T__5, u__5: U) { - return t__5; - } - foo4(t__6: T, u__6: U__6) { - return t__6; - } - foo5(t__7: T__7, u__7: U__7) { - return t__7; - } - foo6() { - var x__8: T__8; - return x__8; - } - foo7(u__9: U__9) { - var x__9: T__9; - return x__9; - } - foo8() { - var x__10: T__10; - return x__10; - } - } - " -); - to_ts!( generated_contextual_typing_01, " diff --git a/ecmascript/transforms/base/tests/ts-resolver/class/constructor/input.ts b/ecmascript/transforms/base/tests/ts-resolver/class/constructor/input.ts new file mode 100644 index 00000000000..0f3d52e939b --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/class/constructor/input.ts @@ -0,0 +1,11 @@ +class G { } +class Foo { + constructor() { + class Foo { + + } + + new G(); + } +} +new G(); \ No newline at end of file diff --git a/ecmascript/transforms/base/tests/ts-resolver/class/constructor/output.ts b/ecmascript/transforms/base/tests/ts-resolver/class/constructor/output.ts new file mode 100644 index 00000000000..4d109d0c829 --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/class/constructor/output.ts @@ -0,0 +1,10 @@ +class G { +} +class Foo { + constructor(){ + class Foo__3 { + } + new G(); + } +} +new G(); diff --git a/ecmascript/transforms/base/tests/ts-resolver/class/getter/input.ts b/ecmascript/transforms/base/tests/ts-resolver/class/getter/input.ts new file mode 100644 index 00000000000..449175d646c --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/class/getter/input.ts @@ -0,0 +1,10 @@ +class G { } +class Foo { + get foo() { + class Foo { + + } + + new G(); + } +} \ No newline at end of file diff --git a/ecmascript/transforms/base/tests/ts-resolver/class/getter/output.ts b/ecmascript/transforms/base/tests/ts-resolver/class/getter/output.ts new file mode 100644 index 00000000000..d183235f584 --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/class/getter/output.ts @@ -0,0 +1,9 @@ +class G { +} +class Foo { + get foo() { + class Foo__3 { + } + new G(); + } +} diff --git a/ecmascript/transforms/base/tests/ts-resolver/simple/003/input.ts b/ecmascript/transforms/base/tests/ts-resolver/simple/003/input.ts new file mode 100644 index 00000000000..3e4519e5bde --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/simple/003/input.ts @@ -0,0 +1,5 @@ +class Foo { } +class A { } +class B { } +new Foo(); +new Foo(); \ No newline at end of file diff --git a/ecmascript/transforms/base/tests/ts-resolver/simple/003/output.ts b/ecmascript/transforms/base/tests/ts-resolver/simple/003/output.ts new file mode 100644 index 00000000000..31e01e4721d --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/simple/003/output.ts @@ -0,0 +1,8 @@ +class Foo { +} +class A { +} +class B { +} +new Foo(); +new Foo(); diff --git a/ecmascript/transforms/base/tests/ts-resolver/tsc/generic-call-type-argument-inference/input.ts b/ecmascript/transforms/base/tests/ts-resolver/tsc/generic-call-type-argument-inference/input.ts new file mode 100644 index 00000000000..7135d57db48 --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/tsc/generic-call-type-argument-inference/input.ts @@ -0,0 +1,39 @@ +class C { + constructor(public t: T, public u: U) { + } + + foo(t: T, u: U) { + return t; + } + + foo2(t: T, u: U) { + return u; + } + + foo3(t: T, u: U) { + return t; + } + + foo4(t: T, u: U) { + return t; + } + + foo5(t: T, u: U) { + return t; + } + + foo6() { + var x: T; + return x; + } + + foo7(u: U) { + var x: T; + return x; + } + + foo8() { + var x: T; + return x; + } +} \ No newline at end of file diff --git a/ecmascript/transforms/base/tests/ts-resolver/tsc/generic-call-type-argument-inference/output.ts b/ecmascript/transforms/base/tests/ts-resolver/tsc/generic-call-type-argument-inference/output.ts new file mode 100644 index 00000000000..9d26ed117c2 --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/tsc/generic-call-type-argument-inference/output.ts @@ -0,0 +1,31 @@ +class C { + constructor(public t__3: T__2, public u__3: U__2){ + } + foo(t__4: T__2, u__4: U__2) { + return t__4; + } + foo2(t__5: T__2, u__5: U__2) { + return u__5; + } + foo3(t__6: T__6, u__6: U__2) { + return t__6; + } + foo4(t__7: T__2, u__7: U__7) { + return t__7; + } + foo5(t__8: T__8, u__8: U__8) { + return t__8; + } + foo6() { + var x__9: T__9; + return x__9; + } + foo7(u__10: U__10) { + var x__10: T__10; + return x__10; + } + foo8() { + var x__11: T__11; + return x__11; + } +} diff --git a/ecmascript/transforms/base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/input.ts b/ecmascript/transforms/base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/input.ts new file mode 100644 index 00000000000..6b068db1f66 --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/input.ts @@ -0,0 +1,53 @@ +// Type parameters are in scope in their own and other type parameter lists +// Some negative cases + +class C { + z: W; // error + foo(x: W): T { + var r: T; + return x; + } +} + +interface I { + x: T; + y: U; + z: W; // error + foo(x: W): T; +} + +function foo(x: T, y: U): V { // error + function bar(): X { // error + function baz(a: X, b: Y): T { + x = y; + return y; + } + } +} + +function foo2(x: T, y: U): W { // error + function bar(): Y { // error + function baz(a: X, b: Y): T { + x = y; + return y; + } + } +} + +var f3 = (x: T, y: U) => { + function bar(r: X, s: Y) { // error + var g = (a: X, b: Y): T => { + x = y; + return y; + } + } +} + +var f4 = (x: V, y: X) => { // error + function bar() { + var g = (a: X, b: Y): T => { + x = y; + return y; + } + } +} \ No newline at end of file diff --git a/ecmascript/transforms/base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/output.ts b/ecmascript/transforms/base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/output.ts new file mode 100644 index 00000000000..caa1c33adb1 --- /dev/null +++ b/ecmascript/transforms/base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/output.ts @@ -0,0 +1,45 @@ +class C { + z__0: W; + foo(x__3: W__3): T__2 { + var r__3: T__2; + return x__3; + } +} +interface I { + x__0: T__4; + y__0: U__4; + z__0: W; + foo__0(x__5: W__5): T__4; +} +function foo(x__6: T__6, y__6: U__6): V { + function bar__6(): X { + function baz__7(a__8: X__8, b__8: Y__8): T__6 { + x__6 = y__6; + return y__6; + } + } +} +function foo2(x__9: T__9, y__9: U__9): W { + function bar__9(): Y { + function baz__10(a__11: X__11, b__11: Y__11): T__9 { + x__9 = y__9; + return y__9; + } + } +} +var f3 = (x__12: T__12, y__12: U__12)=>{ + function bar__12(r__13: X, s__13: Y) { + var g__13 = (a__14: X__14, b__14: Y__14)=>{ + x__12 = y__12; + return y__12; + }; + } +}; +var f4 = (x__15: V, y__15: X)=>{ + function bar__15() { + var g__16 = (a__17: X__17, b__17: Y__17)=>{ + x__15 = y__15; + return y__15; + }; + } +};