Skip to content

Comment and assertion changes transpilation of return statement #60296

Closed
@ei-pi

Description

@ei-pi

🔎 Search Terms

transpile, output, return statement

🕗 Version & Regression Information

  • This changed between versions 4.5.5 and 4.6.0. Versions 4.6.1, 4.6.2, 4.7.4, 4.8.4, 4.9.5, 5.0.4, 5.1.6, 5.2.2, 5.3.3, 5.4.5, 5.5.4, 5.6.3, 5.7.0-beta, and 5.7.0-dev.20241020 (aka the ones accessible from the TS version dropdown on the Playground, along with 4.6.1) have all been explicitly tested on the Playground and all exhibit the behaviour—presumably, the versions in between do too.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEYD2A7AzgF3kgRgKwC54AlcJGYAHkxgEsUBzAGnihQE8A+AKG4Ho+8FEkRIAtmJAosbYEJEByKGgXwAvNgDWUdtwBmAVxRgMtVKwAUASngBvbvEfw4GAzBTwLDpz9x4A2gBEUDhggQC63o42GgCMANzcAL68hsam5jjWdlHOIK7unrk+AtgoEOysohJSWBpI2ro+vvhBIWGRPjHwCcm8pagVrB5KKupaOvpGJmYeYNn2Pi5uHl7NTn5toRGsaEIGYjggMLndvSlT6bPwwAu5y4Vr6-ClAO60GAAW8F8IyDXSeAAangowULA+8BwcCgmjQxQ2rWC23Cu32h2Op3G524QA

💻 Code

declare let x: number | undefined

// no comment and no 'as' = okay
function a() {
    return (
        x
    ) = 1;
}

function b() {
    return (
        // only a comment = okay
        x
    ) = 1;
}

// only an 'as' = okay
function c() {
    return (
        x as number
    ) = 1;
}

function d() {
    return (
        // with the comment + 'as', it breaks
        x as number
    ) = 1;
}

which transpiles to:

"use strict";
// no comment and no 'as' = okay
function a() {
    return (x) = 1;
}
function b() {
    return (
    // only a comment = okay
    x) = 1;
}
// only an 'as' = okay
function c() {
    return x = 1;
}
function d() {
    return 
    // with the comment + 'as', it breaks
    x = 1;
}

🙁 Actual behavior

The issue is function d—this function will always return undefined at runtime

🙂 Expected behavior

As with versions prior to 4.6.0, function d should be transpiled as:

function d() {
    return (
    // with the comment + 'as', it breaks
    x) = 1;
}

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions