8000 `readonly` and `declare` invalid property for `MethodDefinition`. · Issue #2908 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
readonly and declare invalid property for MethodDefinition. #2908
Closed
@sosukesuzuki

Description

@sosukesuzuki
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

// the code you're trying to parse
class Foo {
  readonly bar() {}
  declare baz() {}
}
// the code you're using to do the parse of the aforementioned code
import { parse } from '@typescript-eslint/typescript-estree';

const code = `
class Foo {
  readonly bar() {}
  declare baz() {}
}
`;
const ast = parse(code);

Expected Result

Expect that MethodDefinition nodes have readonly and declare property.

AST
{
  "type": "ClassDeclaration",
  "id": {
    "type": "Identifier",
    "name": "Foo"
  },
  "body": {
    "type": "ClassBody",
    "body": [
      {
        "type": "MethodDefinition",
        "key": {
          "type": "Identifier",
          "name": "bar"
        },
        "value": {
          "type": "FunctionExpression",
          "id": null,
          "generator": false,
          "expression": false,
          "async": false,
          "body": {
            "type": "BlockStatement",
            "body": []
          },
          "params": []
        },
        "computed": false,
        "static": false,
        "kind": "method",
+        "readonly": true
      },
      {
        "type": "MethodDefinition",
        "key": {
          "type": "Identifier",
          "name": "baz"
        },
        "value": {
          "type": "FunctionExpression",
          "id": null,
          "generator": false,
          "expression": false,
          "async": false,
          "body": {
            "type": "BlockStatement",
            "body": []
          },
          "params": []
        },
        "computed": false,
        "static": false,
        "kind": "method",
+       "declare": true
      }
    ]
  },
  "superClass": null
}

Actual Result

AST
{
  "type": "ClassDeclaration",
  "id": {
    "type": "Identifier",
    "name": "Foo"
  },
  "body": {
    "type": "ClassBody",
    "body": [
      {
        "type": "MethodDefinition",
        "key": {
          "type": "Identifier",
          "name": "bar"
        },
        "value": {
          "type": "FunctionExpression",
          "id": null,
          "generator": false,
          "expression": false,
          "async": false,
          "body": {
            "type": "BlockStatement",
            "body": []
          },
          "params": []
        },
        "computed": false,
        "static": false,
        "kind": "method"
      },
      {
        "type": "MethodDefinition",
        "key": {
          "type": "Identifier",
          "name": "baz"
        },
        "value": {
          "type": "FunctionExpression",
          "id": null,
          "generator": false,
          "expression": false,
          "async": false,
          "body": {
            "type": "BlockStatement",
            "body": []
          },
          "params": []
        },
        "computed": false,
        "static": false,
        "kind": "method"
      }
    ]
  },
  "superClass": null
}

Additional Info

prettier/prettier#9760

I know the above code is invalid as TypeScript program (TS Playground). But I think code formatters should correctly treat those cases. What do you think about?

If the request is accepted, I can work on this.

Versions

package version
@typescript-eslint/typescript-estree master
TypeScript 4.1.2
node v14.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: typescript-estreeIssues related to @typescript-eslint/typescript-estreewontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0