fix(es/transforms/base): Fix ts_resolver (#1903)

swc_ecma_transforms_base:
 - `ts_resolver`: Handle class declarations properly.
This commit is contained in:
강동윤 2021-07-08 00:45:45 +09:00 committed by GitHub
parent ed274b02f2
commit fe7f7b691b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 248 additions and 155 deletions

View File

@ -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"

View File

@ -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;

View File

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

View File

@ -1263,73 +1263,6 @@ identical_ts!(
"
);
identical_ts!(
ts_resolver_003,
"
class Foo<T> {}
class A {}
class B {}
new Foo<A>();
new Foo<B>();
"
);
to_ts!(
ts_resolver_class_constructor,
"
class G<T> {}
class Foo {
constructor() {
class Foo {
}
new G<Foo__2>();
}
}
new G<Foo>();
",
"
class G<T> {
}
class Foo {
constructor(){
class Foo__2 {
}
new G<Foo__2>();
}
}
new G<Foo>();
"
);
to_ts!(
ts_resolver_class_getter,
"
class G<T> {}
class Foo {
get foo() {
class Foo {
}
new G<Foo>();
}
}
",
"
class G<T> {
}
class Foo {
get foo() {
class Foo__2 {
}
new G<Foo__2>();
}
}
"
);
to_ts!(
ts_resolver_nested_interface,
"
@ -2136,84 +2069,6 @@ to_ts!(
"
);
to_ts!(
generic_call_type_argument_inference,
"
class C<T, U> {
constructor(public t: T, public u: U) {
}
foo(t: T, u: U) {
return t;
}
foo2(t: T, u: U) {
return u;
}
foo3<T>(t: T, u: U) {
return t;
}
foo4<U>(t: T, u: U) {
return t;
}
foo5<T,U>(t: T, u: U) {
return t;
}
foo6<T, U>() {
var x: T;
return x;
}
foo7<T, U>(u: U) {
var x: T;
return x;
}
foo8<T, U>() {
var x: T;
return x;
}
}
",
"
class C<T, U> {
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: T__5, u__5: U) {
return t__5;
}
foo4<U__6>(t__6: T, u__6: U__6) {
return t__6;
}
foo5<T__7, U__7>(t__7: T__7, u__7: U__7) {
return t__7;
}
foo6<T__8, U__8>() {
var x__8: T__8;
return x__8;
}
foo7<T__9, U__9>(u__9: U__9) {
var x__9: T__9;
return x__9;
}
foo8<T__10, U__10>() {
var x__10: T__10;
return x__10;
}
}
"
);
to_ts!(
generated_contextual_typing_01,
"

View File

@ -0,0 +1,11 @@
class G<T> { }
class Foo {
constructor() {
class Foo {
}
new G<Foo>();
}
}
new G<Foo>();

View File

@ -0,0 +1,10 @@
class G<T__2> {
}
class Foo {
constructor(){
class Foo__3 {
}
new G<Foo__3>();
}
}
new G<Foo>();

View File

@ -0,0 +1,10 @@
class G<T> { }
class Foo {
get foo() {
class Foo {
}
new G<Foo>();
}
}

View File

@ -0,0 +1,9 @@
class G<T__2> {
}
class Foo {
get foo() {
class Foo__3 {
}
new G<Foo__3>();
}
}

View File

@ -0,0 +1,5 @@
class Foo<T> { }
class A { }
class B { }
new Foo<A>();
new Foo<B>();

View File

@ -0,0 +1,8 @@
class Foo<T__2> {
}
class A {
}
class B {
}
new Foo<A>();
new Foo<B>();

View File

@ -0,0 +1,39 @@
class C<T, U> {
constructor(public t: T, public u: U) {
}
foo(t: T, u: U) {
return t;
}
foo2(t: T, u: U) {
return u;
}
foo3<T>(t: T, u: U) {
return t;
}
foo4<U>(t: T, u: U) {
return t;
}
foo5<T, U>(t: T, u: U) {
return t;
}
foo6<T, U>() {
var x: T;
return x;
}
foo7<T, U>(u: U) {
var x: T;
return x;
}
foo8<T, U>() {
var x: T;
return x;
}
}

View File

@ -0,0 +1,31 @@
class C<T__2, U__2> {
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: T__6, u__6: U__2) {
return t__6;
}
foo4<U__7>(t__7: T__2, u__7: U__7) {
return t__7;
}
foo5<T__8, U__8>(t__8: T__8, u__8: U__8) {
return t__8;
}
foo6<T__9, U__9>() {
var x__9: T__9;
return x__9;
}
foo7<T__10, U__10>(u__10: U__10) {
var x__10: T__10;
return x__10;
}
foo8<T__11, U__11>() {
var x__11: T__11;
return x__11;
}
}

View File

@ -0,0 +1,53 @@
// Type parameters are in scope in their own and other type parameter lists
// Some negative cases
class C<T, U extends T, V extends U> {
z: W; // error
foo<W extends V>(x: W): T {
var r: T;
return x;
}
}
interface I<T, U extends T, V extends U> {
x: T;
y: U;
z: W; // error
foo<W extends V>(x: W): T;
}
function foo<T, U extends T>(x: T, y: U): V { // error
function bar<V extends T, W extends U>(): X { // error
function baz<X extends W, Y extends V>(a: X, b: Y): T {
x = y;
return y;
}
}
}
function foo2<U extends T, T>(x: T, y: U): W { // error
function bar<V extends T, W extends U>(): Y { // error
function baz<X extends W, Y extends V>(a: X, b: Y): T {
x = y;
return y;
}
}
}
var f3 = <T, U extends T>(x: T, y: U) => {
function bar<V extends T, W extends U>(r: X, s: Y) { // error
var g = <X extends W, Y extends V>(a: X, b: Y): T => {
x = y;
return y;
}
}
}
var f4 = <U extends T, T>(x: V, y: X) => { // error
function bar<V extends T, W extends U>() {
var g = <X extends W, Y extends V>(a: X, b: Y): T => {
x = y;
return y;
}
}
}

View File

@ -0,0 +1,45 @@
class C<T__2, U__2 extends T__2, V__2 extends U__2> {
z__0: W;
foo<W__3 extends V__2>(x__3: W__3): T__2 {
var r__3: T__2;
return x__3;
}
}
interface I<T__4, U__4 extends T__4, V__4 extends U__4> {
x__0: T__4;
y__0: U__4;
z__0: W;
foo__0<W__5 extends V__4>(x__5: W__5): T__4;
}
function foo<T__6, U__6 extends T__6>(x__6: T__6, y__6: U__6): V {
function bar__6<V__7 extends T__6, W__7 extends U__6>(): X {
function baz__7<X__8 extends W__7, Y__8 extends V__7>(a__8: X__8, b__8: Y__8): T__6 {
x__6 = y__6;
return y__6;
}
}
}
function foo2<U__9 extends T__9, T__9>(x__9: T__9, y__9: U__9): W {
function bar__9<V__10 extends T__9, W__10 extends U__9>(): Y {
function baz__10<X__11 extends W__10, Y__11 extends V__10>(a__11: X__11, b__11: Y__11): T__9 {
x__9 = y__9;
return y__9;
}
}
}
var f3 = <T__12, U__12 extends T__12>(x__12: T__12, y__12: U__12)=>{
function bar__12<V__13 extends T__12, W__13 extends U__12>(r__13: X, s__13: Y) {
var g__13 = <X__14 extends W__13, Y__14 extends V__13>(a__14: X__14, b__14: Y__14)=>{
x__12 = y__12;
return y__12;
};
}
};
var f4 = <U__15 extends T__15, T__15>(x__15: V, y__15: X)=>{
function bar__15<V__16 extends T__15, W__16 extends U__15>() {
var g__16 = <X__17 extends W__16, Y__17 extends V__16>(a__17: X__17, b__17: Y__17)=>{
x__15 = y__15;
return y__15;
};
}
};