Unverified Commit 9b16af81 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Fix `instant-false` codemod to run on route segment filenames (#98880)

### Why?

The codemod incorrectly adds `instant = false` to helper files because
its matcher matches `layout.ts` within `custom-layout.ts` and `page.tsx`
within `homepage.tsx`.

### How?

Require filenames to start with `page.`, `layout.`, or `default.`, while
preserving compound extensions. The codemod does not consult
`pageExtensions`, so files such as `page.test.tsx` remain eligible.

<!-- NEXT_JS_LLM -->
parent 0f3a2bef
/* global jest */
jest.autoMockOff()
const defineTest = require('jscodeshift/dist/testUtils').defineTest
const { readdirSync } = require('fs')
const { readdirSync, readFileSync } = require('fs')
const { join } = require('path')
const transformer = require('../cache-components-instant-false').default
const fixtureDir = 'cache-components-instant-false'
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir)
......@@ -14,3 +15,53 @@ for (const fixture of fixtures) {
const prefix = `${fixtureDir}/${fixture}`
defineTest(__dirname, fixtureDir, null, prefix, { parser: 'tsx' })
}
describe('file paths', () => {
const source = readFileSync(
join(fixtureDirPath, 'basic-page.input.tsx'),
'utf8'
)
const expected = readFileSync(
join(fixtureDirPath, 'basic-page.output.tsx'),
'utf8'
)
beforeEach(() => {
// Exercise the path filter that fixture tests normally bypass.
jest.replaceProperty(process, 'env', {
...process.env,
NODE_ENV: 'production',
})
})
afterEach(() => {
jest.restoreAllMocks()
})
it.each([
'app/page.tsx',
'app/layout.tsx',
'app/default.tsx',
'app/blog/page.ext.tsx',
// Compound extensions are accepted without consulting pageExtensions.
'app/page.test.tsx',
'/project/src/app/blog/page.jsx',
'C:\\project\\src\\app\\blog\\page.tsx',
])('transforms route segment %s', (path) => {
expect(
transformer({ path, source }, {}).replace(/\r\n/g, '\n').trim()
).toBe(expected.replace(/\r\n/g, '\n').trim())
})
it.each([
'app/_lib/custom-layout.ts',
'app/components/homepage.tsx',
'app/components/custom-default.tsx',
'app/blog/route.ts',
'pages/page.tsx',
'my-app/page.tsx',
'C:\\project\\app\\_lib\\custom-layout.ts',
])('leaves non-segment file %s unchanged', (path) => {
expect(transformer({ path, source }, {})).toBe(source)
})
})
......@@ -20,7 +20,9 @@ import { NEXT_CODEMOD_IGNORE_ERROR_PREFIX } from '../lib/utils'
export default function transformer(file: FileInfo, _api: API) {
if (
process.env.NODE_ENV !== 'test' &&
!/(^|[/\\])app[/\\].*?(page|layout|default)\.[^/\\]+$/.test(file.path)
!/(^|[/\\])app[/\\](?:.*[/\\])?(page|layout|default)\.[^/\\]+$/.test(
file.path
)
) {
return file.source
}
......
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