getProp.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  6. 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; };
  7. exports.default = getProp;
  8. var _propName = require('./propName');
  9. var _propName2 = _interopRequireDefault(_propName);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
  12. var DEFAULT_OPTIONS = {
  13. ignoreCase: true
  14. };
  15. /**
  16. * Returns the JSXAttribute itself or undefined, indicating the prop
  17. * is not present on the JSXOpeningElement.
  18. *
  19. */
  20. function getProp() {
  21. var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  22. var prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  23. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_OPTIONS;
  24. function getName(name) {
  25. return options.ignoreCase ? name.toUpperCase() : name;
  26. }
  27. var propToFind = getName(prop);
  28. function isPropToFind(property) {
  29. return property.type === 'Property' && property.key.type === 'Identifier' && propToFind === getName(property.key.name);
  30. }
  31. var foundAttribute = props.find(function (attribute) {
  32. // If the props contain a spread prop, try to find the property in the object expression.
  33. if (attribute.type === 'JSXSpreadAttribute') {
  34. return attribute.argument.type === 'ObjectExpression' && propToFind !== getName('key') // https://github.com/reactjs/rfcs/pull/107
  35. && attribute.argument.properties.some(isPropToFind);
  36. }
  37. return propToFind === getName((0, _propName2.default)(attribute));
  38. });
  39. if (foundAttribute && foundAttribute.type === 'JSXSpreadAttribute') {
  40. return propertyToJSXAttribute(foundAttribute.argument.properties.find(isPropToFind));
  41. }
  42. return foundAttribute;
  43. }
  44. function propertyToJSXAttribute(node) {
  45. var key = node.key,
  46. value = node.value;
  47. return _extends({
  48. type: 'JSXAttribute',
  49. name: _extends({ type: 'JSXIdentifier', name: key.name }, getBaseProps(key)),
  50. value: value.type === 'Literal' ? adjustRangeOfNode(value) : _extends({ type: 'JSXExpressionContainer', expression: adjustExpressionRange(value) }, getBaseProps(value))
  51. }, getBaseProps(node));
  52. }
  53. function adjustRangeOfNode(node) {
  54. var _ref = node.range || [node.start, node.end],
  55. _ref2 = _slicedToArray(_ref, 2),
  56. start = _ref2[0],
  57. end = _ref2[1];
  58. return _extends({}, node, {
  59. end: undefined,
  60. range: [start, end],
  61. start: undefined
  62. });
  63. }
  64. function adjustExpressionRange(_ref3) {
  65. var expressions = _ref3.expressions,
  66. quasis = _ref3.quasis,
  67. expression = _objectWithoutProperties(_ref3, ['expressions', 'quasis']);
  68. return _extends({}, adjustRangeOfNode(expression), expressions ? { expressions: expressions.map(adjustRangeOfNode) } : {}, quasis ? { quasis: quasis.map(adjustRangeOfNode) } : {});
  69. }
  70. function getBaseProps(_ref4) {
  71. var loc = _ref4.loc,
  72. node = _objectWithoutProperties(_ref4, ['loc']);
  73. var _adjustRangeOfNode = adjustRangeOfNode(node),
  74. range = _adjustRangeOfNode.range;
  75. return {
  76. loc: getBaseLocation(loc),
  77. range: range
  78. };
  79. }
  80. function getBaseLocation(_ref5) {
  81. var start = _ref5.start,
  82. end = _ref5.end,
  83. source = _ref5.source,
  84. filename = _ref5.filename;
  85. return _extends({
  86. start: start,
  87. end: end
  88. }, source !== undefined ? { source: source } : {}, filename !== undefined ? { filename: filename } : {});
  89. }