All files / compiler-core/src/transforms transformVBindShorthand.ts

100% Statements 11/11
89.47% Branches 17/19
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                    102x 3672x 1994x   1291x                   15x 15x   1x           1x   14x 14x         14x              
import { camelize } from '@vue/shared'
import {
  NodeTypes,
  type SimpleExpressionNode,
  createSimpleExpression,
} from '../ast'
import type { NodeTransform } from '../transform'
import { ErrorCodes, createCompilerError } from '../errors'
import { validFirstIdentCharRE } from '../utils'
 
export const transformVBindShorthand: NodeTransform = (node, context) => {
  if (node.type === NodeTypes.ELEMENT) {
    for (const prop of node.props) {
      // same-name shorthand - :arg is expanded to :arg="arg"
      if (
        prop.type === NodeTypes.DIRECTIVE &&
        prop.name === 'bind' &&
        (!prop.exp ||
          // #13930 :foo in in-DOM templates will be parsed into :foo="" by browser
          (__BROWSER__ &&
            prop.exp.type === NodeTypes.SIMPLE_EXPRESSION &&
            !prop.exp.content.trim())) &&
        prop.arg
      ) {
        const arg = prop.arg
        if (arg.type !== NodeTypes.SIMPLE_EXPRESSION || !arg.isStatic) {
          // only simple expression is allowed for same-name shorthand
          context.onError(
            createCompilerError(
              ErrorCodes.X_V_BIND_INVALID_SAME_NAME_ARGUMENT,
              arg.loc,
            ),
          )
          prop.exp = createSimpleExpression('', true, arg.loc)
        } else {
          const propName = camelize((arg as SimpleExpressionNode).content)
          Eif (
            validFirstIdentCharRE.test(propName[0]) ||
            // allow hyphen first char for https://github.com/vuejs/language-tools/pull/3424
            propName[0] === '-'
          ) {
            prop.exp = createSimpleExpression(propName, false, arg.loc)
          }
        }
      }
    }
  }
}