Skip to content

Commit 4ac6be1

Browse files
authored
Fix Stringifier if-else constructs (#220)
* Fix space before BlockStatement in Program node * Proper spacing for if/else constructs
1 parent f8e369f commit 4ac6be1

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src-transpiler/Stringifier.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Stringifier {
224224
out += this.toSource(node);
225225
if (needCurly) {
226226
out += '\n';
227-
out += spaces;
227+
out += spaces.slice(0, -2);
228228
out += '}';
229229
}
230230
return out;
@@ -566,7 +566,10 @@ class Stringifier {
566566
const {body, directives} = node;
567567
const spaces = this.spaces;
568568
let out = '';
569-
out += ' {';
569+
if (this.parentType !== 'Program') {
570+
out += ' ';
571+
}
572+
out += '{';
570573
this.numSpaces++;
571574
// Handle Directive/DirectiveLiteral like 'use strict';
572575
if (directives && directives.length) {
@@ -575,7 +578,7 @@ class Stringifier {
575578
}
576579
out += this.generateTypeChecks(node);
577580
if (body.length) {
578-
if (out.length === 2) { // 2 is ' {'
581+
if (out[out.length-1] === '{') {
579582
out += '\n';
580583
}
581584
// out += '/*'+out.length+'*/';
@@ -632,16 +635,26 @@ class Stringifier {
632635
*/
633636
IfStatement(node) {
634637
const {alternate, consequent, test} = node;
635-
const spaces = this.spaces;
636638
let out = '';
637-
//if (this.parentType !== "IfStatement")
638-
//{
639-
out += spaces;
640-
//}
639+
if (this.forceCurly) {
640+
out += this.spaces;
641+
} else {
642+
if (this.parentType === 'BlockStatement') {
643+
out += this.spaces;
644+
} else if (this.parentType === 'IfStatement') {
645+
out += ' ';
646+
}
647+
}
641648
out += `if (${this.toSource(test)})`;
642649
out += this.toSourceCurly(consequent);
643650
if (alternate) {
651+
if (this.forceCurly) {
652+
this.numSpaces++;
653+
}
644654
out += ` else${this.toSourceCurly(alternate)}`;
655+
if (this.forceCurly) {
656+
this.numSpaces--;
657+
}
645658
}
646659
return out;
647660
}

0 commit comments

Comments
 (0)