mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(transformer/typescript): improve function parameters name (#2079)
This commit is contained in:
parent
7711f7abaf
commit
b5b2ef34af
3 changed files with 21 additions and 16 deletions
|
|
@ -6,7 +6,7 @@ use oxc_syntax::{
|
|||
operator::{AssignmentOperator, BinaryOperator, LogicalOperator},
|
||||
NumberBase,
|
||||
};
|
||||
use rustc_hash::FxHashSet;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use std::{mem, rc::Rc};
|
||||
|
||||
mod options;
|
||||
|
|
@ -26,6 +26,7 @@ pub struct TypeScript<'a> {
|
|||
verbatim_module_syntax: bool,
|
||||
export_name_set: FxHashSet<Atom>,
|
||||
options: TypescriptOptions,
|
||||
namespace_arg_names: FxHashMap<Atom, usize>,
|
||||
}
|
||||
|
||||
impl<'a> TypeScript<'a> {
|
||||
|
|
@ -41,6 +42,7 @@ impl<'a> TypeScript<'a> {
|
|||
verbatim_module_syntax,
|
||||
export_name_set: FxHashSet::default(),
|
||||
options: options.typescript.clone().unwrap_or_default(),
|
||||
namespace_arg_names: FxHashMap::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -669,6 +671,12 @@ impl<'a> TypeScript<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn get_namespace_arg_name(&mut self, name: &Atom) -> Atom {
|
||||
let count = self.namespace_arg_names.entry(name.clone()).or_insert(0);
|
||||
*count += 1;
|
||||
format!("_{name}{}", if *count > 1 { count.to_string() } else { String::new() }).into()
|
||||
}
|
||||
|
||||
/// ```TypeScript
|
||||
/// // transform ts module block
|
||||
/// namespace Foo {
|
||||
|
|
@ -696,16 +704,14 @@ impl<'a> TypeScript<'a> {
|
|||
|
||||
let callee = {
|
||||
let body = self.ast.function_body(SPAN, self.ast.new_vec(), body_statements);
|
||||
let arg_name = self.get_namespace_arg_name(name);
|
||||
let params = self.ast.formal_parameters(
|
||||
SPAN,
|
||||
FormalParameterKind::FormalParameter,
|
||||
self.ast.new_vec_single(self.ast.formal_parameter(
|
||||
SPAN,
|
||||
self.ast.binding_pattern(
|
||||
self.ast.binding_pattern_identifier(BindingIdentifier::new(
|
||||
SPAN,
|
||||
format!("_{}", name.clone()).into(),
|
||||
)),
|
||||
self.ast.binding_pattern_identifier(BindingIdentifier::new(SPAN, arg_name)),
|
||||
None,
|
||||
false,
|
||||
),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Passed: 322/1179
|
||||
Passed: 323/1179
|
||||
|
||||
# All Passed:
|
||||
* babel-plugin-transform-numeric-separator
|
||||
|
|
@ -832,7 +832,7 @@ Passed: 322/1179
|
|||
* general/function-duplicate-name/input.js
|
||||
* general/object/input.js
|
||||
|
||||
# babel-plugin-transform-typescript (91/158)
|
||||
# babel-plugin-transform-typescript (92/158)
|
||||
* class/abstract-class-decorated/input.ts
|
||||
* class/abstract-class-decorated-method/input.ts
|
||||
* class/abstract-class-decorated-parameter/input.ts
|
||||
|
|
@ -869,7 +869,6 @@ Passed: 322/1179
|
|||
* namespace/clobber-enum/input.ts
|
||||
* namespace/clobber-export/input.ts
|
||||
* namespace/clobber-import/input.ts
|
||||
* namespace/contentious-names/input.ts
|
||||
* namespace/empty-removed/input.ts
|
||||
* namespace/export-type-only/input.ts
|
||||
* namespace/module-nested/input.ts
|
||||
|
|
|
|||
|
|
@ -631,9 +631,9 @@ let M6;
|
|||
})(A || (A = {}));
|
||||
})(M6 || (M6 = {}));
|
||||
let M6;
|
||||
(function(_M6) {
|
||||
(function(_M62) {
|
||||
export let A;
|
||||
(function(_A) {
|
||||
(function(_A2) {
|
||||
var Color = (Color => {
|
||||
const Yellow = 1;
|
||||
Color[Color['Yellow'] = Yellow] = 'Yellow';
|
||||
|
|
@ -667,7 +667,7 @@ export var E3 = (E3 => {
|
|||
})(E3 || {});
|
||||
})(M || (M = {}));
|
||||
let M;
|
||||
(function(_M) {
|
||||
(function(_M2) {
|
||||
var E1 = (E1 => {
|
||||
const B = 'foo'.length;
|
||||
E1[E1['B'] = B] = 'B';
|
||||
|
|
@ -685,7 +685,7 @@ let M;
|
|||
})(E3 || {});
|
||||
})(M || (M = {}));
|
||||
let M;
|
||||
(function(_M) {
|
||||
(function(_M3) {
|
||||
var E1 = (E1 => {
|
||||
const C = 0;
|
||||
E1[E1['C'] = C] = 'C';
|
||||
|
|
@ -711,7 +711,7 @@ let M1;
|
|||
})(E1 || {});
|
||||
})(M1 || (M1 = {}));
|
||||
let M1;
|
||||
(function(_M1) {
|
||||
(function(_M12) {
|
||||
var E1 = (E1 => {
|
||||
const B = 0;
|
||||
E1[E1['B'] = B] = 'B';
|
||||
|
|
@ -719,7 +719,7 @@ let M1;
|
|||
})(E1 || {});
|
||||
})(M1 || (M1 = {}));
|
||||
let M1;
|
||||
(function(_M1) {
|
||||
(function(_M13) {
|
||||
var E1 = (E1 => {
|
||||
const C = 0;
|
||||
E1[E1['C'] = C] = 'C';
|
||||
|
|
@ -735,7 +735,7 @@ let M2;
|
|||
})(E1 || {});
|
||||
})(M2 || (M2 = {}));
|
||||
let M2;
|
||||
(function(_M2) {
|
||||
(function(_M22) {
|
||||
var E1 = (E1 => {
|
||||
const B = 0;
|
||||
E1[E1['B'] = B] = 'B';
|
||||
|
|
@ -743,7 +743,7 @@ let M2;
|
|||
})(E1 || {});
|
||||
})(M2 || (M2 = {}));
|
||||
let M2;
|
||||
(function(_M2) {
|
||||
(function(_M23) {
|
||||
var E1 = (E1 => {
|
||||
const C = 0;
|
||||
E1[E1['C'] = C] = 'C';
|
||||
|
|
|
|||
Loading…
Reference in a new issue