2024-02-09 02:40:01 +08:00
|
|
|
import type { NumericLiteral, StringLiteral } from '@babel/types'
|
2024-02-08 21:40:20 +08:00
|
|
|
import { isGloballyAllowed } from '@vue/shared'
|
2024-02-08 20:28:10 +08:00
|
|
|
import {
|
|
|
|
type AttributeNode,
|
|
|
|
type ElementNode,
|
|
|
|
NodeTypes,
|
2024-02-08 21:40:20 +08:00
|
|
|
type SimpleExpressionNode,
|
2024-02-08 20:28:10 +08:00
|
|
|
findProp as _findProp,
|
|
|
|
createSimpleExpression,
|
2024-02-08 21:40:20 +08:00
|
|
|
isLiteralWhitelisted,
|
2024-02-08 20:28:10 +08:00
|
|
|
} from '@vue/compiler-dom'
|
|
|
|
import type { VaporDirectiveNode } from './ir'
|
2024-02-09 02:40:01 +08:00
|
|
|
import { EMPTY_EXPRESSION } from './transforms/utils'
|
2024-02-08 20:28:10 +08:00
|
|
|
|
|
|
|
export const findProp = _findProp as (
|
|
|
|
node: ElementNode,
|
|
|
|
name: string,
|
|
|
|
dynamicOnly?: boolean,
|
|
|
|
allowEmpty?: boolean,
|
|
|
|
) => AttributeNode | VaporDirectiveNode | undefined
|
|
|
|
|
|
|
|
export function propToExpression(prop: AttributeNode | VaporDirectiveNode) {
|
|
|
|
return prop.type === NodeTypes.ATTRIBUTE
|
|
|
|
? prop.value
|
|
|
|
? createSimpleExpression(prop.value.content, true, prop.value.loc)
|
|
|
|
: EMPTY_EXPRESSION
|
|
|
|
: prop.exp
|
|
|
|
}
|
2024-02-08 21:40:20 +08:00
|
|
|
|
|
|
|
export function isConstantExpression(exp: SimpleExpressionNode) {
|
|
|
|
return (
|
|
|
|
isLiteralWhitelisted(exp.content) ||
|
|
|
|
isGloballyAllowed(exp.content) ||
|
|
|
|
getLiteralExpressionValue(exp) !== null
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveExpression(exp: SimpleExpressionNode) {
|
|
|
|
if (!exp.isStatic) {
|
|
|
|
const value = getLiteralExpressionValue(exp)
|
|
|
|
if (value !== null) {
|
|
|
|
return createSimpleExpression('' + value, true, exp.loc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return exp
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLiteralExpressionValue(
|
|
|
|
exp: SimpleExpressionNode,
|
|
|
|
): number | string | null {
|
|
|
|
if (
|
|
|
|
!__BROWSER__ &&
|
|
|
|
exp.ast &&
|
|
|
|
['StringLiteral', 'NumericLiteral'].includes(exp.ast.type)
|
|
|
|
) {
|
|
|
|
return (exp.ast as StringLiteral | NumericLiteral).value
|
|
|
|
}
|
|
|
|
return exp.isStatic ? exp.content : null
|
|
|
|
}
|