source: imaps-frontend/node_modules/jsx-ast-utils/lib/values/expressions/index.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: 9.3 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9exports.default = extract;
10exports.extractLiteral = extractLiteral;
11
12var _Literal = require('../Literal');
13
14var _Literal2 = _interopRequireDefault(_Literal);
15
16var _JSXElement = require('../JSXElement');
17
18var _JSXElement2 = _interopRequireDefault(_JSXElement);
19
20var _JSXFragment = require('../JSXFragment');
21
22var _JSXFragment2 = _interopRequireDefault(_JSXFragment);
23
24var _JSXText = require('../JSXText');
25
26var _JSXText2 = _interopRequireDefault(_JSXText);
27
28var _Identifier = require('./Identifier');
29
30var _Identifier2 = _interopRequireDefault(_Identifier);
31
32var _TaggedTemplateExpression = require('./TaggedTemplateExpression');
33
34var _TaggedTemplateExpression2 = _interopRequireDefault(_TaggedTemplateExpression);
35
36var _TemplateLiteral = require('./TemplateLiteral');
37
38var _TemplateLiteral2 = _interopRequireDefault(_TemplateLiteral);
39
40var _FunctionExpression = require('./FunctionExpression');
41
42var _FunctionExpression2 = _interopRequireDefault(_FunctionExpression);
43
44var _LogicalExpression = require('./LogicalExpression');
45
46var _LogicalExpression2 = _interopRequireDefault(_LogicalExpression);
47
48var _MemberExpression = require('./MemberExpression');
49
50var _MemberExpression2 = _interopRequireDefault(_MemberExpression);
51
52var _ChainExpression = require('./ChainExpression');
53
54var _ChainExpression2 = _interopRequireDefault(_ChainExpression);
55
56var _OptionalCallExpression = require('./OptionalCallExpression');
57
58var _OptionalCallExpression2 = _interopRequireDefault(_OptionalCallExpression);
59
60var _OptionalMemberExpression = require('./OptionalMemberExpression');
61
62var _OptionalMemberExpression2 = _interopRequireDefault(_OptionalMemberExpression);
63
64var _CallExpression = require('./CallExpression');
65
66var _CallExpression2 = _interopRequireDefault(_CallExpression);
67
68var _UnaryExpression = require('./UnaryExpression');
69
70var _UnaryExpression2 = _interopRequireDefault(_UnaryExpression);
71
72var _ThisExpression = require('./ThisExpression');
73
74var _ThisExpression2 = _interopRequireDefault(_ThisExpression);
75
76var _ConditionalExpression = require('./ConditionalExpression');
77
78var _ConditionalExpression2 = _interopRequireDefault(_ConditionalExpression);
79
80var _BinaryExpression = require('./BinaryExpression');
81
82var _BinaryExpression2 = _interopRequireDefault(_BinaryExpression);
83
84var _ObjectExpression = require('./ObjectExpression');
85
86var _ObjectExpression2 = _interopRequireDefault(_ObjectExpression);
87
88var _NewExpression = require('./NewExpression');
89
90var _NewExpression2 = _interopRequireDefault(_NewExpression);
91
92var _UpdateExpression = require('./UpdateExpression');
93
94var _UpdateExpression2 = _interopRequireDefault(_UpdateExpression);
95
96var _ArrayExpression = require('./ArrayExpression');
97
98var _ArrayExpression2 = _interopRequireDefault(_ArrayExpression);
99
100var _BindExpression = require('./BindExpression');
101
102var _BindExpression2 = _interopRequireDefault(_BindExpression);
103
104var _SpreadElement = require('./SpreadElement');
105
106var _SpreadElement2 = _interopRequireDefault(_SpreadElement);
107
108var _TypeCastExpression = require('./TypeCastExpression');
109
110var _TypeCastExpression2 = _interopRequireDefault(_TypeCastExpression);
111
112var _SequenceExpression = require('./SequenceExpression');
113
114var _SequenceExpression2 = _interopRequireDefault(_SequenceExpression);
115
116var _TSNonNullExpression = require('./TSNonNullExpression');
117
118var _TSNonNullExpression2 = _interopRequireDefault(_TSNonNullExpression);
119
120var _AssignmentExpression = require('./AssignmentExpression');
121
122var _AssignmentExpression2 = _interopRequireDefault(_AssignmentExpression);
123
124function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
125
126// Composition map of types to their extractor functions.
127var TYPES = {
128 Identifier: _Identifier2.default,
129 Literal: _Literal2.default,
130 JSXElement: _JSXElement2.default,
131 JSXFragment: _JSXFragment2.default,
132 JSXText: _JSXText2.default,
133 TaggedTemplateExpression: _TaggedTemplateExpression2.default,
134 TemplateLiteral: _TemplateLiteral2.default,
135 ArrowFunctionExpression: _FunctionExpression2.default,
136 FunctionExpression: _FunctionExpression2.default,
137 LogicalExpression: _LogicalExpression2.default,
138 MemberExpression: _MemberExpression2.default,
139 ChainExpression: _ChainExpression2.default,
140 OptionalCallExpression: _OptionalCallExpression2.default,
141 OptionalMemberExpression: _OptionalMemberExpression2.default,
142 CallExpression: _CallExpression2.default,
143 UnaryExpression: _UnaryExpression2.default,
144 ThisExpression: _ThisExpression2.default,
145 ConditionalExpression: _ConditionalExpression2.default,
146 BinaryExpression: _BinaryExpression2.default,
147 ObjectExpression: _ObjectExpression2.default,
148 NewExpression: _NewExpression2.default,
149 UpdateExpression: _UpdateExpression2.default,
150 ArrayExpression: _ArrayExpression2.default,
151 BindExpression: _BindExpression2.default,
152 SpreadElement: _SpreadElement2.default,
153 TypeCastExpression: _TypeCastExpression2.default,
154 SequenceExpression: _SequenceExpression2.default,
155 TSNonNullExpression: _TSNonNullExpression2.default,
156 AssignmentExpression: _AssignmentExpression2.default
157};
158
159var noop = function noop() {
160 return null;
161};
162
163var errorMessage = function errorMessage(expression) {
164 return 'The prop value with an expression type of ' + expression + ' could not be resolved. Please file an issue ( https://github.com/jsx-eslint/jsx-ast-utils/issues/new ) to get this fixed immediately.';
165};
166
167/**
168 * This function maps an AST value node
169 * to its correct extractor function for its
170 * given type.
171 *
172 * This will map correctly for *all* possible expression types.
173 *
174 * @param - value - AST Value object with type `JSXExpressionContainer`
175 * @returns The extracted value.
176 */
177function extract(value) {
178 // Value will not have the expression property when we recurse.
179 // The type for expression on ArrowFunctionExpression is a boolean.
180 var expression = void 0;
181 if (typeof value.expression !== 'boolean' && value.expression) {
182 expression = value.expression; // eslint-disable-line prefer-destructuring
183 } else {
184 expression = value;
185 }
186 var _expression = expression,
187 type = _expression.type;
188
189 // Typescript NonNull Expression is wrapped & it would end up in the wrong extractor
190
191 if (expression.object && expression.object.type === 'TSNonNullExpression') {
192 type = 'TSNonNullExpression';
193 }
194
195 while (type === 'TSAsExpression') {
196 var _expression2 = expression;
197 type = _expression2.type;
198
199 if (expression.expression) {
200 var _expression3 = expression;
201 expression = _expression3.expression;
202 }
203 }
204
205 if (TYPES[type] === undefined) {
206 // eslint-disable-next-line no-console
207 console.error(errorMessage(type));
208 return null;
209 }
210
211 return TYPES[type](expression);
212}
213
214// Composition map of types to their extractor functions to handle literals.
215var LITERAL_TYPES = _extends({}, TYPES, {
216 Literal: function Literal(value) {
217 var extractedVal = TYPES.Literal.call(undefined, value);
218 var isNull = extractedVal === null;
219 // This will be convention for attributes that have null
220 // value explicitly defined (<div prop={null} /> maps to 'null').
221 return isNull ? 'null' : extractedVal;
222 },
223 Identifier: function Identifier(value) {
224 var isUndefined = TYPES.Identifier.call(undefined, value) === undefined;
225 return isUndefined ? undefined : null;
226 },
227 JSXElement: noop,
228 JSXFragment: noop,
229 JSXText: noop,
230 ArrowFunctionExpression: noop,
231 FunctionExpression: noop,
232 LogicalExpression: noop,
233 MemberExpression: noop,
234 OptionalCallExpression: noop,
235 OptionalMemberExpression: noop,
236 CallExpression: noop,
237 UnaryExpression: function UnaryExpression(value) {
238 var extractedVal = TYPES.UnaryExpression.call(undefined, value);
239 return extractedVal === undefined ? null : extractedVal;
240 },
241 UpdateExpression: function UpdateExpression(value) {
242 var extractedVal = TYPES.UpdateExpression.call(undefined, value);
243 return extractedVal === undefined ? null : extractedVal;
244 },
245 ThisExpression: noop,
246 ConditionalExpression: noop,
247 BinaryExpression: noop,
248 ObjectExpression: noop,
249 NewExpression: noop,
250 ArrayExpression: function ArrayExpression(value) {
251 var extractedVal = TYPES.ArrayExpression.call(undefined, value);
252 return extractedVal.filter(function (val) {
253 return val !== null;
254 });
255 },
256 BindExpression: noop,
257 SpreadElement: noop,
258 TSNonNullExpression: noop,
259 TSAsExpression: noop,
260 TypeCastExpression: noop,
261 SequenceExpression: noop,
262 ChainExpression: noop
263});
264
265/**
266 * This function maps an AST value node
267 * to its correct extractor function for its
268 * given type.
269 *
270 * This will map correctly for *some* possible types that map to literals.
271 *
272 * @param - value - AST Value object with type `JSXExpressionContainer`
273 * @returns The extracted value.
274 */
275function extractLiteral(value) {
276 // Value will not have the expression property when we recurse.
277 var expression = value.expression || value;
278 var type = expression.type;
279
280
281 if (LITERAL_TYPES[type] === undefined) {
282 // eslint-disable-next-line no-console
283 console.error(errorMessage(type));
284 return null;
285 }
286
287 return LITERAL_TYPES[type](expression);
288}
Note: See TracBrowser for help on using the repository browser.