Skip to content

Proposal: Do expression and async do expression #42437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 98 commits into from

Conversation

Jack-Works
Copy link
Contributor

@Jack-Works Jack-Works commented Jan 21, 2021

This is an experimental PR to support ECMAScript proposal do expression.

Proposal: Do expression and Async do expression

Spec: Do expression and Async do expression

PR preview Playground

Known problems / TODOs:

  • Completion type analysis should be based on control flow analysis.
  • There're many false positives on the Not all code paths return a value check.
  • break or continue inside the for head doesn't lead to a type error (there is no spec yet, only a memo so I don't know how to implement this.)
  • The following code will report type error:
function f(param: number) {
    const double = do {
        if (param === 0) return null;
        param * param;
    }
    return double
}

Type evaluation rule of the do expression:

I made a change to the checker of typescript. Here are the new evaluation rules:

  • Every statement might return void or Type as a type checking result. If it returns void, it is treated as not contribute to the type of the statement.

New type evaluation rules

New Expressions

  • DoExpression: type of its containing block statement. if there is none, fallback to voidType.

Statements

Statement:

  • BlockStatement: type is last type of it's inner statement.
  • VariableStatement: SyntaxError
  • EmptyStatement: Does not contribute. See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
  • ExpressionStatement: type of it's containing expression
  • IfStatement: union type of all branches. if there is no else block, it will treated as voidType
  • BreakableStatement:
    • IterationStatement: SyntaxError
    • SwitchStatement: union type of all clauses
  • ContinueStatement: never
  • BreakStatement: never
  • ReturnStatement: never
  • WithStatement: any
  • LabelledStatement: type of it's inner statement
  • ThrowStatement: never
  • TryStatement: union type of try clause and catch clause. finally clause is ignored.
  • DebuggerStatement: Does not contribute. See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation

Extra checks

A var declaration cannot be used within a do expression unless the target is ESNext
function a() {
    (do {
        var x = 1;
        x;
    });
    x; // 1
}

Not all code paths return a value

Requires noImplicitReturns to be true

image

Inlay hints

Show all "exit points" of the do expression to make it easier to identify.

image

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jan 21, 2021
@Jack-Works Jack-Works changed the title ECMAScript feature: Do expression ECMAScript syntax: Do expression Jan 21, 2021
@Kingwl
Copy link
Contributor

Kingwl commented Jan 22, 2021

Cool!
Let me (us) know if you want to make a tarball package and a playground.

@Kingwl
Copy link
Contributor

Kingwl commented Jan 23, 2021

@typescript-bot pack this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 23, 2021

Heya @Kingwl, I've started to run the tarball bundle task on this PR at 4fad104. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 23, 2021

Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/94041/artifacts?artifactName=tgz&fileId=A7FBEB57B050AF55EBAE4A4B0E554BE4F005E2844E9C42011E87081414201BDE02&fileName=/typescript-4.2.0-insiders.20210123.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@4.2.0-pr-42437-4".;

@Kingwl
Copy link
Contributor

Kingwl commented Jan 24, 2021

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 24, 2021

Heya @Kingwl, I've started to run the tarball bundle task on this PR at 4277519. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 24, 2021

Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/94082/artifacts?artifactName=tgz&fileId=03AB43DA1756835BDCCA79CD1B8F1C9915A59B169C2BEF71FBB9D0BC482BED2F02&fileName=/typescript-4.2.0-insiders.20210124.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@4.2.0-pr-42437-7".;

@Jack-Works
Copy link
Contributor Author

@Kingwl hi can you pack this thanks!

@Kingwl
Copy link
Contributor

Kingwl commented Aug 22, 2021

@typescript-bot pack this.

@Kingwl
Copy link
Contributor

Kingwl commented Aug 22, 2021

Oops, seems I cannot trigger ts bot anymore.

@orta might can give some help to you.

@orta
Copy link
Contributor

orta commented Aug 24, 2021

@typescript-bot pack this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 24, 2021

Heya @orta, I've started to run the tarball bundle task on this PR at 860f554. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 24, 2021

Hey @orta, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/109194/artifacts?artifactName=tgz&fileId=B9DA44164CBE2153BFD07BB1D81F452609651B215184EE0682AF92B3C5E96F7202&fileName=/typescript-4.5.0-insiders.20210824.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@4.5.0-pr-42437-39".;

@Jack-Works
Copy link
Contributor Author

Updates:

  • Transform: fix return inside parameter of a function
  • Transform: fix var declared variable hoisting
  • Transform: support break through switch statement
  • Transform: fix in class fields
  • LSP: support inlay hint for all exit points of the do expression

image

@orta Hi can you let the bot pack this?

@Jack-Works
Copy link
Contributor Author

Got some problems with break analysis...

@Jack-Works
Copy link
Contributor Author

close for now. I'll re-open and rebase this once the proposal gets advanced.

@Jack-Works Jack-Works closed this Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants