2019-10-03 01:11:07 +08:00
|
|
|
import {
|
|
|
|
SourceLocation,
|
|
|
|
Position,
|
|
|
|
ElementNode,
|
|
|
|
NodeTypes,
|
|
|
|
CallExpression,
|
2019-10-03 05:18:11 +08:00
|
|
|
createCallExpression,
|
2019-10-03 11:10:41 +08:00
|
|
|
DirectiveNode,
|
|
|
|
ElementTypes,
|
|
|
|
TemplateChildNode,
|
2019-10-04 00:03:14 +08:00
|
|
|
RootNode,
|
|
|
|
ObjectExpression,
|
|
|
|
Property,
|
|
|
|
JSChildNode,
|
2019-10-06 10:47:20 +08:00
|
|
|
createObjectExpression,
|
|
|
|
SlotOutletNode,
|
2019-10-08 22:50:00 +08:00
|
|
|
TemplateNode,
|
2020-02-12 07:12:56 +08:00
|
|
|
RenderSlotCall,
|
2019-10-19 09:51:34 +08:00
|
|
|
ExpressionNode,
|
2020-02-03 11:28:54 +08:00
|
|
|
IfBranchNode,
|
|
|
|
TextNode,
|
2020-02-12 07:12:56 +08:00
|
|
|
InterpolationNode,
|
2020-07-14 04:48:16 +08:00
|
|
|
VNodeCall,
|
|
|
|
SimpleExpressionNode
|
2019-10-03 01:11:07 +08:00
|
|
|
} from './ast'
|
|
|
|
import { TransformContext } from './transform'
|
2019-11-30 01:42:04 +08:00
|
|
|
import {
|
|
|
|
MERGE_PROPS,
|
2020-03-31 22:52:42 +08:00
|
|
|
TELEPORT,
|
2019-11-30 01:42:04 +08:00
|
|
|
SUSPENSE,
|
|
|
|
KEEP_ALIVE,
|
2020-10-14 00:04:52 +08:00
|
|
|
BASE_TRANSITION,
|
|
|
|
TO_HANDLERS
|
2019-11-30 01:42:04 +08:00
|
|
|
} from './runtimeHelpers'
|
2020-06-11 04:54:23 +08:00
|
|
|
import { isString, isObject, hyphenate, extend } from '@vue/shared'
|
2019-11-30 01:42:04 +08:00
|
|
|
|
2020-07-14 04:48:16 +08:00
|
|
|
export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
|
|
|
|
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
|
|
|
|
|
2019-11-30 01:42:04 +08:00
|
|
|
export const isBuiltInType = (tag: string, expected: string): boolean =>
|
|
|
|
tag === expected || tag === hyphenate(expected)
|
|
|
|
|
|
|
|
export function isCoreComponent(tag: string): symbol | void {
|
2020-03-31 22:52:42 +08:00
|
|
|
if (isBuiltInType(tag, 'Teleport')) {
|
|
|
|
return TELEPORT
|
2019-11-30 01:42:04 +08:00
|
|
|
} else if (isBuiltInType(tag, 'Suspense')) {
|
|
|
|
return SUSPENSE
|
|
|
|
} else if (isBuiltInType(tag, 'KeepAlive')) {
|
|
|
|
return KEEP_ALIVE
|
|
|
|
} else if (isBuiltInType(tag, 'BaseTransition')) {
|
|
|
|
return BASE_TRANSITION
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 01:13:32 +08:00
|
|
|
|
2019-10-10 23:15:24 +08:00
|
|
|
const nonIdentifierRE = /^\d|[^\$\w]/
|
2019-09-25 10:39:20 +08:00
|
|
|
export const isSimpleIdentifier = (name: string): boolean =>
|
2019-10-10 23:15:24 +08:00
|
|
|
!nonIdentifierRE.test(name)
|
|
|
|
|
2021-03-26 05:24:18 +08:00
|
|
|
const memberExpRE = /^[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*(?:\s*\.\s*[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*|\[[^\]]+\])*$/
|
2020-06-10 05:24:48 +08:00
|
|
|
export const isMemberExpression = (path: string): boolean => {
|
|
|
|
if (!path) return false
|
|
|
|
return memberExpRE.test(path.trim())
|
|
|
|
}
|
2019-09-25 10:39:20 +08:00
|
|
|
|
2019-09-20 01:23:49 +08:00
|
|
|
export function getInnerRange(
|
|
|
|
loc: SourceLocation,
|
|
|
|
offset: number,
|
|
|
|
length?: number
|
|
|
|
): SourceLocation {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(offset <= loc.source.length)
|
2019-09-20 01:23:49 +08:00
|
|
|
const source = loc.source.substr(offset, length)
|
|
|
|
const newLoc: SourceLocation = {
|
|
|
|
source,
|
2019-09-20 11:05:51 +08:00
|
|
|
start: advancePositionWithClone(loc.start, loc.source, offset),
|
2019-09-20 01:23:49 +08:00
|
|
|
end: loc.end
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length != null) {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(offset + length <= loc.source.length)
|
2019-09-20 11:05:51 +08:00
|
|
|
newLoc.end = advancePositionWithClone(
|
|
|
|
loc.start,
|
|
|
|
loc.source,
|
|
|
|
offset + length
|
|
|
|
)
|
2019-09-20 01:23:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return newLoc
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:05:51 +08:00
|
|
|
export function advancePositionWithClone(
|
2019-09-20 01:23:49 +08:00
|
|
|
pos: Position,
|
|
|
|
source: string,
|
2019-09-26 07:17:45 +08:00
|
|
|
numberOfCharacters: number = source.length
|
2019-09-20 01:23:49 +08:00
|
|
|
): Position {
|
2020-06-11 04:54:23 +08:00
|
|
|
return advancePositionWithMutation(
|
|
|
|
extend({}, pos),
|
|
|
|
source,
|
|
|
|
numberOfCharacters
|
|
|
|
)
|
2019-09-20 01:59:24 +08:00
|
|
|
}
|
2019-09-20 01:23:49 +08:00
|
|
|
|
2019-09-20 01:59:24 +08:00
|
|
|
// advance by mutation without cloning (for performance reasons), since this
|
|
|
|
// gets called a lot in the parser
|
|
|
|
export function advancePositionWithMutation(
|
|
|
|
pos: Position,
|
|
|
|
source: string,
|
2019-09-26 07:17:45 +08:00
|
|
|
numberOfCharacters: number = source.length
|
2019-09-20 01:59:24 +08:00
|
|
|
): Position {
|
2019-09-20 09:18:18 +08:00
|
|
|
let linesCount = 0
|
|
|
|
let lastNewLinePos = -1
|
|
|
|
for (let i = 0; i < numberOfCharacters; i++) {
|
|
|
|
if (source.charCodeAt(i) === 10 /* newline char code */) {
|
|
|
|
linesCount++
|
|
|
|
lastNewLinePos = i
|
|
|
|
}
|
|
|
|
}
|
2019-09-20 01:23:49 +08:00
|
|
|
|
2019-09-20 01:59:24 +08:00
|
|
|
pos.offset += numberOfCharacters
|
2019-09-20 09:18:18 +08:00
|
|
|
pos.line += linesCount
|
2019-09-20 01:59:24 +08:00
|
|
|
pos.column =
|
2019-09-20 09:18:18 +08:00
|
|
|
lastNewLinePos === -1
|
2019-09-20 01:23:49 +08:00
|
|
|
? pos.column + numberOfCharacters
|
2019-12-31 00:25:06 +08:00
|
|
|
: numberOfCharacters - lastNewLinePos
|
2019-09-20 01:23:49 +08:00
|
|
|
|
2019-09-20 01:59:24 +08:00
|
|
|
return pos
|
2019-09-20 01:23:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function assert(condition: boolean, msg?: string) {
|
2019-09-25 04:35:01 +08:00
|
|
|
/* istanbul ignore if */
|
2019-09-20 01:23:49 +08:00
|
|
|
if (!condition) {
|
2019-09-23 04:50:57 +08:00
|
|
|
throw new Error(msg || `unexpected compiler condition`)
|
2019-09-20 01:23:49 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-02 11:19:48 +08:00
|
|
|
|
2019-10-03 06:03:42 +08:00
|
|
|
export function findDir(
|
2019-10-03 05:18:11 +08:00
|
|
|
node: ElementNode,
|
2019-10-03 06:03:42 +08:00
|
|
|
name: string | RegExp,
|
|
|
|
allowEmpty: boolean = false
|
2019-10-03 05:18:11 +08:00
|
|
|
): DirectiveNode | undefined {
|
|
|
|
for (let i = 0; i < node.props.length; i++) {
|
|
|
|
const p = node.props[i]
|
|
|
|
if (
|
|
|
|
p.type === NodeTypes.DIRECTIVE &&
|
2019-10-03 06:03:42 +08:00
|
|
|
(allowEmpty || p.exp) &&
|
2019-10-03 11:10:41 +08:00
|
|
|
(isString(name) ? p.name === name : name.test(p.name))
|
2019-10-03 05:18:11 +08:00
|
|
|
) {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 11:19:48 +08:00
|
|
|
export function findProp(
|
2019-10-03 05:18:11 +08:00
|
|
|
node: ElementNode,
|
2019-10-17 00:00:55 +08:00
|
|
|
name: string,
|
2020-03-28 11:45:50 +08:00
|
|
|
dynamicOnly: boolean = false,
|
|
|
|
allowEmpty: boolean = false
|
2019-10-02 11:19:48 +08:00
|
|
|
): ElementNode['props'][0] | undefined {
|
2019-10-03 05:18:11 +08:00
|
|
|
for (let i = 0; i < node.props.length; i++) {
|
|
|
|
const p = node.props[i]
|
2019-10-02 11:19:48 +08:00
|
|
|
if (p.type === NodeTypes.ATTRIBUTE) {
|
2019-10-17 00:00:55 +08:00
|
|
|
if (dynamicOnly) continue
|
2020-03-28 11:45:50 +08:00
|
|
|
if (p.name === name && (p.value || allowEmpty)) {
|
2019-10-02 11:19:48 +08:00
|
|
|
return p
|
|
|
|
}
|
2020-08-05 00:01:07 +08:00
|
|
|
} else if (
|
|
|
|
p.name === 'bind' &&
|
|
|
|
(p.exp || allowEmpty) &&
|
|
|
|
isBindKey(p.arg, name)
|
|
|
|
) {
|
2019-10-02 11:19:48 +08:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 01:11:07 +08:00
|
|
|
|
2020-02-06 12:07:23 +08:00
|
|
|
export function isBindKey(arg: DirectiveNode['arg'], name: string): boolean {
|
2020-07-14 04:48:16 +08:00
|
|
|
return !!(arg && isStaticExp(arg) && arg.content === name)
|
2020-02-06 12:07:23 +08:00
|
|
|
}
|
|
|
|
|
2020-02-06 04:21:47 +08:00
|
|
|
export function hasDynamicKeyVBind(node: ElementNode): boolean {
|
|
|
|
return node.props.some(
|
|
|
|
p =>
|
|
|
|
p.type === NodeTypes.DIRECTIVE &&
|
|
|
|
p.name === 'bind' &&
|
|
|
|
(!p.arg || // v-bind="obj"
|
|
|
|
p.arg.type !== NodeTypes.SIMPLE_EXPRESSION || // v-bind:[_ctx.foo]
|
|
|
|
!p.arg.isStatic) // v-bind:[foo]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:28:54 +08:00
|
|
|
export function isText(
|
|
|
|
node: TemplateChildNode
|
|
|
|
): node is TextNode | InterpolationNode {
|
|
|
|
return node.type === NodeTypes.INTERPOLATION || node.type === NodeTypes.TEXT
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:32:53 +08:00
|
|
|
export function isVSlot(p: ElementNode['props'][0]): p is DirectiveNode {
|
|
|
|
return p.type === NodeTypes.DIRECTIVE && p.name === 'slot'
|
|
|
|
}
|
2019-10-03 11:10:41 +08:00
|
|
|
|
2019-11-01 23:32:53 +08:00
|
|
|
export function isTemplateNode(
|
2019-10-03 11:10:41 +08:00
|
|
|
node: RootNode | TemplateChildNode
|
2019-11-01 23:32:53 +08:00
|
|
|
): node is TemplateNode {
|
|
|
|
return (
|
|
|
|
node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.TEMPLATE
|
|
|
|
)
|
|
|
|
}
|
2019-10-04 00:03:14 +08:00
|
|
|
|
2019-11-01 23:32:53 +08:00
|
|
|
export function isSlotOutlet(
|
2019-10-04 00:03:14 +08:00
|
|
|
node: RootNode | TemplateChildNode
|
2019-11-01 23:32:53 +08:00
|
|
|
): node is SlotOutletNode {
|
|
|
|
return node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.SLOT
|
|
|
|
}
|
2019-10-04 00:03:14 +08:00
|
|
|
|
|
|
|
export function injectProp(
|
2020-02-12 07:12:56 +08:00
|
|
|
node: VNodeCall | RenderSlotCall,
|
2019-10-04 00:03:14 +08:00
|
|
|
prop: Property,
|
|
|
|
context: TransformContext
|
2019-10-08 22:50:00 +08:00
|
|
|
) {
|
2020-10-14 00:04:52 +08:00
|
|
|
let propsWithInjection: ObjectExpression | CallExpression | undefined
|
2019-10-08 22:50:00 +08:00
|
|
|
const props =
|
2020-02-12 07:12:56 +08:00
|
|
|
node.type === NodeTypes.VNODE_CALL ? node.props : node.arguments[2]
|
2019-10-08 22:50:00 +08:00
|
|
|
if (props == null || isString(props)) {
|
|
|
|
propsWithInjection = createObjectExpression([prop])
|
2019-10-04 00:03:14 +08:00
|
|
|
} else if (props.type === NodeTypes.JS_CALL_EXPRESSION) {
|
|
|
|
// merged props... add ours
|
|
|
|
// only inject key to object literal if it's the first argument so that
|
|
|
|
// if doesn't override user provided keys
|
|
|
|
const first = props.arguments[0] as string | JSChildNode
|
|
|
|
if (!isString(first) && first.type === NodeTypes.JS_OBJECT_EXPRESSION) {
|
|
|
|
first.properties.unshift(prop)
|
|
|
|
} else {
|
2020-10-14 00:04:52 +08:00
|
|
|
if (props.callee === TO_HANDLERS) {
|
|
|
|
// #2366
|
|
|
|
propsWithInjection = createCallExpression(context.helper(MERGE_PROPS), [
|
|
|
|
createObjectExpression([prop]),
|
|
|
|
props
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
props.arguments.unshift(createObjectExpression([prop]))
|
|
|
|
}
|
2019-10-04 00:03:14 +08:00
|
|
|
}
|
2020-10-14 00:04:52 +08:00
|
|
|
!propsWithInjection && (propsWithInjection = props)
|
2019-10-04 00:03:14 +08:00
|
|
|
} else if (props.type === NodeTypes.JS_OBJECT_EXPRESSION) {
|
2020-01-20 23:15:53 +08:00
|
|
|
let alreadyExists = false
|
|
|
|
// check existing key to avoid overriding user provided keys
|
|
|
|
if (prop.key.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
|
|
const propKeyName = prop.key.content
|
2020-02-03 11:28:54 +08:00
|
|
|
alreadyExists = props.properties.some(
|
|
|
|
p =>
|
|
|
|
p.key.type === NodeTypes.SIMPLE_EXPRESSION &&
|
|
|
|
p.key.content === propKeyName
|
|
|
|
)
|
2020-01-20 23:15:53 +08:00
|
|
|
}
|
|
|
|
if (!alreadyExists) {
|
|
|
|
props.properties.unshift(prop)
|
|
|
|
}
|
2019-10-08 22:50:00 +08:00
|
|
|
propsWithInjection = props
|
2019-10-04 00:03:14 +08:00
|
|
|
} else {
|
|
|
|
// single v-bind with expression, return a merged replacement
|
2019-10-08 22:50:00 +08:00
|
|
|
propsWithInjection = createCallExpression(context.helper(MERGE_PROPS), [
|
2019-10-04 00:03:14 +08:00
|
|
|
createObjectExpression([prop]),
|
|
|
|
props
|
|
|
|
])
|
|
|
|
}
|
2020-02-12 07:12:56 +08:00
|
|
|
if (node.type === NodeTypes.VNODE_CALL) {
|
|
|
|
node.props = propsWithInjection
|
2019-10-08 22:50:00 +08:00
|
|
|
} else {
|
2020-02-12 07:12:56 +08:00
|
|
|
node.arguments[2] = propsWithInjection
|
2019-10-08 22:50:00 +08:00
|
|
|
}
|
2019-10-04 00:03:14 +08:00
|
|
|
}
|
2019-10-06 05:18:25 +08:00
|
|
|
|
|
|
|
export function toValidAssetId(
|
|
|
|
name: string,
|
|
|
|
type: 'component' | 'directive'
|
|
|
|
): string {
|
2019-10-11 01:55:26 +08:00
|
|
|
return `_${type}_${name.replace(/[^\w]/g, '_')}`
|
2019-10-06 05:18:25 +08:00
|
|
|
}
|
2019-10-10 22:33:58 +08:00
|
|
|
|
2019-10-19 09:51:34 +08:00
|
|
|
// Check if a node contains expressions that reference current context scope ids
|
|
|
|
export function hasScopeRef(
|
|
|
|
node: TemplateChildNode | IfBranchNode | ExpressionNode | undefined,
|
|
|
|
ids: TransformContext['identifiers']
|
|
|
|
): boolean {
|
|
|
|
if (!node || Object.keys(ids).length === 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch (node.type) {
|
|
|
|
case NodeTypes.ELEMENT:
|
|
|
|
for (let i = 0; i < node.props.length; i++) {
|
|
|
|
const p = node.props[i]
|
|
|
|
if (
|
|
|
|
p.type === NodeTypes.DIRECTIVE &&
|
|
|
|
(hasScopeRef(p.arg, ids) || hasScopeRef(p.exp, ids))
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node.children.some(c => hasScopeRef(c, ids))
|
|
|
|
case NodeTypes.FOR:
|
|
|
|
if (hasScopeRef(node.source, ids)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return node.children.some(c => hasScopeRef(c, ids))
|
|
|
|
case NodeTypes.IF:
|
|
|
|
return node.branches.some(b => hasScopeRef(b, ids))
|
|
|
|
case NodeTypes.IF_BRANCH:
|
|
|
|
if (hasScopeRef(node.condition, ids)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return node.children.some(c => hasScopeRef(c, ids))
|
|
|
|
case NodeTypes.SIMPLE_EXPRESSION:
|
|
|
|
return (
|
|
|
|
!node.isStatic &&
|
|
|
|
isSimpleIdentifier(node.content) &&
|
|
|
|
!!ids[node.content]
|
|
|
|
)
|
|
|
|
case NodeTypes.COMPOUND_EXPRESSION:
|
|
|
|
return node.children.some(c => isObject(c) && hasScopeRef(c, ids))
|
|
|
|
case NodeTypes.INTERPOLATION:
|
2019-10-22 03:52:29 +08:00
|
|
|
case NodeTypes.TEXT_CALL:
|
2019-10-19 09:51:34 +08:00
|
|
|
return hasScopeRef(node.content, ids)
|
2019-10-22 03:52:29 +08:00
|
|
|
case NodeTypes.TEXT:
|
|
|
|
case NodeTypes.COMMENT:
|
|
|
|
return false
|
2019-10-19 09:51:34 +08:00
|
|
|
default:
|
2019-10-22 03:52:29 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
const exhaustiveCheck: never = node
|
|
|
|
exhaustiveCheck
|
|
|
|
}
|
2019-10-19 09:51:34 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|