Skip to content

Commit

Permalink
fix: renderOnly should backward compatible with old version (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg authored May 31, 2023
1 parent f377ba9 commit a989bb4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/ast/renderer-ast-dfn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export enum SyntaxKind {
RegexpReplace = 28,
JSONStringify = 29,
HelperCall = 30,
GetRootCtxCall = 31,
ComponentReferenceLiteral = 32,
SlotRendererDefinition = 33,
SlotRenderCall = 34,
Expand All @@ -72,7 +73,7 @@ export enum SyntaxKind {
export type Expression = Identifier | FunctionDefinition | Literal | BinaryExpression | UnaryExpression |
CreateComponentInstance | NewExpression | MapLiteral | ComponentRendererReference | FunctionCall | Null |
Undefined | MapAssign | ArrayIncludes | ConditionalExpression | FilterCall | HelperCall | EncodeURIComponent |
ArrayLiteral | RegexpReplace | JSONStringify | ComputedCall | ComponentReferenceLiteral |
ArrayLiteral | RegexpReplace | JSONStringify | ComputedCall | GetRootCtxCall | ComponentReferenceLiteral |
SlotRendererDefinition | SlotRenderCall | ComponentClassReference | CreateComponentPrototype | Typeof

export type Statement = ReturnStatement | ImportHelper | VariableDefinition | AssignmentStatement | If | ElseIf | Else |
Expand All @@ -85,7 +86,7 @@ export type UnaryOperator = '!' | '~' | '+' | '()' | '-'
export class ArrayLiteral implements SyntaxNode {
public readonly kind = SyntaxKind.ArrayLiteral
constructor (
public items: [Expression, boolean][] // [item, isSpread]
public items: [Expression, boolean][]
) {}
}

Expand Down Expand Up @@ -306,6 +307,13 @@ export class FilterCall implements SyntaxNode {
) {}
}

export class GetRootCtxCall implements SyntaxNode {
public readonly kind = SyntaxKind.GetRootCtxCall
constructor (
public args: Expression[]
) {}
}

export class HelperCall implements SyntaxNode {
public readonly kind = SyntaxKind.HelperCall
constructor (
Expand Down
1 change: 1 addition & 0 deletions src/ast/renderer-ast-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function * walk (node: Expression | Statement): Iterable<Expression | Sta
yield * walk(node.trueValue)
break
case SyntaxKind.FilterCall:
case SyntaxKind.GetRootCtxCall:
case SyntaxKind.HelperCall:
for (const arg of node.args) yield * walk(arg)
break
Expand Down
6 changes: 4 additions & 2 deletions src/compilers/anode-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as TypeGuards from '../ast/san-ast-type-guards'
import { IDGenerator } from '../utils/id-generator'
import {
JSONStringify, RegexpReplace, Statement, SlotRendererDefinition, ElseIf, Else, MapAssign, Foreach, If, MapLiteral,
ComponentRendererReference, FunctionCall, SlotRenderCall, Expression, ComponentReferenceLiteral,
ComponentRendererReference, FunctionCall, SlotRenderCall, Expression, GetRootCtxCall, ComponentReferenceLiteral,
ComponentClassReference,
VariableDefinition,
ConditionalExpression,
Expand Down Expand Up @@ -253,7 +253,9 @@ export class ANodeCompiler {
BINARY(
BINARY(I('info'), '.', I('rootOutputData')),
'||',
BINARY(I('ctx'), '.', I('data'))

// 这里保留 GetRootCtxCall 是为了兼容与老版本 san-ssr 的编译产物混用的情况
BINARY(new GetRootCtxCall([I('ctx')]), '.', I('data'))
)
)
const outputDataExpr = BINARY(I('info'), '.', I('outputData'))
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/underscore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ function createInstanceFromClass (Clazz: Component<{}> & ComponentDefineOptions)
return instance
}

function getRootCtx<T extends {parentCtx?: T}> (ctx: T) {
let last = ctx
while (ctx.parentCtx) {
last = ctx
ctx = ctx.parentCtx
}

// 如果跟组件 render 调用的时候传递了 parentCtx,会找到这个对象
// 通过 ctx 是否有 data 来判断是不是真正的 rootCtx
// @ts-ignore
return ctx.data ? ctx : last
}

function handleError (e: Error, instance: Component<{}>, info: string) {
let current: Component<{}> | undefined = instance
while (current) {
Expand Down Expand Up @@ -221,6 +234,7 @@ export const _ = {
xstyleFilter,
xclassFilter,
createFromPrototype,
getRootCtx,
iterate,
callFilter,
callComputed,
Expand Down
5 changes: 5 additions & 0 deletions src/target-js/js-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export class JSEmitter extends Emitter {
this.writeExpressionList(node.args)
this.write(')')
break
case SyntaxKind.GetRootCtxCall:
this.write('_.getRootCtx(')
this.writeExpressionList(node.args)
this.write(')')
break
case SyntaxKind.HelperCall:
this.write(`_.${node.name}(`)
this.writeExpressionList(node.args)
Expand Down

0 comments on commit a989bb4

Please sign in to comment.