Skip to content

Commit 36d3344

Browse files
committed
Add codemod for removing unstable_ prefix
1 parent c6cd1f4 commit 36d3344

21 files changed

+678
-0
lines changed

packages/next-codemod/lib/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ export const TRANSFORMER_INQUIRER_CHOICES = [
132132
value: 'middleware-to-proxy',
133133
version: '15.6.0-canary.54',
134134
},
135+
{
136+
title: 'Remove `unstable_` prefix from stabilized API',
137+
value: 'remove-unstable-prefix',
138+
version: '16.0.0-canary.10',
139+
},
135140
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @ts-nocheck
2+
import { unstable_cacheTag as cacheTagImport, unstable_cacheLife as cacheLifeImport } from 'next/cache'
3+
const { unstable_cacheTag: cacheTagRequire, unstable_cacheLife: cacheLifeRequire } = require('next/cache')
4+
// Alias with same API name, alias should be removed.
5+
import { unstable_cacheTag as cacheTag, unstable_cacheLife as cacheLife } from 'next/cache'
6+
const { unstable_cacheTag: cacheTag, unstable_cacheLife: cacheLife } = require('next/cache')
7+
8+
export function MyComponent() {
9+
const cacheTagImportTag = cacheTagImport('foo')
10+
const cacheLifeImportLife = cacheLifeImport('1 hour')
11+
const cacheTagRequireTag = cacheTagRequire('bar')
12+
const cacheLifeRequireLife = cacheLifeRequire('2 hours')
13+
const cacheTagAlias = cacheTag('redundant')
14+
const cacheLifeAlias = cacheLife('redundant-time')
15+
16+
return (
17+
<div>
18+
<p>Using cache tag: {cacheTagImportTag}</p>
19+
<p>Using cache life: {cacheLifeImportLife}</p>
20+
<p>Using another tag: {cacheTagRequireTag}</p>
21+
<p>Using another life: {cacheLifeRequireLife}</p>
22+
<p>Using same alias tag: {cacheTagAlias}</p>
23+
<p>Using same alias life: {cacheLifeAlias}</p>
24+
</div>
25+
)
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @ts-nocheck
2+
import { cacheTag as cacheTagImport, cacheLife as cacheLifeImport } from 'next/cache'
3+
const { cacheTag: cacheTagRequire, cacheLife: cacheLifeRequire } = require('next/cache')
4+
// Alias with same API name, alias should be removed.
5+
import { cacheTag, cacheLife } from 'next/cache'
6+
const { cacheTag, cacheLife } = require('next/cache')
7+
8+
export function MyComponent() {
9+
const cacheTagImportTag = cacheTagImport('foo')
10+
const cacheLifeImportLife = cacheLifeImport('1 hour')
11+
const cacheTagRequireTag = cacheTagRequire('bar')
12+
const cacheLifeRequireLife = cacheLifeRequire('2 hours')
13+
const cacheTagAlias = cacheTag('redundant')
14+
const cacheLifeAlias = cacheLife('redundant-time')
15+
16+
return (
17+
<div>
18+
<p>Using cache tag: {cacheTagImportTag}</p>
19+
<p>Using cache life: {cacheLifeImportLife}</p>
20+
<p>Using another tag: {cacheTagRequireTag}</p>
21+
<p>Using another life: {cacheLifeRequireLife}</p>
22+
<p>Using same alias tag: {cacheTagAlias}</p>
23+
<p>Using same alias life: {cacheLifeAlias}</p>
24+
</div>
25+
)
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-nocheck
2+
/* eslint-disable */
3+
const cache = require('next/cache')
4+
const dynamicCache = await import('next/cache')
5+
6+
// Bracket notation property access with string literals
7+
const tag = cache['unstable_cacheTag']('my-tag')
8+
const life = dynamicCache['unstable_cacheLife']('2 hours')
9+
10+
// Direct bracket notation access on require
11+
const directTag = require('next/cache')['unstable_cacheTag']
12+
13+
// Bracket notation property access inside a function
14+
function testCache() {
15+
const tag2 = cache['unstable_cacheTag']('tag2')
16+
const life2 = dynamicCache['unstable_cacheLife']('life2')
17+
18+
return { tag2, life2 }
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-nocheck
2+
/* eslint-disable */
3+
const cache = require('next/cache')
4+
const dynamicCache = await import('next/cache')
5+
6+
// Bracket notation property access with string literals
7+
const tag = cache["cacheTag"]('my-tag')
8+
const life = dynamicCache["cacheLife"]('2 hours')
9+
10+
// Direct bracket notation access on require
11+
const directTag = require('next/cache')["cacheTag"]
12+
13+
// Bracket notation property access inside a function
14+
function testCache() {
15+
const tag2 = cache["cacheTag"]('tag2')
16+
const life2 = dynamicCache["cacheLife"]('life2')
17+
18+
return { tag2, life2 }
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @ts-nocheck
2+
/* eslint-disable */
3+
async function loadCache() {
4+
// Dynamic import with destructuring
5+
const { unstable_cacheTag, unstable_cacheLife } = await import('next/cache')
6+
7+
// Dynamic import with property access
8+
const cache = await import('next/cache')
9+
const directTag = cache.unstable_cacheTag
10+
const directLife = cache.unstable_cacheLife
11+
12+
return { directTag, directLife }
13+
}
14+
15+
// Dynamic import without await
16+
const cachePromise = import('next/cache').then(({ unstable_cacheTag, unstable_cacheLife }) => {
17+
const tag = unstable_cacheTag('async-tag')
18+
const life = unstable_cacheLife('3 hours')
19+
return { tag, life }
20+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @ts-nocheck
2+
/* eslint-disable */
3+
async function loadCache() {
4+
// Dynamic import with destructuring
5+
const { cacheTag, cacheLife } = await import('next/cache')
6+
7+
// Dynamic import with property access
8+
const cache = await import('next/cache')
9+
const directTag = cache.cacheTag
10+
const directLife = cache.cacheLife
11+
12+
return { directTag, directLife }
13+
}
14+
15+
// Dynamic import without await
16+
const cachePromise = import('next/cache').then(({ cacheTag, cacheLife }) => {
17+
const tag = cacheTag('async-tag')
18+
const life = cacheLife('3 hours')
19+
return { tag, life }
20+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @ts-nocheck
2+
/* eslint-disable */
3+
function loadCache() {
4+
// Dynamic require with destructuring
5+
const { unstable_cacheTag, unstable_cacheLife } = require('next/cache')
6+
7+
// Dynamic require with property access
8+
const cache = require('next/cache')
9+
const directTag = cache.unstable_cacheTag
10+
const directLife = cache.unstable_cacheLife
11+
12+
// Direct property access on require
13+
const tag = require('next/cache').unstable_cacheTag('my-tag')
14+
const life = require('next/cache').unstable_cacheLife('2 hours')
15+
16+
return { tag, life, directTag, directLife }
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @ts-nocheck
2+
/* eslint-disable */
3+
function loadCache() {
4+
// Dynamic require with destructuring
5+
const { cacheTag, cacheLife } = require('next/cache')
6+
7+
// Dynamic require with property access
8+
const cache = require('next/cache')
9+
const directTag = cache.cacheTag
10+
const directLife = cache.cacheLife
11+
12+
// Direct property access on require
13+
const tag = require('next/cache').cacheTag('my-tag')
14+
const life = require('next/cache').cacheLife('2 hours')
15+
16+
return { tag, life, directTag, directLife }
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @ts-nocheck
2+
/* eslint-disable */
3+
4+
// Re-export unstable APIs from next/cache
5+
export { unstable_cacheTag, unstable_cacheLife, revalidatePath} from 'next/cache'
6+
7+
// Re-export with aliases
8+
export { unstable_cacheTag as createTag, unstable_cacheLife as createLife } from 'next/cache'
9+
10+
// Default export should not be affected
11+
export { default as cache } from 'next/cache'

0 commit comments

Comments
 (0)