@next/codemod 15.0.2-canary.9 → 15.0.2

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/lib/utils.js CHANGED
@@ -91,11 +91,6 @@ exports.TRANSFORMER_INQUIRER_CHOICES = [
91
91
  value: 'next-og-import',
92
92
  version: '14.0.0',
93
93
  },
94
- {
95
- title: 'Transform `next/dynamic` imports accessing named exports to return an object with a `default` property',
96
- value: 'next-dynamic-access-named-export',
97
- version: '15.0.0-canary.44',
98
- },
99
94
  {
100
95
  title: 'Install `@vercel/functions` to replace `geo` and `ip` properties on `NextRequest`',
101
96
  value: 'next-request-geo-ip',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next/codemod",
3
- "version": "15.0.2-canary.9",
3
+ "version": "15.0.2",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -230,7 +230,23 @@ function castTypesOrAddComment(j, path, originRequestApiName, root, filePath, in
230
230
  const newCastExpression = j.tsAsExpression(j.tsAsExpression(path.node, j.tsUnknownKeyword()), j.tsTypeReference(j.identifier(targetType)));
231
231
  // Replace the original expression with the new cast expression,
232
232
  // also wrap () around the new cast expression.
233
- j(path).replaceWith(j.parenthesizedExpression(newCastExpression));
233
+ const parent = path.parent.value;
234
+ const wrappedExpression = j.parenthesizedExpression(newCastExpression);
235
+ path.replace(wrappedExpression);
236
+ // If the wrapped expression `(<expression>)` is the beginning of an expression statement,
237
+ // add a void operator to separate the statement, to avoid syntax error that being treated as part of previous statement.
238
+ // example:
239
+ // input:
240
+ // <expression>
241
+ // <expression>
242
+ // output:
243
+ // (<expression> as ...)
244
+ // void (<expression> as ...)
245
+ if (j.ExpressionStatement.check(parent) &&
246
+ parent.expression === path.node) {
247
+ // append a semicolon to the start of the expression statement
248
+ parent.expression = j.unaryExpression('void', parent.expression);
249
+ }
234
250
  modified = true;
235
251
  // If cast types are not imported, add them to the import list
236
252
  const importDeclaration = root.find(j.ImportDeclaration, {
@@ -1,66 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = transformer;
4
- const parser_1 = require("../lib/parser");
5
- function transformer(file, _api) {
6
- const j = (0, parser_1.createParserFromPath)(file.path);
7
- const root = j(file.source);
8
- // Find the import declaration for 'next/dynamic'
9
- const dynamicImportDeclaration = root.find(j.ImportDeclaration, {
10
- source: { value: 'next/dynamic' },
11
- });
12
- // If the import declaration is found
13
- if (dynamicImportDeclaration.size() > 0) {
14
- const importDecl = dynamicImportDeclaration.get(0).node;
15
- const dynamicImportName = importDecl.specifiers?.[0]?.local?.name;
16
- if (!dynamicImportName) {
17
- return file.source;
18
- }
19
- // Find call expressions where the callee is the imported 'dynamic'
20
- root
21
- .find(j.CallExpression, {
22
- callee: { name: dynamicImportName },
23
- })
24
- .forEach((path) => {
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
- returnStatement.argument?.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
- //# sourceMappingURL=next-dynamic-access-named-export.js.map