UNPKG

firescript

Version:
72 lines (62 loc) 1.63 kB
const FirescriptElement = require('./FirescriptElement') /** * ConditionalExpression * * @class ConditionalExpression * @extends FirescriptElement * * interface ConditionalExpression { * type: 'ConditionalExpression'; * test: Expression; * consequent: Statement; * alternate?: Statement; * } */ class ConditionalExpression extends FirescriptElement { constructor (ast) { super(ast) this.test = this.createElement(ast.test) this.consequent = this.createElement(ast.consequent) this.alternate = this.createElement(ast.alternate) } toFSString (ctx) { const useMultiline = this.getLineLength() > 80 if (useMultiline) { return this.renderElement(this.renderMultiline(ctx)) } else { return this.renderElement(this.renderInline(ctx)) } } renderMultiline (ctx) { const test = this.test.toFSString(ctx) const indention = ctx.indent(1) const consequent = this.consequent.toFSString(ctx) const alternate = this.alternate.toFSString(ctx) ctx.indent(-1) return test + indention + '? ' + consequent + indention + ': ' + alternate + '\n' } renderInline (ctx) { const test = this.test.toFSString(ctx) const consequent = this.consequent.toFSString(ctx) const alternate = this.alternate.toFSString(ctx) return test + ' ? ' + consequent + ' : ' + alternate } getLineLength () { return this.test.getLineLength() + this.consequent.getLineLength() + this.alternate.getLineLength() + 6 } } module.exports = ConditionalExpression