Skip to content

fix: do not treat LuaDoc string "async" as async fun prefix - #3467

Open
cpruijsen wants to merge 1 commit into
LuaLS:masterfrom
cpruijsen:fix/issue-3402
Open

cpruijsen wants to merge 1 commit into
LuaLS:masterfrom
cpruijsen:fix/issue-3402

Conversation

@cpruijsen

Copy link
Copy Markdown
Contributor

Summary

  • parseFunction now accepts the async fun prefix only when the token type is name, the same way parseTypeUnitFunction already recognizes fun. async fun(...) is unchanged.
  • LuaDoc type parsing treated any token whose text was async as the async fun prefix and consumed it. A string literal "async" (for example ---@field mode "async" | "sync") produced <type name> expected. and the rest of the union was lost. When "async" was the second arm ("sync" | "async"), the first arm was kept and "async" was dropped with no warning.
  • Parser AST fixture added for the reported field union; changelog Unreleased FIX links literal strings 'async' is mis-parsed #3402.

Decision

  • checkToken('name', 'async', 1) before consuming the token. That is how this file already distinguishes the fun keyword from other tokens; it is the smallest reversible change that stops consuming a string "async".
  • Alternative: compare peekToken() as tp == 'name' and content == 'async', and/or apply the same kind check to "fun". Happy to switch to the explicit type compare, or to extend the guard to "fun", if you prefer. ("fun" already survives today because parseTypeUnitFunction checks token type before consuming.)

Test plan

  • parser_test AST fixture ---@field mode "async" | "sync" (fails without the parser change; passes with it)
  • ---@type async fun() still parses as an async function type (existing hover coverage)

Fixes #3402

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中文:该修复将 content == 'async' 替换为 checkToken('name', 'async', 1),使 async 前缀检测仅在 token 类型为 name 时触发,从而避免把 LuaDoc 类型中的字符串字面量 "async"(token 类型为 string)误判为 async fun 前缀。这一改动方向正确,且新增的 AST 测试覆盖了 ---@field mode "async" | "sync" 场景。建议确认 checkToken 的第三个参数 1 语义为“向前查看 1 个 token 且不消费”,并确认 checkToken 在文件作用域内已定义、在 parseFunction 调用点可用;同时建议补充一个仍能正确解析 ---@async fun 前缀的回归测试,以防该改动意外破坏原有 async 前缀功能。

English: The fix replaces content == 'async' with checkToken('name', 'async', 1), so the async prefix detection only triggers when the token type is name, avoiding mis-parsing the string literal "async" (token type string) inside LuaDoc types as the async fun prefix. The direction is correct, and the added AST test covers the ---@field mode "async" | "sync" case. Please confirm that the third argument 1 of checkToken means "look ahead 1 token without consuming it", and that checkToken is defined in file scope and available at the parseFunction call site. Also consider adding a regression test that still parses the ---@async fun prefix correctly, to ensure this change does not accidentally break the existing async prefix behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

literal strings 'async' is mis-parsed

1 participant