[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 |
|
---|
| 7 | 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; };
|
---|
| 8 |
|
---|
| 9 | exports.default = extract;
|
---|
| 10 | exports.extractLiteral = extractLiteral;
|
---|
| 11 |
|
---|
| 12 | var _Literal = require('../Literal');
|
---|
| 13 |
|
---|
| 14 | var _Literal2 = _interopRequireDefault(_Literal);
|
---|
| 15 |
|
---|
| 16 | var _JSXElement = require('../JSXElement');
|
---|
| 17 |
|
---|
| 18 | var _JSXElement2 = _interopRequireDefault(_JSXElement);
|
---|
| 19 |
|
---|
| 20 | var _JSXFragment = require('../JSXFragment');
|
---|
| 21 |
|
---|
| 22 | var _JSXFragment2 = _interopRequireDefault(_JSXFragment);
|
---|
| 23 |
|
---|
| 24 | var _JSXText = require('../JSXText');
|
---|
| 25 |
|
---|
| 26 | var _JSXText2 = _interopRequireDefault(_JSXText);
|
---|
| 27 |
|
---|
| 28 | var _Identifier = require('./Identifier');
|
---|
| 29 |
|
---|
| 30 | var _Identifier2 = _interopRequireDefault(_Identifier);
|
---|
| 31 |
|
---|
| 32 | var _TaggedTemplateExpression = require('./TaggedTemplateExpression');
|
---|
| 33 |
|
---|
| 34 | var _TaggedTemplateExpression2 = _interopRequireDefault(_TaggedTemplateExpression);
|
---|
| 35 |
|
---|
| 36 | var _TemplateLiteral = require('./TemplateLiteral');
|
---|
| 37 |
|
---|
| 38 | var _TemplateLiteral2 = _interopRequireDefault(_TemplateLiteral);
|
---|
| 39 |
|
---|
| 40 | var _FunctionExpression = require('./FunctionExpression');
|
---|
| 41 |
|
---|
| 42 | var _FunctionExpression2 = _interopRequireDefault(_FunctionExpression);
|
---|
| 43 |
|
---|
| 44 | var _LogicalExpression = require('./LogicalExpression');
|
---|
| 45 |
|
---|
| 46 | var _LogicalExpression2 = _interopRequireDefault(_LogicalExpression);
|
---|
| 47 |
|
---|
| 48 | var _MemberExpression = require('./MemberExpression');
|
---|
| 49 |
|
---|
| 50 | var _MemberExpression2 = _interopRequireDefault(_MemberExpression);
|
---|
| 51 |
|
---|
| 52 | var _ChainExpression = require('./ChainExpression');
|
---|
| 53 |
|
---|
| 54 | var _ChainExpression2 = _interopRequireDefault(_ChainExpression);
|
---|
| 55 |
|
---|
| 56 | var _OptionalCallExpression = require('./OptionalCallExpression');
|
---|
| 57 |
|
---|
| 58 | var _OptionalCallExpression2 = _interopRequireDefault(_OptionalCallExpression);
|
---|
| 59 |
|
---|
| 60 | var _OptionalMemberExpression = require('./OptionalMemberExpression');
|
---|
| 61 |
|
---|
| 62 | var _OptionalMemberExpression2 = _interopRequireDefault(_OptionalMemberExpression);
|
---|
| 63 |
|
---|
| 64 | var _CallExpression = require('./CallExpression');
|
---|
| 65 |
|
---|
| 66 | var _CallExpression2 = _interopRequireDefault(_CallExpression);
|
---|
| 67 |
|
---|
| 68 | var _UnaryExpression = require('./UnaryExpression');
|
---|
| 69 |
|
---|
| 70 | var _UnaryExpression2 = _interopRequireDefault(_UnaryExpression);
|
---|
| 71 |
|
---|
| 72 | var _ThisExpression = require('./ThisExpression');
|
---|
| 73 |
|
---|
| 74 | var _ThisExpression2 = _interopRequireDefault(_ThisExpression);
|
---|
| 75 |
|
---|
| 76 | var _ConditionalExpression = require('./ConditionalExpression');
|
---|
| 77 |
|
---|
| 78 | var _ConditionalExpression2 = _interopRequireDefault(_ConditionalExpression);
|
---|
| 79 |
|
---|
| 80 | var _BinaryExpression = require('./BinaryExpression');
|
---|
| 81 |
|
---|
| 82 | var _BinaryExpression2 = _interopRequireDefault(_BinaryExpression);
|
---|
| 83 |
|
---|
| 84 | var _ObjectExpression = require('./ObjectExpression');
|
---|
| 85 |
|
---|
| 86 | var _ObjectExpression2 = _interopRequireDefault(_ObjectExpression);
|
---|
| 87 |
|
---|
| 88 | var _NewExpression = require('./NewExpression');
|
---|
| 89 |
|
---|
| 90 | var _NewExpression2 = _interopRequireDefault(_NewExpression);
|
---|
| 91 |
|
---|
| 92 | var _UpdateExpression = require('./UpdateExpression');
|
---|
| 93 |
|
---|
| 94 | var _UpdateExpression2 = _interopRequireDefault(_UpdateExpression);
|
---|
| 95 |
|
---|
| 96 | var _ArrayExpression = require('./ArrayExpression');
|
---|
| 97 |
|
---|
| 98 | var _ArrayExpression2 = _interopRequireDefault(_ArrayExpression);
|
---|
| 99 |
|
---|
| 100 | var _BindExpression = require('./BindExpression');
|
---|
| 101 |
|
---|
| 102 | var _BindExpression2 = _interopRequireDefault(_BindExpression);
|
---|
| 103 |
|
---|
| 104 | var _SpreadElement = require('./SpreadElement');
|
---|
| 105 |
|
---|
| 106 | var _SpreadElement2 = _interopRequireDefault(_SpreadElement);
|
---|
| 107 |
|
---|
| 108 | var _TypeCastExpression = require('./TypeCastExpression');
|
---|
| 109 |
|
---|
| 110 | var _TypeCastExpression2 = _interopRequireDefault(_TypeCastExpression);
|
---|
| 111 |
|
---|
| 112 | var _SequenceExpression = require('./SequenceExpression');
|
---|
| 113 |
|
---|
| 114 | var _SequenceExpression2 = _interopRequireDefault(_SequenceExpression);
|
---|
| 115 |
|
---|
| 116 | var _TSNonNullExpression = require('./TSNonNullExpression');
|
---|
| 117 |
|
---|
| 118 | var _TSNonNullExpression2 = _interopRequireDefault(_TSNonNullExpression);
|
---|
| 119 |
|
---|
| 120 | var _AssignmentExpression = require('./AssignmentExpression');
|
---|
| 121 |
|
---|
| 122 | var _AssignmentExpression2 = _interopRequireDefault(_AssignmentExpression);
|
---|
| 123 |
|
---|
| 124 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 125 |
|
---|
| 126 | // Composition map of types to their extractor functions.
|
---|
| 127 | var 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 |
|
---|
| 159 | var noop = function noop() {
|
---|
| 160 | return null;
|
---|
| 161 | };
|
---|
| 162 |
|
---|
| 163 | var 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 | */
|
---|
| 177 | function 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.
|
---|
| 215 | var 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 | */
|
---|
| 275 | function 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 | } |
---|