testUtils.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {
  2. NodeTypes,
  3. CallExpression,
  4. ElementNode,
  5. locStub,
  6. Namespaces,
  7. ElementTypes
  8. } from '../src'
  9. import { CREATE_VNODE } from '../src/runtimeHelpers'
  10. import { isString } from '@vue/shared'
  11. const leadingBracketRE = /^\[/
  12. const bracketsRE = /^\[|\]$/g
  13. // Create a matcher for an object
  14. // where non-static expressions should be wrapped in []
  15. // e.g.
  16. // - createObjectMatcher({ 'foo': '[bar]' }) matches { foo: bar }
  17. // - createObjectMatcher({ '[foo]': 'bar' }) matches { [foo]: "bar" }
  18. export function createObjectMatcher(obj: any) {
  19. return {
  20. type: NodeTypes.JS_OBJECT_EXPRESSION,
  21. properties: Object.keys(obj).map(key => ({
  22. type: NodeTypes.JS_PROPERTY,
  23. key: {
  24. type: NodeTypes.SIMPLE_EXPRESSION,
  25. content: key.replace(bracketsRE, ''),
  26. isStatic: !leadingBracketRE.test(key)
  27. },
  28. value: isString(obj[key])
  29. ? {
  30. type: NodeTypes.SIMPLE_EXPRESSION,
  31. content: obj[key].replace(bracketsRE, ''),
  32. isStatic: !leadingBracketRE.test(obj[key])
  33. }
  34. : obj[key]
  35. }))
  36. }
  37. }
  38. export function createElementWithCodegen(
  39. args: CallExpression['arguments']
  40. ): ElementNode {
  41. return {
  42. type: NodeTypes.ELEMENT,
  43. loc: locStub,
  44. ns: Namespaces.HTML,
  45. tag: 'div',
  46. tagType: ElementTypes.ELEMENT,
  47. isSelfClosing: false,
  48. props: [],
  49. children: [],
  50. codegenNode: {
  51. type: NodeTypes.JS_CALL_EXPRESSION,
  52. loc: locStub,
  53. callee: CREATE_VNODE,
  54. arguments: args
  55. }
  56. }
  57. }