Skip to content

Commit e82afea

Browse files
committed
refactor(ast): check whether there is a single quasi in TemplateLiteral::quasi
1 parent ab9d91c commit e82afea

File tree

12 files changed

+28
-48
lines changed

12 files changed

+28
-48
lines changed

crates/oxc_ast/src/ast_impl/js.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,12 @@ impl<'a> TemplateLiteral<'a> {
544544
/// - `` `foo` `` => `true`
545545
/// - `` `foo${bar}qux` `` => `false`
546546
pub fn is_no_substitution_template(&self) -> bool {
547-
self.expressions.is_empty() && self.quasis.len() == 1
547+
self.quasis.len() == 1
548548
}
549549

550550
/// Get single quasi from `template`
551551
pub fn quasi(&self) -> Option<Atom<'a>> {
552-
self.quasis.first().and_then(|quasi| quasi.value.cooked)
552+
if self.is_no_substitution_template() { self.quasis[0].value.cooked } else { None }
553553
}
554554
}
555555

crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,9 @@ impl<'a> MayHaveSideEffects<'a> for ComputedMemberExpression<'a> {
417417
Expression::StringLiteral(s) => {
418418
property_access_may_have_side_effects(&self.object, &s.value, ctx)
419419
}
420-
Expression::TemplateLiteral(t) if t.is_no_substitution_template() => {
421-
property_access_may_have_side_effects(
422-
&self.object,
423-
&t.quasi().expect("template literal must have at least one quasi"),
424-
ctx,
425-
)
426-
}
420+
Expression::TemplateLiteral(t) => t.quasi().is_some_and(|quasi| {
421+
property_access_may_have_side_effects(&self.object, &quasi, ctx)
422+
}),
427423
Expression::NumericLiteral(n) => !n.value.to_integer_index().is_some_and(|n| {
428424
!integer_index_property_access_may_have_side_effects(&self.object, n, ctx)
429425
}),

crates/oxc_ecmascript/src/to_primitive.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ pub fn maybe_object_with_to_primitive_related_properties_overridden(
9393
matches!(str.value.as_str(), "toString" | "valueOf")
9494
}
9595
PropertyKey::TemplateLiteral(temp) => {
96-
!temp.is_no_substitution_template()
97-
|| temp
98-
.quasi()
99-
.is_some_and(|val| matches!(val.as_str(), "toString" | "valueOf"))
96+
temp.quasi().is_some_and(|val| matches!(val.as_str(), "toString" | "valueOf"))
10097
}
10198
_ => true,
10299
},

crates/oxc_linter/src/ast_util.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,7 @@ pub fn extract_regex_flags<'a>(
308308
}
309309
let flag_arg = match &args[1] {
310310
Argument::StringLiteral(flag_arg) => flag_arg.value,
311-
Argument::TemplateLiteral(template) if template.is_no_substitution_template() => {
312-
template.quasi().expect("no-substitution templates always have a quasi")
313-
}
311+
Argument::TemplateLiteral(template) => template.quasi()?,
314312
_ => return None,
315313
};
316314
let mut flags = RegExpFlags::empty();

crates/oxc_linter/src/rules/eslint/valid_typeof.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ impl Rule for ValidTypeof {
124124
}
125125

