2019-09-27 23:42:02 +08:00
|
|
|
import { isString } from '@vue/shared'
|
|
|
|
|
2019-09-17 23:07:46 +08:00
|
|
|
// Vue template is a platform-agnostic superset of HTML (syntax only).
|
|
|
|
// More namespaces like SVG and MathML are declared by platform specific
|
|
|
|
// compilers.
|
|
|
|
export type Namespace = number
|
|
|
|
|
|
|
|
export const enum Namespaces {
|
|
|
|
HTML
|
|
|
|
}
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
export const enum NodeTypes {
|
2019-09-18 07:08:47 +08:00
|
|
|
ROOT,
|
|
|
|
ELEMENT,
|
2019-09-17 02:43:29 +08:00
|
|
|
TEXT,
|
|
|
|
COMMENT,
|
2019-09-27 23:42:02 +08:00
|
|
|
SIMPLE_EXPRESSION,
|
|
|
|
INTERPOLATION,
|
2019-09-18 07:08:47 +08:00
|
|
|
ATTRIBUTE,
|
2019-09-17 02:43:29 +08:00
|
|
|
DIRECTIVE,
|
2019-09-22 03:47:26 +08:00
|
|
|
// containers
|
2019-09-23 14:52:54 +08:00
|
|
|
COMPOUND_EXPRESSION,
|
2019-09-18 07:08:47 +08:00
|
|
|
IF,
|
|
|
|
IF_BRANCH,
|
2019-09-22 03:47:26 +08:00
|
|
|
FOR,
|
|
|
|
// codegen
|
2019-09-23 04:50:57 +08:00
|
|
|
JS_CALL_EXPRESSION,
|
|
|
|
JS_OBJECT_EXPRESSION,
|
|
|
|
JS_PROPERTY,
|
2019-09-28 10:25:32 +08:00
|
|
|
JS_ARRAY_EXPRESSION,
|
2019-10-02 04:48:20 +08:00
|
|
|
JS_FUNCTION_EXPRESSION,
|
2019-10-01 21:27:34 +08:00
|
|
|
JS_SEQUENCE_EXPRESSION,
|
|
|
|
JS_CONDITIONAL_EXPRESSION
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const enum ElementTypes {
|
|
|
|
ELEMENT,
|
|
|
|
COMPONENT,
|
2019-09-18 07:08:47 +08:00
|
|
|
SLOT,
|
|
|
|
TEMPLATE
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface Node {
|
|
|
|
type: NodeTypes
|
|
|
|
loc: SourceLocation
|
|
|
|
}
|
|
|
|
|
2019-09-22 03:47:26 +08:00
|
|
|
// The node's range. The `start` is inclusive and `end` is exclusive.
|
|
|
|
// [start, end)
|
|
|
|
export interface SourceLocation {
|
|
|
|
start: Position
|
|
|
|
end: Position
|
|
|
|
source: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Position {
|
|
|
|
offset: number // from start of file
|
|
|
|
line: number
|
|
|
|
column: number
|
|
|
|
}
|
|
|
|
|
2019-09-18 07:08:47 +08:00
|
|
|
export type ParentNode = RootNode | ElementNode | IfBranchNode | ForNode
|
2019-09-22 03:47:26 +08:00
|
|
|
|
2019-09-27 23:42:02 +08:00
|
|
|
export type ExpressionNode = SimpleExpressionNode | CompoundExpressionNode
|
|
|
|
|
2019-10-01 21:27:34 +08:00
|
|
|
export type TemplateChildNode =
|
2019-09-18 07:08:47 +08:00
|
|
|
| ElementNode
|
2019-09-27 23:42:02 +08:00
|
|
|
| InterpolationNode
|
2019-10-01 02:51:55 +08:00
|
|
|
| CompoundExpressionNode
|
2019-09-18 07:08:47 +08:00
|
|
|
| TextNode
|
|
|
|
| CommentNode
|
|
|
|
| IfNode
|
|
|
|
| ForNode
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
export interface RootNode extends Node {
|
|
|
|
type: NodeTypes.ROOT
|
2019-10-01 21:27:34 +08:00
|
|
|
children: TemplateChildNode[]
|
2019-09-23 11:07:36 +08:00
|
|
|
imports: string[]
|
|
|
|
statements: string[]
|
2019-09-26 02:13:33 +08:00
|
|
|
hoists: JSChildNode[]
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: TemplateChildNode | JSChildNode | undefined
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ElementNode extends Node {
|
|
|
|
type: NodeTypes.ELEMENT
|
2019-09-17 23:07:46 +08:00
|
|
|
ns: Namespace
|
2019-09-17 02:43:29 +08:00
|
|
|
tag: string
|
|
|
|
tagType: ElementTypes
|
|
|
|
isSelfClosing: boolean
|
2019-09-22 05:42:12 +08:00
|
|
|
props: Array<AttributeNode | DirectiveNode>
|
2019-10-01 21:27:34 +08:00
|
|
|
children: TemplateChildNode[]
|
2019-09-22 03:47:26 +08:00
|
|
|
codegenNode: CallExpression | undefined
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface TextNode extends Node {
|
|
|
|
type: NodeTypes.TEXT
|
|
|
|
content: string
|
|
|
|
isEmpty: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CommentNode extends Node {
|
|
|
|
type: NodeTypes.COMMENT
|
|
|
|
content: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AttributeNode extends Node {
|
|
|
|
type: NodeTypes.ATTRIBUTE
|
|
|
|
name: string
|
|
|
|
value: TextNode | undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DirectiveNode extends Node {
|
|
|
|
type: NodeTypes.DIRECTIVE
|
|
|
|
name: string
|
|
|
|
exp: ExpressionNode | undefined
|
|
|
|
arg: ExpressionNode | undefined
|
|
|
|
modifiers: string[]
|
|
|
|
}
|
|
|
|
|
2019-09-27 23:42:02 +08:00
|
|
|
export interface SimpleExpressionNode extends Node {
|
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION
|
2019-09-17 02:43:29 +08:00
|
|
|
content: string
|
|
|
|
isStatic: boolean
|
2019-09-28 12:19:24 +08:00
|
|
|
// an expression parsed as the params of a function will track
|
|
|
|
// the identifiers declared inside the function body.
|
|
|
|
identifiers?: string[]
|
2019-09-27 23:42:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface InterpolationNode extends Node {
|
|
|
|
type: NodeTypes.INTERPOLATION
|
|
|
|
content: ExpressionNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// always dynamic
|
|
|
|
export interface CompoundExpressionNode extends Node {
|
|
|
|
type: NodeTypes.COMPOUND_EXPRESSION
|
2019-10-01 02:51:55 +08:00
|
|
|
children: (SimpleExpressionNode | InterpolationNode | TextNode | string)[]
|
2019-09-28 12:19:24 +08:00
|
|
|
// an expression parsed as the params of a function will track
|
|
|
|
// the identifiers declared inside the function body.
|
|
|
|
identifiers?: string[]
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
2019-09-18 07:08:47 +08:00
|
|
|
export interface IfNode extends Node {
|
|
|
|
type: NodeTypes.IF
|
|
|
|
branches: IfBranchNode[]
|
2019-10-02 00:25:13 +08:00
|
|
|
codegenNode: SequenceExpression
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IfBranchNode extends Node {
|
|
|
|
type: NodeTypes.IF_BRANCH
|
|
|
|
condition: ExpressionNode | undefined // else
|
2019-10-01 21:27:34 +08:00
|
|
|
children: TemplateChildNode[]
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ForNode extends Node {
|
|
|
|
type: NodeTypes.FOR
|
|
|
|
source: ExpressionNode
|
2019-09-29 04:02:08 +08:00
|
|
|
valueAlias: ExpressionNode | undefined
|
|
|
|
keyAlias: ExpressionNode | undefined
|
|
|
|
objectIndexAlias: ExpressionNode | undefined
|
2019-10-01 21:27:34 +08:00
|
|
|
children: TemplateChildNode[]
|
2019-10-02 22:47:01 +08:00
|
|
|
codegenNode: SequenceExpression
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
2019-09-23 04:50:57 +08:00
|
|
|
// We also include a number of JavaScript AST nodes for code generation.
|
|
|
|
// The AST is an intentioanlly minimal subset just to meet the exact needs of
|
2019-09-22 03:47:26 +08:00
|
|
|
// Vue render function generation.
|
2019-09-23 04:50:57 +08:00
|
|
|
export type JSChildNode =
|
2019-09-22 03:47:26 +08:00
|
|
|
| CallExpression
|
|
|
|
| ObjectExpression
|
|
|
|
| ArrayExpression
|
|
|
|
| ExpressionNode
|
2019-10-02 04:48:20 +08:00
|
|
|
| FunctionExpression
|
2019-10-01 21:27:34 +08:00
|
|
|
| ConditionalExpression
|
|
|
|
| SequenceExpression
|
2019-09-22 03:47:26 +08:00
|
|
|
|
|
|
|
export interface CallExpression extends Node {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_CALL_EXPRESSION
|
2019-10-01 09:17:12 +08:00
|
|
|
callee: string
|
2019-10-02 11:53:52 +08:00
|
|
|
arguments: (string | JSChildNode | TemplateChildNode | TemplateChildNode[])[]
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
2019-09-22 03:47:26 +08:00
|
|
|
export interface ObjectExpression extends Node {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_OBJECT_EXPRESSION
|
2019-09-22 03:47:26 +08:00
|
|
|
properties: Array<Property>
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Property extends Node {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_PROPERTY
|
2019-09-22 03:47:26 +08:00
|
|
|
key: ExpressionNode
|
2019-09-26 00:39:46 +08:00
|
|
|
value: JSChildNode
|
2019-09-22 03:47:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ArrayExpression extends Node {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_ARRAY_EXPRESSION
|
|
|
|
elements: Array<string | JSChildNode>
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
2019-09-22 05:42:12 +08:00
|
|
|
|
2019-10-02 04:48:20 +08:00
|
|
|
export interface FunctionExpression extends Node {
|
|
|
|
type: NodeTypes.JS_FUNCTION_EXPRESSION
|
|
|
|
params: ExpressionNode | ExpressionNode[] | undefined
|
2019-10-02 11:19:48 +08:00
|
|
|
returns: TemplateChildNode | TemplateChildNode[] | JSChildNode
|
2019-10-02 04:48:20 +08:00
|
|
|
newline: boolean
|
2019-10-01 21:27:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SequenceExpression extends Node {
|
|
|
|
type: NodeTypes.JS_SEQUENCE_EXPRESSION
|
|
|
|
expressions: JSChildNode[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ConditionalExpression extends Node {
|
|
|
|
type: NodeTypes.JS_CONDITIONAL_EXPRESSION
|
|
|
|
test: ExpressionNode
|
|
|
|
consequent: JSChildNode
|
|
|
|
alternate: JSChildNode
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
|
|
|
|
2019-10-02 00:25:13 +08:00
|
|
|
// AST Utilities ---------------------------------------------------------------
|
|
|
|
|
|
|
|
// Some expressions, e.g. sequence and conditional expressions, are never
|
|
|
|
// associated with template nodes, so their source locations are just a stub.
|
|
|
|
// Container types like CompoundExpression also don't need a real location.
|
2019-10-02 03:43:59 +08:00
|
|
|
export const locStub: SourceLocation = {
|
2019-10-02 00:25:13 +08:00
|
|
|
source: '',
|
|
|
|
start: { line: 1, column: 1, offset: 0 },
|
|
|
|
end: { line: 1, column: 1, offset: 0 }
|
|
|
|
}
|
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
export function createArrayExpression(
|
|
|
|
elements: ArrayExpression['elements'],
|
2019-10-02 00:25:13 +08:00
|
|
|
loc: SourceLocation = locStub
|
2019-09-22 05:42:12 +08:00
|
|
|
): ArrayExpression {
|
|
|
|
return {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_ARRAY_EXPRESSION,
|
2019-09-22 05:42:12 +08:00
|
|
|
loc,
|
|
|
|
elements
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createObjectExpression(
|
2019-10-02 04:48:20 +08:00
|
|
|
properties: ObjectExpression['properties'],
|
2019-10-02 00:25:13 +08:00
|
|
|
loc: SourceLocation = locStub
|
2019-09-22 05:42:12 +08:00
|
|
|
): ObjectExpression {
|
|
|
|
return {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_OBJECT_EXPRESSION,
|
2019-09-22 05:42:12 +08:00
|
|
|
loc,
|
|
|
|
properties
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createObjectProperty(
|
2019-10-02 04:48:20 +08:00
|
|
|
key: Property['key'],
|
|
|
|
value: Property['value']
|
2019-09-22 05:42:12 +08:00
|
|
|
): Property {
|
|
|
|
return {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_PROPERTY,
|
2019-10-02 00:25:13 +08:00
|
|
|
loc: locStub,
|
2019-09-22 05:42:12 +08:00
|
|
|
key,
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 23:42:02 +08:00
|
|
|
export function createSimpleExpression(
|
2019-10-02 04:48:20 +08:00
|
|
|
content: SimpleExpressionNode['content'],
|
|
|
|
isStatic: SimpleExpressionNode['isStatic'],
|
2019-10-02 00:25:13 +08:00
|
|
|
loc: SourceLocation = locStub
|
2019-09-27 23:42:02 +08:00
|
|
|
): SimpleExpressionNode {
|
2019-09-22 05:42:12 +08:00
|
|
|
return {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
2019-09-22 05:42:12 +08:00
|
|
|
loc,
|
|
|
|
content,
|
2019-09-27 23:42:02 +08:00
|
|
|
isStatic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createInterpolation(
|
2019-10-02 04:48:20 +08:00
|
|
|
content: InterpolationNode['content'] | string,
|
2019-09-27 23:42:02 +08:00
|
|
|
loc: SourceLocation
|
|
|
|
): InterpolationNode {
|
|
|
|
return {
|
|
|
|
type: NodeTypes.INTERPOLATION,
|
|
|
|
loc,
|
|
|
|
content: isString(content)
|
|
|
|
? createSimpleExpression(content, false, loc)
|
|
|
|
: content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createCompoundExpression(
|
2019-10-02 00:25:13 +08:00
|
|
|
children: CompoundExpressionNode['children']
|
2019-09-27 23:42:02 +08:00
|
|
|
): CompoundExpressionNode {
|
|
|
|
return {
|
|
|
|
type: NodeTypes.COMPOUND_EXPRESSION,
|
2019-10-02 00:25:13 +08:00
|
|
|
loc: locStub,
|
2019-09-27 23:42:02 +08:00
|
|
|
children
|
2019-09-22 05:42:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createCallExpression(
|
2019-10-02 04:48:20 +08:00
|
|
|
callee: CallExpression['callee'],
|
2019-10-02 00:25:13 +08:00
|
|
|
args: CallExpression['arguments'] = [],
|
|
|
|
loc: SourceLocation = locStub
|
2019-09-22 05:42:12 +08:00
|
|
|
): CallExpression {
|
|
|
|
return {
|
2019-09-23 04:50:57 +08:00
|
|
|
type: NodeTypes.JS_CALL_EXPRESSION,
|
2019-09-22 05:42:12 +08:00
|
|
|
loc,
|
|
|
|
callee,
|
|
|
|
arguments: args
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 10:25:32 +08:00
|
|
|
|
|
|
|
export function createFunctionExpression(
|
2019-10-02 04:48:20 +08:00
|
|
|
params: FunctionExpression['params'],
|
|
|
|
returns: FunctionExpression['returns'],
|
|
|
|
newline: boolean = false,
|
2019-10-02 00:25:13 +08:00
|
|
|
loc: SourceLocation = locStub
|
2019-10-02 04:48:20 +08:00
|
|
|
): FunctionExpression {
|
2019-09-28 10:25:32 +08:00
|
|
|
return {
|
2019-10-02 04:48:20 +08:00
|
|
|
type: NodeTypes.JS_FUNCTION_EXPRESSION,
|
2019-09-28 10:25:32 +08:00
|
|
|
params,
|
|
|
|
returns,
|
2019-10-02 04:48:20 +08:00
|
|
|
newline,
|
2019-09-28 10:25:32 +08:00
|
|
|
loc
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 21:27:34 +08:00
|
|
|
|
2019-10-02 00:25:13 +08:00
|
|
|
export function createSequenceExpression(
|
2019-10-02 04:48:20 +08:00
|
|
|
expressions: SequenceExpression['expressions']
|
2019-10-02 00:25:13 +08:00
|
|
|
): SequenceExpression {
|
2019-10-01 21:27:34 +08:00
|
|
|
return {
|
|
|
|
type: NodeTypes.JS_SEQUENCE_EXPRESSION,
|
|
|
|
expressions,
|
|
|
|
loc: locStub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createConditionalExpression(
|
2019-10-02 04:48:20 +08:00
|
|
|
test: ConditionalExpression['test'],
|
|
|
|
consequent: ConditionalExpression['consequent'],
|
|
|
|
alternate: ConditionalExpression['alternate']
|
2019-10-02 00:25:13 +08:00
|
|
|
): ConditionalExpression {
|
2019-10-01 21:27:34 +08:00
|
|
|
return {
|
|
|
|
type: NodeTypes.JS_CONDITIONAL_EXPRESSION,
|
|
|
|
test,
|
|
|
|
consequent,
|
|
|
|
alternate,
|
|
|
|
loc: locStub
|
|
|
|
}
|
|
|
|
}
|