ast.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. import { isString } from '@vue/shared'
  2. import { ForParseResult } from './transforms/vFor'
  3. import {
  4. RENDER_SLOT,
  5. CREATE_SLOTS,
  6. RENDER_LIST,
  7. OPEN_BLOCK,
  8. FRAGMENT,
  9. WITH_DIRECTIVES,
  10. WITH_MEMO
  11. } from './runtimeHelpers'
  12. import { PropsExpression } from './transforms/transformElement'
  13. import { ImportItem, TransformContext } from './transform'
  14. import { getVNodeBlockHelper, getVNodeHelper } from './utils'
  15. // Vue template is a platform-agnostic superset of HTML (syntax only).
  16. // More namespaces like SVG and MathML are declared by platform specific
  17. // compilers.
  18. export type Namespace = number
  19. export const enum Namespaces {
  20. HTML
  21. }
  22. export const enum NodeTypes {
  23. ROOT,
  24. ELEMENT,
  25. TEXT,
  26. COMMENT,
  27. SIMPLE_EXPRESSION,
  28. INTERPOLATION,
  29. ATTRIBUTE,
  30. DIRECTIVE,
  31. // containers
  32. COMPOUND_EXPRESSION,
  33. IF,
  34. IF_BRANCH,
  35. FOR,
  36. TEXT_CALL,
  37. // codegen
  38. VNODE_CALL,
  39. JS_CALL_EXPRESSION,
  40. JS_OBJECT_EXPRESSION,
  41. JS_PROPERTY,
  42. JS_ARRAY_EXPRESSION,
  43. JS_FUNCTION_EXPRESSION,
  44. JS_CONDITIONAL_EXPRESSION,
  45. JS_CACHE_EXPRESSION,
  46. // ssr codegen
  47. JS_BLOCK_STATEMENT,
  48. JS_TEMPLATE_LITERAL,
  49. JS_IF_STATEMENT,
  50. JS_ASSIGNMENT_EXPRESSION,
  51. JS_SEQUENCE_EXPRESSION,
  52. JS_RETURN_STATEMENT
  53. }
  54. export const enum ElementTypes {
  55. ELEMENT,
  56. COMPONENT,
  57. SLOT,
  58. TEMPLATE
  59. }
  60. export interface Node {
  61. type: NodeTypes
  62. loc: SourceLocation
  63. }
  64. // The node's range. The `start` is inclusive and `end` is exclusive.
  65. // [start, end)
  66. export interface SourceLocation {
  67. start: Position
  68. end: Position
  69. source: string
  70. }
  71. export interface Position {
  72. offset: number // from start of file
  73. line: number
  74. column: number
  75. }
  76. export type ParentNode = RootNode | ElementNode | IfBranchNode | ForNode
  77. export type ExpressionNode = SimpleExpressionNode | CompoundExpressionNode
  78. export type TemplateChildNode =
  79. | ElementNode
  80. | InterpolationNode
  81. | CompoundExpressionNode
  82. | TextNode
  83. | CommentNode
  84. | IfNode
  85. | IfBranchNode
  86. | ForNode
  87. | TextCallNode
  88. export interface RootNode extends Node {
  89. type: NodeTypes.ROOT
  90. children: TemplateChildNode[]
  91. helpers: symbol[]
  92. components: string[]
  93. directives: string[]
  94. hoists: (JSChildNode | null)[]
  95. imports: ImportItem[]
  96. cached: number
  97. temps: number
  98. ssrHelpers?: symbol[]
  99. codegenNode?: TemplateChildNode | JSChildNode | BlockStatement
  100. // v2 compat only
  101. filters?: string[]
  102. }
  103. export type ElementNode =
  104. | PlainElementNode
  105. | ComponentNode
  106. | SlotOutletNode
  107. | TemplateNode
  108. export interface BaseElementNode extends Node {
  109. type: NodeTypes.ELEMENT
  110. ns: Namespace
  111. tag: string
  112. tagType: ElementTypes
  113. isSelfClosing: boolean
  114. props: Array<AttributeNode | DirectiveNode>
  115. children: TemplateChildNode[]
  116. }
  117. export interface PlainElementNode extends BaseElementNode {
  118. tagType: ElementTypes.ELEMENT
  119. codegenNode:
  120. | VNodeCall
  121. | SimpleExpressionNode // when hoisted
  122. | CacheExpression // when cached by v-once
  123. | MemoExpression // when cached by v-memo
  124. | undefined
  125. ssrCodegenNode?: TemplateLiteral
  126. }
  127. export interface ComponentNode extends BaseElementNode {
  128. tagType: ElementTypes.COMPONENT
  129. codegenNode:
  130. | VNodeCall
  131. | CacheExpression // when cached by v-once
  132. | MemoExpression // when cached by v-memo
  133. | undefined
  134. ssrCodegenNode?: CallExpression
  135. }
  136. export interface SlotOutletNode extends BaseElementNode {
  137. tagType: ElementTypes.SLOT
  138. codegenNode:
  139. | RenderSlotCall
  140. | CacheExpression // when cached by v-once
  141. | undefined
  142. ssrCodegenNode?: CallExpression
  143. }
  144. export interface TemplateNode extends BaseElementNode {
  145. tagType: ElementTypes.TEMPLATE
  146. // TemplateNode is a container type that always gets compiled away
  147. codegenNode: undefined
  148. }
  149. export interface TextNode extends Node {
  150. type: NodeTypes.TEXT
  151. content: string
  152. }
  153. export interface CommentNode extends Node {
  154. type: NodeTypes.COMMENT
  155. content: string
  156. }
  157. export interface AttributeNode extends Node {
  158. type: NodeTypes.ATTRIBUTE
  159. name: string
  160. value: TextNode | undefined
  161. }
  162. export interface DirectiveNode extends Node {
  163. type: NodeTypes.DIRECTIVE
  164. name: string
  165. exp: ExpressionNode | undefined
  166. arg: ExpressionNode | undefined
  167. modifiers: string[]
  168. /**
  169. * optional property to cache the expression parse result for v-for
  170. */
  171. parseResult?: ForParseResult
  172. }
  173. /**
  174. * Static types have several levels.
  175. * Higher levels implies lower levels. e.g. a node that can be stringified
  176. * can always be hoisted and skipped for patch.
  177. */
  178. export const enum ConstantTypes {
  179. NOT_CONSTANT = 0,
  180. CAN_SKIP_PATCH,
  181. CAN_HOIST,
  182. CAN_STRINGIFY
  183. }
  184. export interface SimpleExpressionNode extends Node {
  185. type: NodeTypes.SIMPLE_EXPRESSION
  186. content: string
  187. isStatic: boolean
  188. constType: ConstantTypes
  189. /**
  190. * Indicates this is an identifier for a hoist vnode call and points to the
  191. * hoisted node.
  192. */
  193. hoisted?: JSChildNode
  194. /**
  195. * an expression parsed as the params of a function will track
  196. * the identifiers declared inside the function body.
  197. */
  198. identifiers?: string[]
  199. isHandlerKey?: boolean
  200. }
  201. export interface InterpolationNode extends Node {
  202. type: NodeTypes.INTERPOLATION
  203. content: ExpressionNode
  204. }
  205. export interface CompoundExpressionNode extends Node {
  206. type: NodeTypes.COMPOUND_EXPRESSION
  207. children: (
  208. | SimpleExpressionNode
  209. | CompoundExpressionNode
  210. | InterpolationNode
  211. | TextNode
  212. | string
  213. | symbol
  214. )[]
  215. /**
  216. * an expression parsed as the params of a function will track
  217. * the identifiers declared inside the function body.
  218. */
  219. identifiers?: string[]
  220. isHandlerKey?: boolean
  221. }
  222. export interface IfNode extends Node {
  223. type: NodeTypes.IF
  224. branches: IfBranchNode[]
  225. codegenNode?: IfConditionalExpression | CacheExpression // <div v-if v-once>
  226. }
  227. export interface IfBranchNode extends Node {
  228. type: NodeTypes.IF_BRANCH
  229. condition: ExpressionNode | undefined // else
  230. children: TemplateChildNode[]
  231. userKey?: AttributeNode | DirectiveNode
  232. isTemplateIf?: boolean
  233. }
  234. export interface ForNode extends Node {
  235. type: NodeTypes.FOR
  236. source: ExpressionNode
  237. valueAlias: ExpressionNode | undefined
  238. keyAlias: ExpressionNode | undefined
  239. objectIndexAlias: ExpressionNode | undefined
  240. parseResult: ForParseResult
  241. children: TemplateChildNode[]
  242. codegenNode?: ForCodegenNode
  243. }
  244. export interface TextCallNode extends Node {
  245. type: NodeTypes.TEXT_CALL
  246. content: TextNode | InterpolationNode | CompoundExpressionNode
  247. codegenNode: CallExpression | SimpleExpressionNode // when hoisted
  248. }
  249. export type TemplateTextChildNode =
  250. | TextNode
  251. | InterpolationNode
  252. | CompoundExpressionNode
  253. export interface VNodeCall extends Node {
  254. type: NodeTypes.VNODE_CALL
  255. tag: string | symbol | CallExpression
  256. props: PropsExpression | undefined
  257. children:
  258. | TemplateChildNode[] // multiple children
  259. | TemplateTextChildNode // single text child
  260. | SlotsExpression // component slots
  261. | ForRenderListExpression // v-for fragment call
  262. | SimpleExpressionNode // hoisted
  263. | undefined
  264. patchFlag: string | undefined
  265. dynamicProps: string | SimpleExpressionNode | undefined
  266. directives: DirectiveArguments | undefined
  267. isBlock: boolean
  268. disableTracking: boolean
  269. isComponent: boolean
  270. }
  271. // JS Node Types ---------------------------------------------------------------
  272. // We also include a number of JavaScript AST nodes for code generation.
  273. // The AST is an intentionally minimal subset just to meet the exact needs of
  274. // Vue render function generation.
  275. export type JSChildNode =
  276. | VNodeCall
  277. | CallExpression
  278. | ObjectExpression
  279. | ArrayExpression
  280. | ExpressionNode
  281. | FunctionExpression
  282. | ConditionalExpression
  283. | CacheExpression
  284. | AssignmentExpression
  285. | SequenceExpression
  286. export interface CallExpression extends Node {
  287. type: NodeTypes.JS_CALL_EXPRESSION
  288. callee: string | symbol
  289. arguments: (
  290. | string
  291. | symbol
  292. | JSChildNode
  293. | SSRCodegenNode
  294. | TemplateChildNode
  295. | TemplateChildNode[]
  296. )[]
  297. }
  298. export interface ObjectExpression extends Node {
  299. type: NodeTypes.JS_OBJECT_EXPRESSION
  300. properties: Array<Property>
  301. }
  302. export interface Property extends Node {
  303. type: NodeTypes.JS_PROPERTY
  304. key: ExpressionNode
  305. value: JSChildNode
  306. }
  307. export interface ArrayExpression extends Node {
  308. type: NodeTypes.JS_ARRAY_EXPRESSION
  309. elements: Array<string | Node>
  310. }
  311. export interface FunctionExpression extends Node {
  312. type: NodeTypes.JS_FUNCTION_EXPRESSION
  313. params: ExpressionNode | string | (ExpressionNode | string)[] | undefined
  314. returns?: TemplateChildNode | TemplateChildNode[] | JSChildNode
  315. body?: BlockStatement | IfStatement
  316. newline: boolean
  317. /**
  318. * This flag is for codegen to determine whether it needs to generate the
  319. * withScopeId() wrapper
  320. */
  321. isSlot: boolean
  322. /**
  323. * __COMPAT__ only, indicates a slot function that should be excluded from
  324. * the legacy $scopedSlots instance property.
  325. */
  326. isNonScopedSlot?: boolean
  327. }
  328. export interface ConditionalExpression extends Node {
  329. type: NodeTypes.JS_CONDITIONAL_EXPRESSION
  330. test: JSChildNode
  331. consequent: JSChildNode
  332. alternate: JSChildNode
  333. newline: boolean
  334. }
  335. export interface CacheExpression extends Node {
  336. type: NodeTypes.JS_CACHE_EXPRESSION
  337. index: number
  338. value: JSChildNode
  339. isVNode: boolean
  340. }
  341. export interface MemoExpression extends CallExpression {
  342. callee: typeof WITH_MEMO
  343. arguments: [ExpressionNode, MemoFactory, string, string]
  344. }
  345. interface MemoFactory extends FunctionExpression {
  346. returns: BlockCodegenNode
  347. }
  348. // SSR-specific Node Types -----------------------------------------------------
  349. export type SSRCodegenNode =
  350. | BlockStatement
  351. | TemplateLiteral
  352. | IfStatement
  353. | AssignmentExpression
  354. | ReturnStatement
  355. | SequenceExpression
  356. export interface BlockStatement extends Node {
  357. type: NodeTypes.JS_BLOCK_STATEMENT
  358. body: (JSChildNode | IfStatement)[]
  359. }
  360. export interface TemplateLiteral extends Node {
  361. type: NodeTypes.JS_TEMPLATE_LITERAL
  362. elements: (string | JSChildNode)[]
  363. }
  364. export interface IfStatement extends Node {
  365. type: NodeTypes.JS_IF_STATEMENT
  366. test: ExpressionNode
  367. consequent: BlockStatement
  368. alternate: IfStatement | BlockStatement | ReturnStatement | undefined
  369. }
  370. export interface AssignmentExpression extends Node {
  371. type: NodeTypes.JS_ASSIGNMENT_EXPRESSION
  372. left: SimpleExpressionNode
  373. right: JSChildNode
  374. }
  375. export interface SequenceExpression extends Node {
  376. type: NodeTypes.JS_SEQUENCE_EXPRESSION
  377. expressions: JSChildNode[]
  378. }
  379. export interface ReturnStatement extends Node {
  380. type: NodeTypes.JS_RETURN_STATEMENT
  381. returns: TemplateChildNode | TemplateChildNode[] | JSChildNode
  382. }
  383. // Codegen Node Types ----------------------------------------------------------
  384. export interface DirectiveArguments extends ArrayExpression {
  385. elements: DirectiveArgumentNode[]
  386. }
  387. export interface DirectiveArgumentNode extends ArrayExpression {
  388. elements: // dir, exp, arg, modifiers
  389. | [string]
  390. | [string, ExpressionNode]
  391. | [string, ExpressionNode, ExpressionNode]
  392. | [string, ExpressionNode, ExpressionNode, ObjectExpression]
  393. }
  394. // renderSlot(...)
  395. export interface RenderSlotCall extends CallExpression {
  396. callee: typeof RENDER_SLOT
  397. arguments: // $slots, name, props, fallback
  398. | [string, string | ExpressionNode]
  399. | [string, string | ExpressionNode, PropsExpression]
  400. | [
  401. string,
  402. string | ExpressionNode,
  403. PropsExpression | '{}',
  404. TemplateChildNode[]
  405. ]
  406. }
  407. export type SlotsExpression = SlotsObjectExpression | DynamicSlotsExpression
  408. // { foo: () => [...] }
  409. export interface SlotsObjectExpression extends ObjectExpression {
  410. properties: SlotsObjectProperty[]
  411. }
  412. export interface SlotsObjectProperty extends Property {
  413. value: SlotFunctionExpression
  414. }
  415. export interface SlotFunctionExpression extends FunctionExpression {
  416. returns: TemplateChildNode[]
  417. }
  418. // createSlots({ ... }, [
  419. // foo ? () => [] : undefined,
  420. // renderList(list, i => () => [i])
  421. // ])
  422. export interface DynamicSlotsExpression extends CallExpression {
  423. callee: typeof CREATE_SLOTS
  424. arguments: [SlotsObjectExpression, DynamicSlotEntries]
  425. }
  426. export interface DynamicSlotEntries extends ArrayExpression {
  427. elements: (ConditionalDynamicSlotNode | ListDynamicSlotNode)[]
  428. }
  429. export interface ConditionalDynamicSlotNode extends ConditionalExpression {
  430. consequent: DynamicSlotNode
  431. alternate: DynamicSlotNode | SimpleExpressionNode
  432. }
  433. export interface ListDynamicSlotNode extends CallExpression {
  434. callee: typeof RENDER_LIST
  435. arguments: [ExpressionNode, ListDynamicSlotIterator]
  436. }
  437. export interface ListDynamicSlotIterator extends FunctionExpression {
  438. returns: DynamicSlotNode
  439. }
  440. export interface DynamicSlotNode extends ObjectExpression {
  441. properties: [Property, DynamicSlotFnProperty]
  442. }
  443. export interface DynamicSlotFnProperty extends Property {
  444. value: SlotFunctionExpression
  445. }
  446. export type BlockCodegenNode = VNodeCall | RenderSlotCall
  447. export interface IfConditionalExpression extends ConditionalExpression {
  448. consequent: BlockCodegenNode | MemoExpression
  449. alternate: BlockCodegenNode | IfConditionalExpression | MemoExpression
  450. }
  451. export interface ForCodegenNode extends VNodeCall {
  452. isBlock: true
  453. tag: typeof FRAGMENT
  454. props: undefined
  455. children: ForRenderListExpression
  456. patchFlag: string
  457. disableTracking: boolean
  458. }
  459. export interface ForRenderListExpression extends CallExpression {
  460. callee: typeof RENDER_LIST
  461. arguments: [ExpressionNode, ForIteratorExpression]
  462. }
  463. export interface ForIteratorExpression extends FunctionExpression {
  464. returns: BlockCodegenNode
  465. }
  466. // AST Utilities ---------------------------------------------------------------
  467. // Some expressions, e.g. sequence and conditional expressions, are never
  468. // associated with template nodes, so their source locations are just a stub.
  469. // Container types like CompoundExpression also don't need a real location.
  470. export const locStub: SourceLocation = {
  471. source: '',
  472. start: { line: 1, column: 1, offset: 0 },
  473. end: { line: 1, column: 1, offset: 0 }
  474. }
  475. export function createRoot(
  476. children: TemplateChildNode[],
  477. loc = locStub
  478. ): RootNode {
  479. return {
  480. type: NodeTypes.ROOT,
  481. children,
  482. helpers: [],
  483. components: [],
  484. directives: [],
  485. hoists: [],
  486. imports: [],
  487. cached: 0,
  488. temps: 0,
  489. codegenNode: undefined,
  490. loc
  491. }
  492. }
  493. export function createVNodeCall(
  494. context: TransformContext | null,
  495. tag: VNodeCall['tag'],
  496. props?: VNodeCall['props'],
  497. children?: VNodeCall['children'],
  498. patchFlag?: VNodeCall['patchFlag'],
  499. dynamicProps?: VNodeCall['dynamicProps'],
  500. directives?: VNodeCall['directives'],
  501. isBlock: VNodeCall['isBlock'] = false,
  502. disableTracking: VNodeCall['disableTracking'] = false,
  503. isComponent: VNodeCall['isComponent'] = false,
  504. loc = locStub
  505. ): VNodeCall {
  506. if (context) {
  507. if (isBlock) {
  508. context.helper(OPEN_BLOCK)
  509. context.helper(getVNodeBlockHelper(context.inSSR, isComponent))
  510. } else {
  511. context.helper(getVNodeHelper(context.inSSR, isComponent))
  512. }
  513. if (directives) {
  514. context.helper(WITH_DIRECTIVES)
  515. }
  516. }
  517. return {
  518. type: NodeTypes.VNODE_CALL,
  519. tag,
  520. props,
  521. children,
  522. patchFlag,
  523. dynamicProps,
  524. directives,
  525. isBlock,
  526. disableTracking,
  527. isComponent,
  528. loc
  529. }
  530. }
  531. export function createArrayExpression(
  532. elements: ArrayExpression['elements'],
  533. loc: SourceLocation = locStub
  534. ): ArrayExpression {
  535. return {
  536. type: NodeTypes.JS_ARRAY_EXPRESSION,
  537. loc,
  538. elements
  539. }
  540. }
  541. export function createObjectExpression(
  542. properties: ObjectExpression['properties'],
  543. loc: SourceLocation = locStub
  544. ): ObjectExpression {
  545. return {
  546. type: NodeTypes.JS_OBJECT_EXPRESSION,
  547. loc,
  548. properties
  549. }
  550. }
  551. export function createObjectProperty(
  552. key: Property['key'] | string,
  553. value: Property['value']
  554. ): Property {
  555. return {
  556. type: NodeTypes.JS_PROPERTY,
  557. loc: locStub,
  558. key: isString(key) ? createSimpleExpression(key, true) : key,
  559. value
  560. }
  561. }
  562. export function createSimpleExpression(
  563. content: SimpleExpressionNode['content'],
  564. isStatic: SimpleExpressionNode['isStatic'] = false,
  565. loc: SourceLocation = locStub,
  566. constType: ConstantTypes = ConstantTypes.NOT_CONSTANT
  567. ): SimpleExpressionNode {
  568. return {
  569. type: NodeTypes.SIMPLE_EXPRESSION,
  570. loc,
  571. content,
  572. isStatic,
  573. constType: isStatic ? ConstantTypes.CAN_STRINGIFY : constType
  574. }
  575. }
  576. export function createInterpolation(
  577. content: InterpolationNode['content'] | string,
  578. loc: SourceLocation
  579. ): InterpolationNode {
  580. return {
  581. type: NodeTypes.INTERPOLATION,
  582. loc,
  583. content: isString(content)
  584. ? createSimpleExpression(content, false, loc)
  585. : content
  586. }
  587. }
  588. export function createCompoundExpression(
  589. children: CompoundExpressionNode['children'],
  590. loc: SourceLocation = locStub
  591. ): CompoundExpressionNode {
  592. return {
  593. type: NodeTypes.COMPOUND_EXPRESSION,
  594. loc,
  595. children
  596. }
  597. }
  598. type InferCodegenNodeType<T> = T extends typeof RENDER_SLOT
  599. ? RenderSlotCall
  600. : CallExpression
  601. export function createCallExpression<T extends CallExpression['callee']>(
  602. callee: T,
  603. args: CallExpression['arguments'] = [],
  604. loc: SourceLocation = locStub
  605. ): InferCodegenNodeType<T> {
  606. return {
  607. type: NodeTypes.JS_CALL_EXPRESSION,
  608. loc,
  609. callee,
  610. arguments: args
  611. } as InferCodegenNodeType<T>
  612. }
  613. export function createFunctionExpression(
  614. params: FunctionExpression['params'],
  615. returns: FunctionExpression['returns'] = undefined,
  616. newline: boolean = false,
  617. isSlot: boolean = false,
  618. loc: SourceLocation = locStub
  619. ): FunctionExpression {
  620. return {
  621. type: NodeTypes.JS_FUNCTION_EXPRESSION,
  622. params,
  623. returns,
  624. newline,
  625. isSlot,
  626. loc
  627. }
  628. }
  629. export function createConditionalExpression(
  630. test: ConditionalExpression['test'],
  631. consequent: ConditionalExpression['consequent'],
  632. alternate: ConditionalExpression['alternate'],
  633. newline = true
  634. ): ConditionalExpression {
  635. return {
  636. type: NodeTypes.JS_CONDITIONAL_EXPRESSION,
  637. test,
  638. consequent,
  639. alternate,
  640. newline,
  641. loc: locStub
  642. }
  643. }
  644. export function createCacheExpression(
  645. index: number,
  646. value: JSChildNode,
  647. isVNode: boolean = false
  648. ): CacheExpression {
  649. return {
  650. type: NodeTypes.JS_CACHE_EXPRESSION,
  651. index,
  652. value,
  653. isVNode,
  654. loc: locStub
  655. }
  656. }
  657. export function createBlockStatement(
  658. body: BlockStatement['body']
  659. ): BlockStatement {
  660. return {
  661. type: NodeTypes.JS_BLOCK_STATEMENT,
  662. body,
  663. loc: locStub
  664. }
  665. }
  666. export function createTemplateLiteral(
  667. elements: TemplateLiteral['elements']
  668. ): TemplateLiteral {
  669. return {
  670. type: NodeTypes.JS_TEMPLATE_LITERAL,
  671. elements,
  672. loc: locStub
  673. }
  674. }
  675. export function createIfStatement(
  676. test: IfStatement['test'],
  677. consequent: IfStatement['consequent'],
  678. alternate?: IfStatement['alternate']
  679. ): IfStatement {
  680. return {
  681. type: NodeTypes.JS_IF_STATEMENT,
  682. test,
  683. consequent,
  684. alternate,
  685. loc: locStub
  686. }
  687. }
  688. export function createAssignmentExpression(
  689. left: AssignmentExpression['left'],
  690. right: AssignmentExpression['right']
  691. ): AssignmentExpression {
  692. return {
  693. type: NodeTypes.JS_ASSIGNMENT_EXPRESSION,
  694. left,
  695. right,
  696. loc: locStub
  697. }
  698. }
  699. export function createSequenceExpression(
  700. expressions: SequenceExpression['expressions']
  701. ): SequenceExpression {
  702. return {
  703. type: NodeTypes.JS_SEQUENCE_EXPRESSION,
  704. expressions,
  705. loc: locStub
  706. }
  707. }
  708. export function createReturnStatement(
  709. returns: ReturnStatement['returns']
  710. ): ReturnStatement {
  711. return {
  712. type: NodeTypes.JS_RETURN_STATEMENT,
  713. returns,
  714. loc: locStub
  715. }
  716. }