ast.ts 3.8 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
import {
  createCompilerError,
fxy060608's avatar
fxy060608 已提交
30 31
  createSimpleExpression,
  DirectiveNode,
fxy060608's avatar
fxy060608 已提交
32 33
  ErrorCodes,
  ExpressionNode,
fxy060608's avatar
fxy060608 已提交
34 35
  locStub,
  NodeTypes,
fxy060608's avatar
fxy060608 已提交
36
} from '@vue/compiler-core'
fxy060608's avatar
fxy060608 已提交
37
import { CodegenScope, CodegenVIfScope } from './options'
fxy060608's avatar
fxy060608 已提交
38 39
import { TransformContext } from './transform'
import { genExpr } from './codegen'
fxy060608's avatar
fxy060608 已提交
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 74 75 76 77

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 已提交
78 79 80 81 82 83 84
// 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 已提交
85

fxy060608's avatar
fxy060608 已提交
86 87 88 89 90 91 92 93 94 95
export function parseExpr(
  code: string | ExpressionNode,
  context: TransformContext,
  node?: ExpressionNode
) {
  if (!isString(code)) {
    node = code
    code = genExpr(code)
  }
  try {
fxy060608's avatar
fxy060608 已提交
96 97 98
    return parseExpression(code, {
      plugins: context.expressionPlugins,
    })
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  } 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 已提交
122 123 124 125
export function isUndefined(expr: Expression) {
  return isIdentifier(expr) && expr.name === 'undefined'
}

fxy060608's avatar
fxy060608 已提交
126
export function isTrueExpr(expr: Literal) {
fxy060608's avatar
fxy060608 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  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 已提交
142
export function parseStringLiteral(
fxy060608's avatar
fxy060608 已提交
143 144 145 146 147 148 149 150 151 152
  expr: Expression | Identifier | StringLiteral | NumericLiteral
) {
  if (isIdentifier(expr)) {
    return stringLiteral(expr.name)
  }
  if (isStringLiteral(expr)) {
    return stringLiteral(expr.value)
  }
  return stringLiteral('')
}
fxy060608's avatar
fxy060608 已提交
153

fxy060608's avatar
fxy060608 已提交
154
function createDirectiveNode(
fxy060608's avatar
fxy060608 已提交
155
  name: string,
fxy060608's avatar
fxy060608 已提交
156 157
  arg: string,
  exp: string
fxy060608's avatar
fxy060608 已提交
158 159 160
): DirectiveNode {
  return {
    type: NodeTypes.DIRECTIVE,
fxy060608's avatar
fxy060608 已提交
161
    name,
fxy060608's avatar
fxy060608 已提交
162 163
    modifiers: [],
    loc: locStub,
fxy060608's avatar
fxy060608 已提交
164 165
    arg: createSimpleExpression(arg, true),
    exp: createSimpleExpression(exp, false),
fxy060608's avatar
fxy060608 已提交
166 167 168
  }
}

fxy060608's avatar
fxy060608 已提交
169 170 171 172 173 174 175
export function createOnDirectiveNode(name: string, value: string) {
  return createDirectiveNode('on', name, value)
}

export function createBindDirectiveNode(name: string, value: string) {
  return createDirectiveNode('bind', name, value)
}