From bc38ac906c427ba060f3da47c64726fe417162ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 19 Jan 2024 15:06:13 +0900 Subject: [PATCH] fix(es/ast): Fix definition of `SetterProp` (#8314) **Related issue:** - Closes #8157 - Closes #8377 --- crates/swc_ecma_ast/src/prop.rs | 4 +- crates/swc_ecma_codegen/src/lib.rs | 8 + crates/swc_ecma_compat_common/src/macros.rs | 4 +- .../src/computed_props.rs | 1 + crates/swc_ecma_parser/src/parser/object.rs | 32 ++-- .../tests/tsc/accessorWithES3.json | 1 + .../tests/tsc/accessorWithES5.json | 1 + .../tsc/computedPropertyNames11_ES5.json | 5 + .../tsc/computedPropertyNames11_ES6.json | 5 + .../tests/tsc/computedPropertyNames1_ES5.json | 1 + .../tests/tsc/computedPropertyNames1_ES6.json | 1 + ...utedPropertyNamesDeclarationEmit5_ES5.json | 1 + ...utedPropertyNamesDeclarationEmit5_ES6.json | 1 + .../jsDeclarationsFunctionLikeClasses2.json | 2 + ...IndexerConstrainsPropertyDeclarations.json | 1 + .../tests/tsc/objectLiteralErrorsES3.json | 2 + .../tsc/objectSpreadSetonlyAccessor.json | 2 + .../tests/tsc/parserAccessors4.json | 1 + .../tsc/parserComputedPropertyName17.json | 1 + .../tests/tsc/parserES3Accessors4.json | 1 + ...IndexerConstrainsPropertyDeclarations.json | 1 + .../tests/tsc/symbolDeclarationEmit10.json | 1 + .../tests/tsc/symbolProperty18.json | 1 + .../tests/tsc/thisTypeInAccessors.json | 86 ++++++++++ .../tsc/thisTypeInAccessorsNegative.json | 37 +++++ .../tests/tsc/thisTypeInObjectLiterals2.json | 1 + .../tests/tsc/typeGuardsObjectMethods.json | 1 + crates/swc_ecma_quote_macros/src/ast/prop.rs | 2 +- .../src/resolver/mod.rs | 1 + .../src/strip_type.rs | 154 +++++++++--------- .../src/analyzer/mod.rs | 1 + .../src/function/fn_env_hoister.rs | 2 + crates/swc_ecma_visit/src/lib.rs | 1 + 33 files changed, 275 insertions(+), 89 deletions(-) diff --git a/crates/swc_ecma_ast/src/prop.rs b/crates/swc_ecma_ast/src/prop.rs index 979027284f4..8dc8c0f36da 100644 --- a/crates/swc_ecma_ast/src/prop.rs +++ b/crates/swc_ecma_ast/src/prop.rs @@ -6,10 +6,9 @@ use crate::{ function::Function, ident::Ident, lit::{BigInt, Number, Str}, - pat::Pat, stmt::BlockStmt, typescript::TsTypeAnn, - Id, MemberProp, + Id, MemberProp, Pat, }; #[ast_node] @@ -76,6 +75,7 @@ pub struct GetterProp { pub struct SetterProp { pub span: Span, pub key: PropName, + pub this_param: Option, pub param: Box, #[cfg_attr(feature = "serde-impl", serde(default))] pub body: Option, diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 945e48371ea..ce2e45a6779 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -2218,7 +2218,15 @@ where formatting_space!(); punct!("("); + if let Some(this) = &node.this_param { + emit!(this); + punct!(","); + + formatting_space!(); + } + emit!(node.param); + punct!(")"); emit!(node.body); diff --git a/crates/swc_ecma_compat_common/src/macros.rs b/crates/swc_ecma_compat_common/src/macros.rs index fa95706d5ab..6a6b27c92e2 100644 --- a/crates/swc_ecma_compat_common/src/macros.rs +++ b/crates/swc_ecma_compat_common/src/macros.rs @@ -77,14 +77,14 @@ macro_rules! impl_visit_mut_fn { let (mut params, body) = self.visit_mut_fn_like( &mut vec![Param { span: DUMMY_SP, - decorators: Default::default(), + decorators: vec![], pat: *f.param.take(), }], &mut f.body.take().unwrap(), ); debug_assert!(params.len() == 1); - f.param = Box::new(params.pop().unwrap().pat); + f.param = Box::new(params.into_iter().next().unwrap().pat); f.body = Some(body); } diff --git a/crates/swc_ecma_compat_es2015/src/computed_props.rs b/crates/swc_ecma_compat_es2015/src/computed_props.rs index f64eb646e24..214e2144a44 100644 --- a/crates/swc_ecma_compat_es2015/src/computed_props.rs +++ b/crates/swc_ecma_compat_es2015/src/computed_props.rs @@ -172,6 +172,7 @@ impl VisitMut for ComputedProps { body, param, key, + .. }) => ( key, Box::new(Function { diff --git a/crates/swc_ecma_parser/src/parser/object.rs b/crates/swc_ecma_parser/src/parser/object.rs index 1b2fbc5af62..9fcd695ab28 100644 --- a/crates/swc_ecma_parser/src/parser/object.rs +++ b/crates/swc_ecma_parser/src/parser/object.rs @@ -1,6 +1,6 @@ //! Parser for object literal. -use swc_common::Spanned; +use swc_common::{Spanned, DUMMY_SP}; use super::*; use crate::parser::class_and_fn::is_not_this; @@ -355,21 +355,33 @@ impl ParseObject> for Parser { |Function { mut params, body, .. }| { - params.retain(|p| match &p.pat { - Pat::Ident(p) => p.sym != "this", - _ => true, - }); + let mut this = None; + if params.len() >= 2 { + this = Some(params.remove(0).pat); + } + + let param = Box::new( + params + .into_iter() + .next() + .map(|v| v.pat) + .unwrap_or_else(|| { + parser.emit_err( + key_span, + SyntaxError::SetterParam, + ); + + Pat::Invalid(Invalid { span: DUMMY_SP }) + }), + ); // debug_assert_eq!(params.len(), 1); PropOrSpread::Prop(Box::new(Prop::Setter(SetterProp { span: span!(parser, start), key, body, - param: Box::new( - params.into_iter().map(|p| p.pat).next().unwrap_or( - Pat::Invalid(Invalid { span: key_span }), - ), - ), + param, + this_param: this, }))) }, ) diff --git a/crates/swc_ecma_parser/tests/tsc/accessorWithES3.json b/crates/swc_ecma_parser/tests/tsc/accessorWithES3.json index 920731b5364..46ab643a628 100644 --- a/crates/swc_ecma_parser/tests/tsc/accessorWithES3.json +++ b/crates/swc_ecma_parser/tests/tsc/accessorWithES3.json @@ -334,6 +334,7 @@ "value": "b", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/accessorWithES5.json b/crates/swc_ecma_parser/tests/tsc/accessorWithES5.json index 7b688a3a0cd..937f346d334 100644 --- a/crates/swc_ecma_parser/tests/tsc/accessorWithES5.json +++ b/crates/swc_ecma_parser/tests/tsc/accessorWithES5.json @@ -334,6 +334,7 @@ "value": "b", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES5.json b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES5.json index 568cf9f4134..e477d7a5a1d 100644 --- a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES5.json +++ b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES5.json @@ -269,6 +269,7 @@ "optional": false } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -408,6 +409,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -518,6 +520,7 @@ "raw": "\"\"" } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -619,6 +622,7 @@ "optional": false } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -748,6 +752,7 @@ ] } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES6.json b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES6.json index 568cf9f4134..e477d7a5a1d 100644 --- a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES6.json +++ b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames11_ES6.json @@ -269,6 +269,7 @@ "optional": false } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -408,6 +409,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -518,6 +520,7 @@ "raw": "\"\"" } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -619,6 +622,7 @@ "optional": false } }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -748,6 +752,7 @@ ] } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES5.json b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES5.json index a6638ba7dfc..3b4e40f31f1 100644 --- a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES5.json +++ b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES5.json @@ -160,6 +160,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES6.json b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES6.json index a6638ba7dfc..3b4e40f31f1 100644 --- a/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES6.json +++ b/crates/swc_ecma_parser/tests/tsc/computedPropertyNames1_ES6.json @@ -160,6 +160,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES5.json b/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES5.json index 8d11066e664..2b5831b1b28 100644 --- a/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES5.json +++ b/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES5.json @@ -270,6 +270,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES6.json b/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES6.json index 8d11066e664..2b5831b1b28 100644 --- a/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES6.json +++ b/crates/swc_ecma_parser/tests/tsc/computedPropertyNamesDeclarationEmit5_ES6.json @@ -270,6 +270,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/jsDeclarationsFunctionLikeClasses2.json b/crates/swc_ecma_parser/tests/tsc/jsDeclarationsFunctionLikeClasses2.json index 7d5620a79f9..536f5987ee3 100644 --- a/crates/swc_ecma_parser/tests/tsc/jsDeclarationsFunctionLikeClasses2.json +++ b/crates/swc_ecma_parser/tests/tsc/jsDeclarationsFunctionLikeClasses2.json @@ -1728,6 +1728,7 @@ "value": "x", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -1935,6 +1936,7 @@ "value": "y", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/numericIndexerConstrainsPropertyDeclarations.json b/crates/swc_ecma_parser/tests/tsc/numericIndexerConstrainsPropertyDeclarations.json index 17b7a98d009..8d47440be00 100644 --- a/crates/swc_ecma_parser/tests/tsc/numericIndexerConstrainsPropertyDeclarations.json +++ b/crates/swc_ecma_parser/tests/tsc/numericIndexerConstrainsPropertyDeclarations.json @@ -2808,6 +2808,7 @@ "value": "X", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/objectLiteralErrorsES3.json b/crates/swc_ecma_parser/tests/tsc/objectLiteralErrorsES3.json index 4173d558c74..4eaedf004dc 100644 --- a/crates/swc_ecma_parser/tests/tsc/objectLiteralErrorsES3.json +++ b/crates/swc_ecma_parser/tests/tsc/objectLiteralErrorsES3.json @@ -148,6 +148,7 @@ "value": "a", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -275,6 +276,7 @@ "value": "a", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/objectSpreadSetonlyAccessor.json b/crates/swc_ecma_parser/tests/tsc/objectSpreadSetonlyAccessor.json index ed3b48a288f..d325f8b54a4 100644 --- a/crates/swc_ecma_parser/tests/tsc/objectSpreadSetonlyAccessor.json +++ b/crates/swc_ecma_parser/tests/tsc/objectSpreadSetonlyAccessor.json @@ -196,6 +196,7 @@ "value": "bar", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -391,6 +392,7 @@ "value": "foo", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/parserAccessors4.json b/crates/swc_ecma_parser/tests/tsc/parserAccessors4.json index 6026c1d72cf..f1d97dc53f8 100644 --- a/crates/swc_ecma_parser/tests/tsc/parserAccessors4.json +++ b/crates/swc_ecma_parser/tests/tsc/parserAccessors4.json @@ -59,6 +59,7 @@ "value": "Foo", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/parserComputedPropertyName17.json b/crates/swc_ecma_parser/tests/tsc/parserComputedPropertyName17.json index 2f8c72a8808..1871ae1c1e5 100644 --- a/crates/swc_ecma_parser/tests/tsc/parserComputedPropertyName17.json +++ b/crates/swc_ecma_parser/tests/tsc/parserComputedPropertyName17.json @@ -67,6 +67,7 @@ "optional": false } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/parserES3Accessors4.json b/crates/swc_ecma_parser/tests/tsc/parserES3Accessors4.json index 1450b828145..702c2011092 100644 --- a/crates/swc_ecma_parser/tests/tsc/parserES3Accessors4.json +++ b/crates/swc_ecma_parser/tests/tsc/parserES3Accessors4.json @@ -59,6 +59,7 @@ "value": "Foo", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/stringIndexerConstrainsPropertyDeclarations.json b/crates/swc_ecma_parser/tests/tsc/stringIndexerConstrainsPropertyDeclarations.json index ebab650b575..402440c61c9 100644 --- a/crates/swc_ecma_parser/tests/tsc/stringIndexerConstrainsPropertyDeclarations.json +++ b/crates/swc_ecma_parser/tests/tsc/stringIndexerConstrainsPropertyDeclarations.json @@ -2808,6 +2808,7 @@ "value": "X", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/symbolDeclarationEmit10.json b/crates/swc_ecma_parser/tests/tsc/symbolDeclarationEmit10.json index 833aafa14be..ebaae0948db 100644 --- a/crates/swc_ecma_parser/tests/tsc/symbolDeclarationEmit10.json +++ b/crates/swc_ecma_parser/tests/tsc/symbolDeclarationEmit10.json @@ -158,6 +158,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/symbolProperty18.json b/crates/swc_ecma_parser/tests/tsc/symbolProperty18.json index 73e86e6494b..39da0afcf8c 100644 --- a/crates/swc_ecma_parser/tests/tsc/symbolProperty18.json +++ b/crates/swc_ecma_parser/tests/tsc/symbolProperty18.json @@ -212,6 +212,7 @@ } } }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessors.json b/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessors.json index a03bd64baf8..8d3fb74b8d9 100644 --- a/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessors.json +++ b/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessors.json @@ -274,6 +274,43 @@ "value": "x", "optional": false }, + "thisParam": { + "type": "Identifier", + "span": { + "start": 204, + "end": 213, + "ctxt": 0 + }, + "value": "this", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 208, + "end": 213, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 210, + "end": 213, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 210, + "end": 213, + "ctxt": 0 + }, + "value": "Foo", + "optional": false + }, + "typeParams": null + } + } + }, "param": { "type": "Identifier", "span": { @@ -526,6 +563,7 @@ "value": "x", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { @@ -746,6 +784,43 @@ "value": "x", "optional": false }, + "thisParam": { + "type": "Identifier", + "span": { + "start": 441, + "end": 450, + "ctxt": 0 + }, + "value": "this", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 445, + "end": 450, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 447, + "end": 450, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 447, + "end": 450, + "ctxt": 0 + }, + "value": "Foo", + "optional": false + }, + "typeParams": null + } + } + }, "param": { "type": "Identifier", "span": { @@ -982,6 +1057,17 @@ "value": "x", "optional": false }, + "thisParam": { + "type": "Identifier", + "span": { + "start": 580, + "end": 584, + "ctxt": 0 + }, + "value": "this", + "optional": false, + "typeAnnotation": null + }, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessorsNegative.json b/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessorsNegative.json index 1a8333bed8f..6925571e7d5 100644 --- a/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessorsNegative.json +++ b/crates/swc_ecma_parser/tests/tsc/thisTypeInAccessorsNegative.json @@ -403,6 +403,43 @@ "value": "x", "optional": false }, + "thisParam": { + "type": "Identifier", + "span": { + "start": 267, + "end": 276, + "ctxt": 0 + }, + "value": "this", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 271, + "end": 276, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 273, + "end": 276, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 273, + "end": 276, + "ctxt": 0 + }, + "value": "Bar", + "optional": false + }, + "typeParams": null + } + } + }, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/thisTypeInObjectLiterals2.json b/crates/swc_ecma_parser/tests/tsc/thisTypeInObjectLiterals2.json index 4b00869f9ca..e272c85e220 100644 --- a/crates/swc_ecma_parser/tests/tsc/thisTypeInObjectLiterals2.json +++ b/crates/swc_ecma_parser/tests/tsc/thisTypeInObjectLiterals2.json @@ -400,6 +400,7 @@ "value": "e", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/typeGuardsObjectMethods.json b/crates/swc_ecma_parser/tests/tsc/typeGuardsObjectMethods.json index d2957762dfd..45b8885110f 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeGuardsObjectMethods.json +++ b/crates/swc_ecma_parser/tests/tsc/typeGuardsObjectMethods.json @@ -1050,6 +1050,7 @@ "value": "prop", "optional": false }, + "thisParam": null, "param": { "type": "Identifier", "span": { diff --git a/crates/swc_ecma_quote_macros/src/ast/prop.rs b/crates/swc_ecma_quote_macros/src/ast/prop.rs index 924896c1bd5..8d267ff252a 100644 --- a/crates/swc_ecma_quote_macros/src/ast/prop.rs +++ b/crates/swc_ecma_quote_macros/src/ast/prop.rs @@ -23,7 +23,7 @@ impl_struct!(KeyValueProp, [key, value]); impl_struct!(AssignProp, [key, value]); impl_struct!(GetterProp, [span, key, type_ann, body]); -impl_struct!(SetterProp, [span, key, param, body]); +impl_struct!(SetterProp, [span, key, param, this_param, body]); impl_struct!(MethodProp, [key, function]); diff --git a/crates/swc_ecma_transforms_base/src/resolver/mod.rs b/crates/swc_ecma_transforms_base/src/resolver/mod.rs index 99cc82944b8..2284b9c2ed1 100644 --- a/crates/swc_ecma_transforms_base/src/resolver/mod.rs +++ b/crates/swc_ecma_transforms_base/src/resolver/mod.rs @@ -1125,6 +1125,7 @@ impl<'a> VisitMut for Resolver<'a> { { self.with_child(ScopeKind::Fn, |child| { child.ident_type = IdentType::Binding; + n.this_param.visit_mut_with(child); n.param.visit_mut_with(child); n.body.visit_mut_with(child); }); diff --git a/crates/swc_ecma_transforms_typescript/src/strip_type.rs b/crates/swc_ecma_transforms_typescript/src/strip_type.rs index df6b38fee55..f04fd224cef 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip_type.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip_type.rs @@ -23,75 +23,16 @@ impl VisitMut for StripType { Box ); - fn visit_mut_module_items(&mut self, n: &mut Vec) { - n.retain(should_retain_module_item); - n.visit_mut_children_with(self); - } - - fn visit_mut_import_specifiers(&mut self, n: &mut Vec) { - n.retain(|s| !matches!(s, ImportSpecifier::Named(named) if named.is_type_only)); - } - - fn visit_mut_export_specifiers(&mut self, n: &mut Vec) { - n.retain(|s| match s { - ExportSpecifier::Named(ExportNamedSpecifier { is_type_only, .. }) => !is_type_only, - _ => true, - }) - } - - fn visit_mut_stmts(&mut self, n: &mut Vec) { - n.retain(should_retain_stmt); - n.visit_mut_children_with(self); - } - - // https://github.com/tc39/proposal-type-annotations#parameter-optionality - fn visit_mut_ident(&mut self, n: &mut Ident) { - n.optional = false; - } - fn visit_mut_array_pat(&mut self, n: &mut ArrayPat) { n.visit_mut_children_with(self); n.optional = false; } - fn visit_mut_object_pat(&mut self, pat: &mut ObjectPat) { - pat.visit_mut_children_with(self); - pat.optional = false; - } - - fn visit_mut_expr(&mut self, n: &mut Expr) { - // https://github.com/tc39/proposal-type-annotations#type-assertions - // https://github.com/tc39/proposal-type-annotations#non-nullable-assertions - while let Expr::TsAs(TsAsExpr { expr, .. }) - | Expr::TsNonNull(TsNonNullExpr { expr, .. }) - | Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }) - | Expr::TsConstAssertion(TsConstAssertion { expr, .. }) - | Expr::TsInstantiation(TsInstantiation { expr, .. }) - | Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) = n - { - *n = *expr.take(); - } - - n.visit_mut_children_with(self); - } - - // https://github.com/tc39/proposal-type-annotations#this-parameters - fn visit_mut_params(&mut self, n: &mut Vec) { - if n.first() - .filter(|param| { - matches!( - ¶m.pat, - Pat::Ident(BindingIdent { - id: Ident { sym, .. }, - .. - }) if &**sym == "this" - ) - }) - .is_some() - { - n.drain(0..1); - } - + fn visit_mut_auto_accessor(&mut self, n: &mut AutoAccessor) { + n.type_ann = None; + n.accessibility = None; + n.definite = false; + n.is_override = false; n.visit_mut_children_with(self); } @@ -130,11 +71,6 @@ impl VisitMut for StripType { n.visit_mut_children_with(self); } - fn visit_mut_constructor(&mut self, n: &mut Constructor) { - n.accessibility = None; - n.visit_mut_children_with(self); - } - fn visit_mut_class_method(&mut self, n: &mut ClassMethod) { n.accessibility = None; n.is_override = false; @@ -153,6 +89,73 @@ impl VisitMut for StripType { prop.visit_mut_children_with(self); } + fn visit_mut_constructor(&mut self, n: &mut Constructor) { + n.accessibility = None; + n.visit_mut_children_with(self); + } + + fn visit_mut_export_specifiers(&mut self, n: &mut Vec) { + n.retain(|s| match s { + ExportSpecifier::Named(ExportNamedSpecifier { is_type_only, .. }) => !is_type_only, + _ => true, + }) + } + + fn visit_mut_expr(&mut self, n: &mut Expr) { + // https://github.com/tc39/proposal-type-annotations#type-assertions + // https://github.com/tc39/proposal-type-annotations#non-nullable-assertions + while let Expr::TsAs(TsAsExpr { expr, .. }) + | Expr::TsNonNull(TsNonNullExpr { expr, .. }) + | Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }) + | Expr::TsConstAssertion(TsConstAssertion { expr, .. }) + | Expr::TsInstantiation(TsInstantiation { expr, .. }) + | Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) = n + { + *n = *expr.take(); + } + + n.visit_mut_children_with(self); + } + + // https://github.com/tc39/proposal-type-annotations#parameter-optionality + fn visit_mut_ident(&mut self, n: &mut Ident) { + n.optional = false; + } + + fn visit_mut_import_specifiers(&mut self, n: &mut Vec) { + n.retain(|s| !matches!(s, ImportSpecifier::Named(named) if named.is_type_only)); + } + + fn visit_mut_module_items(&mut self, n: &mut Vec) { + n.retain(should_retain_module_item); + n.visit_mut_children_with(self); + } + + fn visit_mut_object_pat(&mut self, pat: &mut ObjectPat) { + pat.visit_mut_children_with(self); + pat.optional = false; + } + + // https://github.com/tc39/proposal-type-annotations#this-parameters + fn visit_mut_params(&mut self, n: &mut Vec) { + if n.first() + .filter(|param| { + matches!( + ¶m.pat, + Pat::Ident(BindingIdent { + id: Ident { sym, .. }, + .. + }) if &**sym == "this" + ) + }) + .is_some() + { + n.drain(0..1); + } + + n.visit_mut_children_with(self); + } + fn visit_mut_private_prop(&mut self, prop: &mut PrivateProp) { prop.readonly = false; prop.is_override = false; @@ -162,11 +165,14 @@ impl VisitMut for StripType { prop.visit_mut_children_with(self); } - fn visit_mut_auto_accessor(&mut self, n: &mut AutoAccessor) { - n.type_ann = None; - n.accessibility = None; - n.definite = false; - n.is_override = false; + fn visit_mut_setter_prop(&mut self, n: &mut SetterProp) { + n.this_param = None; + + n.visit_mut_children_with(self); + } + + fn visit_mut_stmts(&mut self, n: &mut Vec) { + n.retain(should_retain_stmt); n.visit_mut_children_with(self); } diff --git a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs index c6d7c6db80e..674d2ab4bca 100644 --- a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs +++ b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs @@ -1379,6 +1379,7 @@ fn for_each_id_ref_in_expr(e: &Expr, op: &mut impl FnMut(&Ident)) { } Prop::Setter(p) => { for_each_id_ref_in_prop_name(&p.key, op); + for_each_id_ref_in_pat(&p.param, op); } Prop::Method(p) => { diff --git a/crates/swc_ecma_utils/src/function/fn_env_hoister.rs b/crates/swc_ecma_utils/src/function/fn_env_hoister.rs index fd586bb6bdd..1509a510c66 100644 --- a/crates/swc_ecma_utils/src/function/fn_env_hoister.rs +++ b/crates/swc_ecma_utils/src/function/fn_env_hoister.rs @@ -665,6 +665,7 @@ fn extend_super( Prop::Setter(SetterProp { span: DUMMY_SP, key: PropName::Ident(quote_ident!("_")), + this_param: None, param: value.clone().into(), body: Some(BlockStmt { span: DUMMY_SP, @@ -721,6 +722,7 @@ fn extend_super( Prop::Setter(SetterProp { span: DUMMY_SP, key: PropName::Ident(quote_ident!("_")), + this_param: None, param: value.clone().into(), body: Some(BlockStmt { span: DUMMY_SP, diff --git a/crates/swc_ecma_visit/src/lib.rs b/crates/swc_ecma_visit/src/lib.rs index 01213861e4b..fff1f07dde1 100644 --- a/crates/swc_ecma_visit/src/lib.rs +++ b/crates/swc_ecma_visit/src/lib.rs @@ -1290,6 +1290,7 @@ define!({ pub struct SetterProp { pub span: Span, pub key: PropName, + pub this_param: Option, pub param: Box, pub body: Option, }