index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  6. exports.default = getValue;
  7. exports.getLiteralValue = getLiteralValue;
  8. var _Literal = require('./Literal');
  9. var _Literal2 = _interopRequireDefault(_Literal);
  10. var _JSXElement = require('./JSXElement');
  11. var _JSXElement2 = _interopRequireDefault(_JSXElement);
  12. var _JSXText = require('./JSXText');
  13. var _JSXText2 = _interopRequireDefault(_JSXText);
  14. var _JSXFragment = require('./JSXFragment');
  15. var _JSXFragment2 = _interopRequireDefault(_JSXFragment);
  16. var _expressions = require('./expressions');
  17. var _expressions2 = _interopRequireDefault(_expressions);
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. // Composition map of types to their extractor functions.
  20. var TYPES = {
  21. Literal: _Literal2.default,
  22. JSXElement: _JSXElement2.default,
  23. JSXExpressionContainer: _expressions2.default,
  24. JSXText: _JSXText2.default,
  25. JSXFragment: _JSXFragment2.default
  26. };
  27. // Composition map of types to their extractor functions to handle literals.
  28. var LITERAL_TYPES = _extends({}, TYPES, {
  29. JSXElement: function JSXElement() {
  30. return null;
  31. },
  32. JSXExpressionContainer: _expressions.extractLiteral
  33. });
  34. /**
  35. * This function maps an AST value node
  36. * to its correct extractor function for its
  37. * given type.
  38. *
  39. * This will map correctly for *all* possible types.
  40. *
  41. * @param value - AST Value object on a JSX Attribute.
  42. */
  43. function getValue(value) {
  44. if (!TYPES[value.type]) console.log(value.type);
  45. return TYPES[value.type](value);
  46. }
  47. /**
  48. * This function maps an AST value node
  49. * to its correct extractor function for its
  50. * given type.
  51. *
  52. * This will map correctly for *some* possible types that map to literals.
  53. *
  54. * @param value - AST Value object on a JSX Attribute.
  55. */
  56. function getLiteralValue(value) {
  57. return LITERAL_TYPES[value.type](value);
  58. }