| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import {
- type ElementNode,
- ElementTypes,
- Namespaces,
- NodeTypes,
- type Property,
- type SimpleExpressionNode,
- type VNodeCall,
- locStub,
- } from '../src'
- import {
- PatchFlagNames,
- type PatchFlags,
- type ShapeFlags,
- isArray,
- isString,
- } from '@vue/shared'
- const leadingBracketRE = /^\[/
- const bracketsRE = /^\[|\]$/g
- // Create a matcher for an object
- // where non-static expressions should be wrapped in []
- // e.g.
- // - createObjectMatcher({ 'foo': '[bar]' }) matches { foo: bar }
- // - createObjectMatcher({ '[foo]': 'bar' }) matches { [foo]: "bar" }
- export function createObjectMatcher(obj: Record<string, any>): {
- type: NodeTypes
- properties: Partial<Property>[]
- } {
- return {
- type: NodeTypes.JS_OBJECT_EXPRESSION,
- properties: Object.keys(obj).map(key => ({
- type: NodeTypes.JS_PROPERTY,
- key: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: key.replace(bracketsRE, ''),
- isStatic: !leadingBracketRE.test(key),
- } as SimpleExpressionNode,
- value: isString(obj[key])
- ? {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: obj[key].replace(bracketsRE, ''),
- isStatic: !leadingBracketRE.test(obj[key]),
- }
- : obj[key],
- })),
- }
- }
- export function createElementWithCodegen(
- tag: VNodeCall['tag'],
- props?: VNodeCall['props'],
- children?: VNodeCall['children'],
- patchFlag?: VNodeCall['patchFlag'],
- dynamicProps?: VNodeCall['dynamicProps'],
- ): ElementNode {
- return {
- type: NodeTypes.ELEMENT,
- loc: locStub,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- props: [],
- children: [],
- codegenNode: {
- type: NodeTypes.VNODE_CALL,
- tag,
- props,
- children,
- patchFlag,
- dynamicProps,
- directives: undefined,
- isBlock: false,
- disableTracking: false,
- isComponent: false,
- loc: locStub,
- },
- }
- }
- type Flags = PatchFlags | ShapeFlags
- export function genFlagText(
- flag: Flags | Flags[],
- names: { [k: number]: string } = PatchFlagNames,
- ): string {
- if (isArray(flag)) {
- let f = 0
- flag.forEach(ff => {
- f |= ff
- })
- return `${f} /* ${flag.map(f => names[f]).join(', ')} */`
- } else {
- return `${flag} /* ${names[flag]} */`
- }
- }
|