Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ pub struct ObjectProperty<'a> {
pub kind: PropertyKind,
pub key: PropertyKey<'a>,
pub value: Expression<'a>,
pub init: Option<Expression<'a>>, // for `CoverInitializedName`
pub method: bool,
pub shorthand: bool,
pub computed: bool,
Expand Down
18 changes: 8 additions & 10 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,15 @@ const _: () = {
assert!(size_of::<ObjectPropertyKind>() == 16usize);
assert!(align_of::<ObjectPropertyKind>() == 8usize);

assert!(size_of::<ObjectProperty>() == 72usize);
assert!(size_of::<ObjectProperty>() == 56usize);
assert!(align_of::<ObjectProperty>() == 8usize);
assert!(offset_of!(ObjectProperty, span) == 0usize);
assert!(offset_of!(ObjectProperty, kind) == 8usize);
assert!(offset_of!(ObjectProperty, key) == 16usize);
assert!(offset_of!(ObjectProperty, value) == 32usize);
assert!(offset_of!(ObjectProperty, init) == 48usize);
assert!(offset_of!(ObjectProperty, method) == 64usize);
assert!(offset_of!(ObjectProperty, shorthand) == 65usize);
assert!(offset_of!(ObjectProperty, computed) == 66usize);
assert!(offset_of!(ObjectProperty, method) == 48usize);
assert!(offset_of!(ObjectProperty, shorthand) == 49usize);
assert!(offset_of!(ObjectProperty, computed) == 50usize);

assert!(size_of::<PropertyKey>() == 16usize);
assert!(align_of::<PropertyKey>() == 8usize);
Expand Down Expand Up @@ -1669,16 +1668,15 @@ const _: () = {
assert!(size_of::<ObjectPropertyKind>() == 8usize);
assert!(align_of::<ObjectPropertyKind>() == 4usize);

assert!(size_of::<ObjectProperty>() == 40usize);
assert!(size_of::<ObjectProperty>() == 32usize);
assert!(align_of::<ObjectProperty>() == 4usize);
assert!(offset_of!(ObjectProperty, span) == 0usize);
assert!(offset_of!(ObjectProperty, kind) == 8usize);
assert!(offset_of!(ObjectProperty, key) == 12usize);
assert!(offset_of!(ObjectProperty, value) == 20usize);
assert!(offset_of!(ObjectProperty, init) == 28usize);
assert!(offset_of!(ObjectProperty, method) == 36usize);
assert!(offset_of!(ObjectProperty, shorthand) == 37usize);
assert!(offset_of!(ObjectProperty, computed) == 38usize);
assert!(offset_of!(ObjectProperty, method) == 28usize);
assert!(offset_of!(ObjectProperty, shorthand) == 29usize);
assert!(offset_of!(ObjectProperty, computed) == 30usize);

assert!(size_of::<PropertyKey>() == 8usize);
assert!(align_of::<PropertyKey>() == 4usize);
Expand Down
14 changes: 3 additions & 11 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,6 @@ impl<'a> AstBuilder<'a> {
/// - kind
/// - key
/// - value
/// - init
/// - method
/// - shorthand
/// - computed
Expand All @@ -1613,15 +1612,12 @@ impl<'a> AstBuilder<'a> {
kind: PropertyKind,
key: PropertyKey<'a>,
value: Expression<'a>,
init: Option<Expression<'a>>,
method: bool,
shorthand: bool,
computed: bool,
) -> ObjectPropertyKind<'a> {
ObjectPropertyKind::ObjectProperty(
self.alloc(
self.object_property(span, kind, key, value, init, method, shorthand, computed),
),
self.alloc(self.object_property(span, kind, key, value, method, shorthand, computed)),
)
}

Expand Down Expand Up @@ -1650,7 +1646,6 @@ impl<'a> AstBuilder<'a> {
/// - kind
/// - key
/// - value
/// - init
/// - method
/// - shorthand
/// - computed
Expand All @@ -1661,12 +1656,11 @@ impl<'a> AstBuilder<'a> {
kind: PropertyKind,
key: PropertyKey<'a>,
value: Expression<'a>,
init: Option<Expression<'a>>,
method: bool,
shorthand: bool,
computed: bool,
) -> ObjectProperty<'a> {
ObjectProperty { span, kind, key, value, init, method, shorthand, computed }
ObjectProperty { span, kind, key, value, method, shorthand, computed }
}

/// Build an [`ObjectProperty`], and store it in the memory arena.
Expand All @@ -1678,7 +1672,6 @@ impl<'a> AstBuilder<'a> {
/// - kind
/// - key
/// - value
/// - init
/// - method
/// - shorthand
/// - computed
Expand All @@ -1689,13 +1682,12 @@ impl<'a> AstBuilder<'a> {
kind: PropertyKind,
key: PropertyKey<'a>,
value: Expression<'a>,
init: Option<Expression<'a>>,
method: bool,
shorthand: bool,
computed: bool,
) -> Box<'a, ObjectProperty<'a>> {
Box::new_in(
self.object_property(span, kind, key, value, init, method, shorthand, computed),
self.object_property(span, kind, key, value, method, shorthand, computed),
self.allocator,
)
}
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for ObjectProperty<'old_alloc>
kind: CloneIn::clone_in(&self.kind, allocator),
key: CloneIn::clone_in(&self.key, allocator),
value: CloneIn::clone_in(&self.value, allocator),
init: CloneIn::clone_in(&self.init, allocator),
method: CloneIn::clone_in(&self.method, allocator),
shorthand: CloneIn::clone_in(&self.shorthand, allocator),
computed: CloneIn::clone_in(&self.computed, allocator),
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_ast/src/generated/derive_content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ impl<'a> ContentEq for ObjectProperty<'a> {
ContentEq::content_eq(&self.kind, &other.kind)
&& ContentEq::content_eq(&self.key, &other.key)
&& ContentEq::content_eq(&self.value, &other.value)
&& ContentEq::content_eq(&self.init, &other.init)
&& ContentEq::content_eq(&self.method, &other.method)
&& ContentEq::content_eq(&self.shorthand, &other.shorthand)
&& ContentEq::content_eq(&self.computed, &other.computed)
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_ast/src/generated/derive_content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ impl<'a> ContentHash for ObjectProperty<'a> {
ContentHash::content_hash(&self.kind, state);
ContentHash::content_hash(&self.key, state);
ContentHash::content_hash(&self.value, state);
ContentHash::content_hash(&self.init, state);
ContentHash::content_hash(&self.method, state);
ContentHash::content_hash(&self.shorthand, state);
ContentHash::content_hash(&self.computed, state);
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ impl<'a> Serialize for ObjectProperty<'a> {
map.serialize_entry("kind", &self.kind)?;
map.serialize_entry("key", &self.key)?;
map.serialize_entry("value", &self.value)?;
map.serialize_entry("init", &self.init)?;
map.serialize_entry("method", &self.method)?;
map.serialize_entry("shorthand", &self.shorthand)?;
map.serialize_entry("computed", &self.computed)?;
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_ast/src/generated/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3157,9 +3157,6 @@ pub mod walk {
visitor.enter_node(kind);
visitor.visit_property_key(&it.key);
visitor.visit_expression(&it.value);
if let Some(init) = &it.init {
visitor.visit_expression(init);
}
visitor.leave_node(kind);
}

Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_ast/src/generated/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3306,9 +3306,6 @@ pub mod walk_mut {
visitor.enter_node(kind);
visitor.visit_property_key(&mut it.key);
visitor.visit_expression(&mut it.value);
if let Some(init) = &mut it.init {
visitor.visit_expression(init);
}
visitor.leave_node(kind);
}

Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,13 @@ pub fn jsx_element_no_match(span: Span, span1: Span, name: &str) -> OxcDiagnosti
.with_labels([span, span1])
}

#[cold]
pub fn cover_initialized_name(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid assignment in object literal")
.with_help("Did you mean to use a ':'? An '=' can only follow a property name when the containing object literal is part of a destructuring pattern.")
.with_label(span)
}

// ================================= MODIFIERS =================================

#[cold]
Expand Down
7 changes: 1 addition & 6 deletions crates/oxc_parser/src/js/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,7 @@ impl<'a> CoverGrammar<'a, ObjectProperty<'a>> for AssignmentTargetProperty<'a> {
_ => return Err(p.unexpected()),
};
// convert `CoverInitializedName`
let init = match property.init {
Some(Expression::AssignmentExpression(assignment_expr)) => {
Some(assignment_expr.unbox().right)
}
_ => None,
};
let init = p.state.cover_initialized_name.remove(&property.span.start).map(|e| e.right);
Ok(p.ast.assignment_target_property_assignment_target_property_identifier(
property.span,
binding,
Expand Down
17 changes: 5 additions & 12 deletions crates/oxc_parser/src/js/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ impl<'a> ParserImpl<'a> {
PropertyKind::Init,
key,
Expression::FunctionExpression(method),
/* init */ None,
/* method */ true,
/* shorthand */ false,
/* computed */ computed,
Expand Down Expand Up @@ -138,24 +137,22 @@ impl<'a> ParserImpl<'a> {
// IdentifierReference ({ foo })
let value = Expression::Identifier(self.alloc(identifier.clone()));
// CoverInitializedName ({ foo = bar })
let init = if self.eat(Kind::Eq) {
if self.eat(Kind::Eq) {
let right = self.parse_assignment_expression_or_higher()?;
let left = AssignmentTarget::AssignmentTargetIdentifier(self.alloc(identifier));
Some(self.ast.expression_assignment(
let expr = self.ast.assignment_expression(
self.end_span(span),
AssignmentOperator::Assign,
left,
right,
))
} else {
None
};
);
self.state.cover_initialized_name.insert(span.start, expr);
}
Ok(self.ast.alloc_object_property(
self.end_span(span),
PropertyKind::Init,
PropertyKey::StaticIdentifier(key),
value,
init,
/* method */ false,
/* shorthand */ true,
/* computed */ false,
Expand All @@ -177,7 +174,6 @@ impl<'a> ParserImpl<'a> {
PropertyKind::Init,
key,
value,
None,
/* method */ false,
/* shorthand */ false,
/* computed */ computed,
Expand Down Expand Up @@ -233,7 +229,6 @@ impl<'a> ParserImpl<'a> {
PropertyKind::Init,
key,
value,
/* init */ None,
/* method */ true,
/* shorthand */ false,
/* computed */ computed,
Expand All @@ -253,7 +248,6 @@ impl<'a> ParserImpl<'a> {
PropertyKind::Get,
key,
value,
/* init */ None,
/* method */ false,
/* shorthand */ false,
/* computed */ computed,
Expand All @@ -273,7 +267,6 @@ impl<'a> ParserImpl<'a> {
PropertyKind::Set,
key,
Expression::FunctionExpression(method),
/* init */ None,
/* method */ false,
/* shorthand */ false,
/* computed */ computed,
Expand Down
12 changes: 12 additions & 0 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ impl<'a> ParserImpl<'a> {
(program, true)
}
};

self.check_unfinished_errors();
let mut errors = vec![];
// only check for `@flow` if the file failed to parse.
if !self.lexer.errors.is_empty() || !self.errors.is_empty() {
Expand All @@ -434,6 +436,7 @@ impl<'a> ParserImpl<'a> {
// initialize cur_token and prev_token by moving onto the first token
self.bump_any();
let expr = self.parse_expr().map_err(|diagnostic| vec![diagnostic])?;
self.check_unfinished_errors();
let errors = self.lexer.errors.into_iter().chain(self.errors).collect::<Vec<_>>();
if !errors.is_empty() {
return Err(errors);
Expand Down Expand Up @@ -492,6 +495,15 @@ impl<'a> ParserImpl<'a> {
}
}

fn check_unfinished_errors(&mut self) {
use oxc_span::GetSpan;
// PropertyDefinition : cover_initialized_name
// It is a Syntax Error if any source text is matched by this production.
for expr in self.state.cover_initialized_name.values() {
self.errors.push(diagnostics::cover_initialized_name(expr.span()));
}
}

/// Check if source length exceeds MAX_LEN, if the file cannot be parsed.
/// Original parsing error is not real - `Lexer::new` substituted "\0" as the source text.
fn overlong_error(&self) -> Option<OxcDiagnostic> {
Expand Down
9 changes: 7 additions & 2 deletions crates/oxc_parser/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use oxc_ast::ast::Decorator;
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};

use oxc_ast::ast::{AssignmentExpression, Decorator};

#[derive(Default)]
pub struct ParserState<'a> {
pub not_parenthesized_arrow: FxHashSet<u32>,

pub decorators: Vec<Decorator<'a>>,

/// Temporary storage for `CoverInitializedName` `({ foo = bar })`.
/// Keyed by `ObjectProperty`'s span.start.
pub cover_initialized_name: FxHashMap<u32, AssignmentExpression<'a>>,
}
12 changes: 0 additions & 12 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,19 +913,7 @@ pub fn check_super<'a>(sup: &Super, node: &AstNode<'a>, ctx: &SemanticBuilder<'a
}
}

fn cover_initialized_name(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid assignment in object literal")
.with_help("Did you mean to use a ':'? An '=' can only follow a property name when the containing object literal is part of a destructuring pattern.")
.with_label(span)
}

pub fn check_object_property(prop: &ObjectProperty, ctx: &SemanticBuilder<'_>) {
// PropertyDefinition : cover_initialized_name
// It is a Syntax Error if any source text is matched by this production.
if let Some(expr) = &prop.init {
ctx.error(cover_initialized_name(expr.span()));
}

if let Expression::FunctionExpression(function) = &prop.value {
match prop.kind {
PropertyKind::Set => check_setter(function, ctx),
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_transformer/src/jsx/display_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ impl<'a, 'ctx> ReactDisplayName<'a, 'ctx> {
PropertyKind::Init,
ctx.ast.property_key_identifier_name(SPAN, DISPLAY_NAME),
ctx.ast.expression_string_literal(SPAN, name),
None,
false,
false,
false,
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_transformer/src/jsx/jsx_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl<'a, 'ctx> JsxImpl<'a, 'ctx> {
let key = Self::get_attribute_name(&attr.name, ctx);
let value = self.transform_jsx_attribute_value(attr.value.as_ref(), ctx);
let object_property = ctx.ast.object_property_kind_object_property(
attr.span, kind, key, value, None, false, false, false,
attr.span, kind, key, value, false, false, false,
);
properties.push(object_property);
}
Expand Down Expand Up @@ -600,7 +600,6 @@ impl<'a, 'ctx> JsxImpl<'a, 'ctx> {
PropertyKind::Init,
ctx.ast.property_key_identifier_name(SPAN, "children"),
value,
None,
false,
false,
false,
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_transformer/src/jsx/jsx_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ impl<'a, 'ctx> JsxSelf<'a, 'ctx> {
let kind = PropertyKind::Init;
let key = ctx.ast.property_key_identifier_name(SPAN, SELF);
let value = ctx.ast.expression_this(SPAN);
ctx.ast
.object_property_kind_object_property(SPAN, kind, key, value, None, false, false, false)
ctx.ast.object_property_kind_object_property(SPAN, kind, key, value, false, false, false)
}

pub fn can_add_self_attribute(ctx: &TraverseCtx<'a>) -> bool {
Expand Down
Loading