@next/codemod 15.0.0-canary.176 → 15.0.0-canary.178

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.176",
3
+ "version": "15.0.0-canary.178",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -144,6 +144,30 @@ function transformDynamicAPI(source, api, filePath) {
144
144
  }
145
145
  }
146
146
  });
147
+ // Handle type usage of async API, e.g. `type Cookie = ReturnType<typeof cookies>`
148
+ // convert it to `type Cookie = Awaited<ReturnType<typeof cookies>>`
149
+ root
150
+ .find(j.TSTypeReference, {
151
+ typeName: {
152
+ type: 'Identifier',
153
+ name: 'ReturnType',
154
+ },
155
+ })
156
+ .forEach((path) => {
157
+ const typeParam = path.node.typeParameters?.params[0];
158
+ // Check if the ReturnType is for 'cookies'
159
+ if (typeParam &&
160
+ j.TSTypeQuery.check(typeParam) &&
161
+ j.Identifier.check(typeParam.exprName) &&
162
+ typeParam.exprName.name === asyncRequestApiName) {
163
+ // Replace ReturnType<typeof cookies> with Awaited<ReturnType<typeof cookies>>
164
+ const awaitedTypeReference = j.tsTypeReference(j.identifier('Awaited'), j.tsTypeParameterInstantiation([
165
+ j.tsTypeReference(j.identifier('ReturnType'), j.tsTypeParameterInstantiation([typeParam])),
166
+ ]));
167
+ j(path).replaceWith(awaitedTypeReference);
168
+ modified = true;
169
+ }
170
+ });
147
171
  }
148
172
  const isClientComponent = (0, utils_1.determineClientDirective)(root, j);
149
173
  // Only transform the valid calls in server or shared components
@@ -141,21 +141,18 @@ function insertReactUseImport(root, j) {
141
141
  if (!hasReactUseImport) {
142
142
  const reactImportDeclaration = root.find(j.ImportDeclaration, {
143
143
  source: {
144
- type: 'Literal',
145
144
  value: 'react',
146
145
  },
147
146
  });
148
147
  if (reactImportDeclaration.size() > 0) {
148
+ const importNode = reactImportDeclaration.get().node;
149
149
  // Add 'use' to existing 'react' import declaration
150
- reactImportDeclaration
151
- .get()
152
- .node.specifiers.push(j.importSpecifier(j.identifier('use')));
150
+ importNode.specifiers.push(j.importSpecifier(j.identifier('use')));
153
151
  }
154
152
  else {
155
153
  // Final all type imports to 'react'
156
154
  const reactImport = root.find(j.ImportDeclaration, {
157
155
  source: {
158
- type: 'Literal',
159
156
  value: 'react',
160
157
  },
161
158
  });