Unverified Commit 87661329 authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

Fix instant codemod matching colocated test files (#99795)

## Summary

- prevent `cache-components-instant-false` from adding route config to
colocated test, spec, and story files
- keep compound page extensions eligible while filtering common
non-route suffixes
- cover POSIX and Windows paths in the regression tests

## Verification

- `pnpm --filter @next/codemod build`
- `pnpm --filter @next/codemod test --runInBand
cache-components-instant-false.test.js`

<!-- NEXT_JS_LLM -->
parent deb98d7e
...@@ -43,8 +43,6 @@ describe('file paths', () => { ...@@ -43,8 +43,6 @@ describe('file paths', () => {
'app/layout.tsx', 'app/layout.tsx',
'app/default.tsx', 'app/default.tsx',
'app/blog/page.ext.tsx', 'app/blog/page.ext.tsx',
// Compound extensions are accepted without consulting pageExtensions.
'app/page.test.tsx',
'/project/src/app/blog/page.jsx', '/project/src/app/blog/page.jsx',
'C:\\project\\src\\app\\blog\\page.tsx', 'C:\\project\\src\\app\\blog\\page.tsx',
])('transforms route segment %s', (path) => { ])('transforms route segment %s', (path) => {
...@@ -58,9 +56,17 @@ describe('file paths', () => { ...@@ -58,9 +56,17 @@ describe('file paths', () => {
'app/components/homepage.tsx', 'app/components/homepage.tsx',
'app/components/custom-default.tsx', 'app/components/custom-default.tsx',
'app/blog/route.ts', 'app/blog/route.ts',
'app/blog/page.test.tsx',
'app/blog/layout.test.tsx',
'app/blog/page.spec.tsx',
'app/blog/layout.spec.tsx',
'app/blog/page.story.tsx',
'app/blog/page.stories.tsx',
'app/blog/default.stories.tsx',
'pages/page.tsx', 'pages/page.tsx',
'my-app/page.tsx', 'my-app/page.tsx',
'C:\\project\\app\\_lib\\custom-layout.ts', 'C:\\project\\app\\_lib\\custom-layout.ts',
'C:\\project\\app\\blog\\page.test.tsx',
])('leaves non-segment file %s unchanged', (path) => { ])('leaves non-segment file %s unchanged', (path) => {
expect(transformer({ path, source }, {})).toBe(source) expect(transformer({ path, source }, {})).toBe(source)
}) })
......
...@@ -18,11 +18,17 @@ import { NEXT_CODEMOD_IGNORE_ERROR_PREFIX } from '../lib/utils' ...@@ -18,11 +18,17 @@ import { NEXT_CODEMOD_IGNORE_ERROR_PREFIX } from '../lib/utils'
* a server segment that accepts route segment config like the other two. * a server segment that accepts route segment config like the other two.
*/ */
export default function transformer(file: FileInfo, _api: API) { export default function transformer(file: FileInfo, _api: API) {
if ( const isRouteSegment =
process.env.NODE_ENV !== 'test' && /(^|[/\\])app[/\\](?:.*[/\\])?(page|layout|default)\.[^/\\]+$/.test(
!/(^|[/\\])app[/\\](?:.*[/\\])?(page|layout|default)\.[^/\\]+$/.test(
file.path file.path
) )
const isColocatedTestOrStory = /\.(?:test|spec|story|stories)\.[^/\\]+$/.test(
file.path
)
if (
process.env.NODE_ENV !== 'test' &&
(!isRouteSegment || isColocatedTestOrStory)
) { ) {
return file.source 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