Object Getter triggers yield to incorrectly parse as YieldExpression

未关闭
#1,982 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
领域
compilers

调研方向

从 parser.ts 第 3257 行附近开始,使用提供的 esprima.parse 示例复现该问题。将生成的 AST 与预期输出进行比较,重点关注解析 getter 是否会使 context.allowYield 保持为已更改的状态。完成的标准是后面的 yield 被表示为 Identifier,并且 getter 仍能被正确解析。

由索引模型根据 Issue 内容生成。

描述

When any get property is encountered the context.allowYield property is set to false and not reverted after parsing the getter body. This results in any future yield to be parsed as a yield expression incorrectly.

esprima.parse(`
({get a(){}});
yield;
`)
Expected output
{
  "type": "Program",
  "sourceType": "script",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "ObjectExpression",
        "properties": [
          {
            "type": "Property",
            "key": {
              "type": "Identifier",
              "name": "a"
            },
            "computed": false,
            "kind": "get",
            "method": false,
            "shorthand": false,
            "value": {
              "type": "FunctionExpression",
              "id": null,
              "params": [],
              "body": {
                "type": "BlockStatement",
                "body": []
              },
              "generator": false,
              "async": false,
              "expression": false
            }
          }
        ]
      }
    },
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "Identifier",
        "name": "yield"
      }
    }
  ]
}
Actual output
{
    "type": "Program",
    "body": [
        {
            "type": "ExpressionStatement",
            "expression": {
                "type": "ObjectExpression",
                "properties": [
                    {
                        "type": "Property",
                        "key": {
                            "type": "Identifier",
                            "name": "a"
                        },
                        "computed": false,
                        "value": {
                            "type": "FunctionExpression",
                            "id": null,
                            "params": [],
                            "body": {
                                "type": "BlockStatement",
                                "body": []
                            },
                            "generator": false,
                            "expression": false,
                            "async": false
                        },
                        "kind": "get",
                        "method": false,
                        "shorthand": false
                    }
                ]
            }
        },
        {
            "type": "ExpressionStatement",
            "expression": {
                "type": "YieldExpression",
                "argument": null,
                "delegate": false
            }
        }
    ],
    "sourceType": "script"
}

This is pulled directly from everything.js

Using this file as and example, I was surprised that removing line 84 will cause this to parse differently as they are completely unrelated.

I believe the offending code is located on line 3257 of parser.ts.

I believe the correct path in the ecma262 spec for line 200 would be

  1. StatementListItem
  2. Statement
  3. ExpressionStatement
  4. Expression
  5. AssignmentExpression
  6. ConditionalExpression
  7. LogicalORExpression
  8. LogicalANDExpression
  9. BitwiseORExpression
  10. BitwiseXORExpression
  11. BitwiseANDExpression
  12. EqualityExpression
  13. RelationalExpression
  14. ShiftExpression
  15. AdditiveExpression
  16. MultiplicativeExpression
  17. ExponentiationExpression
  18. UnaryExpression
  19. UpdateExpression
  20. LeftHandSideExpression
  21. NewExpression
  22. MemberExpression
  23. PrimaryExpression
  24. IdentifierReference
  25. Identifier
主要语言
TypeScript
星标
7.1k
派生
772
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

jquery/esprima 的其他 Issue

查看 jquery/esprima 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。