@next/codemod 15.0.0-canary.42 → 15.0.0-canary.44

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next/codemod",
3
- "version": "15.0.0-canary.42",
3
+ "version": "15.0.0-canary.44",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function transformer(file, api) {
4
+ var _a, _b, _c;
5
+ const j = api.jscodeshift;
6
+ const root = j(file.source);
7
+ // Find the import declaration for 'next/dynamic'
8
+ const dynamicImportDeclaration = root.find(j.ImportDeclaration, {
9
+ source: { value: 'next/dynamic' },
10
+ });
11
+ // If the import declaration is found
12
+ if (dynamicImportDeclaration.size() > 0) {
13
+ const importDecl = dynamicImportDeclaration.get(0).node;
14
+ const dynamicImportName = (_c = (_b = (_a = importDecl.specifiers) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.local) === null || _c === void 0 ? void 0 : _c.name;
15
+ if (!dynamicImportName) {
16
+ return root.toSource();
17
+ }
18
+ // Find call expressions where the callee is the imported 'dynamic'
19
+ root
20
+ .find(j.CallExpression, {
21
+ callee: { name: dynamicImportName },
22
+ })
23
+ .forEach((path) => {
24
+ var _a;
25
+ const arrowFunction = path.node.arguments[0];
26
+ // Ensure the argument is an ArrowFunctionExpression
27
+ if (arrowFunction && arrowFunction.type === 'ArrowFunctionExpression') {
28
+ const importCall = arrowFunction.body;
29
+ // Ensure the parent of the import call is a CallExpression with a .then
30
+ if (importCall &&
31
+ importCall.type === 'CallExpression' &&
32
+ importCall.callee.type === 'MemberExpression' &&
33
+ 'name' in importCall.callee.property &&
34
+ importCall.callee.property.name === 'then') {
35
+ const thenFunction = importCall.arguments[0];
36
+ // handle case of block statement case `=> { return mod.Component }`
37
+ // transform to`=> { return { default: mod.Component } }`
38
+ if (thenFunction &&
39
+ thenFunction.type === 'ArrowFunctionExpression' &&
40
+ thenFunction.body.type === 'BlockStatement') {
41
+ const returnStatement = thenFunction.body.body[0];
42
+ // Ensure the body of the arrow function has a return statement with a MemberExpression
43
+ if (returnStatement &&
44
+ returnStatement.type === 'ReturnStatement' &&
45
+ ((_a = returnStatement.argument) === null || _a === void 0 ? void 0 : _a.type) === 'MemberExpression') {
46
+ returnStatement.argument = j.objectExpression([
47
+ j.property('init', j.identifier('default'), returnStatement.argument),
48
+ ]);
49
+ }
50
+ }
51
+ // handle case `=> mod.Component`
52
+ // transform to`=> ({ default: mod.Component })`
53
+ if (thenFunction &&
54
+ thenFunction.type === 'ArrowFunctionExpression' &&
55
+ thenFunction.body.type === 'MemberExpression') {
56
+ thenFunction.body = j.objectExpression([
57
+ j.property('init', j.identifier('default'), thenFunction.body),
58
+ ]);
59
+ }
60
+ }
61
+ }
62
+ });
63
+ }
64
+ return root.toSource();
65
+ }
66
+ exports.default = transformer;
67
+ //# sourceMappingURL=next-dynamic-access-named-export.js.map