Unverified Commit 2d063a79 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

codemod: replace `revalidate(Tag|Path)` to `expire(Tag|Path)` (#72826)

### Why?

We are renaming `revalidate(Tag|Path)` to `expire(Tag|Path)`.

Closes NDX-482

---------
Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
parent 0f9f442b
......@@ -116,4 +116,10 @@ export const TRANSFORMER_INQUIRER_CHOICES = [
value: 'app-dir-runtime-config-experimental-edge',
version: '15.0.0-canary.179',
},
{
title:
'Transform `revalidateTag` and `revalidatePath` to `expireTag` and `expirePath`',
value: 'revalidate-to-expire',
version: '15.0.4-canary.12',
},
]
import * as cache from "next/cache";
export async function GET() {
cache.revalidatePath("next");
cache.revalidateTag("next");
cache["revalidatePath"]("next");
cache["revalidateTag"]("next");
}
import * as cache from "next/cache";
export async function GET() {
cache.expirePath("next");
cache.expireTag("next");
cache["expirePath"]("next");
cache["expireTag"]("next");
}
import * as cache from "next/cache";
export async function GET() {
cache.revalidatePath("next");
cache.revalidateTag("next");
cache.expirePath("next");
cache.expireTag("next");
}
import * as cache from "next/cache";
export async function GET() {
cache.expirePath("next");
cache.expireTag("next");
cache.expirePath("next");
cache.expireTag("next");
}
import { revalidatePath as revalidatePathAlias, revalidateTag as revalidateTagAlias, expirePath as expirePathAlias, expireTag as expireTagAlias } from "next/cache";
export async function GET() {
revalidatePathAlias("next");
revalidateTagAlias("next");
expirePathAlias("next");
expireTagAlias("next");
}
import { expirePath as expirePathAlias, expireTag as expireTagAlias } from "next/cache";
export async function GET() {
expirePathAlias("next");
expireTagAlias("next");
expirePathAlias("next");
expireTagAlias("next");
}
import { revalidatePath, revalidateTag, expirePath as expirePathAlias, expireTag as expireTagAlias } from "next/cache";
export async function GET() {
revalidatePath("next");
revalidateTag("next");
expirePathAlias("next");
expireTagAlias("next");
}
import { expirePath as expirePathAlias, expireTag as expireTagAlias } from "next/cache";
export async function GET() {
expirePathAlias("next");
expireTagAlias("next");
expirePathAlias("next");
expireTagAlias("next");
}
import { revalidatePath as revalidatePathAlias, revalidateTag as revalidateTagAlias, expirePath, expireTag } from "next/cache";
export async function GET() {
revalidatePathAlias("next");
revalidateTagAlias("next");
expirePath("next");
expireTag("next");
}
import { expirePath, expireTag } from "next/cache";
export async function GET() {
expirePath("next");
expireTag("next");
expirePath("next");
expireTag("next");
}
import { revalidatePath, revalidateTag, expirePath, expireTag } from "next/cache";
export async function GET() {
revalidatePath("next");
revalidateTag("next");
expirePath("next");
expireTag("next");
}
import { expirePath, expireTag } from "next/cache";
export async function GET() {
expirePath("next");
expireTag("next");
expirePath("next");
expireTag("next");
}
import { unstable_cacheTag as cacheTag } from "next/cache";
export async function GET() {
cacheTag("next");
}
import { unstable_cacheTag as cacheTag } from "next/cache";
export async function GET() {
cacheTag("next");
}
import { revalidatePath as revalidatePathAlias } from "next/cache";
export async function GET() {
revalidatePathAlias("next");
}
import { expirePath as revalidatePathAlias } from "next/cache";
export async function GET() {
revalidatePathAlias("next");
}
import { revalidatePath } from "next/cache";
export async function GET() {
revalidatePath("next");
}
\ No newline at end of file
import { expirePath } from "next/cache";
export async function GET() {
expirePath("next");
}
import { revalidateTag as revalidateTagAlias } from "next/cache";
export async function GET() {
revalidateTagAlias("next");
}
import { expireTag as revalidateTagAlias } from "next/cache";
export async function GET() {
revalidateTagAlias("next");
}
import { revalidateTag } from "next/cache";
export async function GET() {
revalidateTag("next");
}
\ No newline at end of file
import { expireTag } from "next/cache";
export async function GET() {
expireTag("next");
}
/* global jest */
jest.autoMockOff()
const defineTest = require('jscodeshift/dist/testUtils').defineTest
const { readdirSync } = require('fs')
const { join } = require('path')
const fixtureDir = 'revalidate-to-expire'
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir)
const fixtures = readdirSync(fixtureDirPath)
.filter(file => file.endsWith('.input.ts'))
.map(file => file.replace('.input.ts', ''))
for (const fixture of fixtures) {
const prefix = `${fixtureDir}/${fixture}`;
defineTest(__dirname, fixtureDir, null, prefix, { parser: 'ts' });
}
import type { API, FileInfo } from 'jscodeshift'
import { createParserFromPath } from '../lib/parser'
export default function transformer(file: FileInfo, _api: API) {
const j = createParserFromPath(file.path)
const root = j(file.source)
if (!root.length) {
return
}
const nextCacheImports = root.find(j.ImportDeclaration, {
source: {
value: 'next/cache',
},
})
if (!nextCacheImports.length) {
return
}
let nextCacheNamespace = ''
let revalidatePathName = 'revalidatePath'
let revalidateTagName = 'revalidateTag'
let expirePathName = 'expirePath'
let expireTagName = 'expireTag'
nextCacheImports.forEach((path) => {
let hasExpirePath = false
let hasExpireTag = false
// Set the alias name for callee if exists.
path.node.specifiers.forEach((specifier) => {
if (specifier.type === 'ImportSpecifier') {
if (specifier.imported.name === 'expirePath') {
expirePathName = specifier.local?.name ?? 'expirePath'
hasExpirePath = true
}
if (specifier.imported.name === 'expireTag') {
expireTagName = specifier.local?.name ?? 'expireTag'
hasExpireTag = true
}
}
})
// Remove the revalidate functions from the import specifiers if
// expire functions are also imported.
path.node.specifiers = path.node.specifiers.filter((specifier) => {
if (specifier.type !== 'ImportSpecifier') {
return true
}
if (hasExpireTag && specifier.imported.name === 'revalidateTag') {
revalidateTagName = specifier.local?.name ?? 'revalidateTag'
return false
}
if (hasExpirePath && specifier.imported.name === 'revalidatePath') {
revalidatePathName = specifier.local?.name ?? 'revalidatePath'
return false
}
return true
})
path.node.specifiers.forEach((specifier) => {
if (specifier.type === 'ImportSpecifier') {
if (specifier.imported.name === 'revalidateTag') {
specifier.imported.name = 'expireTag'
}
if (specifier.imported.name === 'revalidatePath') {
specifier.imported.name = 'expirePath'
}
}
// import * as <namespace> from 'next/cache'
if (specifier.type === 'ImportNamespaceSpecifier') {
nextCacheNamespace = specifier.local.name
}
})
})
root.find(j.CallExpression).forEach((path) => {
const callee = path.node.callee
// Handle direct function calls:
// ```ts
// revalidateTag('next')
// revalidatePath('/next')
// ```
if (
callee.type === 'Identifier' &&
(callee.name === revalidateTagName || callee.name === revalidatePathName)
) {
callee.name =
callee.name === revalidateTagName ? expireTagName : expirePathName
}
// Handle namespace calls:
// ```ts
// import * as cache from 'next/cache'
// ```
if (
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === nextCacheNamespace
) {
// cache.revalidateTag('next')
// cache.revalidatePath('/next')
if (
callee.property.type === 'Identifier' &&
(callee.property.name === 'revalidateTag' ||
callee.property.name === 'revalidatePath')
) {
callee.property.name =
callee.property.name === 'revalidateTag' ? 'expireTag' : 'expirePath'
}
// cache['revalidateTag']('next')
// cache['revalidatePath']('/next')
if (
callee.property.type === 'StringLiteral' &&
(callee.property.value === 'revalidateTag' ||
callee.property.value === 'revalidatePath')
) {
callee.property.value =
callee.property.value === 'revalidateTag' ? 'expireTag' : 'expirePath'
}
}
})
return root.toSource()
}
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