ast.ts 3.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { isString } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
import { parseExpression } from '@babel/parser'
import {
  identifier,
  objectProperty,
  objectExpression,
  spreadElement,
  ObjectProperty,
  ObjectExpression,
  Expression,
  SpreadElement,
  ConditionalExpression,
  Identifier,
  conditionalExpression,
fxy060608's avatar
fxy060608 已提交
15 16 17
  NumericLiteral,
  isNumericLiteral,
  ArrowFunctionExpression,
fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24 25 26
  stringLiteral,
  StringLiteral,
  isIdentifier,
  isStringLiteral,
  isBooleanLiteral,
  isBigIntLiteral,
  isDecimalLiteral,
  Literal,
  isNullLiteral,
fxy060608's avatar
fxy060608 已提交
27
} from '@babel/types'
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32
import {
  createCompilerError,
  ErrorCodes,
  ExpressionNode,
} from '@vue/compiler-core'
fxy060608's avatar
fxy060608 已提交
33
import { CodegenScope, CodegenVIfScope } from './options'
fxy060608's avatar
fxy060608 已提交
34 35
import { TransformContext } from './transform'
import { genExpr } from './codegen'
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

export function createIdentifier(name: string) {
  return identifier(name)
}
export function createObjectProperty(
  name: string,
  value: Expression
): ObjectProperty {
  return objectProperty(identifier(name), value)
}
export function createSpreadElement(argument: ConditionalExpression) {
  return spreadElement(argument)
}
export function createObjectExpression(
  properties: Array<ObjectProperty | SpreadElement>
): ObjectExpression {
  return objectExpression(properties)
}

export function createVIfProperty(condition: Expression, { id }: CodegenScope) {
  return objectProperty(identifier(id.next()), condition)
}

export function createVIfConditionalExpression({
  condition,
  properties,
}: CodegenVIfScope) {
  return conditionalExpression(
    condition!,
    objectExpression(properties),
    objectExpression([])
  )
}

export function createVIfSpreadElement(vIfScope: CodegenVIfScope) {
  return spreadElement(createVIfConditionalExpression(vIfScope))
}

fxy060608's avatar
fxy060608 已提交
74 75 76 77 78 79 80
// function numericLiteralToArrayExpr(num: number) {
//   const elements: NumericLiteral[] = []
//   for (let i = 0; i < num; i++) {
//     elements.push(numericLiteral(i + 1))
//   }
//   return arrayExpression(elements)
// }
fxy060608's avatar
fxy060608 已提交
81

fxy060608's avatar
fxy060608 已提交
82 83 84 85 86 87 88 89 90 91
export function parseExpr(
  code: string | ExpressionNode,
  context: TransformContext,
  node?: ExpressionNode
) {
  if (!isString(code)) {
    node = code
    code = genExpr(code)
  }
  try {
fxy060608's avatar
fxy060608 已提交
92 93 94
    return parseExpression(code, {
      plugins: context.expressionPlugins,
    })
fxy060608's avatar
fxy060608 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  } catch (e: any) {
    context.onError(
      createCompilerError(
        ErrorCodes.X_INVALID_EXPRESSION,
        node && node.loc,
        undefined,
        e.message
      )
    )
  }
}

export function parseParam(
  code: string,
  context: TransformContext,
  node: ExpressionNode
) {
  const {
    params: [expr],
  } = parseExpr(`(${code})=>{}`, context, node) as ArrowFunctionExpression
  return expr
}

fxy060608's avatar
fxy060608 已提交
118 119 120 121
export function isUndefined(expr: Expression) {
  return isIdentifier(expr) && expr.name === 'undefined'
}

fxy060608's avatar
fxy060608 已提交
122
export function isTrueExpr(expr: Literal) {
fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  if (isNullLiteral(expr)) {
    return false
  }
  if (
    isStringLiteral(expr) ||
    isNumericLiteral(expr) ||
    isBooleanLiteral(expr) ||
    isBigIntLiteral(expr) ||
    isDecimalLiteral(expr)
  ) {
    return !!expr.value
  }
  return true
}

fxy060608's avatar
fxy060608 已提交
138
export function parseStringLiteral(
fxy060608's avatar
fxy060608 已提交
139 140 141 142 143 144 145 146 147 148
  expr: Expression | Identifier | StringLiteral | NumericLiteral
) {
  if (isIdentifier(expr)) {
    return stringLiteral(expr.name)
  }
  if (isStringLiteral(expr)) {
    return stringLiteral(expr.value)
  }
  return stringLiteral('')
}