ast.ts 20 KB

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