Boa: new Class().method() has incorrect precedence

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

The precedence in new Class().method() isn't correct. This expression is parsed as new (Class().method()), which is incorrect, the correct parse tree would be equivalent to (new Class()).method().

A real-world example is new Date().getTime().

bug parser

Most helpful comment

im doing it now

All 9 comments

Tokens for new Date().getTime()

[boa\src\lib.rs:26] &tokens = [
    Token {
        data: Keyword(
            New,
        ),
        pos: Position {        
            column_number: 1,  
            line_number: 1,    
        },
    },
    Token {
        data: Identifier(      
            "Date",
        ),
        pos: Position {        
            column_number: 5,  
            line_number: 1,    
        },
    },
    Token {
        data: Punctuator(      
            OpenParen,
        ),
        pos: Position {        
            column_number: 9,  
            line_number: 1,    
        },
    },
    Token {
        data: Punctuator(      
            CloseParen,        
        ),
        pos: Position {       
            column_number: 10,
            line_number: 1,   
        },
    },
    Token {
        data: Punctuator(     
            Dot,
        ),
        pos: Position {       
            column_number: 11,
            line_number: 1,   
        },
    },
    Token {
        data: Identifier(     
            "getTime",        
        ),
        pos: Position {       
            column_number: 12,
            line_number: 1,   
        },
    },
    Token {
        data: Punctuator(     
            OpenParen,        
        ),
        pos: Position {       
            column_number: 19,
            line_number: 1,
        },
    },
    Token {
        data: Punctuator(
            CloseParen,
        ),
        pos: Position {
            column_number: 20,
            line_number: 1,
        },
    },
    Token {
        data: Punctuator(
            Semicolon,
        ),
        pos: Position {
            column_number: 21,
            line_number: 1,
        },
    },
]

Tokens looks fine

Parser output

[boa\src\lib.rs:27] &expr = Expr {
    def: Block(
        [
            Expr {
                def: Construct(
                    Expr {
                        def: GetConstField(        
                            Expr {
                                def: Call(
                                    Expr {
                                        def: Local(
                                            "Date",
                                        ),
                                    },
                                    [],
                                ),
                            },
                            "getTime",
                        ),
                    },    
                    [],   
                ),        
            },
        ],
    ),
}

Yeah looks like there'll need to be some work here:
https://github.com/jasonwilliams/boa/blob/master/boa/src/syntax/parser/mod.rs#L232-L240

Upon deeper inspection the issue is around here: https://github.com/jasonwilliams/boa/blob/master/boa/src/syntax/parser/mod.rs#L688-L721

Normally it just carries on, which is why it continues parsing until the method.
There needs to be some signal to stop after the first call.

if we stopped after here https://github.com/jasonwilliams/boa/blob/master/boa/src/syntax/parser/mod.rs#L719 it would break other things such as function chaining

Might be worth making the parser more spec compliant https://tc39.es/ecma262/#sec-left-hand-side-expressions

new output since 0.7

StatementList(
    [
        Call(
            GetConstField(
                New(
                    Call(
                        Local(     
                            "Date",
                        ),
                        [],        
                    ),
                ),
                "getTime",
            ),
            [],
        ),
    ],
)

The precedence looks fixed here
@HalidOdat @Razican ?

The precedence looks fixed here
@HalidOdat @Razican ?

It looks good, yes! Might be worth adding a test for this?

It looks good, yes! Might be worth adding a test for this?

Are you adding the test, or should I?

im doing it now

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elasmojs picture elasmojs  路  4Comments

croraf picture croraf  路  4Comments

Razican picture Razican  路  5Comments

croraf picture croraf  路  5Comments

attliaLin picture attliaLin  路  3Comments