---
source: crates/react_hermes_parser/tests/parser_test.rs
expression: "format!(\"Input:\\n{input}\\n\\nOutput:\\n{output}\")"
input_file: crates/react_hermes_parser/tests/fixtures/destructuring-mixed-scope-and-local-variables-with-default.js
---
Input:
function Component(props) {
  const post = useFragment(graphql`...`, props.post);
  const allUrls = [];
  // `media` and `urls` are exported from the scope that will wrap this code,
  // but `comments` is not (it doesn't need to be memoized, bc the callback
  // only checks `comments.length`)
  // because of the scope, the let declaration for media and urls are lifted
  // out of the scope, and the destructure statement ends up turning into
  // a reassignment, instead of a const declaration. this means we try to
  // reassign `comments` when there's no declaration for it.
  const { media = null, comments = [], urls = [] } = post;
  const onClick = (e) => {
    if (!comments.length) {
      return;
    }
    console.log(comments.length);
  };
  allUrls.push(...urls);
  return <Media media={media} onClick={onClick} />;
}


Output:
{
  "type": "Program",
  "body": [
    {
      "type": "FunctionDeclaration",
      "id": {
        "type": "Identifier",
        "name": "Component",
        "typeAnnotation": null,
        "loc": null,
        "range": [
          9,
          18
        ]
      },
      "params": [
        {
          "type": "Identifier",
          "name": "props",
          "typeAnnotation": null,
          "loc": null,
          "range": [
            19,
            24
          ]
        }
      ],
      "body": {
        "type": "BlockStatement",
        "body": [
          {
            "type": "VariableDeclaration",
            "kind": "const",
            "declarations": [
              {
                "type": "VariableDeclarator",
                "id": {
                  "type": "Identifier",
                  "name": "post",
                  "typeAnnotation": null,
                  "loc": null,
                  "range": [
                    36,
                    40
                  ]
                },
                "init": {
                  "type": "CallExpression",
                  "callee": {
                    "type": "Identifier",
                    "name": "useFragment",
                    "typeAnnotation": null,
                    "loc": null,
                    "range": [
                      43,
                      54
                    ]
                  },
                  "arguments": [
                    {
                      "type": "TaggedTemplateExpression",
                      "tag": {
                        "type": "Identifier",
                        "name": "graphql",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          55,
                          62
                        ]
                      },
                      "quasi": {
                        "type": "TemplateLiteral",
                        "quasis": [
                          {
                            "type": "TemplateElement",
                            "tail": true,
                            "value": {
                              "cooked": "...",
                              "raw": "..."
                            },
                            "loc": null,
                            "range": [
                              62,
                              67
                            ]
                          }
                        ],
                        "expressions": [],
                        "loc": null,
                        "range": [
                          62,
                          67
                        ]
                      },
                      "loc": null,
                      "range": [
                        55,
                        67
                      ]
                    },
                    {
                      "type": "MemberExpression",
                      "object": {
                        "type": "Identifier",
                        "name": "props",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          69,
                          74
                        ]
                      },
                      "property": {
                        "type": "Identifier",
                        "name": "post",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          75,
                          79
                        ]
                      },
                      "computed": false,
                      "loc": null,
                      "range": [
                        69,
                        79
                      ]
                    }
                  ],
                  "loc": null,
                  "range": [
                    43,
                    80
                  ]
                },
                "loc": null,
                "range": [
                  36,
                  80
                ]
              }
            ],
            "loc": null,
            "range": [
              30,
              81
            ]
          },
          {
            "type": "VariableDeclaration",
            "kind": "const",
            "declarations": [
              {
                "type": "VariableDeclarator",
                "id": {
                  "type": "Identifier",
                  "name": "allUrls",
                  "typeAnnotation": null,
                  "loc": null,
                  "range": [
                    90,
                    97
                  ]
                },
                "init": {
                  "type": "ArrayExpression",
                  "elements": [],
                  "loc": null,
                  "range": [
                    100,
                    102
                  ]
                },
                "loc": null,
                "range": [
                  90,
                  102
                ]
              }
            ],
            "loc": null,
            "range": [
              84,
              103
            ]
          },
          {
            "type": "VariableDeclaration",
            "kind": "const",
            "declarations": [
              {
                "type": "VariableDeclarator",
                "id": {
                  "type": "ObjectPattern",
                  "properties": [
                    {
                      "type": "Property",
                      "key": {
                        "type": "Identifier",
                        "name": "media",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          590,
                          595
                        ]
                      },
                      "value": {
                        "type": "AssignmentPattern",
                        "left": {
                          "type": "Identifier",
                          "name": "media",
                          "typeAnnotation": null,
                          "loc": null,
                          "range": [
                            590,
                            595
                          ]
                        },
                        "right": {
                          "type": "NullLiteral",
                          "loc": null,
                          "range": [
                            598,
                            602
                          ]
                        },
                        "loc": null,
                        "range": [
                          590,
                          602
                        ]
                      },
                      "kind": "init",
                      "computed": false,
                      "shorthand": true,
                      "method": false,
                      "loc": null,
                      "range": [
                        590,
                        602
                      ]
                    },
                    {
                      "type": "Property",
                      "key": {
                        "type": "Identifier",
                        "name": "comments",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          604,
                          612
                        ]
                      },
                      "value": {
                        "type": "AssignmentPattern",
                        "left": {
                          "type": "Identifier",
                          "name": "comments",
                          "typeAnnotation": null,
                          "loc": null,
                          "range": [
                            604,
                            612
                          ]
                        },
                        "right": {
                          "type": "ArrayExpression",
                          "elements": [],
                          "loc": null,
                          "range": [
                            615,
                            617
                          ]
                        },
                        "loc": null,
                        "range": [
                          604,
                          617
                        ]
                      },
                      "kind": "init",
                      "computed": false,
                      "shorthand": true,
                      "method": false,
                      "loc": null,
                      "range": [
                        604,
                        617
                      ]
                    },
                    {
                      "type": "Property",
                      "key": {
                        "type": "Identifier",
                        "name": "urls",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          619,
                          623
                        ]
                      },
                      "value": {
                        "type": "AssignmentPattern",
                        "left": {
                          "type": "Identifier",
                          "name": "urls",
                          "typeAnnotation": null,
                          "loc": null,
                          "range": [
                            619,
                            623
                          ]
                        },
                        "right": {
                          "type": "ArrayExpression",
                          "elements": [],
                          "loc": null,
                          "range": [
                            626,
                            628
                          ]
                        },
                        "loc": null,
                        "range": [
                          619,
                          628
                        ]
                      },
                      "kind": "init",
                      "computed": false,
                      "shorthand": true,
                      "method": false,
                      "loc": null,
                      "range": [
                        619,
                        628
                      ]
                    }
                  ],
                  "loc": null,
                  "range": [
                    588,
                    630
                  ]
                },
                "init": {
                  "type": "Identifier",
                  "name": "post",
                  "typeAnnotation": null,
                  "loc": null,
                  "range": [
                    633,
                    637
                  ]
                },
                "loc": null,
                "range": [
                  588,
                  637
                ]
              }
            ],
            "loc": null,
            "range": [
              582,
              638
            ]
          },
          {
            "type": "VariableDeclaration",
            "kind": "const",
            "declarations": [
              {
                "type": "VariableDeclarator",
                "id": {
                  "type": "Identifier",
                  "name": "onClick",
                  "typeAnnotation": null,
                  "loc": null,
                  "range": [
                    647,
                    654
                  ]
                },
                "init": {
                  "type": "ArrowFunctionExpression",
                  "id": null,
                  "params": [
                    {
                      "type": "Identifier",
                      "name": "e",
                      "typeAnnotation": null,
                      "loc": null,
                      "range": [
                        658,
                        659
                      ]
                    }
                  ],
                  "body": {
                    "type": "BlockStatement",
                    "body": [
                      {
                        "type": "IfStatement",
                        "test": {
                          "type": "UnaryExpression",
                          "operator": "!",
                          "prefix": true,
                          "argument": {
                            "type": "MemberExpression",
                            "object": {
                              "type": "Identifier",
                              "name": "comments",
                              "typeAnnotation": null,
                              "loc": null,
                              "range": [
                                675,
                                683
                              ]
                            },
                            "property": {
                              "type": "Identifier",
                              "name": "length",
                              "typeAnnotation": null,
                              "loc": null,
                              "range": [
                                684,
                                690
                              ]
                            },
                            "computed": false,
                            "loc": null,
                            "range": [
                              675,
                              690
                            ]
                          },
                          "loc": null,
                          "range": [
                            674,
                            690
                          ]
                        },
                        "consequent": {
                          "type": "BlockStatement",
                          "body": [
                            {
                              "type": "ReturnStatement",
                              "argument": null,
                              "loc": null,
                              "range": [
                                700,
                                707
                              ]
                            }
                          ],
                          "loc": null,
                          "range": [
                            692,
                            713
                          ]
                        },
                        "alternate": null,
                        "loc": null,
                        "range": [
                          670,
                          713
                        ]
                      },
                      {
                        "type": "ExpressionStatement",
                        "expression": {
                          "type": "CallExpression",
                          "callee": {
                            "type": "MemberExpression",
                            "object": {
                              "type": "Identifier",
                              "name": "console",
                              "typeAnnotation": null,
                              "loc": null,
                              "range": [
                                718,
                                725
                              ]
                            },
                            "property": {
                              "type": "Identifier",
                              "name": "log",
                              "typeAnnotation": null,
                              "loc": null,
                              "range": [
                                726,
                                729
                              ]
                            },
                            "computed": false,
                            "loc": null,
                            "range": [
                              718,
                              729
                            ]
                          },
                          "arguments": [
                            {
                              "type": "MemberExpression",
                              "object": {
                                "type": "Identifier",
                                "name": "comments",
                                "typeAnnotation": null,
                                "loc": null,
                                "range": [
                                  730,
                                  738
                                ]
                              },
                              "property": {
                                "type": "Identifier",
                                "name": "length",
                                "typeAnnotation": null,
                                "loc": null,
                                "range": [
                                  739,
                                  745
                                ]
                              },
                              "computed": false,
                              "loc": null,
                              "range": [
                                730,
                                745
                              ]
                            }
                          ],
                          "loc": null,
                          "range": [
                            718,
                            746
                          ]
                        },
                        "directive": null,
                        "loc": null,
                        "range": [
                          718,
                          747
                        ]
                      }
                    ],
                    "loc": null,
                    "range": [
                      664,
                      751
                    ]
                  },
                  "generator": false,
                  "async": false,
                  "loc": null,
                  "range": [
                    657,
                    751
                  ],
                  "expression": false,
                  "loc": null,
                  "range": [
                    657,
                    751
                  ]
                },
                "loc": null,
                "range": [
                  647,
                  751
                ]
              }
            ],
            "loc": null,
            "range": [
              641,
              752
            ]
          },
          {
            "type": "ExpressionStatement",
            "expression": {
              "type": "CallExpression",
              "callee": {
                "type": "MemberExpression",
                "object": {
                  "type": "Identifier",
                  "name": "allUrls",
                  "typeAnnotation": null,
                  "loc": null,
                  "range": [
                    755,
                    762
                  ]
                },
                "property": {
                  "type": "Identifier",
                  "name": "push",
                  "typeAnnotation": null,
                  "loc": null,
                  "range": [
                    763,
                    767
                  ]
                },
                "computed": false,
                "loc": null,
                "range": [
                  755,
                  767
                ]
              },
              "arguments": [
                {
                  "type": "SpreadElement",
                  "argument": {
                    "type": "Identifier",
                    "name": "urls",
                    "typeAnnotation": null,
                    "loc": null,
                    "range": [
                      771,
                      775
                    ]
                  },
                  "loc": null,
                  "range": [
                    768,
                    775
                  ]
                }
              ],
              "loc": null,
              "range": [
                755,
                776
              ]
            },
            "directive": null,
            "loc": null,
            "range": [
              755,
              777
            ]
          },
          {
            "type": "ReturnStatement",
            "argument": {
              "type": "JSXElement",
              "openingElement": {
                "type": "JSXOpeningElement",
                "name": {
                  "type": "JSXIdentifier",
                  "name": "Media",
                  "loc": null,
                  "range": [
                    788,
                    793
                  ]
                },
                "attributes": [
                  {
                    "type": "JSXAttribute",
                    "name": {
                      "type": "JSXIdentifier",
                      "name": "media",
                      "loc": null,
                      "range": [
                        794,
                        799
                      ]
                    },
                    "value": {
                      "type": "JSXExpressionContainer",
                      "expression": {
                        "type": "Identifier",
                        "name": "media",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          801,
                          806
                        ]
                      },
                      "loc": null,
                      "range": [
                        800,
                        807
                      ]
                    },
                    "loc": null,
                    "range": [
                      794,
                      807
                    ]
                  },
                  {
                    "type": "JSXAttribute",
                    "name": {
                      "type": "JSXIdentifier",
                      "name": "onClick",
                      "loc": null,
                      "range": [
                        808,
                        815
                      ]
                    },
                    "value": {
                      "type": "JSXExpressionContainer",
                      "expression": {
                        "type": "Identifier",
                        "name": "onClick",
                        "typeAnnotation": null,
                        "loc": null,
                        "range": [
                          817,
                          824
                        ]
                      },
                      "loc": null,
                      "range": [
                        816,
                        825
                      ]
                    },
                    "loc": null,
                    "range": [
                      808,
                      825
                    ]
                  }
                ],
                "selfClosing": true,
                "loc": null,
                "range": [
                  787,
                  828
                ]
              },
              "children": [],
              "closingElement": null,
              "loc": null,
              "range": [
                787,
                828
              ]
            },
            "loc": null,
            "range": [
              780,
              829
            ]
          }
        ],
        "loc": null,
        "range": [
          26,
          831
        ]
      },
      "generator": false,
      "async": false,
      "loc": null,
      "range": [
        0,
        831
      ],
      "loc": null,
      "range": [
        0,
        831
      ]
    }
  ],
  "sourceType": "script",
  "loc": null,
  "range": [
    0,
    831
  ]
}
