Unverified Commit 6bd5c47b authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Add codemod for removing `unstable_` prefix (#84974)

Stacked on https://github.com/vercel/next.js/pull/84877,
https://github.com/vercel/next.js/pull/84880

This PR adds a codemod for removing the `unstable_` prefix for
stabilized APIs. It is considered to scale by adding more APIs to the
`UNSTABLE_TO_STABLE_MAPPING` map in the future to this codemod.

Closes NEXT-4739
parent 828211ef
......@@ -132,4 +132,9 @@ export const TRANSFORMER_INQUIRER_CHOICES = [
value: 'middleware-to-proxy',
version: '15.6.0-canary.54',
},
{
title: 'Remove `unstable_` prefix from stabilized API',
value: 'remove-unstable-prefix',
version: '16.0.0-canary.10',
},
]
// @ts-nocheck
import { unstable_cacheTag as cacheTagImport, unstable_cacheLife as cacheLifeImport } from 'next/cache'
const { unstable_cacheTag: cacheTagRequire, unstable_cacheLife: cacheLifeRequire } = require('next/cache')
// Alias with same API name, alias should be removed.
import { unstable_cacheTag as cacheTag, unstable_cacheLife as cacheLife } from 'next/cache'
const { unstable_cacheTag: cacheTag, unstable_cacheLife: cacheLife } = require('next/cache')
export function MyComponent() {
const cacheTagImportTag = cacheTagImport('foo')
const cacheLifeImportLife = cacheLifeImport('1 hour')
const cacheTagRequireTag = cacheTagRequire('bar')
const cacheLifeRequireLife = cacheLifeRequire('2 hours')
const cacheTagAlias = cacheTag('redundant')
const cacheLifeAlias = cacheLife('redundant-time')
return (
<div>
<p>Using cache tag: {cacheTagImportTag}</p>
<p>Using cache life: {cacheLifeImportLife}</p>
<p>Using another tag: {cacheTagRequireTag}</p>
<p>Using another life: {cacheLifeRequireLife}</p>
<p>Using same alias tag: {cacheTagAlias}</p>
<p>Using same alias life: {cacheLifeAlias}</p>
</div>
)
}
\ No newline at end of file
// @ts-nocheck
import { cacheTag as cacheTagImport, cacheLife as cacheLifeImport } from 'next/cache'
const { cacheTag: cacheTagRequire, cacheLife: cacheLifeRequire } = require('next/cache')
// Alias with same API name, alias should be removed.
import { cacheTag, cacheLife } from 'next/cache'
const { cacheTag, cacheLife } = require('next/cache')
export function MyComponent() {
const cacheTagImportTag = cacheTagImport('foo')
const cacheLifeImportLife = cacheLifeImport('1 hour')
const cacheTagRequireTag = cacheTagRequire('bar')
const cacheLifeRequireLife = cacheLifeRequire('2 hours')
const cacheTagAlias = cacheTag('redundant')
const cacheLifeAlias = cacheLife('redundant-time')
return (
<div>
<p>Using cache tag: {cacheTagImportTag}</p>
<p>Using cache life: {cacheLifeImportLife}</p>
<p>Using another tag: {cacheTagRequireTag}</p>
<p>Using another life: {cacheLifeRequireLife}</p>
<p>Using same alias tag: {cacheTagAlias}</p>
<p>Using same alias life: {cacheLifeAlias}</p>
</div>
)
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
const cache = require('next/cache')
const dynamicCache = await import('next/cache')
// Bracket notation property access with string literals
const tag = cache['unstable_cacheTag']('my-tag')
const life = dynamicCache['unstable_cacheLife']('2 hours')
// Direct bracket notation access on require
const directTag = require('next/cache')['unstable_cacheTag']
// Bracket notation property access inside a function
function testCache() {
const tag2 = cache['unstable_cacheTag']('tag2')
const life2 = dynamicCache['unstable_cacheLife']('life2')
return { tag2, life2 }
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
const cache = require('next/cache')
const dynamicCache = await import('next/cache')
// Bracket notation property access with string literals
const tag = cache["cacheTag"]('my-tag')
const life = dynamicCache["cacheLife"]('2 hours')
// Direct bracket notation access on require
const directTag = require('next/cache')["cacheTag"]
// Bracket notation property access inside a function
function testCache() {
const tag2 = cache["cacheTag"]('tag2')
const life2 = dynamicCache["cacheLife"]('life2')
return { tag2, life2 }
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
async function loadCache() {
// Dynamic import with destructuring
const { unstable_cacheTag, unstable_cacheLife } = await import('next/cache')
// Dynamic import with property access
const cache = await import('next/cache')
const directTag = cache.unstable_cacheTag
const directLife = cache.unstable_cacheLife
return { directTag, directLife }
}
// Dynamic import without await
const cachePromise = import('next/cache').then(({ unstable_cacheTag, unstable_cacheLife }) => {
const tag = unstable_cacheTag('async-tag')
const life = unstable_cacheLife('3 hours')
return { tag, life }
})
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
async function loadCache() {
// Dynamic import with destructuring
const { cacheTag, cacheLife } = await import('next/cache')
// Dynamic import with property access
const cache = await import('next/cache')
const directTag = cache.cacheTag
const directLife = cache.cacheLife
return { directTag, directLife }
}
// Dynamic import without await
const cachePromise = import('next/cache').then(({ cacheTag, cacheLife }) => {
const tag = cacheTag('async-tag')
const life = cacheLife('3 hours')
return { tag, life }
})
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
function loadCache() {
// Dynamic require with destructuring
const { unstable_cacheTag, unstable_cacheLife } = require('next/cache')
// Dynamic require with property access
const cache = require('next/cache')
const directTag = cache.unstable_cacheTag
const directLife = cache.unstable_cacheLife
// Direct property access on require
const tag = require('next/cache').unstable_cacheTag('my-tag')
const life = require('next/cache').unstable_cacheLife('2 hours')
return { tag, life, directTag, directLife }
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
function loadCache() {
// Dynamic require with destructuring
const { cacheTag, cacheLife } = require('next/cache')
// Dynamic require with property access
const cache = require('next/cache')
const directTag = cache.cacheTag
const directLife = cache.cacheLife
// Direct property access on require
const tag = require('next/cache').cacheTag('my-tag')
const life = require('next/cache').cacheLife('2 hours')
return { tag, life, directTag, directLife }
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
// Re-export unstable APIs from next/cache
export { unstable_cacheTag, unstable_cacheLife, revalidatePath} from 'next/cache'
// Re-export with aliases
export { unstable_cacheTag as createTag, unstable_cacheLife as createLife } from 'next/cache'
// Default export should not be affected
export { default as cache } from 'next/cache'
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
// Re-export unstable APIs from next/cache
export { cacheTag, cacheLife, revalidatePath} from 'next/cache'
// Re-export with aliases
export { cacheTag as createTag, cacheLife as createLife } from 'next/cache'
// Default export should not be affected
export { default as cache } from 'next/cache'
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
import { unstable_cacheTag, revalidatePath, unstable_cacheLife } from 'next/cache'
export function MyComponent() {
const tag = unstable_cacheTag('my-tag')
const life = unstable_cacheLife('2 hours')
// This should remain unchanged
revalidatePath('/app')
return (
<div>
<p>Using cache tag: {tag}</p>
<p>Using cache life: {life}</p>
</div>
)
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
import { cacheTag, revalidatePath, cacheLife } from 'next/cache'
export function MyComponent() {
const tag = cacheTag('my-tag')
const life = cacheLife('2 hours')
// This should remain unchanged
revalidatePath('/app')
return (
<div>
<p>Using cache tag: {tag}</p>
<p>Using cache life: {life}</p>
</div>
)
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
import * as cache from 'next/cache'
export function testNamespaceAccess() {
// Namespace property access
const tag = cache.unstable_cacheTag('tag')
const life = cache.unstable_cacheLife('1 hour')
// This should remain unchanged
const path = cache.revalidatePath('/app')
return { tag, life, path }
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
import * as cache from 'next/cache'
export function testNamespaceAccess() {
// Namespace property access
const tag = cache.cacheTag('tag')
const life = cache.cacheLife('1 hour')
// This should remain unchanged
const path = cache.revalidatePath('/app')
return { tag, life, path }
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
const { unstable_cacheTag, unstable_cacheLife, revalidatePath } = require('next/cache')
export function MyComponent() {
const tag = unstable_cacheTag('my-tag')
const life = unstable_cacheLife('2 hours')
// This should remain unchanged
revalidatePath('/app')
return (
<div>
<p>Using cache tag: {tag}</p>
<p>Using cache life: {life}</p>
</div>
)
}
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
const { cacheTag, cacheLife, revalidatePath } = require('next/cache')
export function MyComponent() {
const tag = cacheTag('my-tag')
const life = cacheLife('2 hours')
// This should remain unchanged
revalidatePath('/app')
return (
<div>
<p>Using cache tag: {tag}</p>
<p>Using cache life: {life}</p>
</div>
)
}
\ No newline at end of file
// @ts-nocheck
// Type imports
import type { unstable_cacheTag, unstable_cacheLife } from 'next/cache'
// Type imports as alias
import type { unstable_cacheTag as regularTag, unstable_cacheLife as regularLife } from 'next/cache'
// @ts-nocheck
// Type imports
import type { cacheTag, cacheLife } from 'next/cache'
// Type imports as alias
import type { cacheTag as regularTag, cacheLife as regularLife } from 'next/cache'
/* global jest */
jest.autoMockOff()
const defineTest = require('jscodeshift/dist/testUtils').defineTest
const { readdirSync } = require('fs')
const { join } = require('path')
const fixtureDir = 'remove-unstable-prefix'
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir)
const fixtures = readdirSync(fixtureDirPath)
.filter(file => file.endsWith('.input.tsx'))
.map(file => file.replace('.input.tsx', ''))
for (const fixture of fixtures) {
const prefix = `${fixtureDir}/${fixture}`;
defineTest(__dirname, fixtureDir, null, prefix, { parser: 'tsx' });
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment