From d31a667e23333c1453a0157562263bb59aba4885 Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 20 Oct 2023 23:15:06 +0800 Subject: [PATCH] feat(transformer): drop `this` parameter from typescript functions (#1019) --- crates/oxc_transformer/src/lib.rs | 10 ++++++++++ crates/oxc_transformer/src/typescript/mod.rs | 8 ++++++++ tasks/transform_conformance/babel.snap.md | 6 ++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 9619e641f5500..ddd2885f9e1d5 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -144,4 +144,14 @@ impl<'a> VisitMut<'a> for Transformer<'a> { self.visit_class_element(class_element); }); } + + fn visit_formal_parameters(&mut self, params: &mut FormalParameters<'a>) { + self.typescript.as_mut().map(|t| t.transform_formal_parameters(params)); + for param in params.items.iter_mut() { + self.visit_formal_parameter(param); + } + if let Some(rest) = &mut params.rest { + self.visit_rest_element(rest); + } + } } diff --git a/crates/oxc_transformer/src/typescript/mod.rs b/crates/oxc_transformer/src/typescript/mod.rs index 17a6ce5b5b34a..2d9beafd2631c 100644 --- a/crates/oxc_transformer/src/typescript/mod.rs +++ b/crates/oxc_transformer/src/typescript/mod.rs @@ -1,3 +1,4 @@ +use oxc_ast::ast::*; use oxc_ast::AstBuilder; use std::rc::Rc; @@ -15,4 +16,11 @@ impl<'a> TypeScript<'a> { pub fn new(_ast: Rc>) -> Self { Self { _ast } } + + #[allow(clippy::unused_self)] + pub fn transform_formal_parameters(&self, params: &mut FormalParameters<'a>) { + if params.items.get(0).is_some_and(|param| matches!(¶m.pattern.kind, BindingPatternKind::BindingIdentifier(ident) if ident.name =="this")) { + params.items.remove(0); + } + } } diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index 02c4b89806f7e..8cef9337d37f9 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,4 +1,4 @@ -Passed: 160/1087 +Passed: 162/1087 # All Passed: * babel-plugin-transform-numeric-separator @@ -714,7 +714,7 @@ Passed: 160/1087 * unicode-regex/negated-set/input.js * unicode-regex/slash/input.js -# babel-plugin-transform-typescript (71/181) +# babel-plugin-transform-typescript (73/181) * class/abstract-class-decorated/input.ts * class/abstract-class-decorated-method/input.ts * class/abstract-class-decorated-parameter/input.ts @@ -749,7 +749,6 @@ Passed: 160/1087 * enum/string-value-template/input.ts * enum/string-values-computed/input.ts * enum/ts5.0-const-foldable/input.ts -* exports/declare-shadowed/input.ts * exports/declared-types/input.ts * exports/export-const-enums/input.ts * exports/export-type/input.ts @@ -763,7 +762,6 @@ Passed: 160/1087 * exports/type-only-export-specifier-2/input.ts * exports/type-only-export-specifier-3/input.ts * function/overloads-exports/input.mjs -* function/this-parameter/input.ts * imports/elide-injected/input.ts * imports/elide-preact/input.ts * imports/elide-react/input.ts