Skip to content

Add separate variants to ObjectPropertyKind for methods, getters and setters #142

@overlookmotel

Description

@overlookmotel

oxc-project/oxc#7175 demonstrates that it's easy to confuse a function which is the property value of an object expression with an object method. i.e. to conflate these 2:

obj = {
    foo() {}
};

obj = {
    foo: function() {}
};

We could avoid this confusing by changing ObjectPropertyKind to have separate variants for methods, getters and setters.

Current:

pub enum ObjectPropertyKind<'a> {
    // This variant can be either a property or a method
    ObjectProperty(Box<'a, ObjectProperty<'a>>),
    SpreadProperty(Box<'a, SpreadElement<'a>>),
}

Proposed:

pub enum ObjectPropertyKind<'a> {
    ObjectProperty(Box<'a, ObjectProperty<'a>>),
    SpreadProperty(Box<'a, SpreadElement<'a>>),
    Method(Box<'a, ObjectMethod<'a>>),
    Getter(Box<'a, ObjectMethod<'a>>),
    Setter(Box<'a, ObjectMethod<'a>>),
}

pub struct ObjectProperty<'a> {
    pub span: Span,
    // pub kind: PropertyKind, <-- removed
    pub key: PropertyKey<'a>,
    pub value: Expression<'a>,
    pub init: Option<Expression<'a>>, // for `CoverInitializedName`
    // pub method: bool, <-- removed
    pub shorthand: bool,
    pub computed: bool,
}

pub struct ObjectMethod<'a> {
    pub span: Span,
    pub key: PropertyKey<'a>,
    pub func: Function<'a>,
    pub computed: bool,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions