Skip to content

Missing optional when the key is MemberExpression in a computed optional methods. #1097

Closed
@sosukesuzuki

Description

@sosukesuzuki

This issue is related on prettier/prettier#6601 (comment)

What code were you trying to parse?

class Foo {
  [foo.bar]?() {}
}

What did you expect to happen?

{
  "type": "Program",
  "body": [
    {
      "type": "ClassDeclaration",
      "id": {
        "type": "Identifier",
        "name": "Foo",
        "range": [
          6,
          9
        ],
        "loc": {
          "start": {
            "line": 1,
            "column": 6
          },
          "end": {
            "line": 1,
            "column": 9
          }
        }
      },
      "body": {
        "type": "ClassBody",
        "body": [
          {
            "type": "MethodDefinition",
            "key": {
              "type": "MemberExpression",
              "object": {
                "type": "Identifier",
                "name": "foo",
                "range": [
                  17,
                  20
                ],
                "loc": {
                  "start": {
                    "line": 2,
                    "column": 5
                  },
                  "end": {
                    "line": 2,
                    "column": 8
                  }
                }
              },
              "property": {
                "type": "Identifier",
                "name": "bar",
                "range": [
                  21,
                  24
                ],
                "loc": {
                  "start": {
                    "line": 2,
                    "column": 9
                  },
                  "end": {
                    "line": 2,
                    "column": 12
                  }
                }
              },
              "computed": false,
              "optional": true, // <= this `optional` should be `true`
              "range": [
                17,
                24
              ],
              "loc": {
                "start": {
                  "line": 2,
                  "column": 5
                },
                "end": {
                  "line": 2,
                  "column": 12
                }
              }
            },
            "value": {
              "type": "FunctionExpression",
              "id": null,
              "generator": false,
              "expression": false,
              "async": false,
              "body": {
                "type": "BlockStatement",
                "body": [],
                "range": [
                  29,
                  31
                ],
                "loc": {
                  "start": {
                    "line": 2,
                    "column": 17
                  },
                  "end": {
                    "line": 2,
                    "column": 19
                  }
                }
              },
              "range": [
                26,
                31
              ],
              "params": [],
              "loc": {
                "start": {
                  "line": 2,
                  "column": 14
                },
                "end": {
                  "line": 2,
                  "column": 19
                }
              }
            },
            "computed": true,
            "static": false,
            "kind": "method",
            "range": [
              16,
              31
            ],
            "loc": {
              "start": {
                "line": 2,
                "column": 4
              },
              "end": {
                "line": 2,
                "column": 19
              }
            }
          }
        ],
        "range": [
          10,
          33
        ],
        "loc": {
          "start": {
            "line": 1,
            "column": 10
          },
          "end": {
            "line": 3,
            "column": 1
          }
        }
      },
      "superClass": null,
      "range": [
        0,
        33
      ],
      "loc": {
        "start": {
          "line": 1,
          "column": 0
        },
        "end": {
          "line": 3,
          "column": 1
        }
      }
    }
  ],
  "sourceType": "script",
  "range": [
    0,
    34
  ],
  "loc": {
    "start": {
      "line": 1,
      "column": 0
    },
    "end": {
      "line": 4,
      "column": 0
    }
  }
}

What actually happened?

{
  "type": "Program",
  "body": [
    {
      "type": "ClassDeclaration",
      "id": {
        "type": "Identifier",
        "name": "Foo",
        "range": [
          6,
          9
        ],
        "loc": {
          "start": {
            "line": 1,
            "column": 6
          },
          "end": {
            "line": 1,
            "column": 9
          }
        }
      },
      "body": {
        "type": "ClassBody",
        "body": [
          {
            "type": "MethodDefinition",
            "key": {
              "type": "MemberExpression",
              "object": {
                "type": "Identifier",
                "name": "foo",
                "range": [
                  17,
                  20
                ],
                "loc": {
                  "start": {
                    "line": 2,
                    "column": 5
                  },
                  "end": {
                    "line": 2,
                    "column": 8
                  }
                }
              },
              "property": {
                "type": "Identifier",
                "name": "bar",
                "range": [
                  21,
                  24
                ],
                "loc": {
                  "start": {
                    "line": 2,
                    "column": 9
                  },
                  "end": {
                    "line": 2,
                    "column": 12
                  }
                }
              },
              "computed": false,
              "optional": false, // <= this `optional` should be `true`
              "range": [
                17,
                24
              ],
              "loc": {
                "start": {
                  "line": 2,
                  "column": 5
                },
                "end": {
                  "line": 2,
                  "column": 12
                }
              }
            },
            "value": {
              "type": "FunctionExpression",
              "id": null,
              "generator": false,
              "expression": false,
              "async": false,
              "body": {
                "type": "BlockStatement",
                "body": [],
                "range": [
                  29,
                  31
                ],
                "loc": {
                  "start": {
                    "line": 2,
                    "column": 17
                  },
                  "end": {
                    "line": 2,
                    "column": 19
                  }
                }
              },
              "range": [
                26,
                31
              ],
              "params": [],
              "loc": {
                "start": {
                  "line": 2,
                  "column": 14
                },
                "end": {
                  "line": 2,
                  "column": 19
                }
              }
            },
            "computed": true,
            "static": false,
            "kind": "method",
            "range": [
              16,
              31
            ],
            "loc": {
              "start": {
                "line": 2,
                "column": 4
              },
              "end": {
                "line": 2,
                "column": 19
              }
            }
          }
        ],
        "range": [
          10,
          33
        ],
        "loc": {
          "start": {
            "line": 1,
            "column": 10
          },
          "end": {
            "line": 3,
            "column": 1
          }
        }
      },
      "superClass": null,
      "range": [
        0,
        33
      ],
      "loc": {
        "start": {
          "line": 1,
          "column": 0
        },
        "end": {
          "line": 3,
          "column": 1
        }
      }
    }
  ],
  "sourceType": "script",
  "range": [
    0,
    34
  ],
  "loc": {
    "start": {
      "line": 1,
      "column": 0
    },
    "end": {
      "line": 4,
      "column": 0
    }
  }
}

Versions

package version
@typescript-eslint/typescript-estree 2.4.0
TypeScript 3.6.4
node 10.16.3
npm 6.9.0

Note

This bug does not happen with simple identifer.

class Foo {
  [foo]?() {}
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpackage: typescript-estreeIssues related to @typescript-eslint/typescript-estree

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions