Skip to content

Commit

Permalink
Fix Stringifier if-else constructs (#220)
Browse files Browse the repository at this point in the history
* Fix space before BlockStatement in Program node

* Proper spacing for if/else constructs
  • Loading branch information
kungfooman authored Nov 12, 2024
1 parent f8e369f commit 4ac6be1
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src-transpiler/Stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Stringifier {
out += this.toSource(node);
if (needCurly) {
out += '\n';
out += spaces;
out += spaces.slice(0, -2);
out += '}';
}
return out;
Expand Down Expand Up @@ -566,7 +566,10 @@ class Stringifier {
const {body, directives} = node;
const spaces = this.spaces;
let out = '';
out += ' {';
if (this.parentType !== 'Program') {
out += ' ';
}
out += '{';
this.numSpaces++;
// Handle Directive/DirectiveLiteral like 'use strict';
if (directives && directives.length) {
Expand All @@ -575,7 +578,7 @@ class Stringifier {
}
out += this.generateTypeChecks(node);
if (body.length) {
if (out.length === 2) { // 2 is ' {'
if (out[out.length-1] === '{') {
out += '\n';
}
// out += '/*'+out.length+'*/';
Expand Down Expand Up @@ -632,16 +635,26 @@ class Stringifier {
*/
IfStatement(node) {
const {alternate, consequent, test} = node;
const spaces = this.spaces;
let out = '';
//if (this.parentType !== "IfStatement")
//{
out += spaces;
//}
if (this.forceCurly) {
out += this.spaces;
} else {
if (this.parentType === 'BlockStatement') {
out += this.spaces;
} else if (this.parentType === 'IfStatement') {
out += ' ';
}
}
out += `if (${this.toSource(test)})`;
out += this.toSourceCurly(consequent);
if (alternate) {
if (this.forceCurly) {
this.numSpaces++;
}
out += ` else${this.toSourceCurly(alternate)}`;
if (this.forceCurly) {
this.numSpaces--;
}
}
return out;
}
Expand Down

0 comments on commit 4ac6be1

Please sign in to comment.