From 1ba2a247e1e91f1e0e8685bcad0ad132a70c0603 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sat, 12 Oct 2024 08:48:45 +0000 Subject: [PATCH] refactor(ecmascript): remove `HasProto` which is not part of the spec (#6470) --- crates/oxc_ecmascript/src/has_proto.rs | 14 -------------- crates/oxc_ecmascript/src/lib.rs | 3 +-- crates/oxc_transformer/src/react/jsx.rs | 10 +++++++--- 3 files changed, 8 insertions(+), 19 deletions(-) delete mode 100644 crates/oxc_ecmascript/src/has_proto.rs diff --git a/crates/oxc_ecmascript/src/has_proto.rs b/crates/oxc_ecmascript/src/has_proto.rs deleted file mode 100644 index 341d198714eba..0000000000000 --- a/crates/oxc_ecmascript/src/has_proto.rs +++ /dev/null @@ -1,14 +0,0 @@ -use oxc_ast::ast::ObjectExpression; - -use crate::PropName; - -pub trait HasProto { - /// Returns `true` if this object has a property named `__proto__` - fn has_proto(&self) -> bool; -} - -impl<'a> HasProto for ObjectExpression<'a> { - fn has_proto(&self) -> bool { - self.properties.iter().any(|p| p.prop_name().is_some_and(|name| name.0 == "__proto__")) - } -} diff --git a/crates/oxc_ecmascript/src/lib.rs b/crates/oxc_ecmascript/src/lib.rs index 37f834615c9b6..54dfca81ec385 100644 --- a/crates/oxc_ecmascript/src/lib.rs +++ b/crates/oxc_ecmascript/src/lib.rs @@ -1,12 +1,11 @@ //! Methods defined in the [ECMAScript Language Specification](https://tc39.es/ecma262). mod bound_names; -mod has_proto; mod is_simple_parameter_list; mod private_bound_identifiers; mod prop_name; pub use self::{ - bound_names::BoundNames, has_proto::HasProto, is_simple_parameter_list::IsSimpleParameterList, + bound_names::BoundNames, is_simple_parameter_list::IsSimpleParameterList, private_bound_identifiers::PrivateBoundIdentifiers, prop_name::PropName, }; diff --git a/crates/oxc_transformer/src/react/jsx.rs b/crates/oxc_transformer/src/react/jsx.rs index 4b8d290ee28fe..23306a5c5d3f5 100644 --- a/crates/oxc_transformer/src/react/jsx.rs +++ b/crates/oxc_transformer/src/react/jsx.rs @@ -90,7 +90,7 @@ use oxc_allocator::Vec; use oxc_ast::{ast::*, AstBuilder, NONE}; -use oxc_ecmascript::HasProto; +use oxc_ecmascript::PropName; use oxc_span::{Atom, GetSpan, Span, SPAN}; use oxc_syntax::{ identifier::{is_irregular_whitespace, is_line_terminator}, @@ -545,7 +545,7 @@ impl<'a, 'ctx> ReactJsx<'a, 'ctx> { JSXAttributeItem::SpreadAttribute(spread) => { if is_classic && attributes.len() == 1 { // deopt if spreading an object with `__proto__` key - if !matches!(&spread.argument, Expression::ObjectExpression(o) if o.has_proto()) + if !matches!(&spread.argument, Expression::ObjectExpression(o) if has_proto(o)) { arguments.push(Argument::from({ // SAFETY: `ast.copy` is unsound! We need to fix. @@ -557,7 +557,7 @@ impl<'a, 'ctx> ReactJsx<'a, 'ctx> { // Add attribute to prop object match &spread.argument { - Expression::ObjectExpression(expr) if !expr.has_proto() => { + Expression::ObjectExpression(expr) if !has_proto(expr) => { // SAFETY: `ast.copy` is unsound! We need to fix. properties.extend(unsafe { ctx.ast.copy(&expr.properties) }); } @@ -1047,3 +1047,7 @@ fn create_static_member_expression<'a>( let property = ctx.ast.identifier_name(SPAN, property_name); ctx.ast.member_expression_static(SPAN, object, property, false).into() } + +fn has_proto(e: &ObjectExpression<'_>) -> bool { + e.properties.iter().any(|p| p.prop_name().is_some_and(|name| name.0 == "__proto__")) +}