mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(ecmascript): remove the lifetime annotation on MayHaveSideEffects (#8717)
This commit is contained in:
parent
e7ab96cbb6
commit
58002e270b
6 changed files with 18 additions and 16 deletions
|
|
@ -14,7 +14,7 @@ pub use is_literal_value::IsLiteralValue;
|
|||
pub use value::ConstantValue;
|
||||
pub use value_type::ValueType;
|
||||
|
||||
pub trait ConstantEvaluation<'a>: MayHaveSideEffects<'a> {
|
||||
pub trait ConstantEvaluation<'a>: MayHaveSideEffects {
|
||||
fn resolve_binding(&self, ident: &IdentifierReference<'a>) -> Option<ConstantValue<'a>> {
|
||||
match ident.name.as_str() {
|
||||
"undefined" if self.is_global_reference(ident) => Some(ConstantValue::Undefined),
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ use oxc_ast::ast::*;
|
|||
/// Returns true if subtree changes application state.
|
||||
///
|
||||
/// Ported from [closure-compiler](https://github.com/google/closure-compiler/blob/f3ce5ed8b630428e311fe9aa2e20d36560d975e2/src/com/google/javascript/jscomp/AstAnalyzer.java#L94)
|
||||
pub trait MayHaveSideEffects<'a> {
|
||||
fn is_global_reference(&self, ident: &oxc_ast::ast::IdentifierReference<'a>) -> bool {
|
||||
pub trait MayHaveSideEffects {
|
||||
fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> bool {
|
||||
matches!(ident.name.as_str(), "undefined" | "NaN" | "Infinity")
|
||||
}
|
||||
|
||||
fn expression_may_have_side_efffects(&self, e: &Expression<'a>) -> bool {
|
||||
fn expression_may_have_side_efffects(&self, e: &Expression<'_>) -> bool {
|
||||
match e {
|
||||
// Reference read can have a side effect.
|
||||
Expression::Identifier(ident) => self.is_global_reference(ident),
|
||||
|
|
@ -50,7 +50,7 @@ pub trait MayHaveSideEffects<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn unary_expression_may_have_side_effects(&self, e: &UnaryExpression<'a>) -> bool {
|
||||
fn unary_expression_may_have_side_effects(&self, e: &UnaryExpression<'_>) -> bool {
|
||||
/// A "simple" operator is one whose children are expressions, has no direct side-effects.
|
||||
fn is_simple_unary_operator(operator: UnaryOperator) -> bool {
|
||||
operator != UnaryOperator::Delete
|
||||
|
|
@ -61,7 +61,7 @@ pub trait MayHaveSideEffects<'a> {
|
|||
true
|
||||
}
|
||||
|
||||
fn binary_expression_may_have_side_effects(&self, e: &BinaryExpression<'a>) -> bool {
|
||||
fn binary_expression_may_have_side_effects(&self, e: &BinaryExpression<'_>) -> bool {
|
||||
// `instanceof` and `in` can throw `TypeError`
|
||||
if matches!(e.operator, BinaryOperator::In | BinaryOperator::Instanceof) {
|
||||
return true;
|
||||
|
|
@ -72,7 +72,7 @@ pub trait MayHaveSideEffects<'a> {
|
|||
|
||||
fn array_expression_element_may_have_side_effects(
|
||||
&self,
|
||||
e: &ArrayExpressionElement<'a>,
|
||||
e: &ArrayExpressionElement<'_>,
|
||||
) -> bool {
|
||||
match e {
|
||||
ArrayExpressionElement::SpreadElement(e) => {
|
||||
|
|
@ -85,7 +85,7 @@ pub trait MayHaveSideEffects<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn object_property_kind_may_have_side_effects(&self, e: &ObjectPropertyKind<'a>) -> bool {
|
||||
fn object_property_kind_may_have_side_effects(&self, e: &ObjectPropertyKind<'_>) -> bool {
|
||||
match e {
|
||||
ObjectPropertyKind::ObjectProperty(o) => self.object_property_may_have_side_effects(o),
|
||||
ObjectPropertyKind::SpreadProperty(e) => {
|
||||
|
|
@ -94,12 +94,12 @@ pub trait MayHaveSideEffects<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn object_property_may_have_side_effects(&self, e: &ObjectProperty<'a>) -> bool {
|
||||
fn object_property_may_have_side_effects(&self, e: &ObjectProperty<'_>) -> bool {
|
||||
self.property_key_may_have_side_effects(&e.key)
|
||||
|| self.expression_may_have_side_efffects(&e.value)
|
||||
}
|
||||
|
||||
fn property_key_may_have_side_effects(&self, key: &PropertyKey<'a>) -> bool {
|
||||
fn property_key_may_have_side_effects(&self, key: &PropertyKey<'_>) -> bool {
|
||||
match key {
|
||||
PropertyKey::StaticIdentifier(_) | PropertyKey::PrivateIdentifier(_) => false,
|
||||
match_expression!(PropertyKey) => {
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ impl<'a, 'b> Deref for Ctx<'a, 'b> {
|
|||
|
||||
impl<'a> ConstantEvaluation<'a> for Ctx<'a, '_> {}
|
||||
|
||||
impl<'a> MayHaveSideEffects<'a> for Ctx<'a, '_> {
|
||||
fn is_global_reference(&self, ident: &IdentifierReference<'a>) -> bool {
|
||||
impl MayHaveSideEffects for Ctx<'_, '_> {
|
||||
fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> bool {
|
||||
ident.is_global_reference(self.0.symbols())
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,10 @@ impl<'a> Ctx<'a, '_> {
|
|||
self.0.symbols()
|
||||
}
|
||||
|
||||
pub fn is_global_reference(self, ident: &IdentifierReference<'a>) -> bool {
|
||||
ident.is_global_reference(self.0.symbols())
|
||||
}
|
||||
|
||||
pub fn eval_binary(self, e: &BinaryExpression<'a>) -> Option<Expression<'a>> {
|
||||
self.eval_binary_expression(e).map(|v| self.value_to_expr(e.span, v))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ use oxc_allocator::Vec;
|
|||
use oxc_ast::{ast::*, NONE};
|
||||
use oxc_ecmascript::{
|
||||
constant_evaluation::{ConstantEvaluation, ValueType},
|
||||
side_effects::MayHaveSideEffects,
|
||||
ToInt32,
|
||||
};
|
||||
use oxc_span::{cmp::ContentEq, GetSpan};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
use oxc_allocator::Vec;
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_ecmascript::side_effects::MayHaveSideEffects;
|
||||
use oxc_semantic::IsGlobalReference;
|
||||
use oxc_span::GetSpan;
|
||||
use oxc_syntax::scope::ScopeFlags;
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ use cow_utils::CowUtils;
|
|||
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_ecmascript::{
|
||||
constant_evaluation::ConstantEvaluation, side_effects::MayHaveSideEffects, StringCharAt,
|
||||
StringCharCodeAt, StringIndexOf, StringLastIndexOf, StringSubstring, ToInt32,
|
||||
constant_evaluation::ConstantEvaluation, StringCharAt, StringCharCodeAt, StringIndexOf,
|
||||
StringLastIndexOf, StringSubstring, ToInt32,
|
||||
};
|
||||
use oxc_span::SPAN;
|
||||
use oxc_syntax::es_target::ESTarget;
|
||||
|
|
|
|||
Loading…
Reference in a new issue