Unverified Commit 8cbc9269 authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

codemod: bump eslint alongside eslint-config-next (#95314)

## Why

Today, running \`npx @next/codemod upgrade canary\` against any
default-CNA-15 project fails at \`npm install\` with an \`ERESOLVE\`
peer-dep error:

\`\`\`
Found: eslint@8.57.1
Could not resolve dependency:
peer eslint@">=9.0.0" from eslint-config-next@16.3.0-canary.71
\`\`\`

The codemod bumps \`eslint-config-next\` (Next 15 -> Next 16) but leaves
\`eslint@^8\` alone, so npm can't reconcile peers and the install dies
before any jscodeshift transforms can run. Caught while testing
vercel/next.js#95312 against \`cna-15.0.0-app\`.

## What

Adds \`eslint\` to the \`versionMapping\` in \`upgrade.ts\` when the
project has \`eslint\` installed. The new version is picked by reading
\`eslint-config-next@<target>\`'s \`peerDependencies.eslint\` range and
resolving the highest matching published version (same pattern
\`@types/react\` already uses).

If anything goes wrong fetching the peer range (network blip, missing
peer entry), the bump is skipped and a verbose-only warning is printed —
the user just hits whatever they would have hit without this change.

## How

\`pnpm --filter @next/codemod build\` clean. To test locally: downgrade
a Next 16 project to Next 15 + eslint 8 + react 18, then run \`node
packages/next-codemod/bin/next-codemod.js upgrade canary --yes\` and
confirm the install succeeds.

<!-- NEXT_JS_LLM_PR -->
parent b3118fca
......@@ -362,6 +362,46 @@ export async function runUpgrade(
}
}
// Bump `eslint` alongside `eslint-config-next` so the install doesn't fail
// on a peer-dep mismatch. e.g. `eslint-config-next@16.x` requires
// `eslint@>=9`, but a project upgrading from Next 15 will still have
// `eslint@^8` from create-next-app. Skip silently if anything goes wrong;
// the worst case is the user hits the same peer-dep error they would have
// without this bump.
//
// Only act when the project is actually using `eslint-config-next` — we
// don't want to silently upgrade eslint majors for projects that use
// eslint for unrelated reasons.
if (allDependencies['eslint'] && allDependencies['eslint-config-next']) {
try {
const eslintConfigNextPeerDepsJSON = execSync(
`npm --silent view "eslint-config-next@${targetNextVersion}" peerDependencies --json`,
{ encoding: 'utf-8' }
)
const eslintConfigNextPeerDeps =
eslintConfigNextPeerDepsJSON.trim() === ''
? {}
: JSON.parse(eslintConfigNextPeerDepsJSON)
const eslintRange = eslintConfigNextPeerDeps?.eslint
if (eslintRange) {
const targetEslintVersion = await loadHighestNPMVersionMatching(
`eslint@${eslintRange}`
)
versionMapping['eslint'] = {
version: targetEslintVersion,
required: false,
}
}
} catch (e) {
if (verbose) {
console.warn(
` Could not determine eslint peer range from eslint-config-next@${targetNextVersion}. Leaving eslint version alone.`,
e
)
}
}
}
// Even though we only need those if we alias `@types/react` to types-react,
// we still do it out of safety due to https://github.com/microsoft/DefinitelyTyped-tools/issues/433.
const overrides: Record<string, string> = {}
......
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