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,
|
2021-07-10 09:41:44 +08:00
|
|
|
SimpleExpressionNode,
|
|
|
|
BlockCodegenNode,
|
|
|
|
MemoExpression
|
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,
|
2021-06-23 07:15:20 +08:00
|
|
|
TO_HANDLERS,
|
|
|
|
NORMALIZE_PROPS,
|
|
|
|
GUARD_REACTIVE_PROPS,
|
|
|
|
CREATE_BLOCK,
|
|
|
|
CREATE_ELEMENT_BLOCK,
|
|
|
|
CREATE_VNODE,
|
2021-07-10 09:41:44 +08:00
|
|
|
CREATE_ELEMENT_VNODE,
|
|
|
|
WITH_MEMO,
|
|
|
|
OPEN_BLOCK
|
2019-11-30 01:42:04 +08:00
|
|
|
} from './runtimeHelpers'
|
2021-12-10 16:09:23 +08:00
|
|
|
import { isString, isObject, hyphenate, extend, NOOP } from '@vue/shared'
|
2021-06-23 07:15:20 +08:00
|
|
|
import { PropsExpression } from './transforms/transformElement'
|
2021-09-22 00:19:27 +08:00
|
|
|
import { parseExpression } from '@babel/parser'
|
|
|
|
import { Expression } from '@babel/types'
|
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-06-09 23:57:48 +08:00
|
|
|
const enum MemberExpLexState {
|
|
|
|
inMemberExp,
|
|
|
|
inBrackets,
|
2021-07-16 01:34:42 +08:00
|
|
|
inParens,
|
2021-06-09 23:57:48 +08:00
|
|
|
inString
|
|
|
|
}
|
|
|
|
|
|
|
|
const validFirstIdentCharRE = /[A-Za-z_$\xA0-\uFFFF]/
|
2021-07-16 00:23:07 +08:00
|
|
|
const validIdentCharRE = /[\.\?\w$\xA0-\uFFFF]/
|
2021-06-09 23:57:48 +08:00
|
|
|
const whitespaceRE = /\s+[.[]\s*|\s*[.[]\s+/g
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple lexer to check if an expression is a member expression. This is
|
|
|
|
* lax and only checks validity at the root level (i.e. does not validate exps
|
|
|
|
* inside square brackets), but it's ok since these are only used on template
|
|
|
|
* expressions and false positives are invalid expressions in the first place.
|
|
|
|
*/
|
2021-09-22 00:19:27 +08:00
|
|
|
export const isMemberExpressionBrowser = (path: string): boolean => {
|
2021-06-09 23:57:48 +08:00
|
|
|
// remove whitespaces around . or [ first
|
|
|
|
path = path.trim().replace(whitespaceRE, s => s.trim())
|
|
|
|
|
|
|
|
let state = MemberExpLexState.inMemberExp
|
2021-07-16 01:26:09 +08:00
|
|
|
let stateStack: MemberExpLexState[] = []
|
2021-06-09 23:57:48 +08:00
|
|
|
let currentOpenBracketCount = 0
|
2021-07-16 01:34:42 +08:00
|
|
|
let currentOpenParensCount = 0
|
2021-06-09 23:57:48 +08:00
|
|
|
let currentStringType: "'" | '"' | '`' | null = null
|
|
|
|
|
|
|
|
for (let i = 0; i < path.length; i++) {
|
|
|
|
const char = path.charAt(i)
|
|
|
|
switch (state) {
|
|
|
|
case MemberExpLexState.inMemberExp:
|
|
|
|
if (char === '[') {
|
2021-07-16 01:26:09 +08:00
|
|
|
stateStack.push(state)
|
2021-06-09 23:57:48 +08:00
|
|
|
state = MemberExpLexState.inBrackets
|
|
|
|
currentOpenBracketCount++
|
2021-07-16 01:34:42 +08:00
|
|
|
} else if (char === '(') {
|
|
|
|
stateStack.push(state)
|
|
|
|
state = MemberExpLexState.inParens
|
|
|
|
currentOpenParensCount++
|
2021-06-09 23:57:48 +08:00
|
|
|
} else if (
|
|
|
|
!(i === 0 ? validFirstIdentCharRE : validIdentCharRE).test(char)
|
|
|
|
) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case MemberExpLexState.inBrackets:
|
|
|
|
if (char === `'` || char === `"` || char === '`') {
|
2021-07-16 01:26:09 +08:00
|
|
|
stateStack.push(state)
|
2021-06-09 23:57:48 +08:00
|
|
|
state = MemberExpLexState.inString
|
|
|
|
currentStringType = char
|
|
|
|
} else if (char === `[`) {
|
|
|
|
currentOpenBracketCount++
|
|
|
|
} else if (char === `]`) {
|
|
|
|
if (!--currentOpenBracketCount) {
|
2021-07-16 01:26:09 +08:00
|
|
|
state = stateStack.pop()!
|
2021-06-09 23:57:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
2021-07-16 01:34:42 +08:00
|
|
|
case MemberExpLexState.inParens:
|
|
|
|
if (char === `'` || char === `"` || char === '`') {
|
|
|
|
stateStack.push(state)
|
|
|
|
state = MemberExpLexState.inString
|
|
|
|
currentStringType = char
|
|
|
|
} else if (char === `(`) {
|
|
|
|
currentOpenParensCount++
|
|
|
|
} else if (char === `)`) {
|
|
|
|
// if the exp ends as a call then it should not be considered valid
|
|
|
|
if (i === path.length - 1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!--currentOpenParensCount) {
|
|
|
|
state = stateStack.pop()!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
2021-06-09 23:57:48 +08:00
|
|
|
case MemberExpLexState.inString:
|
|
|
|
if (char === currentStringType) {
|
2021-07-16 01:26:09 +08:00
|
|
|
state = stateStack.pop()!
|
2021-06-09 23:57:48 +08:00
|
|
|
currentStringType = null
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 01:34:42 +08:00
|
|
|
return !currentOpenBracketCount && !currentOpenParensCount
|
2020-06-10 05:24:48 +08:00
|
|
|
}
|
2019-09-25 10:39:20 +08:00
|
|
|
|
2021-09-24 11:20:53 +08:00
|
|
|
export const isMemberExpressionNode = __BROWSER__
|
2021-09-25 00:34:31 +08:00
|
|
|
? (NOOP as any as (path: string, context: TransformContext) => boolean)
|
2021-09-24 11:20:53 +08:00
|
|
|
: (path: string, context: TransformContext): boolean => {
|
|
|
|
try {
|
|
|
|
let ret: Expression = parseExpression(path, {
|
2021-10-08 07:32:59 +08:00
|
|
|
plugins: context.expressionPlugins
|
2021-09-24 11:20:53 +08:00
|
|
|
})
|
|
|
|
if (ret.type === 'TSAsExpression' || ret.type === 'TSTypeAssertion') {
|
|
|
|
ret = ret.expression
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
ret.type === 'MemberExpression' ||
|
|
|
|
ret.type === 'OptionalMemberExpression' ||
|
|
|
|
ret.type === 'Identifier'
|
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
return false
|
|
|
|
}
|
2021-09-22 00:19:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const isMemberExpression = __BROWSER__
|
|
|
|
? isMemberExpressionBrowser
|
|
|
|
: isMemberExpressionNode
|
|
|
|
|
2019-09-20 01:23:49 +08:00
|
|
|
export function getInnerRange(
|
|
|
|
loc: SourceLocation,
|
|
|
|
offset: number,
|
2021-10-09 00:31:34 +08:00
|
|
|
length: number
|
2019-09-20 01:23:49 +08:00
|
|
|
): SourceLocation {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(offset <= loc.source.length)
|
2021-10-09 00:31:34 +08:00
|
|
|
const source = loc.source.slice(offset, offset + length)
|
2019-09-20 01:23:49 +08:00
|
|
|
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) &&
|
2021-12-10 15:34:23 +08:00
|
|
|
isStaticArgOf(p.arg, name)
|
2020-08-05 00:01:07 +08:00
|
|
|
) {
|
2019-10-02 11:19:48 +08:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 01:11:07 +08:00
|
|
|
|
2021-12-10 15:34:23 +08:00
|
|
|
export function isStaticArgOf(
|
|
|
|
arg: DirectiveNode['arg'],
|
2021-12-10 16:09:23 +08:00
|
|
|
name: string
|
2021-12-10 15:34:23 +08:00
|
|
|
): boolean {
|
2021-12-10 16:09:23 +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"
|
2021-07-20 06:24:18 +08:00
|
|
|
p.arg.type !== NodeTypes.SIMPLE_EXPRESSION || // v-bind:[_ctx.foo]
|
2020-02-06 04:21:47 +08:00
|
|
|
!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
|
|
|
|
2021-06-23 07:15:20 +08:00
|
|
|
export function getVNodeHelper(ssr: boolean, isComponent: boolean) {
|
|
|
|
return ssr || isComponent ? CREATE_VNODE : CREATE_ELEMENT_VNODE
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getVNodeBlockHelper(ssr: boolean, isComponent: boolean) {
|
|
|
|
return ssr || isComponent ? CREATE_BLOCK : CREATE_ELEMENT_BLOCK
|
|
|
|
}
|
|
|
|
|
|
|
|
const propsHelperSet = new Set([NORMALIZE_PROPS, GUARD_REACTIVE_PROPS])
|
|
|
|
|
|
|
|
function getUnnormalizedProps(
|
|
|
|
props: PropsExpression | '{}',
|
|
|
|
callPath: CallExpression[] = []
|
|
|
|
): [PropsExpression | '{}', CallExpression[]] {
|
|
|
|
if (
|
|
|
|
props &&
|
|
|
|
!isString(props) &&
|
|
|
|
props.type === NodeTypes.JS_CALL_EXPRESSION
|
|
|
|
) {
|
|
|
|
const callee = props.callee
|
|
|
|
if (!isString(callee) && propsHelperSet.has(callee)) {
|
|
|
|
return getUnnormalizedProps(
|
|
|
|
props.arguments[0] as PropsExpression,
|
|
|
|
callPath.concat(props)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [props, callPath]
|
|
|
|
}
|
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
|
2021-06-23 07:15:20 +08:00
|
|
|
/**
|
|
|
|
* 1. mergeProps(...)
|
|
|
|
* 2. toHandlers(...)
|
|
|
|
* 3. normalizeProps(...)
|
|
|
|
* 4. normalizeProps(guardReactiveProps(...))
|
|
|
|
*
|
|
|
|
* we need to get the real props before normalization
|
|
|
|
*/
|
2021-12-10 15:34:23 +08:00
|
|
|
let props =
|
|
|
|
node.type === NodeTypes.VNODE_CALL ? node.props : node.arguments[2]
|
2021-06-23 07:15:20 +08:00
|
|
|
let callPath: CallExpression[] = []
|
|
|
|
let parentCall: CallExpression | undefined
|
|
|
|
if (
|
|
|
|
props &&
|
|
|
|
!isString(props) &&
|
|
|
|
props.type === NodeTypes.JS_CALL_EXPRESSION
|
|
|
|
) {
|
|
|
|
const ret = getUnnormalizedProps(props)
|
|
|
|
props = ret[0]
|
|
|
|
callPath = ret[1]
|
|
|
|
parentCall = callPath[callPath.length - 1]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
])
|
2021-06-23 07:15:20 +08:00
|
|
|
// in the case of nested helper call, e.g. `normalizeProps(guardReactiveProps(props))`,
|
|
|
|
// it will be rewritten as `normalizeProps(mergeProps({ key: 0 }, props))`,
|
|
|
|
// the `guardReactiveProps` will no longer be needed
|
|
|
|
if (parentCall && parentCall.callee === GUARD_REACTIVE_PROPS) {
|
|
|
|
parentCall = callPath[callPath.length - 2]
|
|
|
|
}
|
2019-10-04 00:03:14 +08:00
|
|
|
}
|
2020-02-12 07:12:56 +08:00
|
|
|
if (node.type === NodeTypes.VNODE_CALL) {
|
2021-06-23 07:15:20 +08:00
|
|
|
if (parentCall) {
|
|
|
|
parentCall.arguments[0] = propsWithInjection
|
|
|
|
} else {
|
|
|
|
node.props = propsWithInjection
|
|
|
|
}
|
2019-10-08 22:50:00 +08:00
|
|
|
} else {
|
2021-06-23 07:15:20 +08:00
|
|
|
if (parentCall) {
|
|
|
|
parentCall.arguments[0] = propsWithInjection
|
|
|
|
} else {
|
|
|
|
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,
|
2021-04-20 00:08:26 +08:00
|
|
|
type: 'component' | 'directive' | 'filter'
|
2019-10-06 05:18:25 +08:00
|
|
|
): string {
|
2021-08-24 23:48:08 +08:00
|
|
|
// see issue#4422, we need adding identifier on validAssetId if variable `name` has specific character
|
|
|
|
return `_${type}_${name.replace(/[^\w]/g, (searchValue, replaceValue) => {
|
|
|
|
return searchValue === '-' ? '_' : name.charCodeAt(replaceValue).toString()
|
|
|
|
})}`
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-07-10 09:41:44 +08:00
|
|
|
|
|
|
|
export function getMemoedVNodeCall(node: BlockCodegenNode | MemoExpression) {
|
|
|
|
if (node.type === NodeTypes.JS_CALL_EXPRESSION && node.callee === WITH_MEMO) {
|
|
|
|
return node.arguments[1].returns as VNodeCall
|
|
|
|
} else {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function makeBlock(
|
|
|
|
node: VNodeCall,
|
|
|
|
{ helper, removeHelper, inSSR }: TransformContext
|
|
|
|
) {
|
|
|
|
if (!node.isBlock) {
|
|
|
|
node.isBlock = true
|
|
|
|
removeHelper(getVNodeHelper(inSSR, node.isComponent))
|
|
|
|
helper(OPEN_BLOCK)
|
|
|
|
helper(getVNodeBlockHelper(inSSR, node.isComponent))
|
|
|
|
}
|
|
|
|
}
|