Microsoft/TypeScript

`const enum` references in the body of nodes with grammar errors are not inlined

Open

#58,209 建立於 2024年4月15日

在 GitHub 查看
 (0 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: JS EmitHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

Which results in output that's more broken than the input.

For example,

const enum A {
    a = 1,
    b = 2
}

A.a;

return {
    f() {
        return A.a;
    }
}

A top-level return is a grammar error, but is valid in old-style cjs modules at runtime, so is actually a vaguely reasonable grammar error to //@ts-ignore. The issue, however, is that the const enum reference within the body of that return statement -A.a is left as-is rather than being inlined, so at runtime it refers to something which does not exist.

This doesn't seem to be in any way intentional - this is a side effect of getConstantValue in the checker only using a cached resolvedSymbol, rather than ensuring one is available by checking/resolving the access expression first (which is also an issue for emit done before a full checker walk).

貢獻者指南