refactor(es/transforms): Use recommended ast apis (#3735)

This commit is contained in:
Donny/강동윤 2022-02-25 15:50:19 +09:00 committed by GitHub
parent 65637e7419
commit fd223793e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 55 additions and 158 deletions

View File

@ -233,6 +233,15 @@ impl Str {
}
}
/// A boolean literal.
///
///
/// # Creation
///
/// If you are creating a boolean literal with a dummy span, please use
/// `true.into()` or `false.into()`, instead of creating this struct directly.
///
/// All of `Box<Expr>`, `Expr`, `Lit`, `Bool` implements `From<bool>`.
#[ast_node("BooleanLiteral")]
#[derive(Copy, Eq, Hash, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
@ -289,6 +298,17 @@ impl<'a> arbitrary::Arbitrary<'a> for Regex {
}
}
/// A numeric literal.
///
///
/// # Creation
///
/// If you are creating a numeric literal with a dummy span, please use
/// `literal.into()`, instead of creating this struct directly.
///
/// All of `Box<Expr>`, `Expr`, `Lit`, `Number` implements `From<64>` and
/// `From<usize>`.
#[ast_node("NumericLiteral")]
#[derive(Copy, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]

View File

@ -25,10 +25,7 @@ pub(super) struct Loc {
impl Loc {
#[allow(clippy::wrong_self_convention)]
pub fn to_stmt_index(&self) -> Expr {
Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: self.stmt_index as _,
}))
self.stmt_index.into()
}
/// Creates an invalid expression pointing `self`
@ -841,10 +838,7 @@ impl CaseHandler<'_> {
for (i, stmt) in stmts.iter_mut().enumerate() {
let case = SwitchCase {
span: DUMMY_SP,
test: Some(Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: i as _,
})))),
test: Some(i.into()),
cons: vec![],
};
@ -1595,10 +1589,7 @@ impl VisitMut for InvalidToLit<'_> {
if let Some(Loc { stmt_index, .. }) =
self.map.iter().find(|loc| loc.id == span.lo.0)
{
*e = Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: (*stmt_index) as _,
}));
*e = (*stmt_index).into();
}
}
}

View File

@ -376,21 +376,13 @@ impl Regenerator {
// Intentionally fall through to the "end" case...
cases.push(SwitchCase {
span: DUMMY_SP,
test: Some(Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: handler.final_loc() as _,
})))),
test: Some(handler.final_loc().into()),
// fallthrough
cons: vec![],
});
cases.push(SwitchCase {
span: DUMMY_SP,
test: Some(Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: "end".into(),
has_escape: false,
kind: Default::default(),
})))),
test: Some("end".into()),
cons: vec![ReturnStmt {
span: DUMMY_SP,
// _ctx.stop()
@ -406,10 +398,7 @@ impl Regenerator {
let stmts = vec![Stmt::While(WhileStmt {
span: DUMMY_SP,
test: Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: 1.0,
}))),
test: 1.0.into(),
body: Box::new(
SwitchStmt {
span: DUMMY_SP,

View File

@ -65,8 +65,7 @@ impl VisitMut for TypeOfSymbol {
{
match &**arg {
Expr::Ident(..) => {
let undefined_str = quote_str!("undefined");
let undefined_str = Box::new(Expr::Lit(Lit::Str(undefined_str)));
let undefined_str: Box<Expr> = quote_str!("undefined").into();
let test = Box::new(Expr::Bin(BinExpr {
span: DUMMY_SP,

View File

@ -600,10 +600,7 @@ fn handle_await_for(stmt: &mut Stmt, is_async_generator: bool) {
span: DUMMY_SP,
op: op!("="),
left: PatOrExpr::Pat(iterator_abrupt_completion.clone().into()),
right: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: false,
}))),
right: false.into(),
}))),
body: Box::new(Stmt::Block(for_loop_body)),
});
@ -622,10 +619,7 @@ fn handle_await_for(stmt: &mut Stmt, is_async_generator: bool) {
span: DUMMY_SP,
op: op!("="),
left: PatOrExpr::Pat(did_iteration_error.clone().into()),
right: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
right: true.into(),
})),
});
// _iteratorError = err;
@ -695,7 +689,7 @@ fn handle_await_for(stmt: &mut Stmt, is_async_generator: bool) {
span: DUMMY_SP,
op: op!("!="),
left: Box::new(iterator.make_member(quote_ident!("return"))),
right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
right: Null { span: DUMMY_SP }.into(),
})),
})),
cons: Box::new(Stmt::Block(BlockStmt {
@ -741,20 +735,14 @@ fn handle_await_for(stmt: &mut Stmt, is_async_generator: bool) {
VarDeclarator {
span: DUMMY_SP,
name: iterator_abrupt_completion.into(),
init: Some(Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: false,
})))),
init: Some(false.into()),
definite: false,
},
// var _didIteratorError = false;
VarDeclarator {
span: DUMMY_SP,
name: did_iteration_error.into(),
init: Some(Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: false,
})))),
init: Some(false.into()),
definite: false,
},
// var _iteratorError;

View File

@ -718,10 +718,7 @@ impl ClassProperties {
props: vec![
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("writable")),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
value: true.into(),
}))),
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("value")),
@ -859,10 +856,7 @@ impl ClassProperties {
PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!("writable")),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
value: true,
span: DUMMY_SP,
}))),
value: true.into(),
},
))),
// value: value,

