source: imaps-frontend/node_modules/jsx-ast-utils/src/values/expressions/index.js@ d565449

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.8 KB
Line 
1import Literal from '../Literal';
2import JSXElement from '../JSXElement';
3import JSXFragment from '../JSXFragment';
4import JSXText from '../JSXText';
5import Identifier from './Identifier';
6import TaggedTemplateExpression from './TaggedTemplateExpression';
7import TemplateLiteral from './TemplateLiteral';
8import FunctionExpression from './FunctionExpression';
9import LogicalExpression from './LogicalExpression';
10import MemberExpression from './MemberExpression';
11import ChainExpression from './ChainExpression';
12import OptionalCallExpression from './OptionalCallExpression';
13import OptionalMemberExpression from './OptionalMemberExpression';
14import CallExpression from './CallExpression';
15import UnaryExpression from './UnaryExpression';
16import ThisExpression from './ThisExpression';
17import ConditionalExpression from './ConditionalExpression';
18import BinaryExpression from './BinaryExpression';
19import ObjectExpression from './ObjectExpression';
20import NewExpression from './NewExpression';
21import UpdateExpression from './UpdateExpression';
22import ArrayExpression from './ArrayExpression';
23import BindExpression from './BindExpression';
24import SpreadElement from './SpreadElement';
25import TypeCastExpression from './TypeCastExpression';
26import SequenceExpression from './SequenceExpression';
27import TSNonNullExpression from './TSNonNullExpression';
28import AssignmentExpression from './AssignmentExpression';
29
30// Composition map of types to their extractor functions.
31const TYPES = {
32 Identifier,
33 Literal,
34 JSXElement,
35 JSXFragment,
36 JSXText,
37 TaggedTemplateExpression,
38 TemplateLiteral,
39 ArrowFunctionExpression: FunctionExpression,
40 FunctionExpression,
41 LogicalExpression,
42 MemberExpression,
43 ChainExpression,
44 OptionalCallExpression,
45 OptionalMemberExpression,
46 CallExpression,
47 UnaryExpression,
48 ThisExpression,
49 ConditionalExpression,
50 BinaryExpression,
51 ObjectExpression,
52 NewExpression,
53 UpdateExpression,
54 ArrayExpression,
55 BindExpression,
56 SpreadElement,
57 TypeCastExpression,
58 SequenceExpression,
59 TSNonNullExpression,
60 AssignmentExpression,
61};
62
63const noop = () => null;
64
65const errorMessage = (expression) => `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.`;
66
67/**
68 * This function maps an AST value node
69 * to its correct extractor function for its
70 * given type.
71 *
72 * This will map correctly for *all* possible expression types.
73 *
74 * @param - value - AST Value object with type `JSXExpressionContainer`
75 * @returns The extracted value.
76 */
77export default function extract(value) {
78 // Value will not have the expression property when we recurse.
79 // The type for expression on ArrowFunctionExpression is a boolean.
80 let expression;
81 if (
82 typeof value.expression !== 'boolean'
83 && value.expression
84 ) {
85 expression = value.expression; // eslint-disable-line prefer-destructuring
86 } else {
87 expression = value;
88 }
89 let { type } = expression;
90
91 // Typescript NonNull Expression is wrapped & it would end up in the wrong extractor
92 if (expression.object && expression.object.type === 'TSNonNullExpression') {
93 type = 'TSNonNullExpression';
94 }
95
96 while (type === 'TSAsExpression') {
97 ({ type } = expression);
98 if (expression.expression) {
99 ({ expression } = expression);
100 }
101 }
102
103 if (TYPES[type] === undefined) {
104 // eslint-disable-next-line no-console
105 console.error(errorMessage(type));
106 return null;
107 }
108
109 return TYPES[type](expression);
110}
111
112// Composition map of types to their extractor functions to handle literals.
113const LITERAL_TYPES = {
114 ...TYPES,
115 Literal: (value) => {
116 const extractedVal = TYPES.Literal.call(undefined, value);
117 const isNull = extractedVal === null;
118 // This will be convention for attributes that have null
119 // value explicitly defined (<div prop={null} /> maps to 'null').
120 return isNull ? 'null' : extractedVal;
121 },
122 Identifier: (value) => {
123 const isUndefined = TYPES.Identifier.call(undefined, value) === undefined;
124 return isUndefined ? undefined : null;
125 },
126 JSXElement: noop,
127 JSXFragment: noop,
128 JSXText: noop,
129 ArrowFunctionExpression: noop,
130 FunctionExpression: noop,
131 LogicalExpression: noop,
132 MemberExpression: noop,
133 OptionalCallExpression: noop,
134 OptionalMemberExpression: noop,
135 CallExpression: noop,
136 UnaryExpression: (value) => {
137 const extractedVal = TYPES.UnaryExpression.call(undefined, value);
138 return extractedVal === undefined ? null : extractedVal;
139 },
140 UpdateExpression: (value) => {
141 const extractedVal = TYPES.UpdateExpression.call(undefined, value);
142 return extractedVal === undefined ? null : extractedVal;
143 },
144 ThisExpression: noop,
145 ConditionalExpression: noop,
146 BinaryExpression: noop,
147 ObjectExpression: noop,
148 NewExpression: noop,
149 ArrayExpression: (value) => {
150 const extractedVal = TYPES.ArrayExpression.call(undefined, value);
151 return extractedVal.filter((val) => val !== null);
152 },
153 BindExpression: noop,
154 SpreadElement: noop,
155 TSNonNullExpression: noop,
156 TSAsExpression: noop,
157 TypeCastExpression: noop,
158 SequenceExpression: noop,
159 ChainExpression: noop,
160};
161
162/**
163 * This function maps an AST value node
164 * to its correct extractor function for its
165 * given type.
166 *
167 * This will map correctly for *some* possible types that map to literals.
168 *
169 * @param - value - AST Value object with type `JSXExpressionContainer`
170 * @returns The extracted value.
171 */
172export function extractLiteral(value) {
173 // Value will not have the expression property when we recurse.
174 const expression = value.expression || value;
175 const { type } = expression;
176
177 if (LITERAL_TYPES[type] === undefined) {
178 // eslint-disable-next-line no-console
179 console.error(errorMessage(type));
180 return null;
181 }
182
183 return LITERAL_TYPES[type](expression);
184}
Note: See TracBrowser for help on using the repository browser.