1 | import Literal from '../Literal';
|
---|
2 | import JSXElement from '../JSXElement';
|
---|
3 | import JSXFragment from '../JSXFragment';
|
---|
4 | import JSXText from '../JSXText';
|
---|
5 | import Identifier from './Identifier';
|
---|
6 | import TaggedTemplateExpression from './TaggedTemplateExpression';
|
---|
7 | import TemplateLiteral from './TemplateLiteral';
|
---|
8 | import FunctionExpression from './FunctionExpression';
|
---|
9 | import LogicalExpression from './LogicalExpression';
|
---|
10 | import MemberExpression from './MemberExpression';
|
---|
11 | import ChainExpression from './ChainExpression';
|
---|
12 | import OptionalCallExpression from './OptionalCallExpression';
|
---|
13 | import OptionalMemberExpression from './OptionalMemberExpression';
|
---|
14 | import CallExpression from './CallExpression';
|
---|
15 | import UnaryExpression from './UnaryExpression';
|
---|
16 | import ThisExpression from './ThisExpression';
|
---|
17 | import ConditionalExpression from './ConditionalExpression';
|
---|
18 | import BinaryExpression from './BinaryExpression';
|
---|
19 | import ObjectExpression from './ObjectExpression';
|
---|
20 | import NewExpression from './NewExpression';
|
---|
21 | import UpdateExpression from './UpdateExpression';
|
---|
22 | import ArrayExpression from './ArrayExpression';
|
---|
23 | import BindExpression from './BindExpression';
|
---|
24 | import SpreadElement from './SpreadElement';
|
---|
25 | import TypeCastExpression from './TypeCastExpression';
|
---|
26 | import SequenceExpression from './SequenceExpression';
|
---|
27 | import TSNonNullExpression from './TSNonNullExpression';
|
---|
28 | import AssignmentExpression from './AssignmentExpression';
|
---|
29 |
|
---|
30 | // Composition map of types to their extractor functions.
|
---|
31 | const 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 |
|
---|
63 | const noop = () => null;
|
---|
64 |
|
---|
65 | const 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 | */
|
---|
77 | export 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.
|
---|
113 | const 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 | */
|
---|
172 | export 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 | }
|
---|