Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default function parameters are incompletely parsed as AssignPattern #138

Open
Vannevelj opened this issue Mar 20, 2022 · 1 comment
Open

Comments

@Vannevelj
Copy link

It appears as if default function values are parsed in an unexpected manner. Specifically, the AssignPattern.key() value always returns None, when I would expect it to be a SinglePattern. I can however query its child nodes to retrieve the data from the SinglePattern.

Using the following code:

fn print_parameter() {
    let source = "function foo(a = 5) {}";
    let parse = parse_text(source, 0);
    let ast = parse.syntax();
    print_ast(&ast);

    for descendant in ast.descendants() {
        match descendant.kind() {
            rslint_parser::SyntaxKind::ASSIGN_PATTERN => {
                let assign = descendant.to::<AssignPattern>();
                println!("Assigned {:?}", assign.key());
                for child in descendant.children() {
                    println!("child: {child:?}");
                }
            },
            _ => continue
        }
    }
}

I get as output:

Assigned None
child: NAME@13..14
child: LITERAL@17..18

Printing the AST I can see the following:

Script (SCRIPT) [0-22]
 Fn decl (FN_DECL) [0-22]
  Name (NAME) [9-12]
  Parameter list (PARAMETER_LIST) [12-19]
   Assign pattern (ASSIGN_PATTERN) [13-18]
    Name (NAME) [13-14]
    Literal (LITERAL) [17-18]
  Block statement (BLOCK_STMT) [20-22]

I would have expected the Name and Literal to be wrapped in a SinglePattern. Is my expectation incorrect or is there indeed a piece missing here?

Versions used:

  • rslint_parser = "0.3.1"
  • rslint_rowan = "0.10.0"
  • rslint_errors = "0.2.0"
  • rslint_core = "0.3.0"
@Stupremee
Copy link
Member

This seems like a small issue in the AST generation. It should be relatively easy to fix. I will try to fix it today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants