source: imaps-frontend/node_modules/jsx-ast-utils/lib/getProp.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.4 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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"); } }; }();
8
9var _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; };
10
11exports.default = getProp;
12
13var _propName = require('./propName');
14
15var _propName2 = _interopRequireDefault(_propName);
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19function _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; }
20
21var DEFAULT_OPTIONS = {
22 ignoreCase: true
23};
24
25/**
26 * Returns the JSXAttribute itself or undefined, indicating the prop
27 * is not present on the JSXOpeningElement.
28 *
29 */
30function getProp() {
31 var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
32 var prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
33 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_OPTIONS;
34
35 function getName(name) {
36 return options.ignoreCase ? name.toUpperCase() : name;
37 }
38 var propToFind = getName(prop);
39 function isPropToFind(property) {
40 return property.type === 'Property' && property.key.type === 'Identifier' && propToFind === getName(property.key.name);
41 }
42
43 var foundAttribute = props.find(function (attribute) {
44 // If the props contain a spread prop, try to find the property in the object expression.
45 if (attribute.type === 'JSXSpreadAttribute') {
46 return attribute.argument.type === 'ObjectExpression' && propToFind !== getName('key') // https://github.com/reactjs/rfcs/pull/107
47 && attribute.argument.properties.some(isPropToFind);
48 }
49
50 return propToFind === getName((0, _propName2.default)(attribute));
51 });
52
53 if (foundAttribute && foundAttribute.type === 'JSXSpreadAttribute') {
54 return propertyToJSXAttribute(foundAttribute.argument.properties.find(isPropToFind));
55 }
56
57 return foundAttribute;
58}
59
60function propertyToJSXAttribute(node) {
61 var key = node.key,
62 value = node.value;
63
64 return _extends({
65 type: 'JSXAttribute',
66 name: _extends({ type: 'JSXIdentifier', name: key.name }, getBaseProps(key)),
67 value: value.type === 'Literal' ? adjustRangeOfNode(value) : _extends({ type: 'JSXExpressionContainer', expression: adjustExpressionRange(value) }, getBaseProps(value))
68 }, getBaseProps(node));
69}
70
71function adjustRangeOfNode(node) {
72 var _ref = node.range || [node.start, node.end],
73 _ref2 = _slicedToArray(_ref, 2),
74 start = _ref2[0],
75 end = _ref2[1];
76
77 return _extends({}, node, {
78 end: undefined,
79 range: [start, end],
80 start: undefined
81 });
82}
83
84function adjustExpressionRange(_ref3) {
85 var expressions = _ref3.expressions,
86 quasis = _ref3.quasis,
87 expression = _objectWithoutProperties(_ref3, ['expressions', 'quasis']);
88
89 return _extends({}, adjustRangeOfNode(expression), expressions ? { expressions: expressions.map(adjustRangeOfNode) } : {}, quasis ? { quasis: quasis.map(adjustRangeOfNode) } : {});
90}
91
92function getBaseProps(_ref4) {
93 var loc = _ref4.loc,
94 node = _objectWithoutProperties(_ref4, ['loc']);
95
96 var _adjustRangeOfNode = adjustRangeOfNode(node),
97 range = _adjustRangeOfNode.range;
98
99 return {
100 loc: getBaseLocation(loc),
101 range: range
102 };
103}
104
105function getBaseLocation(_ref5) {
106 var start = _ref5.start,
107 end = _ref5.end,
108 source = _ref5.source,
109 filename = _ref5.filename;
110
111 return _extends({
112 start: start,
113 end: end
114 }, source !== undefined ? { source: source } : {}, filename !== undefined ? { filename: filename } : {});
115}
Note: See TracBrowser for help on using the repository browser.