javascriptabstract-syntax-tree

What do computed and optional mean in a JavaScript MemberExpression AST node?


I have a.x = b.y to be the AST node:

{
  "type": "ExpressionStatement",
  "start": 122,
  "end": 131,
  "expression": {
    "type": "AssignmentExpression",
    "start": 122,
    "end": 131,
    "operator": "=",
    "left": {
      "type": "MemberExpression",
      "start": 122,
      "end": 125,
      "object": {
        "type": "Identifier",
        "start": 122,
        "end": 123,
        "name": "a"
      },
      "property": {
        "type": "Identifier",
        "start": 124,
        "end": 125,
        "name": "x"
      },
      "computed": false,
      "optional": false
    },
    "right": {
      "type": "MemberExpression",
      "start": 128,
      "end": 131,
      "object": {
        "type": "Identifier",
        "start": 128,
        "end": 129,
        "name": "b"
      },
      "property": {
        "type": "Identifier",
        "start": 130,
        "end": 131,
        "name": "y"
      },
      "computed": false,
      "optional": false
    }
  }
}

What does computed and optional mean? What are some examples?


Solution

  • This is what a.b[c.d[e].f].g looks like:

    {
      "type": "Program",
      "body": [
        {
          "type": "MemberExpression",
          "object": {
            "type": "MemberExpression",
            "object": {
              "type": "MemberExpression",
              "object": {
                "type": "Identifier",
                "start": 1,
                "end": 2,
                "name": "a"
              },
              "property": {
                "type": "Identifier",
                "start": 3,
                "end": 4,
                "name": "b"
              },
              "computed": false
            },
            "property": {
              "type": "MemberExpression",
              "object": {
                "type": "MemberExpression",
                "object": {
                  "type": "MemberExpression",
                  "object": {
                    "type": "Identifier",
                    "start": 5,
                    "end": 6,
                    "name": "c"
                  },
                  "property": {
                    "type": "Identifier",
                    "start": 7,
                    "end": 8,
                    "name": "d"
                  },
                  "computed": false
                },
                "property": {
                  "type": "Identifier",
                  "start": 9,
                  "end": 10,
                  "name": "e"
                },
                "computed": true
              },
              "property": {
                "type": "Identifier",
                "start": 12,
                "end": 13,
                "name": "f"
              },
              "computed": false
            },
            "computed": true
          },
          "property": {
            "type": "Identifier",
            "start": 15,
            "end": 16,
            "name": "g"
          },
          "computed": false
        }
      ]
    }
    

    The computed is for object[property].