getPropValue.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import getValue, { getLiteralValue } from './values';
  2. const extractValue = (attribute, extractor) => {
  3. if (attribute && attribute.type === 'JSXAttribute') {
  4. if (attribute.value === null) {
  5. // Null valued attributes imply truthiness.
  6. // For example: <div aria-hidden />
  7. // See: https://facebook.github.io/react/docs/jsx-in-depth.html#boolean-attributes
  8. return true;
  9. }
  10. return extractor(attribute.value);
  11. }
  12. return undefined;
  13. };
  14. /**
  15. * Returns the value of a given attribute.
  16. * Different types of attributes have their associated
  17. * values in different properties on the object.
  18. *
  19. * This function should return the most *closely* associated
  20. * value with the intention of the JSX.
  21. *
  22. * @param attribute - The JSXAttribute collected by AST parser.
  23. */
  24. export default function getPropValue(attribute) {
  25. return extractValue(attribute, getValue);
  26. }
  27. /**
  28. * Returns the value of a given attribute.
  29. * Different types of attributes have their associated
  30. * values in different properties on the object.
  31. *
  32. * This function should return a value only if we can extract
  33. * a literal value from its attribute (i.e. values that have generic
  34. * types in JavaScript - strings, numbers, booleans, etc.)
  35. *
  36. * @param attribute - The JSXAttribute collected by AST parser.
  37. */
  38. export function getLiteralPropValue(attribute) {
  39. return extractValue(attribute, getLiteralValue);
  40. }