From b19229593fb27d4074f0fc1e5dcaca22962ebcb7 Mon Sep 17 00:00:00 2001 From: harttle Date: Fri, 12 Jun 2020 19:04:16 +0800 Subject: [PATCH] refactor: isRootElement, needOutputData --- src/target-js/compilers/anode-compiler.ts | 14 +++++++------- src/target-js/compilers/element-compiler.ts | 4 ++-- src/target-js/compilers/renderer-compiler.ts | 11 ++--------- .../target-js/compilers/element-compiler.spec.ts | 4 ++-- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/target-js/compilers/anode-compiler.ts b/src/target-js/compilers/anode-compiler.ts index 5fb3dcba..2bbad781 100644 --- a/src/target-js/compilers/anode-compiler.ts +++ b/src/target-js/compilers/anode-compiler.ts @@ -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) @@ -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) { @@ -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) } @@ -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[] = [] @@ -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, ` + diff --git a/src/target-js/compilers/element-compiler.ts b/src/target-js/compilers/element-compiler.ts index 3f412634..f9ea928e 100644 --- a/src/target-js/compilers/element-compiler.ts +++ b/src/target-js/compilers/element-compiler.ts @@ -38,7 +38,7 @@ export class ElementCompiler { emitter.writeHTMLLiteral('<' + tagName) } else { emitter.writeHTMLLiteral('<') - emitter.writeHTMLExpression('tagName || "div"') + emitter.writeHTMLExpression('tagName') } // element properties @@ -146,7 +146,7 @@ export class ElementCompiler { } } else { emitter.writeHTMLLiteral('') } } diff --git a/src/target-js/compilers/renderer-compiler.ts b/src/target-js/compilers/renderer-compiler.ts index 44e778dc..1fd5ed20 100644 --- a/src/target-js/compilers/renderer-compiler.ts +++ b/src/target-js/compilers/renderer-compiler.ts @@ -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 @@ -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() } diff --git a/test/unit/target-js/compilers/element-compiler.spec.ts b/test/unit/target-js/compilers/element-compiler.spec.ts index 9788f479..64330f4e 100644 --- a/test/unit/target-js/compilers/element-compiler.spec.ts +++ b/test/unit/target-js/compilers/element-compiler.spec.ts @@ -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 += ">"; `) })