@wise/wds-codemods 1.0.0-experimental-601f194 → 1.0.0-experimental-49d2d08

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.
@@ -31,6 +31,8 @@ let node_path = require("node:path");
31
31
  node_path = __toESM(node_path);
32
32
  let __inquirer_prompts = require("@inquirer/prompts");
33
33
  let node_fs = require("node:fs");
34
+ let jscodeshift = require("jscodeshift");
35
+ jscodeshift = __toESM(jscodeshift);
34
36
 
35
37
  //#region node_modules/.pnpm/semver@7.7.2/node_modules/semver/internal/constants.js
36
38
  var require_constants = /* @__PURE__ */ __commonJS({ "node_modules/.pnpm/semver@7.7.2/node_modules/semver/internal/constants.js": ((exports, module) => {
@@ -1947,6 +1949,384 @@ async function runTransformPrompts(codemodPath) {
1947
1949
  return answers;
1948
1950
  }
1949
1951
 
1952
+ //#endregion
1953
+ //#region src/helpers/hasImport.ts
1954
+ /**
1955
+ * Checks if a specific import exists in the given root collection and provides
1956
+ * a method to remove it if found.
1957
+ */
1958
+ function hasImport(root, sourceValue, importName, j) {
1959
+ const importDeclarations = root.find(j.ImportDeclaration, { source: { value: sourceValue } });
1960
+ /**
1961
+ * Finds all ImportSpecifier nodes that expose `importName` but
1962
+ * from a different source than `sourceValue`.
1963
+ */
1964
+ const conflictingImports = (() => {
1965
+ const result = [];
1966
+ root.find(j.ImportDeclaration).filter((path$3) => path$3.node.source.value !== sourceValue).forEach((path$3) => {
1967
+ for (const specifier of path$3.node.specifiers ?? []) if (specifier.type === "ImportSpecifier" && specifier.imported.name === importName && specifier.local?.name === importName) result.push(specifier);
1968
+ });
1969
+ return result;
1970
+ })();
1971
+ if (importDeclarations.size() === 0) return {
1972
+ exists: false,
1973
+ remove: () => {},
1974
+ resolvedName: importName,
1975
+ conflictingImports
1976
+ };
1977
+ const namedImport = importDeclarations.find(j.ImportSpecifier, { imported: { name: importName } });
1978
+ const defaultImport = importDeclarations.find(j.ImportDefaultSpecifier, { local: { name: importName } });
1979
+ const aliasImport = importDeclarations.find(j.ImportSpecifier).filter((path$3) => {
1980
+ return path$3.node.imported.name === importName && path$3.node.imported.name !== path$3.node.local?.name;
1981
+ });
1982
+ const exists = namedImport.size() > 0 || defaultImport.size() > 0;
1983
+ const resolveName = () => {
1984
+ if (aliasImport.size() > 0) {
1985
+ const localName = aliasImport.get(0).node.local?.name;
1986
+ if (typeof localName === "string") return localName;
1987
+ if (localName && typeof localName === "object" && "name" in localName && typeof localName.name === "string") return localName.name;
1988
+ return importName;
1989
+ }
1990
+ return importName;
1991
+ };
1992
+ const remove = () => {
1993
+ importDeclarations.forEach((path$3) => {
1994
+ const filteredSpecifiers = path$3.node.specifiers?.filter((specifier) => {
1995
+ if (specifier.type === "ImportSpecifier" && specifier.imported.name === importName) return false;
1996
+ if (specifier.type === "ImportDefaultSpecifier" && specifier.local?.name === importName) return false;
1997
+ return true;
1998
+ }) ?? [];
1999
+ if (filteredSpecifiers.length === 0) path$3.prune();
2000
+ else j(path$3).replaceWith(j.importDeclaration(filteredSpecifiers, path$3.node.source, path$3.node.importKind));
2001
+ });
2002
+ };
2003
+ return {
2004
+ exists,
2005
+ remove,
2006
+ aliases: aliasImport,
2007
+ resolvedName: resolveName(),
2008
+ conflictingImports
2009
+ };
2010
+ }
2011
+ var hasImport_default = hasImport;
2012
+
2013
+ //#endregion
2014
+ //#region src/helpers/addImport.ts
2015
+ /**
2016
+ * Adds a named import if it doesn't already exist.
2017
+ */
2018
+ function addImport(root, sourceValue, importName, j) {
2019
+ const existingImports = root.find(j.ImportDeclaration, { source: { value: sourceValue } });
2020
+ if (existingImports.size() > 0) {
2021
+ if (existingImports.find(j.ImportSpecifier, { imported: { name: importName } }).size() > 0) return;
2022
+ existingImports.forEach((path$3) => {
2023
+ if (path$3.node.specifiers) path$3.node.specifiers.push(j.importSpecifier(j.identifier(importName)));
2024
+ });
2025
+ } else {
2026
+ const newImport = j.importDeclaration([j.importSpecifier(j.identifier(importName))], j.literal(sourceValue));
2027
+ const firstImport = root.find(j.ImportDeclaration).at(0);
2028
+ if (firstImport.size() > 0) firstImport.insertBefore(newImport);
2029
+ else {
2030
+ const program = root.find(j.Program);
2031
+ if (program.size() > 0) program.get("body", 0).insertBefore(newImport);
2032
+ }
2033
+ }
2034
+ }
2035
+ var addImport_default = addImport;
2036
+
2037
+ //#endregion
2038
+ //#region src/helpers/iconUtils.ts
2039
+ /**
2040
+ * Process children of a JSX element to detect icon components and add iconStart or iconEnd attributes accordingly.
2041
+ * This is specific to icon handling but can be reused in codemods dealing with icon children.
2042
+ */
2043
+ const processIconChildren = (j, children, iconImports, openingElement) => {
2044
+ if (!children || !openingElement.attributes) return;
2045
+ const unwrapJsxElement = (node) => {
2046
+ if (typeof node === "object" && node !== null && "type" in node && node.type === "JSXExpressionContainer" && j.JSXElement.check(node.expression)) return node.expression;
2047
+ return node;
2048
+ };
2049
+ const totalChildren = children.length;
2050
+ const iconChildIndex = children.findIndex((child) => {
2051
+ const unwrapped = unwrapJsxElement(child);
2052
+ return j.JSXElement.check(unwrapped) && unwrapped.openingElement.name.type === "JSXIdentifier" && iconImports.has(unwrapped.openingElement.name.name);
2053
+ });
2054
+ if (iconChildIndex === -1) return;
2055
+ const iconChild = unwrapJsxElement(children[iconChildIndex]);
2056
+ if (!iconChild || iconChild.openingElement.name.type !== "JSXIdentifier") return;
2057
+ iconChild.openingElement.name.name;
2058
+ const iconPropName = iconChildIndex <= totalChildren - 1 - iconChildIndex ? "addonStart" : "addonEnd";
2059
+ const iconObject = j.objectExpression([j.property("init", j.identifier("type"), j.literal("icon")), j.property("init", j.identifier("value"), iconChild)]);
2060
+ const iconProp = j.jsxAttribute(j.jsxIdentifier(iconPropName), j.jsxExpressionContainer(iconObject));
2061
+ openingElement.attributes.push(iconProp);
2062
+ children.splice(iconChildIndex, 1);
2063
+ const isWhitespaceJsxText = (node) => {
2064
+ return typeof node === "object" && node !== null && node.type === "JSXText" && typeof node.value === "string" && node.value.trim() === "";
2065
+ };
2066
+ if (iconChildIndex - 1 >= 0 && isWhitespaceJsxText(children[iconChildIndex - 1])) children.splice(iconChildIndex - 1, 1);
2067
+ else if (isWhitespaceJsxText(children[iconChildIndex])) children.splice(iconChildIndex, 1);
2068
+ };
2069
+ var iconUtils_default = processIconChildren;
2070
+
2071
+ //#endregion
2072
+ //#region src/helpers/jsxElementUtils.ts
2073
+ /**
2074
+ * Rename a JSX element name if it is a JSXIdentifier.
2075
+ */
2076
+ const setNameIfJSXIdentifier = (elementName, newName) => {
2077
+ if (elementName && elementName.type === "JSXIdentifier") return {
2078
+ ...elementName,
2079
+ name: newName
2080
+ };
2081
+ return elementName;
2082
+ };
2083
+ /**
2084
+ * Check if a list of attributes contains a specific attribute by name.
2085
+ */
2086
+ const hasAttribute = (attributes, attributeName) => {
2087
+ return Array.isArray(attributes) && attributes.some((attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === attributeName);
2088
+ };
2089
+ /**
2090
+ * Check if a JSX element's openingElement has a specific attribute.
2091
+ */
2092
+ const hasAttributeOnElement = (element, attributeName) => {
2093
+ return hasAttribute(element.attributes, attributeName);
2094
+ };
2095
+ /**
2096
+ * Add specified attributes to a JSX element's openingElement if they are not already present.
2097
+ */
2098
+ const addAttributesIfMissing = (j, openingElement, attributesToAdd) => {
2099
+ if (!Array.isArray(openingElement.attributes)) return;
2100
+ const attrs = openingElement.attributes;
2101
+ attributesToAdd.forEach(({ attribute, name }) => {
2102
+ if (!hasAttributeOnElement(openingElement, name)) attrs.push(attribute);
2103
+ });
2104
+ };
2105
+ /**
2106
+ * Returns a collection of JSX elements that match the specified
2107
+ * exported name or names of the found aliases.
2108
+ */
2109
+ const findJSXElementsByName = (root, j) => (exportedName, aliases) => {
2110
+ const aliasNames = aliases?.size() ? aliases.paths().map((path$3) => path$3.node.local?.name) : [];
2111
+ return root.find(j.JSXElement).filter((path$3) => {
2112
+ const { name } = path$3.node.openingElement;
2113
+ return name.type === "JSXIdentifier" && (name.name === exportedName || aliasNames.includes(name.name));
2114
+ });
2115
+ };
2116
+ /**
2117
+ * Removes an attribute by name from a JSX element's openingElement.
2118
+ */
2119
+ const removeAttributeByName = (j, openingElement, attributeName) => {
2120
+ if (!Array.isArray(openingElement.attributes)) return;
2121
+ openingElement.attributes = openingElement.attributes.filter((attr) => {
2122
+ return !(attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === attributeName);
2123
+ });
2124
+ };
2125
+
2126
+ //#endregion
2127
+ //#region src/helpers/jsxReportingUtils.ts
2128
+ /**
2129
+ * CodemodReporter is a utility class for reporting issues found during codemod transformations.
2130
+ * It provides methods to report issues related to JSX elements, props, and attributes.
2131
+ *
2132
+ * @example
2133
+ * ```typescript
2134
+ * const issues: string[] = [];
2135
+ * const reporter = createReporter(j, issues);
2136
+ *
2137
+ * // Report a deprecated prop
2138
+ * reporter.reportDeprecatedProp(buttonElement, 'flat', 'variant="text"');
2139
+ *
2140
+ * // Report complex expression that needs review
2141
+ * reporter.reportAmbiguousExpression(element, 'size');
2142
+ *
2143
+ * // Auto-detect common issues
2144
+ * reporter.reportAttributeIssues(element);
2145
+ * ```
2146
+ */
2147
+ var CodemodReporter = class {
2148
+ j;
2149
+ issues;
2150
+ constructor(options) {
2151
+ this.j = options.jscodeshift;
2152
+ this.issues = options.issues;
2153
+ }
2154
+ /**
2155
+ * Reports an issue with a JSX element
2156
+ */
2157
+ reportElement(element, reason) {
2158
+ const node = this.getNode(element);
2159
+ const componentName = this.getComponentName(node);
2160
+ const line = this.getLineNumber(node);
2161
+ this.addIssue(`Manual review required: <${componentName}> at line ${line} ${reason}.`);
2162
+ }
2163
+ /**
2164
+ * Reports an issue with a specific prop
2165
+ */
2166
+ reportProp(element, propName, reason) {
2167
+ const node = this.getNode(element);
2168
+ const componentName = this.getComponentName(node);
2169
+ const line = this.getLineNumber(node);
2170
+ this.addIssue(`Manual review required: prop "${propName}" on <${componentName}> at line ${line} ${reason}.`);
2171
+ }
2172
+ /**
2173
+ * Reports an issue with a JSX attribute directly
2174
+ */
2175
+ reportAttribute(attr, element, reason) {
2176
+ const node = this.getNode(element);
2177
+ const componentName = this.getComponentName(node);
2178
+ const propName = this.getAttributeName(attr);
2179
+ const line = this.getLineNumber(attr) || this.getLineNumber(node);
2180
+ const defaultReason = this.getAttributeReason(attr);
2181
+ const finalReason = reason || defaultReason;
2182
+ this.addIssue(`Manual review required: prop "${propName}" on <${componentName}> at line ${line} ${finalReason}.`);
2183
+ }
2184
+ /**
2185
+ * Reports spread props on an element
2186
+ */
2187
+ reportSpreadProps(element) {
2188
+ this.reportElement(element, "contains spread props that need manual review");
2189
+ }
2190
+ /**
2191
+ * Reports conflicting prop and children
2192
+ */
2193
+ reportPropWithChildren(element, propName) {
2194
+ this.reportProp(element, propName, `conflicts with children - both "${propName}" prop and children are present`);
2195
+ }
2196
+ /**
2197
+ * Reports unsupported prop value
2198
+ */
2199
+ reportUnsupportedValue(element, propName, value) {
2200
+ this.reportProp(element, propName, `has unsupported value "${value}"`);
2201
+ }
2202
+ /**
2203
+ * Reports ambiguous expression in prop
2204
+ */
2205
+ reportAmbiguousExpression(element, propName) {
2206
+ this.reportProp(element, propName, "contains a complex expression that needs manual review");
2207
+ }
2208
+ /**
2209
+ * Reports ambiguous children (like dynamic icons)
2210
+ */
2211
+ reportAmbiguousChildren(element, childType = "content") {
2212
+ this.reportElement(element, `contains ambiguous ${childType} that needs manual review`);
2213
+ }
2214
+ /**
2215
+ * Reports deprecated prop usage
2216
+ */
2217
+ reportDeprecatedProp(element, propName, alternative) {
2218
+ const suggestion = alternative ? ` Use ${alternative} instead` : "";
2219
+ this.reportProp(element, propName, `is deprecated${suggestion}`);
2220
+ }
2221
+ /**
2222
+ * Reports missing required prop
2223
+ */
2224
+ reportMissingRequiredProp(element, propName) {
2225
+ this.reportProp(element, propName, "is required but missing");
2226
+ }
2227
+ /**
2228
+ * Reports conflicting props
2229
+ */
2230
+ reportConflictingProps(element, propNames) {
2231
+ const propList = propNames.map((name) => `"${name}"`).join(", ");
2232
+ this.reportElement(element, `has conflicting props: ${propList} cannot be used together`);
2233
+ }
2234
+ /**
2235
+ * Auto-detects and reports common attribute issues
2236
+ */
2237
+ reportAttributeIssues(element) {
2238
+ const { attributes } = this.getNode(element).openingElement;
2239
+ if (!attributes) return;
2240
+ if (attributes.some((attr) => attr.type === "JSXSpreadAttribute")) this.reportSpreadProps(element);
2241
+ attributes.forEach((attr) => {
2242
+ if (attr.type === "JSXAttribute" && attr.value?.type === "JSXExpressionContainer") this.reportAttribute(attr, element);
2243
+ });
2244
+ }
2245
+ /**
2246
+ * Finds and reports instances of components that are under an alias (imported with a different name)
2247
+ */
2248
+ reportAliases(element) {
2249
+ this.reportElement(element, "is used via an import alias and needs manual review");
2250
+ }
2251
+ /**
2252
+ * Finds and reports instances of non-DS import declarations that conflict with the component name
2253
+ */
2254
+ reportConflictingImports(node) {
2255
+ this.addIssue(`Manual review required: Non-WDS package resulting in an import conflict at line ${this.getLineNumber(node)}.`);
2256
+ }
2257
+ /**
2258
+ * Reports enum usage for future conversion tracking
2259
+ */
2260
+ reportEnumUsage(element, propName, enumValue) {
2261
+ this.reportProp(element, propName, `uses enum value "${enumValue}" which has been preserved but should be migrated to a string literal in the future`);
2262
+ }
2263
+ getNode(element) {
2264
+ return "node" in element ? element.node : element;
2265
+ }
2266
+ getComponentName(node) {
2267
+ const { name } = node.openingElement;
2268
+ if (name.type === "JSXIdentifier") return name.name;
2269
+ return this.j(name).toSource();
2270
+ }
2271
+ getLineNumber(node) {
2272
+ return node.loc?.start.line?.toString() || "unknown";
2273
+ }
2274
+ getAttributeName(attr) {
2275
+ if (attr.name.type === "JSXIdentifier") return attr.name.name;
2276
+ return this.j(attr.name).toSource();
2277
+ }
2278
+ getAttributeReason(attr) {
2279
+ if (!attr.value) return "has no value";
2280
+ if (attr.value.type === "JSXExpressionContainer") {
2281
+ const expr = attr.value.expression;
2282
+ const expressionType = expr.type.replace("Expression", "").toLowerCase();
2283
+ if (expr.type === "Identifier" || expr.type === "MemberExpression") return `contains a ${expressionType} (${this.j(expr).toSource()})`;
2284
+ return `contains a complex ${expressionType} expression`;
2285
+ }
2286
+ return "needs manual review";
2287
+ }
2288
+ addIssue(message) {
2289
+ this.issues.push(message);
2290
+ }
2291
+ };
2292
+ const createReporter = (j, issues) => {
2293
+ return new CodemodReporter({
2294
+ jscodeshift: j,
2295
+ issues
2296
+ });
2297
+ };
2298
+
2299
+ //#endregion
2300
+ //#region src/helpers/index.ts
2301
+ const EXT = /\.(?:jsx?|tsx?)$/iu;
2302
+ function isCodeFile(p) {
2303
+ return EXT.test(p);
2304
+ }
2305
+ function walk(p) {
2306
+ if ((0, node_fs.statSync)(p).isFile()) return isCodeFile(p) ? [p] : [];
2307
+ return (0, node_fs.readdirSync)(p, { withFileTypes: true }).flatMap((d) => {
2308
+ if (d.name === "node_modules" || d.name.startsWith(".")) return [];
2309
+ return walk((0, node_path.join)(p, d.name));
2310
+ });
2311
+ }
2312
+ function findFilesWithImports(targetPaths, source, names, parser = "tsx") {
2313
+ const j = jscodeshift.default.withParser(parser);
2314
+ return Array.from(new Set(targetPaths.map((p) => (0, node_path.resolve)(p)).flatMap((p) => {
2315
+ try {
2316
+ return (0, node_fs.statSync)(p).isDirectory() ? walk(p) : isCodeFile(p) ? [p] : [];
2317
+ } catch {
2318
+ return [];
2319
+ }
2320
+ }))).filter((file) => {
2321
+ try {
2322
+ const root = j((0, node_fs.readFileSync)(file, "utf8"));
2323
+ return names.some((n) => hasImport_default(root, source, n, j).exists);
2324
+ } catch {
2325
+ return false;
2326
+ }
2327
+ });
2328
+ }
2329
+
1950
2330
  //#endregion
1951
2331
  Object.defineProperty(exports, '__toESM', {
1952
2332
  enumerable: true,
@@ -1954,12 +2334,42 @@ Object.defineProperty(exports, '__toESM', {
1954
2334
  return __toESM;
1955
2335
  }
1956
2336
  });
2337
+ Object.defineProperty(exports, 'addAttributesIfMissing', {
2338
+ enumerable: true,
2339
+ get: function () {
2340
+ return addAttributesIfMissing;
2341
+ }
2342
+ });
2343
+ Object.defineProperty(exports, 'addImport_default', {
2344
+ enumerable: true,
2345
+ get: function () {
2346
+ return addImport_default;
2347
+ }
2348
+ });
1957
2349
  Object.defineProperty(exports, 'assessPrerequisites', {
1958
2350
  enumerable: true,
1959
2351
  get: function () {
1960
2352
  return assessPrerequisites;
1961
2353
  }
1962
2354
  });
2355
+ Object.defineProperty(exports, 'createReporter', {
2356
+ enumerable: true,
2357
+ get: function () {
2358
+ return createReporter;
2359
+ }
2360
+ });
2361
+ Object.defineProperty(exports, 'findFilesWithImports', {
2362
+ enumerable: true,
2363
+ get: function () {
2364
+ return findFilesWithImports;
2365
+ }
2366
+ });
2367
+ Object.defineProperty(exports, 'findJSXElementsByName', {
2368
+ enumerable: true,
2369
+ get: function () {
2370
+ return findJSXElementsByName;
2371
+ }
2372
+ });
1963
2373
  Object.defineProperty(exports, 'findPackages', {
1964
2374
  enumerable: true,
1965
2375
  get: function () {
@@ -1978,6 +2388,24 @@ Object.defineProperty(exports, 'getOptions_default', {
1978
2388
  return getOptions_default;
1979
2389
  }
1980
2390
  });
2391
+ Object.defineProperty(exports, 'hasAttributeOnElement', {
2392
+ enumerable: true,
2393
+ get: function () {
2394
+ return hasAttributeOnElement;
2395
+ }
2396
+ });
2397
+ Object.defineProperty(exports, 'hasImport_default', {
2398
+ enumerable: true,
2399
+ get: function () {
2400
+ return hasImport_default;
2401
+ }
2402
+ });
2403
+ Object.defineProperty(exports, 'iconUtils_default', {
2404
+ enumerable: true,
2405
+ get: function () {
2406
+ return iconUtils_default;
2407
+ }
2408
+ });
1981
2409
  Object.defineProperty(exports, 'loadTransformModules_default', {
1982
2410
  enumerable: true,
1983
2411
  get: function () {
@@ -1990,6 +2418,12 @@ Object.defineProperty(exports, 'logToInquirer', {
1990
2418
  return logToInquirer;
1991
2419
  }
1992
2420
  });
2421
+ Object.defineProperty(exports, 'removeAttributeByName', {
2422
+ enumerable: true,
2423
+ get: function () {
2424
+ return removeAttributeByName;
2425
+ }
2426
+ });
1993
2427
  Object.defineProperty(exports, 'reportManualReview_default', {
1994
2428
  enumerable: true,
1995
2429
  get: function () {
@@ -2002,4 +2436,10 @@ Object.defineProperty(exports, 'runTransformPrompts', {
2002
2436
  return runTransformPrompts;
2003
2437
  }
2004
2438
  });
2005
- //# sourceMappingURL=helpers-RWhTD5Is.js.map
2439
+ Object.defineProperty(exports, 'setNameIfJSXIdentifier', {
2440
+ enumerable: true,
2441
+ get: function () {
2442
+ return setNameIfJSXIdentifier;
2443
+ }
2444
+ });
2445
+ //# sourceMappingURL=helpers-59xSDDPO.js.map