2019-10-02 03:43:59 +08:00
|
|
|
import {
|
|
|
|
NodeTypes,
|
|
|
|
ElementNode,
|
|
|
|
locStub,
|
|
|
|
Namespaces,
|
2019-10-06 10:47:20 +08:00
|
|
|
ElementTypes,
|
2020-02-12 07:40:21 +08:00
|
|
|
VNodeCall
|
2019-10-02 03:43:59 +08:00
|
|
|
} from '../src'
|
2021-06-23 07:15:20 +08:00
|
|
|
import {
|
|
|
|
isString,
|
|
|
|
PatchFlags,
|
|
|
|
PatchFlagNames,
|
|
|
|
isArray,
|
|
|
|
ShapeFlags
|
|
|
|
} from '@vue/shared'
|
2019-10-02 03:04:58 +08:00
|
|
|
|
|
|
|
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" }
|
2019-10-16 22:31:40 +08:00
|
|
|
export function createObjectMatcher(obj: Record<string, any>) {
|
2019-10-02 03:04:58 +08:00
|
|
|
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)
|
|
|
|
},
|
2019-10-03 11:10:41 +08:00
|
|
|
value: isString(obj[key])
|
|
|
|
? {
|
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
|
|
|
content: obj[key].replace(bracketsRE, ''),
|
|
|
|
isStatic: !leadingBracketRE.test(obj[key])
|
|
|
|
}
|
|
|
|
: obj[key]
|
2019-10-02 03:04:58 +08:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 03:43:59 +08:00
|
|
|
|
|
|
|
export function createElementWithCodegen(
|
2020-02-12 07:40:21 +08:00
|
|
|
tag: VNodeCall['tag'],
|
|
|
|
props?: VNodeCall['props'],
|
|
|
|
children?: VNodeCall['children'],
|
|
|
|
patchFlag?: VNodeCall['patchFlag'],
|
|
|
|
dynamicProps?: VNodeCall['dynamicProps']
|
2019-10-02 03:43:59 +08:00
|
|
|
): ElementNode {
|
|
|
|
return {
|
|
|
|
type: NodeTypes.ELEMENT,
|
|
|
|
loc: locStub,
|
|
|
|
ns: Namespaces.HTML,
|
|
|
|
tag: 'div',
|
|
|
|
tagType: ElementTypes.ELEMENT,
|
|
|
|
isSelfClosing: false,
|
|
|
|
props: [],
|
|
|
|
children: [],
|
|
|
|
codegenNode: {
|
2020-02-12 07:40:21 +08:00
|
|
|
type: NodeTypes.VNODE_CALL,
|
|
|
|
tag,
|
|
|
|
props,
|
|
|
|
children,
|
|
|
|
patchFlag,
|
|
|
|
dynamicProps,
|
|
|
|
directives: undefined,
|
|
|
|
isBlock: false,
|
2020-06-18 04:13:14 +08:00
|
|
|
disableTracking: false,
|
2021-06-23 07:15:20 +08:00
|
|
|
isComponent: false,
|
2020-02-12 07:40:21 +08:00
|
|
|
loc: locStub
|
2019-10-02 03:43:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 05:12:22 +08:00
|
|
|
|
2021-06-23 07:15:20 +08:00
|
|
|
type Flags = PatchFlags | ShapeFlags
|
|
|
|
export function genFlagText(
|
|
|
|
flag: Flags | Flags[],
|
|
|
|
names: { [k: number]: string } = PatchFlagNames
|
|
|
|
) {
|
2019-10-08 05:12:22 +08:00
|
|
|
if (isArray(flag)) {
|
|
|
|
let f = 0
|
|
|
|
flag.forEach(ff => {
|
|
|
|
f |= ff
|
|
|
|
})
|
2021-06-23 07:15:20 +08:00
|
|
|
return `${f} /* ${flag.map(f => names[f]).join(', ')} */`
|
2019-10-08 05:12:22 +08:00
|
|
|
} else {
|
2021-06-23 07:15:20 +08:00
|
|
|
return `${flag} /* ${names[flag]} */`
|
2019-10-08 05:12:22 +08:00
|
|
|
}
|
|
|
|
}
|