Repeated single line comment after variable declaration in if/for block when targeting ES5
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
}
}
Similar buggy behaviors:
- with a semicolon and a trailing comment.
- without a type declaration (also fail in 4.6.4)
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 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
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.