@wise/wds-codemods 0.0.1-experimental-cc95209 → 0.0.1-experimental-731cdc7
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/index.js +133 -134
- package/dist/index.js.map +1 -1
- package/dist/reportManualReview-DQ00-OKx.js +50 -0
- package/dist/reportManualReview-DQ00-OKx.js.map +1 -0
- package/dist/transforms/button.js +493 -583
- package/dist/transforms/button.js.map +1 -1
- package/package.json +28 -23
- package/.changeset/better-impalas-drop.md +0 -5
- package/.changeset/config.json +0 -13
- package/.changeset/quick-mails-joke.md +0 -128
- package/.github/CODEOWNERS +0 -1
- package/.github/actions/bootstrap/action.yml +0 -49
- package/.github/actions/commitlint/action.yml +0 -27
- package/.github/actions/test/action.yml +0 -23
- package/.github/workflows/cd-cd.yml +0 -127
- package/.github/workflows/renovate.yml +0 -16
- package/.husky/commit-msg +0 -1
- package/.husky/pre-commit +0 -1
- package/.nvmrc +0 -1
- package/.prettierignore +0 -1
- package/.prettierrc.js +0 -5
- package/DEVELOPER.md +0 -783
- package/babel.config.js +0 -28
- package/commitlint.config.js +0 -3
- package/dist/index.d.ts +0 -1
- package/dist/transforms/button.d.ts +0 -16
- package/eslint.config.js +0 -15
- package/jest.config.js +0 -9
- package/mkdocs.yml +0 -4
- package/renovate.json +0 -9
- package/scripts/build.sh +0 -10
- package/src/__tests__/runCodemod.test.ts +0 -96
- package/src/index.ts +0 -4
- package/src/runCodemod.ts +0 -88
- package/src/transforms/button/__tests__/button.test.tsx +0 -153
- package/src/transforms/button/button.ts +0 -447
- package/src/transforms/helpers/__tests__/createTestTransform.test.ts +0 -27
- package/src/transforms/helpers/__tests__/hasImport.test.ts +0 -52
- package/src/transforms/helpers/__tests__/iconUtils.test.ts +0 -207
- package/src/transforms/helpers/__tests__/jsxElementUtils.test.ts +0 -130
- package/src/transforms/helpers/__tests__/jsxReportingUtils.test.ts +0 -265
- package/src/transforms/helpers/createTestTransform.ts +0 -18
- package/src/transforms/helpers/hasImport.ts +0 -60
- package/src/transforms/helpers/iconUtils.ts +0 -87
- package/src/transforms/helpers/index.ts +0 -5
- package/src/transforms/helpers/jsxElementUtils.ts +0 -67
- package/src/transforms/helpers/jsxReportingUtils.ts +0 -224
- package/src/utils/__tests__/getOptions.test.ts +0 -170
- package/src/utils/__tests__/handleError.test.ts +0 -18
- package/src/utils/__tests__/loadTransformModules.test.ts +0 -51
- package/src/utils/__tests__/reportManualReview.test.ts +0 -42
- package/src/utils/getOptions.ts +0 -63
- package/src/utils/handleError.ts +0 -6
- package/src/utils/index.ts +0 -4
- package/src/utils/loadTransformModules.ts +0 -28
- package/src/utils/reportManualReview.ts +0 -17
- package/test-button.tsx +0 -230
- package/test-file.js +0 -2
- package/tsconfig.json +0 -14
- package/tsup.config.js +0 -13
|
@@ -1,606 +1,516 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import path from "path";
|
|
4
|
-
var REPORT_PATH = path.resolve(process.cwd(), "codemod-report.txt");
|
|
5
|
-
var reportManualReview = async (filePath, message) => {
|
|
6
|
-
const lineMatch = /at line (\d+)/u.exec(message);
|
|
7
|
-
const lineNumber = lineMatch?.[1];
|
|
8
|
-
const cleanMessage = message.replace(/ at line \d+/u, "");
|
|
9
|
-
const lineInfo = lineNumber ? `:${lineNumber}` : "";
|
|
10
|
-
await fs.appendFile(REPORT_PATH, `[${filePath}${lineInfo}] ${cleanMessage}
|
|
11
|
-
`, "utf8");
|
|
12
|
-
};
|
|
13
|
-
var reportManualReview_default = reportManualReview;
|
|
14
|
-
|
|
15
|
-
// src/transforms/helpers/createTestTransform.ts
|
|
16
|
-
import { applyTransform } from "jscodeshift/src/testUtils";
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
const require_reportManualReview = require('../reportManualReview-DQ00-OKx.js');
|
|
17
3
|
|
|
18
|
-
|
|
4
|
+
//#region src/transforms/helpers/hasImport.ts
|
|
5
|
+
/**
|
|
6
|
+
* Checks if a specific import exists in the given root collection and provides
|
|
7
|
+
* a method to remove it if found.
|
|
8
|
+
*/
|
|
19
9
|
function hasImport(root, sourceValue, importName, j) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (specifier.type === "ImportDefaultSpecifier" && specifier.local?.name === importName) {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
return true;
|
|
47
|
-
}) ?? [];
|
|
48
|
-
if (filteredSpecifiers.length === 0) {
|
|
49
|
-
path2.prune();
|
|
50
|
-
} else {
|
|
51
|
-
j(path2).replaceWith(
|
|
52
|
-
j.importDeclaration(filteredSpecifiers, path2.node.source, path2.node.importKind)
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
return { exists, remove };
|
|
10
|
+
const importDeclarations = root.find(j.ImportDeclaration, { source: { value: sourceValue } });
|
|
11
|
+
if (importDeclarations.size() === 0) return {
|
|
12
|
+
exists: false,
|
|
13
|
+
remove: () => {}
|
|
14
|
+
};
|
|
15
|
+
const namedImport = importDeclarations.find(j.ImportSpecifier, { imported: { name: importName } });
|
|
16
|
+
const defaultImport = importDeclarations.find(j.ImportDefaultSpecifier, { local: { name: importName } });
|
|
17
|
+
const exists = namedImport.size() > 0 || defaultImport.size() > 0;
|
|
18
|
+
const remove = () => {
|
|
19
|
+
importDeclarations.forEach((path) => {
|
|
20
|
+
const filteredSpecifiers = path.node.specifiers?.filter((specifier) => {
|
|
21
|
+
if (specifier.type === "ImportSpecifier" && specifier.imported.name === importName) return false;
|
|
22
|
+
if (specifier.type === "ImportDefaultSpecifier" && specifier.local?.name === importName) return false;
|
|
23
|
+
return true;
|
|
24
|
+
}) ?? [];
|
|
25
|
+
if (filteredSpecifiers.length === 0) path.prune();
|
|
26
|
+
else j(path).replaceWith(j.importDeclaration(filteredSpecifiers, path.node.source, path.node.importKind));
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
exists,
|
|
31
|
+
remove
|
|
32
|
+
};
|
|
58
33
|
}
|
|
59
|
-
var hasImport_default = hasImport;
|
|
60
34
|
|
|
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
|
-
if (iconChildIndex - 1 >= 0 && isWhitespaceJsxText(children[iconChildIndex - 1])) {
|
|
96
|
-
children.splice(iconChildIndex - 1, 1);
|
|
97
|
-
} else if (isWhitespaceJsxText(children[iconChildIndex])) {
|
|
98
|
-
children.splice(iconChildIndex, 1);
|
|
99
|
-
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/transforms/helpers/iconUtils.ts
|
|
37
|
+
/**
|
|
38
|
+
* Process children of a JSX element to detect icon components and add iconStart or iconEnd attributes accordingly.
|
|
39
|
+
* This is specific to icon handling but can be reused in codemods dealing with icon children.
|
|
40
|
+
*/
|
|
41
|
+
const processIconChildren = (j, children, iconImports, openingElement) => {
|
|
42
|
+
if (!children || !openingElement.attributes) return;
|
|
43
|
+
const unwrapJsxElement = (node) => {
|
|
44
|
+
if (typeof node === "object" && node !== null && "type" in node && node.type === "JSXExpressionContainer" && j.JSXElement.check(node.expression)) return node.expression;
|
|
45
|
+
return node;
|
|
46
|
+
};
|
|
47
|
+
const totalChildren = children.length;
|
|
48
|
+
const iconChildIndex = children.findIndex((child) => {
|
|
49
|
+
const unwrapped = unwrapJsxElement(child);
|
|
50
|
+
return j.JSXElement.check(unwrapped) && unwrapped.openingElement.name.type === "JSXIdentifier" && iconImports.has(unwrapped.openingElement.name.name);
|
|
51
|
+
});
|
|
52
|
+
if (iconChildIndex === -1) return;
|
|
53
|
+
const iconChild = unwrapJsxElement(children[iconChildIndex]);
|
|
54
|
+
if (!iconChild || iconChild.openingElement.name.type !== "JSXIdentifier") return;
|
|
55
|
+
iconChild.openingElement.name.name;
|
|
56
|
+
const distanceToStart = iconChildIndex;
|
|
57
|
+
const distanceToEnd = totalChildren - 1 - iconChildIndex;
|
|
58
|
+
const iconPropName = distanceToStart <= distanceToEnd ? "addonStart" : "addonEnd";
|
|
59
|
+
const iconObject = j.objectExpression([j.property("init", j.identifier("type"), j.literal("icon")), j.property("init", j.identifier("value"), iconChild)]);
|
|
60
|
+
const iconProp = j.jsxAttribute(j.jsxIdentifier(iconPropName), j.jsxExpressionContainer(iconObject));
|
|
61
|
+
openingElement.attributes.push(iconProp);
|
|
62
|
+
children.splice(iconChildIndex, 1);
|
|
63
|
+
const isWhitespaceJsxText = (node) => {
|
|
64
|
+
return typeof node === "object" && node !== null && node.type === "JSXText" && typeof node.value === "string" && node.value.trim() === "";
|
|
65
|
+
};
|
|
66
|
+
if (iconChildIndex - 1 >= 0 && isWhitespaceJsxText(children[iconChildIndex - 1])) children.splice(iconChildIndex - 1, 1);
|
|
67
|
+
else if (isWhitespaceJsxText(children[iconChildIndex])) children.splice(iconChildIndex, 1);
|
|
100
68
|
};
|
|
101
|
-
var iconUtils_default = processIconChildren;
|
|
102
69
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/transforms/helpers/jsxElementUtils.ts
|
|
72
|
+
/**
|
|
73
|
+
* Rename a JSX element name if it is a JSXIdentifier.
|
|
74
|
+
*/
|
|
75
|
+
const setNameIfJSXIdentifier = (elementName, newName) => {
|
|
76
|
+
if (elementName && elementName.type === "JSXIdentifier") return {
|
|
77
|
+
...elementName,
|
|
78
|
+
name: newName
|
|
79
|
+
};
|
|
80
|
+
return elementName;
|
|
109
81
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Check if a list of attributes contains a specific attribute by name.
|
|
84
|
+
*/
|
|
85
|
+
const hasAttribute = (attributes, attributeName) => {
|
|
86
|
+
return Array.isArray(attributes) && attributes.some((attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === attributeName);
|
|
114
87
|
};
|
|
115
|
-
|
|
116
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Check if a JSX element's openingElement has a specific attribute.
|
|
90
|
+
*/
|
|
91
|
+
const hasAttributeOnElement = (element, attributeName) => {
|
|
92
|
+
return hasAttribute(element.attributes, attributeName);
|
|
117
93
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Add specified attributes to a JSX element's openingElement if they are not already present.
|
|
96
|
+
*/
|
|
97
|
+
const addAttributesIfMissing = (j, openingElement, attributesToAdd) => {
|
|
98
|
+
if (!Array.isArray(openingElement.attributes)) return;
|
|
99
|
+
const attrs = openingElement.attributes;
|
|
100
|
+
attributesToAdd.forEach(({ attribute, name }) => {
|
|
101
|
+
if (!hasAttributeOnElement(openingElement, name)) attrs.push(attribute);
|
|
102
|
+
});
|
|
126
103
|
};
|
|
127
104
|
|
|
128
|
-
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/transforms/helpers/jsxReportingUtils.ts
|
|
107
|
+
/**
|
|
108
|
+
* CodemodReporter is a utility class for reporting issues found during codemod transformations.
|
|
109
|
+
* It provides methods to report issues related to JSX elements, props, and attributes.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const issues: string[] = [];
|
|
114
|
+
* const reporter = createReporter(j, issues);
|
|
115
|
+
*
|
|
116
|
+
* // Report a deprecated prop
|
|
117
|
+
* reporter.reportDeprecatedProp(buttonElement, 'flat', 'variant="text"');
|
|
118
|
+
*
|
|
119
|
+
* // Report complex expression that needs review
|
|
120
|
+
* reporter.reportAmbiguousExpression(element, 'size');
|
|
121
|
+
*
|
|
122
|
+
* // Auto-detect common issues
|
|
123
|
+
* reporter.reportAttributeIssues(element);
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
129
126
|
var CodemodReporter = class {
|
|
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
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
getAttributeReason(attr) {
|
|
261
|
-
if (!attr.value) return "has no value";
|
|
262
|
-
if (attr.value.type === "JSXExpressionContainer") {
|
|
263
|
-
const expr = attr.value.expression;
|
|
264
|
-
const expressionType = expr.type.replace("Expression", "").toLowerCase();
|
|
265
|
-
if (expr.type === "Identifier" || expr.type === "MemberExpression") {
|
|
266
|
-
const valueText = this.j(expr).toSource();
|
|
267
|
-
return `contains a ${expressionType} (${valueText})`;
|
|
268
|
-
}
|
|
269
|
-
return `contains a complex ${expressionType} expression`;
|
|
270
|
-
}
|
|
271
|
-
return "needs manual review";
|
|
272
|
-
}
|
|
273
|
-
addIssue(message) {
|
|
274
|
-
this.issues.push(message);
|
|
275
|
-
}
|
|
127
|
+
j;
|
|
128
|
+
issues;
|
|
129
|
+
constructor(options) {
|
|
130
|
+
this.j = options.jscodeshift;
|
|
131
|
+
this.issues = options.issues;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Reports an issue with a JSX element
|
|
135
|
+
*/
|
|
136
|
+
reportElement(element, reason) {
|
|
137
|
+
const node = this.getNode(element);
|
|
138
|
+
const componentName = this.getComponentName(node);
|
|
139
|
+
const line = this.getLineNumber(node);
|
|
140
|
+
this.addIssue(`Manual review required: <${componentName}> at line ${line} ${reason}.`);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Reports an issue with a specific prop
|
|
144
|
+
*/
|
|
145
|
+
reportProp(element, propName, reason) {
|
|
146
|
+
const node = this.getNode(element);
|
|
147
|
+
const componentName = this.getComponentName(node);
|
|
148
|
+
const line = this.getLineNumber(node);
|
|
149
|
+
this.addIssue(`Manual review required: prop "${propName}" on <${componentName}> at line ${line} ${reason}.`);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Reports an issue with a JSX attribute directly
|
|
153
|
+
*/
|
|
154
|
+
reportAttribute(attr, element, reason) {
|
|
155
|
+
const node = this.getNode(element);
|
|
156
|
+
const componentName = this.getComponentName(node);
|
|
157
|
+
const propName = this.getAttributeName(attr);
|
|
158
|
+
const line = this.getLineNumber(attr) || this.getLineNumber(node);
|
|
159
|
+
const defaultReason = this.getAttributeReason(attr);
|
|
160
|
+
const finalReason = reason || defaultReason;
|
|
161
|
+
this.addIssue(`Manual review required: prop "${propName}" on <${componentName}> at line ${line} ${finalReason}.`);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Reports spread props on an element
|
|
165
|
+
*/
|
|
166
|
+
reportSpreadProps(element) {
|
|
167
|
+
this.reportElement(element, "contains spread props that need manual review");
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Reports conflicting prop and children
|
|
171
|
+
*/
|
|
172
|
+
reportPropWithChildren(element, propName) {
|
|
173
|
+
this.reportProp(element, propName, `conflicts with children - both "${propName}" prop and children are present`);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Reports unsupported prop value
|
|
177
|
+
*/
|
|
178
|
+
reportUnsupportedValue(element, propName, value) {
|
|
179
|
+
this.reportProp(element, propName, `has unsupported value "${value}"`);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Reports ambiguous expression in prop
|
|
183
|
+
*/
|
|
184
|
+
reportAmbiguousExpression(element, propName) {
|
|
185
|
+
this.reportProp(element, propName, "contains a complex expression that needs manual review");
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Reports ambiguous children (like dynamic icons)
|
|
189
|
+
*/
|
|
190
|
+
reportAmbiguousChildren(element, childType = "content") {
|
|
191
|
+
this.reportElement(element, `contains ambiguous ${childType} that needs manual review`);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Reports deprecated prop usage
|
|
195
|
+
*/
|
|
196
|
+
reportDeprecatedProp(element, propName, alternative) {
|
|
197
|
+
const suggestion = alternative ? ` Use ${alternative} instead` : "";
|
|
198
|
+
this.reportProp(element, propName, `is deprecated${suggestion}`);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Reports missing required prop
|
|
202
|
+
*/
|
|
203
|
+
reportMissingRequiredProp(element, propName) {
|
|
204
|
+
this.reportProp(element, propName, "is required but missing");
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Reports conflicting props
|
|
208
|
+
*/
|
|
209
|
+
reportConflictingProps(element, propNames) {
|
|
210
|
+
const propList = propNames.map((name) => `"${name}"`).join(", ");
|
|
211
|
+
this.reportElement(element, `has conflicting props: ${propList} cannot be used together`);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Auto-detects and reports common attribute issues
|
|
215
|
+
*/
|
|
216
|
+
reportAttributeIssues(element) {
|
|
217
|
+
const node = this.getNode(element);
|
|
218
|
+
const { attributes } = node.openingElement;
|
|
219
|
+
if (!attributes) return;
|
|
220
|
+
if (attributes.some((attr) => attr.type === "JSXSpreadAttribute")) this.reportSpreadProps(element);
|
|
221
|
+
attributes.forEach((attr) => {
|
|
222
|
+
if (attr.type === "JSXAttribute" && attr.value?.type === "JSXExpressionContainer") this.reportAttribute(attr, element);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
getNode(element) {
|
|
226
|
+
return "node" in element ? element.node : element;
|
|
227
|
+
}
|
|
228
|
+
getComponentName(node) {
|
|
229
|
+
const { name } = node.openingElement;
|
|
230
|
+
if (name.type === "JSXIdentifier") return name.name;
|
|
231
|
+
return this.j(name).toSource();
|
|
232
|
+
}
|
|
233
|
+
getLineNumber(node) {
|
|
234
|
+
return node.loc?.start.line?.toString() || "unknown";
|
|
235
|
+
}
|
|
236
|
+
getAttributeName(attr) {
|
|
237
|
+
if (attr.name.type === "JSXIdentifier") return attr.name.name;
|
|
238
|
+
return this.j(attr.name).toSource();
|
|
239
|
+
}
|
|
240
|
+
getAttributeReason(attr) {
|
|
241
|
+
if (!attr.value) return "has no value";
|
|
242
|
+
if (attr.value.type === "JSXExpressionContainer") {
|
|
243
|
+
const expr = attr.value.expression;
|
|
244
|
+
const expressionType = expr.type.replace("Expression", "").toLowerCase();
|
|
245
|
+
if (expr.type === "Identifier" || expr.type === "MemberExpression") {
|
|
246
|
+
const valueText = this.j(expr).toSource();
|
|
247
|
+
return `contains a ${expressionType} (${valueText})`;
|
|
248
|
+
}
|
|
249
|
+
return `contains a complex ${expressionType} expression`;
|
|
250
|
+
}
|
|
251
|
+
return "needs manual review";
|
|
252
|
+
}
|
|
253
|
+
addIssue(message) {
|
|
254
|
+
this.issues.push(message);
|
|
255
|
+
}
|
|
276
256
|
};
|
|
277
|
-
|
|
278
|
-
|
|
257
|
+
const createReporter = (j, issues) => {
|
|
258
|
+
return new CodemodReporter({
|
|
259
|
+
jscodeshift: j,
|
|
260
|
+
issues
|
|
261
|
+
});
|
|
279
262
|
};
|
|
280
263
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
264
|
+
//#endregion
|
|
265
|
+
//#region src/transforms/button/transformer.ts
|
|
266
|
+
const parser = "tsx";
|
|
267
|
+
const priorityMapping = {
|
|
268
|
+
accent: {
|
|
269
|
+
primary: "primary",
|
|
270
|
+
secondary: "secondary-neutral",
|
|
271
|
+
tertiary: "tertiary"
|
|
272
|
+
},
|
|
273
|
+
positive: {
|
|
274
|
+
primary: "primary",
|
|
275
|
+
secondary: "secondary-neutral",
|
|
276
|
+
tertiary: "secondary-neutral"
|
|
277
|
+
},
|
|
278
|
+
negative: {
|
|
279
|
+
primary: "primary",
|
|
280
|
+
secondary: "secondary",
|
|
281
|
+
tertiary: "secondary"
|
|
282
|
+
}
|
|
299
283
|
};
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
284
|
+
const sizeMap = {
|
|
285
|
+
EXTRA_SMALL: "xs",
|
|
286
|
+
SMALL: "sm",
|
|
287
|
+
MEDIUM: "md",
|
|
288
|
+
LARGE: "lg",
|
|
289
|
+
EXTRA_LARGE: "xl",
|
|
290
|
+
xs: "sm",
|
|
291
|
+
sm: "sm",
|
|
292
|
+
md: "md",
|
|
293
|
+
lg: "lg",
|
|
294
|
+
xl: "xl"
|
|
311
295
|
};
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
return sizeMap[size] || size;
|
|
296
|
+
const resolveSize = (size) => {
|
|
297
|
+
if (!size) return size;
|
|
298
|
+
const match = /^Size\.(EXTRA_SMALL|SMALL|MEDIUM|LARGE|EXTRA_LARGE)$/u.exec(size);
|
|
299
|
+
if (match) return sizeMap[match[1]];
|
|
300
|
+
return sizeMap[size] || size;
|
|
319
301
|
};
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
return priority;
|
|
302
|
+
const resolvePriority = (type, priority) => {
|
|
303
|
+
if (type && priority) return priorityMapping[type]?.[priority] || priority;
|
|
304
|
+
return priority;
|
|
325
305
|
};
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
"button",
|
|
340
|
-
"reset",
|
|
341
|
-
"submit"
|
|
342
|
-
];
|
|
343
|
-
return type && legacyButtonTypes.includes(type) ? type : null;
|
|
306
|
+
const resolveType = (type, htmlType) => {
|
|
307
|
+
if (htmlType) return htmlType;
|
|
308
|
+
const legacyButtonTypes = [
|
|
309
|
+
"accent",
|
|
310
|
+
"negative",
|
|
311
|
+
"positive",
|
|
312
|
+
"primary",
|
|
313
|
+
"pay",
|
|
314
|
+
"secondary",
|
|
315
|
+
"danger",
|
|
316
|
+
"link"
|
|
317
|
+
];
|
|
318
|
+
return type && legacyButtonTypes.includes(type) ? type : null;
|
|
344
319
|
};
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
"HtmlType.SUBMIT": "submit",
|
|
358
|
-
"HtmlType.BUTTON": "button",
|
|
359
|
-
"HtmlType.RESET": "reset",
|
|
360
|
-
"Sentiment.NEGATIVE": "negative"
|
|
361
|
-
};
|
|
362
|
-
const wasEnum = strippedValue in enumMapping;
|
|
363
|
-
const converted = enumMapping[strippedValue] || strippedValue;
|
|
364
|
-
return { converted, wasEnum };
|
|
320
|
+
const convertEnumValue = (value) => {
|
|
321
|
+
if (!value) return value;
|
|
322
|
+
const strippedValue = value.replace(/^['"]|['"]$/gu, "");
|
|
323
|
+
const enumMapping = {
|
|
324
|
+
"Priority.SECONDARY": "secondary",
|
|
325
|
+
"Priority.PRIMARY": "primary",
|
|
326
|
+
"Priority.TERTIARY": "tertiary",
|
|
327
|
+
"ControlType.NEGATIVE": "negative",
|
|
328
|
+
"ControlType.POSITIVE": "positive",
|
|
329
|
+
"ControlType.ACCENT": "accent"
|
|
330
|
+
};
|
|
331
|
+
return enumMapping[strippedValue] || strippedValue;
|
|
365
332
|
};
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
}
|
|
545
|
-
const rawSentiment = legacyProps.sentiment;
|
|
546
|
-
if (rawSentiment === "negative") {
|
|
547
|
-
newAttributes.push(j.jsxAttribute(j.jsxIdentifier("sentiment"), j.literal("negative")));
|
|
548
|
-
}
|
|
549
|
-
Array.prototype.push.apply(newAttributes, keepAttributes);
|
|
550
|
-
let asIndex = -1;
|
|
551
|
-
let asValue = null;
|
|
552
|
-
let hrefExists = false;
|
|
553
|
-
let asAmbiguous = false;
|
|
554
|
-
let hrefAmbiguous = false;
|
|
555
|
-
newAttributes.forEach((attr, index) => {
|
|
556
|
-
if (attr.type === "JSXAttribute" && attr.name) {
|
|
557
|
-
if (attr.name.name === "as") {
|
|
558
|
-
if (attr.value) {
|
|
559
|
-
if (attr.value.type === "StringLiteral") {
|
|
560
|
-
asValue = attr.value.value;
|
|
561
|
-
} else if (attr.value.type === "JSXExpressionContainer") {
|
|
562
|
-
asAmbiguous = true;
|
|
563
|
-
reporter.reportAttribute(attr, path2);
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
asIndex = index;
|
|
567
|
-
}
|
|
568
|
-
if (attr.name.name === "href") {
|
|
569
|
-
hrefExists = true;
|
|
570
|
-
if (attr.value && attr.value.type !== "StringLiteral") {
|
|
571
|
-
hrefAmbiguous = true;
|
|
572
|
-
reporter.reportAttribute(attr, path2);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
});
|
|
577
|
-
if (asValue && asValue !== "a") {
|
|
578
|
-
reporter.reportUnsupportedValue(path2, "as", asValue);
|
|
579
|
-
}
|
|
580
|
-
if (asValue === "a") {
|
|
581
|
-
if (asIndex !== -1) {
|
|
582
|
-
newAttributes.splice(asIndex, 1);
|
|
583
|
-
}
|
|
584
|
-
if (!hrefExists) {
|
|
585
|
-
newAttributes.push(j.jsxAttribute(j.jsxIdentifier("href"), j.literal("#")));
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
openingElement.attributes = newAttributes;
|
|
589
|
-
if ((openingElement.attributes ?? []).some((attr) => attr.type === "JSXSpreadAttribute")) {
|
|
590
|
-
reporter.reportSpreadProps(path2);
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
}
|
|
594
|
-
if (manualReviewIssues.length > 0) {
|
|
595
|
-
manualReviewIssues.forEach(async (issue) => {
|
|
596
|
-
await reportManualReview_default(file.path, issue);
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
return root.toSource();
|
|
600
|
-
};
|
|
601
|
-
var button_default = transformer;
|
|
602
|
-
export {
|
|
603
|
-
button_default as default,
|
|
604
|
-
parser
|
|
333
|
+
/**
|
|
334
|
+
* This transform function modifies the Button and ActionButton components from the @transferwise/components library.
|
|
335
|
+
* It updates the ActionButton component to use the Button component with specific attributes and mappings.
|
|
336
|
+
* It also processes icon children and removes legacy props.
|
|
337
|
+
*
|
|
338
|
+
* @param {FileInfo} file - The file information object.
|
|
339
|
+
* @param {API} api - The API object for jscodeshift.
|
|
340
|
+
* @param {Options} options - The options object for jscodeshift.
|
|
341
|
+
* @returns {string} - The transformed source code.
|
|
342
|
+
*/
|
|
343
|
+
const transformer = (file, api, options) => {
|
|
344
|
+
const j = api.jscodeshift;
|
|
345
|
+
const root = j(file.source);
|
|
346
|
+
const manualReviewIssues = [];
|
|
347
|
+
const reporter = createReporter(j, manualReviewIssues);
|
|
348
|
+
const { exists: hasButtonImport } = hasImport(root, "@transferwise/components", "Button", j);
|
|
349
|
+
const { exists: hasActionButtonImport, remove: removeActionButtonImport } = hasImport(root, "@transferwise/components", "ActionButton", j);
|
|
350
|
+
const iconImports = /* @__PURE__ */ new Set();
|
|
351
|
+
root.find(j.ImportDeclaration, { source: { value: "@transferwise/icons" } }).forEach((path) => {
|
|
352
|
+
path.node.specifiers?.forEach((specifier) => {
|
|
353
|
+
if ((specifier.type === "ImportDefaultSpecifier" || specifier.type === "ImportSpecifier") && specifier.local) {
|
|
354
|
+
const localName = specifier.local.name;
|
|
355
|
+
iconImports.add(localName);
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
if (hasActionButtonImport) {
|
|
360
|
+
root.findJSXElements("ActionButton").forEach((path) => {
|
|
361
|
+
const { openingElement, closingElement } = path.node;
|
|
362
|
+
openingElement.name = setNameIfJSXIdentifier(openingElement.name, "Button");
|
|
363
|
+
if (closingElement) closingElement.name = setNameIfJSXIdentifier(closingElement.name, "Button");
|
|
364
|
+
addAttributesIfMissing(j, openingElement, [{
|
|
365
|
+
attribute: j.jsxAttribute(j.jsxIdentifier("v2")),
|
|
366
|
+
name: "v2"
|
|
367
|
+
}, {
|
|
368
|
+
attribute: j.jsxAttribute(j.jsxIdentifier("size"), j.literal("sm")),
|
|
369
|
+
name: "size"
|
|
370
|
+
}]);
|
|
371
|
+
processIconChildren(j, path.node.children, iconImports, openingElement);
|
|
372
|
+
if ((openingElement.attributes ?? []).some((attr) => attr.type === "JSXSpreadAttribute")) reporter.reportSpreadProps(path);
|
|
373
|
+
const legacyPropNames = ["priority", "text"];
|
|
374
|
+
const legacyProps = {};
|
|
375
|
+
openingElement.attributes?.forEach((attr) => {
|
|
376
|
+
if (attr.type === "JSXAttribute" && attr.name && attr.name.type === "JSXIdentifier") {
|
|
377
|
+
const { name } = attr.name;
|
|
378
|
+
if (legacyPropNames.includes(name)) {
|
|
379
|
+
if (attr.value) {
|
|
380
|
+
if (attr.value.type === "StringLiteral") legacyProps[name] = attr.value.value;
|
|
381
|
+
else if (attr.value.type === "JSXExpressionContainer") reporter.reportAttribute(attr, path);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
const hasTextProp = openingElement.attributes?.some((attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === "text");
|
|
387
|
+
const hasChildren = path.node.children?.some((child) => child.type === "JSXText" && child.value.trim() !== "" || child.type === "JSXElement" || child.type === "JSXExpressionContainer");
|
|
388
|
+
if (hasTextProp && hasChildren) reporter.reportPropWithChildren(path, "text");
|
|
389
|
+
(path.node.children || []).forEach((child) => {
|
|
390
|
+
if (child.type === "JSXExpressionContainer") {
|
|
391
|
+
const expr = child.expression;
|
|
392
|
+
if (expr.type === "ConditionalExpression" || expr.type === "CallExpression" || expr.type === "Identifier" || expr.type === "MemberExpression") reporter.reportAmbiguousChildren(path, "icon");
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
removeActionButtonImport();
|
|
397
|
+
}
|
|
398
|
+
if (hasButtonImport) root.findJSXElements("Button").forEach((path) => {
|
|
399
|
+
const { openingElement } = path.node;
|
|
400
|
+
if (hasAttributeOnElement(openingElement, "v2")) return;
|
|
401
|
+
addAttributesIfMissing(j, openingElement, [{
|
|
402
|
+
attribute: j.jsxAttribute(j.jsxIdentifier("v2")),
|
|
403
|
+
name: "v2"
|
|
404
|
+
}]);
|
|
405
|
+
processIconChildren(j, path.node.children, iconImports, openingElement);
|
|
406
|
+
const legacyProps = {};
|
|
407
|
+
const legacyPropNames = [
|
|
408
|
+
"priority",
|
|
409
|
+
"size",
|
|
410
|
+
"type",
|
|
411
|
+
"htmlType",
|
|
412
|
+
"sentiment"
|
|
413
|
+
];
|
|
414
|
+
openingElement.attributes?.forEach((attr) => {
|
|
415
|
+
if (attr.type === "JSXAttribute" && attr.name && attr.name.type === "JSXIdentifier") {
|
|
416
|
+
const { name } = attr.name;
|
|
417
|
+
if (legacyPropNames.includes(name)) if (attr.value) {
|
|
418
|
+
if (attr.value.type === "StringLiteral") legacyProps[name] = attr.value.value;
|
|
419
|
+
else if (attr.value.type === "JSXExpressionContainer") legacyProps[name] = convertEnumValue(String(j(attr.value.expression).toSource()));
|
|
420
|
+
} else legacyProps[name] = void 0;
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
if (openingElement.attributes) openingElement.attributes = openingElement.attributes.filter((attr) => !(attr.type === "JSXAttribute" && attr.name && legacyPropNames.includes(attr.name.name)));
|
|
424
|
+
if ("size" in legacyProps) {
|
|
425
|
+
const rawValue = legacyProps.size;
|
|
426
|
+
const resolved = resolveSize(rawValue);
|
|
427
|
+
const supportedSizes = [
|
|
428
|
+
"xs",
|
|
429
|
+
"sm",
|
|
430
|
+
"md",
|
|
431
|
+
"lg",
|
|
432
|
+
"xl"
|
|
433
|
+
];
|
|
434
|
+
if (typeof rawValue === "string" && typeof resolved === "string" && supportedSizes.includes(resolved)) openingElement.attributes?.push(j.jsxAttribute(j.jsxIdentifier("size"), j.literal(resolved)));
|
|
435
|
+
else if (typeof rawValue === "string") reporter.reportUnsupportedValue(path, "size", rawValue);
|
|
436
|
+
else if (rawValue !== void 0) reporter.reportAmbiguousExpression(path, "size");
|
|
437
|
+
}
|
|
438
|
+
if ("priority" in legacyProps) {
|
|
439
|
+
const rawValue = legacyProps.priority;
|
|
440
|
+
const converted = convertEnumValue(rawValue);
|
|
441
|
+
const mapped = resolvePriority(legacyProps.type, converted);
|
|
442
|
+
const supportedPriorities = [
|
|
443
|
+
"primary",
|
|
444
|
+
"secondary",
|
|
445
|
+
"tertiary",
|
|
446
|
+
"secondary-neutral"
|
|
447
|
+
];
|
|
448
|
+
if (typeof rawValue === "string" && typeof mapped === "string" && supportedPriorities.includes(mapped)) openingElement.attributes?.push(j.jsxAttribute(j.jsxIdentifier("priority"), j.literal(mapped)));
|
|
449
|
+
else if (typeof rawValue === "string") reporter.reportUnsupportedValue(path, "priority", rawValue);
|
|
450
|
+
else if (rawValue !== void 0) reporter.reportAmbiguousExpression(path, "priority");
|
|
451
|
+
}
|
|
452
|
+
if ("type" in legacyProps || "htmlType" in legacyProps) {
|
|
453
|
+
const rawType = legacyProps.type;
|
|
454
|
+
const rawHtmlType = legacyProps.htmlType;
|
|
455
|
+
const resolvedType = typeof rawType === "string" ? rawType : rawType && typeof rawType === "object" ? convertEnumValue(j(rawType).toSource()) : void 0;
|
|
456
|
+
const resolved = resolveType(resolvedType, rawHtmlType);
|
|
457
|
+
const supportedTypes = [
|
|
458
|
+
"accent",
|
|
459
|
+
"negative",
|
|
460
|
+
"positive",
|
|
461
|
+
"primary",
|
|
462
|
+
"pay",
|
|
463
|
+
"secondary",
|
|
464
|
+
"danger",
|
|
465
|
+
"link",
|
|
466
|
+
"submit",
|
|
467
|
+
"button",
|
|
468
|
+
"reset"
|
|
469
|
+
];
|
|
470
|
+
if (typeof resolved === "string" && supportedTypes.includes(resolved)) {
|
|
471
|
+
openingElement.attributes?.push(j.jsxAttribute(j.jsxIdentifier("type"), j.literal(resolved)));
|
|
472
|
+
if (resolved === "negative") openingElement.attributes?.push(j.jsxAttribute(j.jsxIdentifier("sentiment"), j.literal("negative")));
|
|
473
|
+
} else if (typeof rawType === "string" || typeof rawHtmlType === "string") reporter.reportUnsupportedValue(path, "type", rawType ?? rawHtmlType ?? "");
|
|
474
|
+
else if (rawType !== void 0 || rawHtmlType !== void 0) reporter.reportAmbiguousExpression(path, "type");
|
|
475
|
+
}
|
|
476
|
+
if ("sentiment" in legacyProps) {
|
|
477
|
+
const rawValue = legacyProps.sentiment;
|
|
478
|
+
if (rawValue === "negative") openingElement.attributes?.push(j.jsxAttribute(j.jsxIdentifier("sentiment"), j.literal("negative")));
|
|
479
|
+
else if (typeof rawValue === "string") reporter.reportUnsupportedValue(path, "sentiment", rawValue);
|
|
480
|
+
else if (rawValue !== void 0) reporter.reportAmbiguousExpression(path, "sentiment");
|
|
481
|
+
}
|
|
482
|
+
let asIndex = -1;
|
|
483
|
+
let asValue = null;
|
|
484
|
+
let hrefExists = false;
|
|
485
|
+
openingElement.attributes?.forEach((attr, index) => {
|
|
486
|
+
if (attr.type === "JSXAttribute" && attr.name) {
|
|
487
|
+
if (attr.name.name === "as") {
|
|
488
|
+
if (attr.value) {
|
|
489
|
+
if (attr.value.type === "StringLiteral") asValue = attr.value.value;
|
|
490
|
+
else if (attr.value.type === "JSXExpressionContainer") reporter.reportAttribute(attr, path);
|
|
491
|
+
}
|
|
492
|
+
asIndex = index;
|
|
493
|
+
}
|
|
494
|
+
if (attr.name.name === "href") {
|
|
495
|
+
hrefExists = true;
|
|
496
|
+
if (attr.value && attr.value.type !== "StringLiteral") reporter.reportAttribute(attr, path);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
if (asValue && asValue !== "a") reporter.reportUnsupportedValue(path, "as", asValue);
|
|
501
|
+
if (asValue === "a") {
|
|
502
|
+
if (asIndex !== -1) openingElement.attributes = openingElement.attributes?.filter((_, idx) => idx !== asIndex);
|
|
503
|
+
if (!hrefExists) openingElement.attributes = [...openingElement.attributes ?? [], j.jsxAttribute(j.jsxIdentifier("href"), j.literal("#"))];
|
|
504
|
+
}
|
|
505
|
+
if ((openingElement.attributes ?? []).some((attr) => attr.type === "JSXSpreadAttribute")) reporter.reportSpreadProps(path);
|
|
506
|
+
});
|
|
507
|
+
if (manualReviewIssues.length > 0) manualReviewIssues.forEach(async (issue) => {
|
|
508
|
+
await require_reportManualReview.reportManualReview(file.path, issue);
|
|
509
|
+
});
|
|
510
|
+
return root.toSource();
|
|
605
511
|
};
|
|
512
|
+
|
|
513
|
+
//#endregion
|
|
514
|
+
exports.default = transformer;
|
|
515
|
+
exports.parser = parser;
|
|
606
516
|
//# sourceMappingURL=button.js.map
|