@base44/vite-plugin 0.2.21 → 0.2.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/jsx-processor.d.ts +15 -0
- package/dist/jsx-processor.d.ts.map +1 -0
- package/dist/jsx-processor.js +106 -0
- package/dist/jsx-processor.js.map +1 -0
- package/dist/jsx-utils.d.ts +7 -0
- package/dist/jsx-utils.d.ts.map +1 -0
- package/dist/jsx-utils.js +12 -0
- package/dist/jsx-utils.js.map +1 -0
- package/dist/processors/shared-utils.d.ts +19 -0
- package/dist/processors/shared-utils.d.ts.map +1 -0
- package/dist/processors/shared-utils.js +77 -0
- package/dist/processors/shared-utils.js.map +1 -0
- package/dist/processors/static-array-processor.d.ts +31 -0
- package/dist/processors/static-array-processor.d.ts.map +1 -0
- package/dist/processors/static-array-processor.js +175 -0
- package/dist/processors/static-array-processor.js.map +1 -0
- package/dist/visual-edit-plugin.d.ts +1 -0
- package/dist/visual-edit-plugin.d.ts.map +1 -1
- package/dist/visual-edit-plugin.js +21 -1
- package/dist/visual-edit-plugin.js.map +1 -1
- package/package.json +2 -1
- package/src/jsx-processor.ts +147 -0
- package/src/jsx-utils.ts +15 -0
- package/src/processors/shared-utils.ts +116 -0
- package/src/processors/static-array-processor.ts +257 -0
- package/src/visual-edit-plugin.md +358 -0
- package/src/visual-edit-plugin.ts +24 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { NodePath } from "@babel/traverse";
|
|
2
|
+
import type * as t from "@babel/types";
|
|
3
|
+
import { StaticArrayProcessor } from "./processors/static-array-processor.js";
|
|
4
|
+
|
|
5
|
+
export class JSXProcessor {
|
|
6
|
+
private staticArrayProcessor: StaticArrayProcessor;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
private types: typeof t,
|
|
10
|
+
private filename: string
|
|
11
|
+
) {
|
|
12
|
+
this.staticArrayProcessor = new StaticArrayProcessor(types);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
processJSXElement(path: NodePath<t.JSXOpeningElement>): void {
|
|
16
|
+
if (this.hasAttribute(path, "data-source-location")) return;
|
|
17
|
+
|
|
18
|
+
this.addSourceLocationAttribute(path);
|
|
19
|
+
this.addDynamicContentAttribute(path);
|
|
20
|
+
this.addContentEditableAttribute(path);
|
|
21
|
+
this.staticArrayProcessor.process(path);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private addSourceLocationAttribute(
|
|
25
|
+
path: NodePath<t.JSXOpeningElement>
|
|
26
|
+
): void {
|
|
27
|
+
const { line, column } = path.node.loc?.start || { line: 1, column: 0 };
|
|
28
|
+
const value = `${this.filename}:${line}:${column}`;
|
|
29
|
+
|
|
30
|
+
path.node.attributes.push(
|
|
31
|
+
this.types.jsxAttribute(
|
|
32
|
+
this.types.jsxIdentifier("data-source-location"),
|
|
33
|
+
this.types.stringLiteral(value)
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private addDynamicContentAttribute(
|
|
39
|
+
path: NodePath<t.JSXOpeningElement>
|
|
40
|
+
): void {
|
|
41
|
+
const parentElement = path.parentPath;
|
|
42
|
+
if (!parentElement?.isJSXElement()) return;
|
|
43
|
+
|
|
44
|
+
const isDynamic = this.checkIfElementHasDynamicContent(
|
|
45
|
+
parentElement.node as t.JSXElement
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
path.node.attributes.push(
|
|
49
|
+
this.types.jsxAttribute(
|
|
50
|
+
this.types.jsxIdentifier("data-dynamic-content"),
|
|
51
|
+
this.types.stringLiteral(isDynamic ? "true" : "false")
|
|
52
|
+
)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private addContentEditableAttribute(
|
|
57
|
+
path: NodePath<t.JSXOpeningElement>
|
|
58
|
+
): void {
|
|
59
|
+
path.node.attributes.push(
|
|
60
|
+
this.types.jsxAttribute(
|
|
61
|
+
this.types.jsxIdentifier("content-editable"),
|
|
62
|
+
this.types.stringLiteral("true")
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private hasAttribute(
|
|
68
|
+
path: NodePath<t.JSXOpeningElement>,
|
|
69
|
+
name: string
|
|
70
|
+
): boolean {
|
|
71
|
+
return path.node.attributes.some(
|
|
72
|
+
(attr: t.JSXAttribute | t.JSXSpreadAttribute) =>
|
|
73
|
+
this.types.isJSXAttribute(attr) &&
|
|
74
|
+
this.types.isJSXIdentifier(attr.name) &&
|
|
75
|
+
attr.name.name === name
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private checkIfElementHasDynamicContent(jsxElement: t.JSXElement): boolean {
|
|
80
|
+
let hasDynamicContent = false;
|
|
81
|
+
|
|
82
|
+
const checkNode = (node: t.Node): boolean => {
|
|
83
|
+
if (this.types.isJSXExpressionContainer(node)) {
|
|
84
|
+
const expression = (node as t.JSXExpressionContainer).expression;
|
|
85
|
+
if (this.types.isJSXEmptyExpression(expression)) return false;
|
|
86
|
+
if (!this.types.isLiteral(expression)) return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (
|
|
90
|
+
this.types.isTemplateLiteral(node) &&
|
|
91
|
+
(node as t.TemplateLiteral).expressions.length > 0
|
|
92
|
+
) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
if (this.types.isMemberExpression(node)) return true;
|
|
96
|
+
if (this.types.isCallExpression(node)) return true;
|
|
97
|
+
if (this.types.isConditionalExpression(node)) return true;
|
|
98
|
+
|
|
99
|
+
if (this.types.isIdentifier(node)) {
|
|
100
|
+
const dynamicNames = [
|
|
101
|
+
"props",
|
|
102
|
+
"state",
|
|
103
|
+
"data",
|
|
104
|
+
"item",
|
|
105
|
+
"value",
|
|
106
|
+
"text",
|
|
107
|
+
"content",
|
|
108
|
+
];
|
|
109
|
+
if (dynamicNames.some((name) => (node as t.Identifier).name.includes(name))) {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const traverseNode = (node: Record<string, unknown>): void => {
|
|
118
|
+
if (hasDynamicContent) return;
|
|
119
|
+
if (checkNode(node as unknown as t.Node)) {
|
|
120
|
+
hasDynamicContent = true;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
for (const key of Object.keys(node)) {
|
|
125
|
+
if (hasDynamicContent) return;
|
|
126
|
+
const value = node[key];
|
|
127
|
+
|
|
128
|
+
if (Array.isArray(value)) {
|
|
129
|
+
for (const child of value) {
|
|
130
|
+
if (child && typeof child === "object" && "type" in child) {
|
|
131
|
+
traverseNode(child as Record<string, unknown>);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} else if (value && typeof value === "object" && "type" in value) {
|
|
135
|
+
traverseNode(value as Record<string, unknown>);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
for (const child of jsxElement.children) {
|
|
141
|
+
if (hasDynamicContent) break;
|
|
142
|
+
traverseNode(child as unknown as Record<string, unknown>);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return hasDynamicContent;
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/jsx-utils.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type * as t from "@babel/types";
|
|
2
|
+
|
|
3
|
+
export class JSXUtils {
|
|
4
|
+
private static types: typeof t;
|
|
5
|
+
|
|
6
|
+
static init(types: typeof t) {
|
|
7
|
+
this.types = types;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static getAttributeName(attr: t.JSXAttribute): string {
|
|
11
|
+
return this.types.isJSXNamespacedName(attr.name)
|
|
12
|
+
? `${attr.name.namespace.name}:${attr.name.name.name}`
|
|
13
|
+
: attr.name.name;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { NodePath } from "@babel/traverse";
|
|
2
|
+
import type * as t from "@babel/types";
|
|
3
|
+
import { JSXUtils } from "../jsx-utils.js";
|
|
4
|
+
|
|
5
|
+
export class JSXAttributeUtils {
|
|
6
|
+
constructor(private types: typeof t) {}
|
|
7
|
+
|
|
8
|
+
hasAttribute(
|
|
9
|
+
path: NodePath<t.JSXOpeningElement>,
|
|
10
|
+
attributeName: string
|
|
11
|
+
): boolean {
|
|
12
|
+
return path.node.attributes.some(
|
|
13
|
+
(attr: t.JSXAttribute | t.JSXSpreadAttribute) =>
|
|
14
|
+
this.types.isJSXAttribute(attr) &&
|
|
15
|
+
JSXUtils.getAttributeName(attr) === attributeName
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
addStringAttribute(
|
|
20
|
+
path: NodePath<t.JSXOpeningElement>,
|
|
21
|
+
attributeName: string,
|
|
22
|
+
value: string
|
|
23
|
+
): void {
|
|
24
|
+
if (!this.hasAttribute(path, attributeName)) {
|
|
25
|
+
path.node.attributes.push(
|
|
26
|
+
this.types.jsxAttribute(
|
|
27
|
+
this.types.jsxIdentifier(attributeName),
|
|
28
|
+
this.types.stringLiteral(value)
|
|
29
|
+
)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
addExpressionAttribute(
|
|
35
|
+
path: NodePath<t.JSXOpeningElement>,
|
|
36
|
+
attributeName: string,
|
|
37
|
+
expression: t.Expression
|
|
38
|
+
): void {
|
|
39
|
+
if (!this.hasAttribute(path, attributeName)) {
|
|
40
|
+
path.node.attributes.push(
|
|
41
|
+
this.types.jsxAttribute(
|
|
42
|
+
this.types.jsxIdentifier(attributeName),
|
|
43
|
+
this.types.jsxExpressionContainer(expression)
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class StaticValueUtils {
|
|
51
|
+
constructor(private types: typeof t) {}
|
|
52
|
+
|
|
53
|
+
isPrimitiveLiteral(path: NodePath<t.Node>): boolean {
|
|
54
|
+
return (
|
|
55
|
+
path.isStringLiteral() ||
|
|
56
|
+
path.isNumericLiteral() ||
|
|
57
|
+
path.isBooleanLiteral() ||
|
|
58
|
+
path.isNullLiteral()
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
isStaticValue(
|
|
63
|
+
path: NodePath<t.Node>,
|
|
64
|
+
visited: Set<string> = new Set()
|
|
65
|
+
): boolean {
|
|
66
|
+
if (this.isPrimitiveLiteral(path)) return true;
|
|
67
|
+
if (path.isIdentifier()) return this.isStaticIdentifier(path, visited);
|
|
68
|
+
if (path.isObjectExpression()) return this.isStaticObject(path, visited);
|
|
69
|
+
if (path.isArrayExpression())
|
|
70
|
+
return this.isStaticArrayExpression(path, visited);
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
isStaticIdentifier(
|
|
75
|
+
path: NodePath<t.Identifier>,
|
|
76
|
+
visited: Set<string> = new Set()
|
|
77
|
+
): boolean {
|
|
78
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
79
|
+
if (!binding) return false;
|
|
80
|
+
|
|
81
|
+
if (binding.kind === "module") return true;
|
|
82
|
+
|
|
83
|
+
if (binding.kind === "const" && binding.path.isVariableDeclarator()) {
|
|
84
|
+
const name = path.node.name;
|
|
85
|
+
if (visited.has(name)) return false;
|
|
86
|
+
visited.add(name);
|
|
87
|
+
|
|
88
|
+
const init = binding.path.get("init");
|
|
89
|
+
if (init.hasNode()) {
|
|
90
|
+
return this.isStaticValue(init as NodePath<t.Node>, visited);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
isStaticObject(
|
|
98
|
+
path: NodePath<t.ObjectExpression>,
|
|
99
|
+
visited: Set<string> = new Set()
|
|
100
|
+
): boolean {
|
|
101
|
+
return path.get("properties").every((prop: NodePath) => {
|
|
102
|
+
if (!prop.isObjectProperty()) return false;
|
|
103
|
+
return this.isStaticValue(prop.get("value") as NodePath<t.Node>, visited);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
isStaticArrayExpression(
|
|
108
|
+
arrayExpression: NodePath<t.ArrayExpression>,
|
|
109
|
+
visited: Set<string> = new Set()
|
|
110
|
+
): boolean {
|
|
111
|
+
return arrayExpression.get("elements").every((element) => {
|
|
112
|
+
if (!element.node || element.isSpreadElement()) return true;
|
|
113
|
+
return this.isStaticValue(element as unknown as NodePath<t.Node>, visited);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import type { NodePath } from "@babel/traverse";
|
|
2
|
+
import type * as t from "@babel/types";
|
|
3
|
+
import { JSXAttributeUtils, StaticValueUtils } from "./shared-utils.js";
|
|
4
|
+
|
|
5
|
+
export const DATA_ARR_INDEX = "data-arr-index";
|
|
6
|
+
export const DATA_ARR_VARIABLE_NAME = "data-arr-variable-name";
|
|
7
|
+
export const DATA_ARR_FIELD = "data-arr-field";
|
|
8
|
+
const GENERATED_INDEX_PARAM = "__arrIdx__";
|
|
9
|
+
|
|
10
|
+
interface ArrayMapInfo {
|
|
11
|
+
arrayExpression: NodePath<t.Expression>;
|
|
12
|
+
callbackParam: string;
|
|
13
|
+
indexParam: string | null;
|
|
14
|
+
arrayVariableName: string | null;
|
|
15
|
+
mapCallPath: NodePath<t.CallExpression>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class StaticArrayProcessor {
|
|
19
|
+
private attributeUtils: JSXAttributeUtils;
|
|
20
|
+
private staticValueUtils: StaticValueUtils;
|
|
21
|
+
|
|
22
|
+
constructor(private types: typeof t) {
|
|
23
|
+
this.attributeUtils = new JSXAttributeUtils(types);
|
|
24
|
+
this.staticValueUtils = new StaticValueUtils(types);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
process(path: NodePath<t.JSXOpeningElement>): void {
|
|
28
|
+
const arrayInfo = this.findParentArrayMap(path);
|
|
29
|
+
if (!arrayInfo) return;
|
|
30
|
+
|
|
31
|
+
if (!this.isStaticArray(arrayInfo.arrayExpression)) return;
|
|
32
|
+
|
|
33
|
+
const indexIdentifier = this.ensureIndexParam(arrayInfo);
|
|
34
|
+
this.addDataAttributes(path, arrayInfo, indexIdentifier);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private addDataAttributes(
|
|
38
|
+
path: NodePath<t.JSXOpeningElement>,
|
|
39
|
+
arrayInfo: ArrayMapInfo,
|
|
40
|
+
indexIdentifier: string
|
|
41
|
+
): void {
|
|
42
|
+
this.attributeUtils.addExpressionAttribute(
|
|
43
|
+
path,
|
|
44
|
+
DATA_ARR_INDEX,
|
|
45
|
+
this.types.identifier(indexIdentifier)
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
if (arrayInfo.arrayVariableName) {
|
|
49
|
+
this.attributeUtils.addStringAttribute(
|
|
50
|
+
path,
|
|
51
|
+
DATA_ARR_VARIABLE_NAME,
|
|
52
|
+
arrayInfo.arrayVariableName
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const fieldPath = this.findTextContentFieldPath(
|
|
57
|
+
path,
|
|
58
|
+
arrayInfo.callbackParam
|
|
59
|
+
);
|
|
60
|
+
if (fieldPath) {
|
|
61
|
+
this.attributeUtils.addStringAttribute(path, DATA_ARR_FIELD, fieldPath);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private findTextContentFieldPath(
|
|
66
|
+
path: NodePath<t.JSXOpeningElement>,
|
|
67
|
+
callbackParam: string
|
|
68
|
+
): string | null {
|
|
69
|
+
const parentElement = path.parentPath;
|
|
70
|
+
if (!parentElement?.isJSXElement()) return null;
|
|
71
|
+
|
|
72
|
+
for (const child of parentElement.get("children")) {
|
|
73
|
+
const fieldPath = this.extractFieldPathFromChild(child, callbackParam);
|
|
74
|
+
if (fieldPath) return fieldPath;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private extractFieldPathFromChild(
|
|
81
|
+
child: NodePath<t.JSXElement["children"][number]>,
|
|
82
|
+
callbackParam: string
|
|
83
|
+
): string | null {
|
|
84
|
+
if (!child.isJSXExpressionContainer()) return null;
|
|
85
|
+
|
|
86
|
+
const expression = child.get("expression");
|
|
87
|
+
if (!expression.isMemberExpression()) return null;
|
|
88
|
+
|
|
89
|
+
return this.extractFieldPath(expression, callbackParam);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private extractFieldPath(
|
|
93
|
+
expr: NodePath<t.MemberExpression>,
|
|
94
|
+
callbackParam: string
|
|
95
|
+
): string | null {
|
|
96
|
+
const parts = this.collectMemberExpressionParts(expr);
|
|
97
|
+
if (!parts) return null;
|
|
98
|
+
|
|
99
|
+
const { rootName, propertyNames } = parts;
|
|
100
|
+
return rootName === callbackParam ? propertyNames.join(".") : null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private collectMemberExpressionParts(
|
|
104
|
+
expr: NodePath<t.MemberExpression>
|
|
105
|
+
): { rootName: string; propertyNames: string[] } | null {
|
|
106
|
+
const propertyNames: string[] = [];
|
|
107
|
+
let current: NodePath<t.Expression> = expr;
|
|
108
|
+
|
|
109
|
+
while (current.isMemberExpression()) {
|
|
110
|
+
const property = current.get("property");
|
|
111
|
+
if (!property.isIdentifier()) return null;
|
|
112
|
+
|
|
113
|
+
propertyNames.unshift(property.node.name);
|
|
114
|
+
current = current.get("object") as NodePath<t.Expression>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!current.isIdentifier()) return null;
|
|
118
|
+
|
|
119
|
+
return { rootName: current.node.name, propertyNames };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private ensureIndexParam(arrayInfo: ArrayMapInfo): string {
|
|
123
|
+
if (arrayInfo.indexParam) {
|
|
124
|
+
return arrayInfo.indexParam;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.addIndexParamToCallback(arrayInfo.mapCallPath);
|
|
128
|
+
return GENERATED_INDEX_PARAM;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private addIndexParamToCallback(
|
|
132
|
+
mapCallPath: NodePath<t.CallExpression>
|
|
133
|
+
): void {
|
|
134
|
+
const callback = this.getMapCallback(mapCallPath);
|
|
135
|
+
if (!callback) return;
|
|
136
|
+
|
|
137
|
+
const params = callback.get("params");
|
|
138
|
+
if (params.length === 1) {
|
|
139
|
+
callback.node.params.push(this.types.identifier(GENERATED_INDEX_PARAM));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private getMapCallback(
|
|
144
|
+
mapCallPath: NodePath<t.CallExpression>
|
|
145
|
+
): NodePath<t.Function> | null {
|
|
146
|
+
const args = mapCallPath.get("arguments");
|
|
147
|
+
const firstArg = args[0];
|
|
148
|
+
if (firstArg && firstArg.isFunction()) {
|
|
149
|
+
return firstArg as NodePath<t.Function>;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private findParentArrayMap(
|
|
155
|
+
path: NodePath<t.JSXOpeningElement>,
|
|
156
|
+
maxDepth: number = 5
|
|
157
|
+
): ArrayMapInfo | null {
|
|
158
|
+
let currentPath: NodePath = path;
|
|
159
|
+
let depth = 0;
|
|
160
|
+
|
|
161
|
+
while (currentPath.parentPath && depth < maxDepth) {
|
|
162
|
+
const mapInfo = this.tryExtractMapInfo(currentPath.parentPath);
|
|
163
|
+
if (mapInfo) return mapInfo;
|
|
164
|
+
|
|
165
|
+
currentPath = currentPath.parentPath;
|
|
166
|
+
depth++;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private tryExtractMapInfo(parent: NodePath): ArrayMapInfo | null {
|
|
173
|
+
if (!parent.isCallExpression()) return null;
|
|
174
|
+
if (!this.isMapCall(parent)) return null;
|
|
175
|
+
|
|
176
|
+
return this.extractArrayMapInfo(parent);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private isMapCall(callExpr: NodePath<t.CallExpression>): boolean {
|
|
180
|
+
const callee = callExpr.get("callee");
|
|
181
|
+
if (!callee.isMemberExpression()) return false;
|
|
182
|
+
|
|
183
|
+
const property = callee.get("property");
|
|
184
|
+
return property.isIdentifier() && property.node.name === "map";
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private extractArrayMapInfo(
|
|
188
|
+
mapCall: NodePath<t.CallExpression>
|
|
189
|
+
): ArrayMapInfo | null {
|
|
190
|
+
const callback = this.getMapCallback(mapCall);
|
|
191
|
+
if (!callback) return null;
|
|
192
|
+
|
|
193
|
+
const params = callback.get("params");
|
|
194
|
+
const firstParam = params[0];
|
|
195
|
+
if (!firstParam || !firstParam.isIdentifier()) return null;
|
|
196
|
+
|
|
197
|
+
const callee = mapCall.get("callee") as NodePath<t.MemberExpression>;
|
|
198
|
+
const arrayExpression = callee.get("object") as NodePath<t.Expression>;
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
arrayExpression,
|
|
202
|
+
callbackParam: firstParam.node.name,
|
|
203
|
+
indexParam: this.extractIndexParam(params),
|
|
204
|
+
arrayVariableName: this.extractArrayVariableName(arrayExpression),
|
|
205
|
+
mapCallPath: mapCall,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private extractIndexParam(params: NodePath<t.Node>[]): string | null {
|
|
210
|
+
const secondParam = params[1];
|
|
211
|
+
return secondParam && secondParam.isIdentifier()
|
|
212
|
+
? secondParam.node.name
|
|
213
|
+
: null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
private extractArrayVariableName(
|
|
217
|
+
arrayExpression: NodePath<t.Expression>
|
|
218
|
+
): string | null {
|
|
219
|
+
return arrayExpression.isIdentifier() ? arrayExpression.node.name : null;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private isStaticArray(arrayExpression: NodePath<t.Expression>): boolean {
|
|
223
|
+
const arrayExpr = this.resolveArrayExpression(arrayExpression);
|
|
224
|
+
return arrayExpr ? this.isStaticArrayExpression(arrayExpr) : false;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private resolveArrayExpression(
|
|
228
|
+
expression: NodePath<t.Expression>
|
|
229
|
+
): NodePath<t.ArrayExpression> | null {
|
|
230
|
+
if (expression.isArrayExpression()) {
|
|
231
|
+
return expression;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (expression.isIdentifier()) {
|
|
235
|
+
return this.resolveIdentifierToArray(expression);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private resolveIdentifierToArray(
|
|
242
|
+
identifier: NodePath<t.Identifier>
|
|
243
|
+
): NodePath<t.ArrayExpression> | null {
|
|
244
|
+
const binding = identifier.scope.getBinding(identifier.node.name);
|
|
245
|
+
|
|
246
|
+
if (!binding?.path.isVariableDeclarator()) return null;
|
|
247
|
+
|
|
248
|
+
const init = binding.path.get("init");
|
|
249
|
+
return init.isArrayExpression() ? init : null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private isStaticArrayExpression(
|
|
253
|
+
arrayExpression: NodePath<t.ArrayExpression>
|
|
254
|
+
): boolean {
|
|
255
|
+
return this.staticValueUtils.isStaticArrayExpression(arrayExpression);
|
|
256
|
+
}
|
|
257
|
+
}
|