forked from patternfly/pf-codemods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSXAttributes.ts
More file actions
182 lines (160 loc) · 5.27 KB
/
JSXAttributes.ts
File metadata and controls
182 lines (160 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Rule, Scope } from "eslint";
import {
Expression,
JSXAttribute,
JSXElement,
JSXEmptyExpression,
JSXFragment,
JSXOpeningElement,
MemberExpression,
Property,
SpreadElement,
} from "estree-jsx";
export function getAttribute(
node: JSXElement | JSXOpeningElement,
attributeName: string
) {
const nodeProperty = node.type === "JSXElement" ? node.openingElement : node;
return nodeProperty.attributes.find(
(attr) => attr.type === "JSXAttribute" && attr.name.name === attributeName
) as JSXAttribute | undefined;
}
export function getAnyAttribute(
node: JSXElement | JSXOpeningElement,
attributeNames: string[]
) {
let foundAttribute = undefined;
for (const attribute of attributeNames) {
foundAttribute = getAttribute(node, attribute);
if (foundAttribute) {
break;
}
}
return foundAttribute;
}
/**
* Attribute value and its type
*/
type Attribute =
| { type: "string"; value: string }
| {
type: "Literal";
value: string | number | bigint | boolean | RegExp | null | undefined;
}
| { type: "MemberExpression"; value: MemberExpression }
| { type: "ObjectExpression"; value: (Property | SpreadElement)[] }
| { type: "undefined"; value: undefined };
const UNDEFINED: Attribute = { type: "undefined", value: undefined };
/**
* Helper to get the raw value from a JSXAttribute value. If the JSXAttribute value is an Identifier, it tries to get the value of that variable. If the JSXAttribute value is a JSXExpressionContainer: {"value"}, it returns the inner content.
* MemberExpressions and ObjectExpressions are not parsed further.
* @param context Rule context
* @param node JSXAttribute value
* @returns Attribute in this form: { type: [specific type of the returned value], value: [actual value]}. Literal types are further processed, if their value is of type "string", type: "string" is returned, otherwise type: "Literal"
*/
export function getAttributeValue(
context: Rule.RuleContext,
node?: JSXAttribute["value"]
): Attribute {
if (!node) {
return UNDEFINED;
}
const valueType = node.type;
if (valueType === "Literal") {
if (typeof node.value === "string") {
return { type: "string", value: node.value };
}
return { type: "Literal", value: node.value };
}
if (valueType !== "JSXExpressionContainer") {
return UNDEFINED;
}
if (node.expression.type === "Identifier") {
const variableScope = context.getSourceCode().getScope(node);
return getVariableValue(node.expression.name, variableScope, context);
}
if (node.expression.type === "MemberExpression") {
return { type: "MemberExpression", value: node.expression };
}
if (node.expression.type === "Literal") {
if (typeof node.expression.value === "string") {
return { type: "string", value: node.expression.value };
}
return { type: "Literal", value: node.expression.value };
}
if (node.expression.type === "ObjectExpression") {
return { type: "ObjectExpression", value: node.expression.properties };
}
return UNDEFINED;
}
export function getExpression(node?: JSXAttribute["value"]) {
if (!node) {
return;
}
if (node.type === "JSXExpressionContainer") {
return node.expression as Expression | JSXEmptyExpression | JSXFragment;
}
}
export function getVariableDeclaration(
name: string,
scope: Scope.Scope | null
) {
while (scope !== null) {
const variable = scope.variables.find((v) => v.name === name);
if (variable) {
return variable;
}
scope = scope.upper;
}
return undefined;
}
export function getVariableInit(
variableDeclaration: Scope.Variable | undefined
) {
if (!variableDeclaration || !variableDeclaration.defs.length) {
return;
}
const variableDefinition = variableDeclaration.defs[0];
if (variableDefinition.type !== "Variable") {
return;
}
return variableDefinition.node.init;
}
/**
* Helper to get the raw value of a variable, given by its name. Returns an Attribute object, similarly to getAttributeValue helper.
* @param name Variable name
* @param scope Scope where to look for the variable declaration
* @param context Rule context
* @returns Attribute in this form: { type: [specific type of the returned value], value: [actual value]}. Literal types are further processed, if their value is of type "string", type: "string" is returned, otherwise type: "Literal"
*/
export function getVariableValue(
name: string,
scope: Scope.Scope | null,
context: Rule.RuleContext
): Attribute {
const variableDeclaration = getVariableDeclaration(name, scope);
const variableInit = getVariableInit(variableDeclaration);
if (!variableInit) {
return UNDEFINED;
}
if (variableInit.type === "Identifier") {
return getVariableValue(
variableInit.name,
context.getSourceCode().getScope(variableInit),
context
);
}
if (variableInit.type === "Literal") {
if (typeof variableInit.value === "string") {
return { type: "string", value: variableInit.value };
}
return { type: "Literal", value: variableInit.value };
}
if (variableInit.type === "MemberExpression") {
return { type: "MemberExpression", value: variableInit };
}
if (variableInit.type === "ObjectExpression") {
return { type: "ObjectExpression", value: variableInit.properties };
}
return UNDEFINED;
}