|
|
@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
import repeat from './repeat';
|
|
|
|
|
|
|
|
|
|
|
|
enum ChunkType {
|
|
|
|
enum ChunkType {
|
|
|
|
Line,
|
|
|
|
Line,
|
|
|
|
Block
|
|
|
|
Block
|
|
|
@ -8,6 +10,8 @@ export default class CodeBuilder {
|
|
|
|
first: ChunkType;
|
|
|
|
first: ChunkType;
|
|
|
|
last: ChunkType;
|
|
|
|
last: ChunkType;
|
|
|
|
lastCondition: string;
|
|
|
|
lastCondition: string;
|
|
|
|
|
|
|
|
conditionStack: string[];
|
|
|
|
|
|
|
|
indent: string;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(str = '') {
|
|
|
|
constructor(str = '') {
|
|
|
|
this.result = str;
|
|
|
|
this.result = str;
|
|
|
@ -19,19 +23,21 @@ export default class CodeBuilder {
|
|
|
|
this.last = initial;
|
|
|
|
this.last = initial;
|
|
|
|
|
|
|
|
|
|
|
|
this.lastCondition = null;
|
|
|
|
this.lastCondition = null;
|
|
|
|
|
|
|
|
this.conditionStack = [];
|
|
|
|
|
|
|
|
this.indent = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addConditional(condition: string, body: string) {
|
|
|
|
addConditional(condition: string, body: string) {
|
|
|
|
body = body.replace(/^/gm, '\t');
|
|
|
|
body = body.replace(/^/gm, `${this.indent}\t`);
|
|
|
|
|
|
|
|
|
|
|
|
if (condition === this.lastCondition) {
|
|
|
|
if (condition === this.lastCondition) {
|
|
|
|
this.result += `\n${body}`;
|
|
|
|
this.result += `\n${body}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (this.lastCondition) {
|
|
|
|
if (this.lastCondition) {
|
|
|
|
this.result += `\n}`;
|
|
|
|
this.result += `\n${this.indent}}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.result += `${this.last === ChunkType.Block ? '\n\n' : '\n'}if ( ${condition} ) {\n${body}`;
|
|
|
|
this.result += `${this.last === ChunkType.Block ? '\n\n' : '\n'}${this.indent}if ( ${condition} ) {\n${body}`;
|
|
|
|
this.lastCondition = condition;
|
|
|
|
this.lastCondition = condition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -40,14 +46,14 @@ export default class CodeBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
addLine(line: string) {
|
|
|
|
addLine(line: string) {
|
|
|
|
if (this.lastCondition) {
|
|
|
|
if (this.lastCondition) {
|
|
|
|
this.result += `\n}`;
|
|
|
|
this.result += `\n${this.indent}}`;
|
|
|
|
this.lastCondition = null;
|
|
|
|
this.lastCondition = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.last === ChunkType.Block) {
|
|
|
|
if (this.last === ChunkType.Block) {
|
|
|
|
this.result += `\n\n${line}`;
|
|
|
|
this.result += `\n\n${this.indent}${line}`;
|
|
|
|
} else if (this.last === ChunkType.Line) {
|
|
|
|
} else if (this.last === ChunkType.Line) {
|
|
|
|
this.result += `\n${line}`;
|
|
|
|
this.result += `\n${this.indent}${line}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.result += line;
|
|
|
|
this.result += line;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -58,9 +64,9 @@ export default class CodeBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
addLineAtStart(line: string) {
|
|
|
|
addLineAtStart(line: string) {
|
|
|
|
if (this.first === ChunkType.Block) {
|
|
|
|
if (this.first === ChunkType.Block) {
|
|
|
|
this.result = `${line}\n\n${this.result}`;
|
|
|
|
this.result = `${line}\n\n${this.indent}${this.result}`;
|
|
|
|
} else if (this.first === ChunkType.Line) {
|
|
|
|
} else if (this.first === ChunkType.Line) {
|
|
|
|
this.result = `${line}\n${this.result}`;
|
|
|
|
this.result = `${line}\n${this.indent}${this.result}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.result += line;
|
|
|
|
this.result += line;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -70,13 +76,15 @@ export default class CodeBuilder {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addBlock(block: string) {
|
|
|
|
addBlock(block: string) {
|
|
|
|
|
|
|
|
if (this.indent) block = block.replace(/^/gm, `${this.indent}`);
|
|
|
|
|
|
|
|
|
|
|
|
if (this.lastCondition) {
|
|
|
|
if (this.lastCondition) {
|
|
|
|
this.result += `\n}`;
|
|
|
|
this.result += `\n${this.indent}}`;
|
|
|
|
this.lastCondition = null;
|
|
|
|
this.lastCondition = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.result) {
|
|
|
|
if (this.result) {
|
|
|
|
this.result += `\n\n${block}`;
|
|
|
|
this.result += `\n\n${this.indent}${block}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.result += block;
|
|
|
|
this.result += block;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -87,7 +95,7 @@ export default class CodeBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
addBlockAtStart(block: string) {
|
|
|
|
addBlockAtStart(block: string) {
|
|
|
|
if (this.result) {
|
|
|
|
if (this.result) {
|
|
|
|
this.result = `${block}\n\n${this.result}`;
|
|
|
|
this.result = `${block}\n\n${this.indent}${this.result}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.result += block;
|
|
|
|
this.result += block;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -100,6 +108,16 @@ export default class CodeBuilder {
|
|
|
|
return this.result === '';
|
|
|
|
return this.result === '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pushCondition(condition: string) {
|
|
|
|
|
|
|
|
this.conditionStack.push(condition);
|
|
|
|
|
|
|
|
this.indent = repeat('\t', this.conditionStack.length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
popCondition() {
|
|
|
|
|
|
|
|
this.conditionStack.pop();
|
|
|
|
|
|
|
|
this.indent = repeat('\t', this.conditionStack.length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
toString() {
|
|
|
|
return this.result.trim() + (this.lastCondition ? `\n}` : ``);
|
|
|
|
return this.result.trim() + (this.lastCondition ? `\n}` : ``);
|
|
|
|
}
|
|
|
|
}
|
|
|
|