index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import Literal from './Literal';
  2. import JSXElement from './JSXElement';
  3. import JSXText from './JSXText';
  4. import JSXExpressionContainer, { extractLiteral } from './expressions';
  5. // Composition map of types to their extractor functions.
  6. const TYPES = {
  7. Literal,
  8. JSXElement,
  9. JSXExpressionContainer,
  10. JSXText,
  11. };
  12. // Composition map of types to their extractor functions to handle literals.
  13. const LITERAL_TYPES = {
  14. ...TYPES,
  15. JSXElement: () => null,
  16. JSXExpressionContainer: extractLiteral,
  17. };
  18. /**
  19. * This function maps an AST value node
  20. * to its correct extractor function for its
  21. * given type.
  22. *
  23. * This will map correctly for *all* possible types.
  24. *
  25. * @param value - AST Value object on a JSX Attribute.
  26. */
  27. export default function getValue(value) {
  28. return TYPES[value.type](value);
  29. }
  30. /**
  31. * This function maps an AST value node
  32. * to its correct extractor function for its
  33. * given type.
  34. *
  35. * This will map correctly for *some* possible types that map to literals.
  36. *
  37. * @param value - AST Value object on a JSX Attribute.
  38. */
  39. export function getLiteralValue(value) {
  40. return LITERAL_TYPES[value.type](value);
  41. }