vBind.ts 1003 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import {
  2. createCompilerError,
  3. createSimpleExpression,
  4. ErrorCodes,
  5. } from '@vue/compiler-core'
  6. import { camelize } from '@vue/shared'
  7. import { IRNodeTypes } from '../ir'
  8. import type { DirectiveTransform } from '../transform'
  9. export const transformVBind: DirectiveTransform = (dir, node, context) => {
  10. let { arg, exp, loc } = dir
  11. if (!arg) {
  12. // TODO support v-bind="{}"
  13. return
  14. }
  15. if (!exp) {
  16. // shorthand syntax https://github.com/vuejs/core/pull/9451
  17. const propName = camelize(arg.content)
  18. exp = createSimpleExpression(propName, false, arg.loc)
  19. exp.ast = null
  20. }
  21. if (!exp.content.trim()) {
  22. context.options.onError(
  23. createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
  24. )
  25. context.template += ` ${arg.content}=""`
  26. return
  27. }
  28. context.registerEffect(
  29. [exp],
  30. [
  31. {
  32. type: IRNodeTypes.SET_PROP,
  33. loc: dir.loc,
  34. element: context.reference(),
  35. name: arg,
  36. value: exp,
  37. },
  38. ],
  39. )
  40. }