126126
if let Expression::TemplateLiteral(template) = sibling {
127-
if template.expressions.is_empty() {
128-
if template.quasi().is_some_and(|value| !VALID_TYPES.contains(&value.as_str())) {
127+
if let Some(quasi) = template.quasi() {
128+
if !VALID_TYPES.contains(&quasi.as_str()) {
129129
ctx.diagnostic(invalid_value(None, sibling.span()));
130130
}
131131
return;

crates/oxc_linter/src/rules/jest/valid_title.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,6 @@ impl ValidTitle {
180180
);
181181
}
182182
Argument::TemplateLiteral(template_literal) => {
183-
if !template_literal.is_no_substitution_template() {
184-
return;
185-
}
186183
if let Some(quasi) = template_literal.quasi() {
187184
validate_title(
188185
quasi.as_str(),

crates/oxc_linter/src/rules/react/button_has_type.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,9 @@ impl ButtonHasType {
180180
Expression::StringLiteral(str) => {
181181
self.is_valid_button_type_prop_string_literal(str.value.as_str())
182182
}
183-
Expression::TemplateLiteral(template_literal) => {
184-
if !template_literal.is_no_substitution_template() {
185-
return false;
186-
}
187-
if let Some(quasi) = template_literal.quasi() {
188-
return self.is_valid_button_type_prop_string_literal(quasi.as_str());
189-
}
190-
false
191-
}
183+
Expression::TemplateLiteral(template_literal) => template_literal
184+
.quasi()
185+
.is_some_and(|quasi| self.is_valid_button_type_prop_string_literal(quasi.as_str())),
192186
Expression::ConditionalExpression(conditional_expr) => {
193187
self.is_valid_button_type_prop_expression(&conditional_expr.consequent)
194188
&& self.is_valid_button_type_prop_expression(&conditional_expr.alternate)

crates/oxc_linter/src/rules/react/jsx_curly_brace_presence.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,9 @@ impl JsxCurlyBracePresence {
462462
}
463463
}
464464
Expression::TemplateLiteral(template) => {
465-
if allowed.is_never() && template.is_no_substitution_template() {
466-
let string = template.quasi().unwrap();
465+
if allowed.is_never()
466+
&& let Some(string) = template.quasi()
467+
{
467468
if contains_quote_characters(string.as_str())
468469
|| is_allowed_string_like(
469470
ctx,

crates/oxc_linter/src/utils/express.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub fn as_endpoint_registration<'a, 'n>(
3434
Expression::StringLiteral(path) => {
3535
Some((Some(path.value), &call.arguments.as_slice()[1..]))
3636
}
37-
Expression::TemplateLiteral(template) if template.is_no_substitution_template() => {
38-
Some((template.quasi(), &call.arguments.as_slice()[1..]))
37+
Expression::TemplateLiteral(template) => {
38+
template.quasi().map(|quasi| (Some(quasi), &call.arguments.as_slice()[1..]))
3939
}
4040
_ => Some((None, call.arguments.as_slice())),
4141
}

crates/oxc_linter/src/utils/jest.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Cow;
33
use oxc_ast::{
44
AstKind,
55
ast::{
6-
CallExpression, Expression, ImportDeclaration, ImportDeclarationSpecifier, TemplateLiteral,
6+
CallExpression, Expression, ImportDeclaration, ImportDeclarationSpecifier,
77
match_member_expression,
88
},
99
};
@@ -271,8 +271,10 @@ pub fn get_node_name_vec<'a>(expr: &'a Expression<'a>) -> Vec<Cow<'a, str>> {
271271
Expression::StringLiteral(string_literal) => {
272272
chain.push(Cow::Borrowed(&string_literal.value));
273273
}
274-
Expression::TemplateLiteral(template_literal) if is_pure_string(template_literal) => {
275-
chain.push(Cow::Borrowed(template_literal.quasi().unwrap().as_str()));
274+
Expression::TemplateLiteral(template_literal) => {
275+
if let Some(quasi) = template_literal.quasi() {
276+
chain.push(Cow::Borrowed(quasi.as_str()));
277+
}
276278
}
277279
Expression::TaggedTemplateExpression(tagged_expr) => {
278280
chain.extend(get_node_name_vec(&tagged_expr.tag));
@@ -294,10 +296,6 @@ pub fn get_node_name_vec<'a>(expr: &'a Expression<'a>) -> Vec<Cow<'a, str>> {
294296
chain
295297
}
296298

297-
fn is_pure_string(template_literal: &TemplateLiteral) -> bool {
298-
template_literal.expressions.is_empty() && template_literal.quasis.len() == 1
299-
}
300-
301299
pub fn is_equality_matcher(matcher: &KnownMemberExpressionProperty) -> bool {
302300
matcher.is_name_equal("toBe")
303301
|| matcher.is_name_equal("toEqual")

0 commit comments

Comments
 (0)