Skip to content

Commit ad00720

Browse files
stragerBoshen
authored andcommitted
refactor: avoid unstable let_chains
The let_chains Rust feature is unstable, preventing Oxc from using a stable Rust compiler. Refactor the code to avoid let_chains.
1 parent 2203d08 commit ad00720

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+680
-605
lines changed

crates/oxc_ast/src/ast/js.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ impl<'a> Expression<'a> {
112112
matches!(self, Self::StringLiteral(_) | Self::TemplateLiteral(_))
113113
}
114114

115+
pub fn is_specific_string_literal(&self, string: &str) -> bool {
116+
match self {
117+
Self::StringLiteral(s) => s.value == string,
118+
_ => false,
119+
}
120+
}
121+
115122
/// Determines whether the given expr is a `null` literal
116123
pub fn is_null(&self) -> bool {
117124
matches!(self, Expression::NullLiteral(_))
@@ -129,13 +136,12 @@ impl<'a> Expression<'a> {
129136

130137
/// Determines whether the given expr is a `void 0`
131138
pub fn is_void_0(&self) -> bool {
132-
if let Self::UnaryExpression(expr) = self
133-
&& expr.operator == UnaryOperator::Void
134-
&& let Self::NumberLiteral(lit) = &expr.argument
135-
&& lit.value == 0.0 {
136-
return true
139+
match self {
140+
Self::UnaryExpression(expr) if expr.operator == UnaryOperator::Void => {
141+
matches!(&expr.argument, Self::NumberLiteral(lit) if lit.value == 0.0)
142+
}
143+
_ => false,
137144
}
138-
false
139145
}
140146

141147
/// Determines whether the given expr is a `0`
@@ -195,6 +201,10 @@ impl<'a> Expression<'a> {
195201
}
196202
}
197203

204+
pub fn is_identifier_reference(&self) -> bool {
205+
matches!(self, Expression::Identifier(_))
206+
}
207+
198208
pub fn get_identifier_reference(&self) -> Option<&IdentifierReference> {
199209
match self.get_inner_expression() {
200210
Expression::Identifier(ident) => Some(ident),
@@ -355,9 +365,20 @@ impl<'a> PropertyKey<'a> {
355365
}
356366
}
357367

368+
pub fn is_specific_static_name(&self, name: &str) -> bool {
369+
self.static_name().is_some_and(|n| n == name)
370+
}
371+
358372
pub fn is_private_identifier(&self) -> bool {
359373
matches!(self, Self::PrivateIdentifier(_))
360374
}
375+
376+
pub fn is_specific_id(&self, name: &str) -> bool {
377+
match self {
378+
PropertyKey::Identifier(ident) => ident.name == name,
379+
_ => false,
380+
}
381+
}
361382
}
362383

363384
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -568,13 +589,12 @@ impl<'a> CallExpression<'a> {
568589
}
569590

570591
pub fn common_js_require(&self) -> Option<&StringLiteral> {
571-
if let Expression::Identifier(ident) = &self.callee
572-
&& ident.name =="require"
573-
&& self.arguments.len() == 1
574-
&& let Argument::Expression(Expression::StringLiteral(str_literal)) = &self.arguments[0] {
575-
Some(str_literal)
576-
} else {
577-
None
592+
if !(self.callee.is_specific_id("require") && self.arguments.len() == 1) {
593+
return None;
594+
}
595+
match &self.arguments[0] {
596+
Argument::Expression(Expression::StringLiteral(str_literal)) => Some(str_literal),
597+
_ => None,
578598
}
579599
}
580600
}

crates/oxc_ast/src/ast_kind.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ impl<'a> AstKind<'a> {
223223
pub fn is_jsx(self) -> bool {
224224
matches!(self, Self::JSXElement(_) | Self::JSXOpeningElement(_) | Self::JSXElementName(_))
225225
}
226+
227+
pub fn is_specific_id_reference(&self, name: &str) -> bool {
228+
match self {
229+
Self::IdentifierReference(ident) => ident.name == name,
230+
_ => false,
231+
}
232+
}
226233
}
227234

228235
impl<'a> GetSpan for AstKind<'a> {

crates/oxc_ast/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
//! ## Cargo Features
88
//! * `"serde"` enables support for serde serialization
99
10-
#![feature(let_chains)]
11-
1210
#[cfg(feature = "serde")]
1311
mod serialize;
1412

crates/oxc_formatter/src/gen.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ impl<'a> Gen for ExpressionStatement<'a> {
7272
fn gen(&self, p: &mut Formatter) {
7373
p.print_indent();
7474
self.expression.gen(p);
75-
if let Expression::Identifier(ident) = &self.expression
76-
&& ident.name == "let" {
75+
if self.expression.is_specific_id("let") {
7776
p.print_semicolon();
7877
} else {
7978
p.print_semicolon_after_statement();

crates/oxc_formatter/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! AST Formatter with whitespace minification
22
//! code adapted from [esbuild](https://github.com/evanw/esbuild/blob/main/internal/js_formatter/js_formatter.go)
33
4-
#![feature(let_chains)]
5-
64
mod gen;
75

86
#[allow(clippy::wildcard_imports)]

crates/oxc_hir/src/hir.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ impl<'a> Expression<'a> {
112112
matches!(self, Self::StringLiteral(_) | Self::TemplateLiteral(_))
113113
}
114114

115+
pub fn is_specific_string_literal(&self, string: &str) -> bool {
116+
match self {
117+
Self::StringLiteral(s) => s.value == string,
118+
_ => false,
119+
}
120+
}
121+
115122
/// Determines whether the given expr is a `null` literal
116123
pub fn is_null(&self) -> bool {
117124
matches!(self, Expression::NullLiteral(_))
@@ -129,13 +136,12 @@ impl<'a> Expression<'a> {
129136

130137
/// Determines whether the given expr is a `void 0`
131138
pub fn is_void_0(&self) -> bool {
132-
if let Self::UnaryExpression(expr) = self
133-
&& expr.operator == UnaryOperator::Void
134-
&& let Self::NumberLiteral(lit) = &expr.argument
135-
&& lit.value == 0.0 {
136-
return true
139+
match self {
140+
Self::UnaryExpression(expr) if expr.operator == UnaryOperator::Void => {
141+
matches!(&expr.argument, Self::NumberLiteral(lit) if lit.value == 0.0)
142+
}
143+
_ => false,
137144
}
138-
false
139145
}
140146

141147
/// Determines whether the given expr is a `0`
@@ -725,13 +731,12 @@ impl<'a> CallExpression<'a> {
725731
}
726732

727733
pub fn common_js_require(&self) -> Option<&StringLiteral> {
728-
if let Expression::Identifier(ident) = &self.callee
729-
&& ident.name =="require"
730-
&& self.arguments.len() == 1
731-
&& let Argument::Expression(Expression::StringLiteral(str_literal)) = &self.arguments[0] {
732-
Some(str_literal)
733-
} else {
734-
None
734+
if !(self.callee.is_specific_id("require") && self.arguments.len() == 1) {
735+
return None;
736+
}
737+
match &self.arguments[0] {
738+
Argument::Expression(Expression::StringLiteral(str_literal)) => Some(str_literal),
739+
_ => None,
735740
}
736741
}
737742
}

crates/oxc_hir/src/hir_util.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ pub fn get_boolean_value(expr: &Expression) -> Option<bool> {
410410
Expression::StringLiteral(string_literal) => Some(!string_literal.value.is_empty()),
411411
Expression::TemplateLiteral(template_literal) => {
412412
// only for ``
413-
if let Some(quasi) = template_literal.quasis.get(0) && quasi.tail {
414-
Some(quasi.value.cooked.as_ref().map_or(false, |cooked| !cooked.is_empty()))
415-
} else {
416-
None
417-
}
413+
template_literal
414+
.quasis
415+
.get(0)
416+
.filter(|quasi| quasi.tail)
417+
.and_then(|quasi| quasi.value.cooked.as_ref())
418+
.map(|cooked| !cooked.is_empty())
418419
}
419420
Expression::Identifier(ident) => {
420421
if expr.is_undefined() || ident.name == "NaN" {
@@ -502,11 +503,12 @@ fn get_string_value<'a>(expr: &'a Expression) -> Option<Cow<'a, str>> {
502503
Expression::TemplateLiteral(template_literal) => {
503504
// TODO: I don't know how to iterate children of TemplateLiteral in order,so only checkout string like `hi`.
504505
// Closure-compiler do more: [case TEMPLATELIT](https://github.com/google/closure-compiler/blob/e13f5cd0a5d3d35f2db1e6c03fdf67ef02946009/src/com/google/javascript/jscomp/NodeUtil.java#L241-L256).
505-
if let Some(quasi) = template_literal.quasis.get(0) && quasi.tail {
506-
quasi.value.cooked.as_ref().map(|cooked| Cow::Borrowed(cooked.as_str()))
507-
} else {
508-
None
509-
}
506+
template_literal
507+
.quasis
508+
.get(0)
509+
.filter(|quasi| quasi.tail)
510+
.and_then(|quasi| quasi.value.cooked.as_ref())
511+
.map(|cooked| Cow::Borrowed(cooked.as_str()))
510512
}
511513
Expression::Identifier(ident) => {
512514
let name = ident.name.as_str();

crates/oxc_hir/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(let_chains)]
2-
31
#[cfg(feature = "serde")]
42
mod serialize;
53

crates/oxc_linter/src/jest_ast_util.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ pub fn parse_jest_fn_call<'a>(
4343
let resolved = resolve_to_jest_fn(call_expr, ctx)?;
4444

4545
let chain = get_node_chain(callee);
46-
if let Some(first) = chain.first() && let Some(last) = chain.last() {
46+
if let (Some(first), Some(last)) = (chain.first(), chain.last()) {
4747
// if we're an `each()`, ensure we're the outer CallExpression (i.e `.each()()`)
4848
if last == "each"
49-
&& !matches!( callee, Expression::CallExpression(_) | Expression::TaggedTemplateExpression(_))
49+
&& !matches!(
50+
callee,
51+
Expression::CallExpression(_) | Expression::TaggedTemplateExpression(_)
52+
)
5053
{
5154
return None;
5255
}

crates/oxc_linter/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![allow(clippy::self_named_module_files)] // for rules.rs
2-
#![feature(let_chains)]
32

43
#[cfg(test)]
54
mod tester;

0 commit comments

Comments
 (0)