Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformer): drop this parameter from typescript functions #1019

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that we need to place a lot of AST visitor functions?
TypeScript AST are in many syntactic positions, and they produce a lot of extra statements.

However, when these AST visitor functions need to cooperate with each other in context, this will become very complicated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, our codegen only prints javascript now so we don't need to remove nodes in the transformer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a clever method.
So, I supposed there should be some methods, such as getInnerExpr, to get the JavaScript context AST that is wrapped by the TypeScript AST. Right?

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);
}
}
}
8 changes: 8 additions & 0 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use oxc_ast::ast::*;
use oxc_ast::AstBuilder;

use std::rc::Rc;
Expand All @@ -15,4 +16,11 @@ impl<'a> TypeScript<'a> {
pub fn new(_ast: Rc<AstBuilder<'a>>) -> 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!(&param.pattern.kind, BindingPatternKind::BindingIdentifier(ident) if ident.name =="this")) {
params.items.remove(0);
}
}
}
6 changes: 2 additions & 4 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Passed: 160/1087
Passed: 162/1087

# All Passed:
* babel-plugin-transform-numeric-separator
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down