TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

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

Open toyobayashi opened this issue 3 years ago • 1 comments

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
    }
}

toyobayashi avatar Aug 06 '22 09:08 toyobayashi

Similar buggy behaviors:

whzx5byb avatar Aug 10 '22 00:08 whzx5byb

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.

toyobayashi avatar Nov 18 '22 15:11 toyobayashi

@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.

RyanCavanaugh avatar Nov 19 '22 00:11 RyanCavanaugh

@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.

toyobayashi avatar Nov 19 '22 01:11 toyobayashi