View File

@ -502,10 +502,7 @@ where
Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(Ident::new(export, DUMMY_SP)),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
value: true.into(),
},
))))
})

View File

@ -325,12 +325,7 @@ where
key: PropName::Ident(Ident::new(
export, DUMMY_SP,
)),
value: Box::new(Expr::Lit(Lit::Bool(
Bool {
span: DUMMY_SP,
value: true,
},
))),
value: true.into(),
}),
)))
})

View File

@ -511,10 +511,7 @@ where
Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(Ident::new(export, DUMMY_SP)),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
value: true.into(),
},
))))
})

View File

@ -861,13 +861,7 @@ pub(super) fn define_es_module(exports: Ident) -> Stmt {
span: DUMMY_SP,
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("value")),
value: Box::new(
Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
})
.into(),
),
value: true.into(),
})))],
}
.as_arg(),
@ -932,13 +926,7 @@ pub(super) fn make_descriptor(get_expr: Box<Expr>) -> ObjectLit {
props: vec![
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("enumerable")),
value: Box::new(
Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
})
.into(),
),
value: true.into(),
}))),
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("get")),

View File

@ -1111,13 +1111,7 @@ impl VisitMut for SimplifyExpr {
Some(Expr::Lit(..) | Expr::Ident(..)) => {}
_ => {
tracing::debug!("Injecting `0` to preserve `this = undefined`");
seq.exprs.insert(
0,
Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: 0.0,
}))),
);
seq.exprs.insert(0, 0.0.into());
}
}
@ -1339,10 +1333,7 @@ impl VisitMut for SimplifyExpr {
match *expr {
Expr::Lit(Lit::Num(n)) if self.in_callee && n.value == 0.0 => {
if exprs.is_empty() {
exprs.push(Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: 0.0,
}))));
exprs.push(0.0.into());
tracing::trace!("expr_simplifier: Preserving first zero");
}
@ -1352,10 +1343,7 @@ impl VisitMut for SimplifyExpr {
if exprs.is_empty() {
self.changed = true;
exprs.push(Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: 0.0,
}))));
exprs.push(0.0.into());
tracing::debug!("expr_simplifier: Injected first zero");
}

View File

@ -98,11 +98,7 @@ impl ParamMetadata {
} else {
quote_ident!("key").as_arg()
},
Lit::Num(Number {
span: DUMMY_SP,
value: param_index as _,
})
.as_arg(),
param_index.as_arg(),
],
type_args: Default::default(),
}))),

View File

@ -531,25 +531,16 @@ impl Legacy {
// configurable: true,
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: quote_ident!("configurable").into(),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
value: true.into(),
}))), // enumerable: true,
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: quote_ident!("enumerable").into(),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
value: true.into(),
}))),
// writable: true,
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: quote_ident!("writable").into(),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
value: true.into(),
}))),
// initializer: function () {
// return 2;

View File

@ -357,10 +357,7 @@ impl Decorators {
.chain(if method.is_static {
Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("static")),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
value: true,
span: DUMMY_SP,
}))),
value: true.into(),
}))))
} else {
None
@ -471,10 +468,7 @@ impl Decorators {
Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!("static")),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
value: true,
span: DUMMY_SP,
}))),
value: true.into(),
},
))))
} else {

View File

@ -406,13 +406,7 @@ where
let args = if self.development {
args.chain(once(undefined(DUMMY_SP).as_arg()))
.chain(once(
Lit::Bool(Bool {
span: DUMMY_SP,
value: use_jsxs,
})
.as_arg(),
))
.chain(once(use_jsxs.as_arg()))
.collect()
} else {
args.collect()
@ -552,10 +546,7 @@ where
let value = match attr.value {
Some(v) => jsx_attr_value_to_expr(v)
.expect("empty expression container?"),
None => Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
None => true.into(),
};
// TODO: Check if `i` is a valid identifier.
@ -593,10 +584,7 @@ where
let value = match attr.value {
Some(v) => jsx_attr_value_to_expr(v)
.expect("empty expression container?"),
None => Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
None => true.into(),
};
let key = Str {
@ -678,13 +666,7 @@ where
None => undefined(DUMMY_SP).as_arg(),
};
args.chain(once(key))
.chain(once(
Lit::Bool(Bool {
span: DUMMY_SP,
value: use_jsxs,
})
.as_arg(),
))
.chain(once(use_jsxs.as_arg()))
.chain(once(source_props))
.chain(once(self_props))
.collect()

View File

@ -58,17 +58,11 @@ impl VisitMut for JsxSrc {
}))),
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("lineNumber")),
value: Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: loc.line as _,
}))),
value: loc.line.into(),
}))),
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("columnNumber")),
value: Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: (loc.col.0 + 1) as _,
}))),
value: (loc.col.0 + 1).into(),
}))),
],
}

View File

@ -144,13 +144,7 @@ impl<'a> HookRegister<'a> {
}
if should_reset || !custom_hook_in_scope.is_empty() {
args.push(
Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: should_reset,
}))
.as_arg(),
);
args.push(should_reset.as_arg());
}
if !custom_hook_in_scope.is_empty() {