microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.75k stars 12.46k forks source link

“Lexical environment is not suspended” when visitEachChile #58020

Open cqzooge opened 6 months ago

cqzooge commented 6 months ago

🔎 Search Terms

Lexical environment is not suspended

🕗 Version & Regression Information

⏯ Playground Link

No response

💻 Code

function f(): {get a(): void} | void {}

🙁 Actual behavior

function f(): {get a(): void} | void {}

this code won't report error in playground, but if we parse this code and use visitEachChild to visit each node with transform context we will have crash like

Error: Debug Failure. False expression: Lexical environment is suspended.

the test code:

const ts = require("typescript")

function test2() {
    const sourceCode = "function f(): {get a(): void} | void {}";
    const result = ts.transpileModule(sourceCode, {
        transformers: {before: [createVisiter] }
    })
    console.log(result.outputText);
}

function createVisiter(context) {
    return (node) => visit(node);
    function visit(node) {
        return ts.visitEachChild(node, visit, context);
    }
}

test2()

🙂 Expected behavior

don't have assert error when we visitEachChild

Additional information about the issue

seems the reason is that we set lexicalEnvironmentSuspended as true after visit ParameterList, and before visit FunctionBody. But if we have something between ParameterList and FunctionBody and need check lexicalEnvironmentSuspended, we will have assert error. like get/set accessor

so if we add a objectliteral type with get/set accessor as the return type of a function or method or get/set accessor, we will have crash when we use visitEachChild to visit the nodes

Is it a bug? or I use something wrong?

rbuckton commented 6 months ago

This certainly looks like a bug, we try to push a new lexical environment for the get/set while processing them as declarations in a type when we shouldn't.

cqzooge commented 6 months ago

thanks,when will this problem fixed?

by the way, what does lexicalEnvironmentSuspended used for?

fatcerberus commented 6 months ago

wait, why are we visiting Chile again

RyanCavanaugh commented 6 months ago

'cuz I'm a forEach chile ya hear, lord knows I'm a forEach chile

https://youtu.be/rb53-sDII0o