Skip to content

Commit

Permalink
refactor: isRootElement, needOutputData
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jun 12, 2020
1 parent 9007876 commit b192295
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/target-js/compilers/anode-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ANodeCompiler {
)
}

compile (aNode: ANode, needOutputData: boolean) {
compile (aNode: ANode, isRootElement: boolean) {
if (TypeGuards.isATextNode(aNode)) return this.compileText(aNode)
if (TypeGuards.isAIfNode(aNode)) return this.compileIf(aNode)
if (TypeGuards.isAForNode(aNode)) return this.compileFor(aNode)
Expand All @@ -45,9 +45,9 @@ export class ANodeCompiler {
const ComponentClass = this.componentInfo.getChildComponentClass(aNode)
if (ComponentClass) {
const info = this.componentTree.addComponentClass(ComponentClass)
return info ? this.compileComponent(aNode, info, needOutputData) : undefined
return info ? this.compileComponent(aNode, info, isRootElement) : undefined
}
return this.compileElement(aNode, needOutputData)
return this.compileElement(aNode, isRootElement)
}

private compileText (aNode: ATextNode) {
Expand Down Expand Up @@ -207,9 +207,9 @@ export class ANodeCompiler {
emitter.writeLine(`ctx.slotRenderers.${rendererId}();`)
}

private compileElement (aNode: ANode, needOutputData: boolean) {
private compileElement (aNode: ANode, isRootElement: boolean) {
this.elementCompiler.tagStart(aNode)
if (needOutputData) this.outputData()
if (isRootElement && !this.ssrOnly) this.outputData()
this.elementCompiler.inner(aNode)
this.elementCompiler.tagEnd(aNode)
}
Expand All @@ -218,7 +218,7 @@ export class ANodeCompiler {
this.emitter.writeIf('!noDataOutput', () => this.emitter.writeDataComment())
}

private compileComponent (aNode: ANode, info: ComponentInfo, needOutputData: boolean) {
private compileComponent (aNode: ANode, info: ComponentInfo, isRootElement: boolean) {
const { emitter } = this

const defaultSourceSlots: ANode[] = []
Expand Down Expand Up @@ -252,7 +252,7 @@ export class ANodeCompiler {
emitter.writeLine(', ' + expr(sourceSlotCode.prop.expr) + ']);')
}

const ndo = needOutputData ? 'noDataOutput' : 'true'
const ndo = isRootElement ? 'noDataOutput' : 'true'
const funcName = 'sanssrRuntime.renderer' + info.cid
emitter.nextLine(`html += ${funcName}(`)
emitter.write(this.componentDataCode(aNode) + `, ${ndo}, sanssrRuntime, ctx, currentCtx, ` +
Expand Down
4 changes: 2 additions & 2 deletions src/target-js/compilers/element-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ElementCompiler {
emitter.writeHTMLLiteral('<' + tagName)
} else {
emitter.writeHTMLLiteral('<')
emitter.writeHTMLExpression('tagName || "div"')
emitter.writeHTMLExpression('tagName')
}

// element properties
Expand Down Expand Up @@ -146,7 +146,7 @@ export class ElementCompiler {
}
} else {
emitter.writeHTMLLiteral('</')
emitter.writeHTMLExpression('tagName || "div"')
emitter.writeHTMLExpression('tagName')
emitter.writeHTMLLiteral('>')
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/target-js/compilers/renderer-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import { ComponentInfo } from '../../models/component-info'
import { JSEmitter } from '../emitters/emitter'
import { SanData } from '../../models/san-data'
import { Renderer } from '../../models/renderer'
import { expr } from './expr-compiler'
import { stringifier } from './stringifier'
import { COMPONENT_RESERVED_MEMBERS } from '../../models/component'

// * 参数列表用于 toSource 和 toRender 两处,anode-compiler 中递归时要与此保持一致
// * 前两个参数是为了保持和最终的 renderer 兼容,如此就不需要包装
const RENDERER_ARGS = ['data = {}', 'noDataOutput', 'sanssrRuntime', 'ownerCtx', 'parentCtx', 'tagName', 'sourceSlots']
const RENDERER_ARGS = ['data = {}', 'noDataOutput', 'sanssrRuntime', 'ownerCtx', 'parentCtx', 'tagName = "div"', 'sourceSlots']

export type ExpressionEvaluator = (ctx: CompileContext) => any

Expand Down Expand Up @@ -136,13 +133,9 @@ export class RendererCompiler {
emitter.writeLine('data[$computedName] = ctx.instance.computed[$computedName].apply(ctx.instance);')
})

const ifDirective = info.rootANode.directives['if'] // eslint-disable-line dot-notation
if (ifDirective) emitter.writeLine('if (' + expr(ifDirective.value) + ') {')

const aNodeCompiler = new ANodeCompiler(info, this.componentTree, this.ssrOnly, emitter)
aNodeCompiler.compile(info.rootANode, !this.ssrOnly)
aNodeCompiler.compile(info.rootANode, true)

if (ifDirective) emitter.writeLine('}')
emitter.writeLine('return html;')
return emitter.fullText()
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/target-js/compilers/element-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('target-js/compilers/element-compiler', () => {
compiler.tagStart(aNode, 'tagName')
compiler.tagEnd(aNode, 'tagName')
expect(compiler.emitter.fullText()).toEqual(`html += "<";
html += tagName || "div";
html += tagName;
html += "></";
html += tagName || "div";
html += tagName;
html += ">";
`)
})
Expand Down

0 comments on commit b192295

Please sign in to comment.