index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 = extract;
  7. exports.extractLiteral = extractLiteral;
  8. var _Literal = require('../Literal');
  9. var _Literal2 = _interopRequireDefault(_Literal);
  10. var _JSXElement = require('../JSXElement');
  11. var _JSXElement2 = _interopRequireDefault(_JSXElement);
  12. var _Identifier = require('./Identifier');
  13. var _Identifier2 = _interopRequireDefault(_Identifier);
  14. var _TaggedTemplateExpression = require('./TaggedTemplateExpression');
  15. var _TaggedTemplateExpression2 = _interopRequireDefault(_TaggedTemplateExpression);
  16. var _TemplateLiteral = require('./TemplateLiteral');
  17. var _TemplateLiteral2 = _interopRequireDefault(_TemplateLiteral);
  18. var _FunctionExpression = require('./FunctionExpression');
  19. var _FunctionExpression2 = _interopRequireDefault(_FunctionExpression);
  20. var _LogicalExpression = require('./LogicalExpression');
  21. var _LogicalExpression2 = _interopRequireDefault(_LogicalExpression);
  22. var _MemberExpression = require('./MemberExpression');
  23. var _MemberExpression2 = _interopRequireDefault(_MemberExpression);
  24. var _ChainExpression = require('./ChainExpression');
  25. var _ChainExpression2 = _interopRequireDefault(_ChainExpression);
  26. var _OptionalCallExpression = require('./OptionalCallExpression');
  27. var _OptionalCallExpression2 = _interopRequireDefault(_OptionalCallExpression);
  28. var _OptionalMemberExpression = require('./OptionalMemberExpression');
  29. var _OptionalMemberExpression2 = _interopRequireDefault(_OptionalMemberExpression);
  30. var _CallExpression = require('./CallExpression');
  31. var _CallExpression2 = _interopRequireDefault(_CallExpression);
  32. var _UnaryExpression = require('./UnaryExpression');
  33. var _UnaryExpression2 = _interopRequireDefault(_UnaryExpression);
  34. var _ThisExpression = require('./ThisExpression');
  35. var _ThisExpression2 = _interopRequireDefault(_ThisExpression);
  36. var _ConditionalExpression = require('./ConditionalExpression');
  37. var _ConditionalExpression2 = _interopRequireDefault(_ConditionalExpression);
  38. var _BinaryExpression = require('./BinaryExpression');
  39. var _BinaryExpression2 = _interopRequireDefault(_BinaryExpression);
  40. var _ObjectExpression = require('./ObjectExpression');
  41. var _ObjectExpression2 = _interopRequireDefault(_ObjectExpression);
  42. var _NewExpression = require('./NewExpression');
  43. var _NewExpression2 = _interopRequireDefault(_NewExpression);
  44. var _UpdateExpression = require('./UpdateExpression');
  45. var _UpdateExpression2 = _interopRequireDefault(_UpdateExpression);
  46. var _ArrayExpression = require('./ArrayExpression');
  47. var _ArrayExpression2 = _interopRequireDefault(_ArrayExpression);
  48. var _BindExpression = require('./BindExpression');
  49. var _BindExpression2 = _interopRequireDefault(_BindExpression);
  50. var _SpreadElement = require('./SpreadElement');
  51. var _SpreadElement2 = _interopRequireDefault(_SpreadElement);
  52. var _TypeCastExpression = require('./TypeCastExpression');
  53. var _TypeCastExpression2 = _interopRequireDefault(_TypeCastExpression);
  54. var _SequenceExpression = require('./SequenceExpression');
  55. var _SequenceExpression2 = _interopRequireDefault(_SequenceExpression);
  56. var _TSNonNullExpression = require('./TSNonNullExpression');
  57. var _TSNonNullExpression2 = _interopRequireDefault(_TSNonNullExpression);
  58. var _AssignmentExpression = require('./AssignmentExpression');
  59. var _AssignmentExpression2 = _interopRequireDefault(_AssignmentExpression);
  60. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  61. // Composition map of types to their extractor functions.
  62. var TYPES = {
  63. Identifier: _Identifier2.default,
  64. Literal: _Literal2.default,
  65. JSXElement: _JSXElement2.default,
  66. TaggedTemplateExpression: _TaggedTemplateExpression2.default,
  67. TemplateLiteral: _TemplateLiteral2.default,
  68. ArrowFunctionExpression: _FunctionExpression2.default,
  69. FunctionExpression: _FunctionExpression2.default,
  70. LogicalExpression: _LogicalExpression2.default,
  71. MemberExpression: _MemberExpression2.default,
  72. ChainExpression: _ChainExpression2.default,
  73. OptionalCallExpression: _OptionalCallExpression2.default,
  74. OptionalMemberExpression: _OptionalMemberExpression2.default,
  75. CallExpression: _CallExpression2.default,
  76. UnaryExpression: _UnaryExpression2.default,
  77. ThisExpression: _ThisExpression2.default,
  78. ConditionalExpression: _ConditionalExpression2.default,
  79. BinaryExpression: _BinaryExpression2.default,
  80. ObjectExpression: _ObjectExpression2.default,
  81. NewExpression: _NewExpression2.default,
  82. UpdateExpression: _UpdateExpression2.default,
  83. ArrayExpression: _ArrayExpression2.default,
  84. BindExpression: _BindExpression2.default,
  85. SpreadElement: _SpreadElement2.default,
  86. TypeCastExpression: _TypeCastExpression2.default,
  87. SequenceExpression: _SequenceExpression2.default,
  88. TSNonNullExpression: _TSNonNullExpression2.default,
  89. AssignmentExpression: _AssignmentExpression2.default
  90. };
  91. var noop = function noop() {
  92. return null;
  93. };
  94. var errorMessage = function errorMessage(expression) {
  95. return 'The prop value with an expression type of ' + expression + ' could not be resolved. Please file issue to get this fixed immediately.';
  96. };
  97. /**
  98. * This function maps an AST value node
  99. * to its correct extractor function for its
  100. * given type.
  101. *
  102. * This will map correctly for *all* possible expression types.
  103. *
  104. * @param - value - AST Value object with type `JSXExpressionContainer`
  105. * @returns The extracted value.
  106. */
  107. function extract(value) {
  108. // Value will not have the expression property when we recurse.
  109. // The type for expression on ArrowFunctionExpression is a boolean.
  110. var expression = void 0;
  111. if (typeof value.expression !== 'boolean' && value.expression) {
  112. expression = value.expression; // eslint-disable-line prefer-destructuring
  113. } else {
  114. expression = value;
  115. }
  116. var _expression = expression,
  117. type = _expression.type;
  118. // Typescript NonNull Expression is wrapped & it would end up in the wrong extractor
  119. if (expression.object && expression.object.type === 'TSNonNullExpression') {
  120. type = 'TSNonNullExpression';
  121. }
  122. while (type === 'TSAsExpression') {
  123. var _expression2 = expression;
  124. type = _expression2.type;
  125. if (expression.expression) {
  126. var _expression3 = expression;
  127. expression = _expression3.expression;
  128. }
  129. }
  130. if (TYPES[type] === undefined) {
  131. // eslint-disable-next-line no-console
  132. console.error(errorMessage(type));
  133. return null;
  134. }
  135. return TYPES[type](expression);
  136. }
  137. // Composition map of types to their extractor functions to handle literals.
  138. var LITERAL_TYPES = _extends({}, TYPES, {
  139. Literal: function Literal(value) {
  140. var extractedVal = TYPES.Literal.call(undefined, value);
  141. var isNull = extractedVal === null;
  142. // This will be convention for attributes that have null
  143. // value explicitly defined (<div prop={null} /> maps to 'null').
  144. return isNull ? 'null' : extractedVal;
  145. },
  146. Identifier: function Identifier(value) {
  147. var isUndefined = TYPES.Identifier.call(undefined, value) === undefined;
  148. return isUndefined ? undefined : null;
  149. },
  150. JSXElement: noop,
  151. ArrowFunctionExpression: noop,
  152. FunctionExpression: noop,
  153. LogicalExpression: noop,
  154. MemberExpression: noop,
  155. OptionalCallExpression: noop,
  156. OptionalMemberExpression: noop,
  157. CallExpression: noop,
  158. UnaryExpression: function UnaryExpression(value) {
  159. var extractedVal = TYPES.UnaryExpression.call(undefined, value);
  160. return extractedVal === undefined ? null : extractedVal;
  161. },
  162. UpdateExpression: function UpdateExpression(value) {
  163. var extractedVal = TYPES.UpdateExpression.call(undefined, value);
  164. return extractedVal === undefined ? null : extractedVal;
  165. },
  166. ThisExpression: noop,
  167. ConditionalExpression: noop,
  168. BinaryExpression: noop,
  169. ObjectExpression: noop,
  170. NewExpression: noop,
  171. ArrayExpression: function ArrayExpression(value) {
  172. var extractedVal = TYPES.ArrayExpression.call(undefined, value);
  173. return extractedVal.filter(function (val) {
  174. return val !== null;
  175. });
  176. },
  177. BindExpression: noop,
  178. SpreadElement: noop,
  179. TSNonNullExpression: noop,
  180. TSAsExpression: noop,
  181. TypeCastExpression: noop,
  182. SequenceExpression: noop
  183. });
  184. /**
  185. * This function maps an AST value node
  186. * to its correct extractor function for its
  187. * given type.
  188. *
  189. * This will map correctly for *some* possible types that map to literals.
  190. *
  191. * @param - value - AST Value object with type `JSXExpressionContainer`
  192. * @returns The extracted value.
  193. */
  194. function extractLiteral(value) {
  195. // Value will not have the expression property when we recurse.
  196. var expression = value.expression || value;
  197. var type = expression.type;
  198. if (LITERAL_TYPES[type] === undefined) {
  199. // eslint-disable-next-line no-console
  200. console.error(errorMessage(type));
  201. return null;
  202. }
  203. return LITERAL_TYPES[type](expression);
  204. }