Skip to content
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

Repeated single line comment after variable declaration in if/for block when targeting ES5 #50209

Open
toyobayashi opened this issue Aug 6, 2022 · 4 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@toyobayashi
Copy link

toyobayashi commented Aug 6, 2022

Bug Report

πŸ”Ž Search Terms

comment

πŸ•— Version & Regression Information

  • This changed between versions 4.6.4 and 4.7.4

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

let x: number
// #if MEMORY64
x = 8
// #else
x = 4
// #endif

function f (b: boolean) {
  if (b) {
    let y: number
    // #if MEMORY64
    y = 8
    // #else
    y = 4
    // #endif
  }

  for (let i = 0; i < 10; ++i) {
    let z: number
    // #if MEMORY64
    z = 8
    // #else
    z = 4
    // #endif
  }
}

πŸ™ Actual behavior

Output JavaScript using typescript 4.7.4

"use strict";
var x;
// #if MEMORY64
x = 8;
// #else
x = 4;
// #endif
function f(b) {
    if (b) {
        var y 
        // #if MEMORY64               // <----- ???
        = void 0;
        // #if MEMORY64
        y = 8;
        // #else
        y = 4;
        // #endif
    }
    for (var i = 0; i < 10; ++i) {
        var z 
        // #if MEMORY64               // <----- ???
        = void 0;
        // #if MEMORY64
        z = 8;
        // #else
        z = 4;
        // #endif
    }
}

πŸ™‚ Expected behavior

Output JavaScript using typescript 4.6.4

"use strict";
var x;
// #if MEMORY64
x = 8;
// #else
x = 4;
// #endif
function f(b) {
    if (b) {
        var y = void 0;
        // #if MEMORY64
        y = 8;
        // #else
        y = 4;
        // #endif
    }
    for (var i = 0; i < 10; ++i) {
        var z = void 0;
        // #if MEMORY64
        z = 8;
        // #else
        z = 4;
        // #endif
    }
}
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Aug 8, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 8, 2022
@whzx5byb
Copy link

whzx5byb commented Aug 10, 2022

Similar buggy behaviors:

@toyobayashi
Copy link
Author

Any progress on this? I try typescript 4.9 still experienced this problem, I can not update typescript version due to it break my code which is relying on preprocessing those comments.

@RyanCavanaugh
Copy link
Member

@toyobayashi this isn't assigned for any milestone -- in other words, we don't plan to fix it ourselves. If you need to do file-level preprocessing I'd strongly recommend processing the TS file before it gets transpiled.

@toyobayashi
Copy link
Author

toyobayashi commented Nov 19, 2022

@RyanCavanaugh

I'd strongly recommend processing the TS file before it gets transpiled.

My transpile output target is Emscripten's JavaScript library file (example), it can contain those #if / #else / #endif:

mergeInto(LibraryManager.library, {
  getPointerSize: function () {
#if MEMORY64
    return 8
#else
    return 4
#endif
  }
})

tsc can not transpile files which include directives, so I make them be comments, after tsc output JavaScript, then just remove the // by using Node.js script. I need to keep directives in the output JavaScript files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants