Skip to content

Commit

Permalink
feat: support custom output data
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Apr 20, 2022
1 parent 230c7ac commit e3b8664
Show file tree
Hide file tree
Showing 4 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 @@ -66,14 +66,15 @@ export enum SyntaxKind {
TryStatement = 36,
CatchClause = 37,
ComponentClassReference = 38,
CreateComponentPrototype = 39
CreateComponentPrototype = 39,
Typeof = 40
}

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 | GetRootCtxCall | ComponentReferenceLiteral |
SlotRendererDefinition | SlotRenderCall | ComponentClassReference | CreateComponentPrototype
SlotRendererDefinition | SlotRenderCall | ComponentClassReference | CreateComponentPrototype | Typeof

export type Statement = ReturnStatement | ImportHelper | VariableDefinition | AssignmentStatement | If | ElseIf | Else |
Foreach | ExpressionStatement | TryStatement
Expand Down Expand Up @@ -425,3 +426,10 @@ export class ReturnStatement implements SyntaxNode {
public value: Expression
) {}
}

export class Typeof implements SyntaxNode {
public readonly kind = SyntaxKind.Typeof
constructor (
public value: Expression
) {}
}
1 change: 1 addition & 0 deletions src/ast/renderer-ast-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function * walk (node: Expression | Statement): Iterable<Expression | Sta
case SyntaxKind.EncodeURIComponent:
case SyntaxKind.ReturnStatement:
case SyntaxKind.ExpressionStatement:
case SyntaxKind.Typeof:
yield * walk(node.value)
break
case SyntaxKind.Literal:
Expand Down
20 changes: 18 additions & 2 deletions src/compilers/anode-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { IDGenerator } from '../utils/id-generator'
import {
JSONStringify, RegexpReplace, Statement, SlotRendererDefinition, ElseIf, Else, MapAssign, Foreach, If, MapLiteral,
ComponentRendererReference, FunctionCall, SlotRenderCall, Expression, GetRootCtxCall, ComponentReferenceLiteral,
ComponentClassReference
ComponentClassReference,
VariableDefinition,
ConditionalExpression,
Typeof,
AssignmentStatement
} from '../ast/renderer-ast-dfn'
import {
CTX_DATA, createHTMLExpressionAppend, createHTMLLiteralAppend, L, I, ASSIGN, STATEMENT, UNARY, DEF, BINARY, RETURN
Expand Down Expand Up @@ -211,9 +215,21 @@ export class ANodeCompiler {

private createDataComment () {
const dataExpr = BINARY(new GetRootCtxCall([I('ctx')]), '.', I('data'))
const outputDataExpr = BINARY(I('info'), '.', I('outputData'))
return [
new VariableDefinition('data', dataExpr),
new If(outputDataExpr, [
new AssignmentStatement(
I('data'),
new ConditionalExpression(
BINARY(new Typeof(outputDataExpr), '===', L('function')),
new FunctionCall(outputDataExpr, [dataExpr]),
outputDataExpr
)
)
]),
createHTMLLiteralAppend('<!--s-data:'),
createHTMLExpressionAppend(new RegexpReplace(new JSONStringify(dataExpr), '(?<=-)-', L('\\-'))),
createHTMLExpressionAppend(new RegexpReplace(new JSONStringify(I('data')), '(?<=-)-', L('\\-'))),
createHTMLLiteralAppend('-->')
]
}
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 @@ -209,6 +209,11 @@ export class JSEmitter extends Emitter {
this.write(') ')
this.writeBlockStatements(node.handler.body)
break
case SyntaxKind.Typeof:
this.write('typeof (')
this.writeSyntaxNode(node.value)
this.write(')')
break
default: assertNever(node)
}
}
Expand Down

0 comments on commit e3b8664

Please sign in to comment.