Boa: MethodDefinition parsing

Created on 31 Mar 2020  路  6Comments  路  Source: boa-dev/boa

We currently don't parse getters and setters on object literals.
There needs to be a new parsing section for MethodDefinitions.

We already have readObjectLiteral, so the logic would need to be called from within there.
https://github.com/jasonwilliams/boa/blob/master/boa/src/syntax/parser/mod.rs#L1599

This would either need to be rebased on https://github.com/jasonwilliams/boa/pull/281 or on master once #281 is in.

Spec:
https://tc39.es/ecma262/#prod-MethodDefinition

Previous Rust Code (you don't have to use this)

        TODO need to revisit this
        if let TokenKind::Identifier(name) = tok.kind {
            if name == "get" || name == "set" {
                let may_identifier = self.peek_skip_lineterminator();
                if may_identifier.is_ok()
                    && matches!(may_identifier.unwrap().kind, TokenKind::Identifier(_))
                {
                    let f = self.read_function_expression()?;
                    let func_name = if let NodeBase::FunctionExpr(ref name, _, _) = f.base {
                        name.clone().unwrap()
                    } else {
                        panic!()
                    };
                    return Ok(PropertyDefinition::MethodDefinition(
                        if name == "get" {
                            MethodDefinitionKind::Get
                        } else {
                            MethodDefinitionKind::Set
                        },
                        func_name,
                        f,
                    ));
                }
            }

            return Ok(PropertyDefinition::IdentifierReference(name));
        }
good first issue parser

Most helpful comment

I'll like to work on this.

All 6 comments

I'll like to work on this.

I'll like to work on this.

Go for it. :+1:

I think the link in the description should point to a specific file but it points to a whole pull request with several files and I don't know where to look. Also, Is the code snippet in the description the part I should refactor?

I think the link in the description should point to a specific file but it points to a whole pull request with several files and I don't know where to look. Also, Is the code snippet in the description the part I should refactor?

Good catch, I updated the link in the description. It should be https://github.com/jasonwilliams/boa/blob/master/boa/src/syntax/parser/mod.rs#L1599

if I get this correctly: I will have to define a different method to parse js methods. Right now only Ordinary methods are parsed within the read_property_definition method of the Parser, which returns a result with PropertyDefintion. So, if I am to define a new method for parsing all method definitions, I will return a result with MethodDefinition. This will mean extracting the MethodDefinition variant of the PropertyDefinition enum as its own separed struct/enum. This method will be called in read_object_literal (the link above). So a method shouldn't be parsed within property definition.

if I get this correctly: I will have to define a different method to parse js methods. Right now only Ordinary methods are parsed within the read_property_definition method of the Parser, which returns a result with PropertyDefintion. So, if I am to define a new method for parsing all method definitions, I will return a result with MethodDefinition. This will mean extracting the MethodDefinition variant of the PropertyDefinition enum as its own separed struct/enum. This method will be called in read_object_literal (the link above). So a method shouldn't be parsed within property definition.

Pretty much.

This will mean extracting the MethodDefinition variant of the PropertyDefinition enum as its own separed struct/enum.

You don't really have to extract MethodDefinition. You can have the new function return PropertyDefintion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

croraf picture croraf  路  4Comments

elasmojs picture elasmojs  路  4Comments

jasonwilliams picture jasonwilliams  路  6Comments

attliaLin picture attliaLin  路  3Comments

Razican picture Razican  路  5Comments