getPropValue.js 1.7 KB

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