Skip to content

Commit

Permalink
feat: beautify generated code for s-bind
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 18, 2019
1 parent 87536d4 commit 9ea684d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/compilers/php-render-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,33 +650,30 @@ const elementSourceCompiler = {
})

if (bindDirective) {
emitter.writeLine(
'(function ($bindObj) use (&$html){foreach ($bindObj as $key => $value) {'
)
emitter.nextLine('(')
emitter.writeAnonymousFunction(['$bindObj'], ['&$html'], () => {
emitter.writeForeach('$bindObj as $key => $value', () => {
if (tagName === 'textarea') {
emitter.writeIf('$key == "value"', () => emitter.writeContinue())
}

if (tagName === 'textarea') {
emitter.writeIf('$key == "value"', () => {
emitter.writeLine('continue;')
emitter.writeSwitch('$key', () => {
emitter.writeCase('"readonly"')
emitter.writeCase('"disabled"')
emitter.writeCase('"multiple"', () => {
emitter.writeLine('$html .= _::boolAttrFilter($key, _::escapeHTML($value));')
emitter.writeBreak()
})
emitter.writeDefault(() => {
emitter.writeLine('$html .= _::attrFilter($key, _::escapeHTML($value));')
})
})
})
}

emitter.writeLine('switch ($key) {\n' +
'case "readonly":\n' +
'case "disabled":\n' +
'case "multiple":\n' +
'case "multiple":\n' +
'$html .= _::boolAttrFilter($key, _::escapeHTML($value));\n' +
'break;\n' +
'default:\n' +
'$html .= _::attrFilter($key, _::escapeHTML($value));' +
'}'
)
})

emitter.writeLine(
'}})(' +
ExpressionEmitter.expr(bindDirective.value) +
');'
)
emitter.write(')(')
emitter.write(ExpressionEmitter.expr(bindDirective.value))
emitter.feedLine(');')
}

emitter.bufferHTMLLiteral('>')
Expand Down Expand Up @@ -1182,7 +1179,7 @@ function genComponentContextCode (component, emitter) {
emitter.writeLine('"slotRenderers" => []')

emitter.unindent()
emitter.feedLine('];')
emitter.writeLine('];')
emitter.writeLine('$ctx->instance = _::createComponent($ctx);')
}

Expand Down
32 changes: 32 additions & 0 deletions src/emitters/php-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ export class PHPEmitter extends Emitter {
this.writeHTML(ExpressionEmitter.stringLiteralize(buffer))
}

/**
* switch
*/
public writeSwitch (expr: string, body: Function) {
this.writeLine(`switch (${expr}) {`)
this.indent()
body()
this.unindent()
this.writeLine('}')
}

public writeCase (expr: string, body: Function = () => null) {
this.writeLine(`case ${expr}:`)
this.indent()
body()
this.unindent()
}

public writeBreak () {
this.writeLine('break;')
}

public writeDefault (body: Function = () => null) {
this.writeLine('default:')
this.indent()
body()
this.unindent()
}

/**
* function
*/
Expand Down Expand Up @@ -108,6 +137,9 @@ export class PHPEmitter extends Emitter {
public endForeach () {
this.endBlock()
}
public writeContinue () {
this.writeLine('continue;')
}

/**
* block
Expand Down

0 comments on commit 9ea684d

Please sign in to comment.