Unverified Commit 30c08f56 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Skip ESLint migration when scripts do not use next lint (#99103)

The ESLint migration currently changes lint configuration and
dependencies even when no package script uses `next lint`. Projects
using another linter or ESLint directly should keep their existing
setup.

Also, pin to v9 at max because plugins used by `eslint-config-next`
depended on v9
parent a48eee28
......@@ -386,8 +386,14 @@ export async function runUpgrade(
: JSON.parse(eslintConfigNextPeerDepsJSON)
const eslintRange = eslintConfigNextPeerDeps?.eslint
if (eslintRange) {
// TODO: Target ESLint 10 once eslint-config-next's plugins, especially
// eslint-plugin-react, support its API removals (e.g. context.getFilename).
const cappedRange = eslintRange
.split('||')
.map((range) => `${range.trim()} <10`)
.join(' || ')
const targetEslintVersion = await loadHighestNPMVersionMatching(
`eslint@${eslintRange}`
`eslint@${cappedRange}`
)
versionMapping['eslint'] = {
version: targetEslintVersion,
......
......@@ -35,6 +35,39 @@ describe('next-lint-to-eslint-cli', () => {
}
})
it.each([
['missing scripts', undefined],
['empty scripts', {}],
['ESLint', { lint: 'eslint .' }],
['Biome', { lint: 'biome check .' }],
])('skips projects with %s', (_name, scripts) => {
const testDir = fs.mkdtempSync(path.join(isolatedDir, 'no-next-lint-'))
const packageJsonPath = path.join(testDir, 'package.json')
const packageJsonContent = JSON.stringify({
scripts,
devDependencies: { eslint: '^8.0.0', '@eslint/eslintrc': '^2.0.0' },
})
fs.writeFileSync(packageJsonPath, packageJsonContent)
transformer([testDir])
expect(fs.readdirSync(testDir)).toEqual(['package.json'])
expect(fs.readFileSync(packageJsonPath, 'utf8')).toBe(packageJsonContent)
const configPath = path.join(testDir, '.eslintrc.json')
const configContent = '{"extends":"next/core-web-vitals"}'
fs.writeFileSync(configPath, configContent)
transformer([testDir])
expect(fs.readdirSync(testDir).sort()).toEqual([
'.eslintrc.json',
'package.json',
])
expect(fs.readFileSync(configPath, 'utf8')).toBe(configContent)
expect(fs.readFileSync(packageJsonPath, 'utf8')).toBe(packageJsonContent)
})
describe('flat-config', () => {
it('should keep config unchanged and transform package.json', () => {
const testDir = path.join(fixturesDir, 'flat-config')
......
......@@ -1066,6 +1066,18 @@ export default function transformer(
return
}
const packageJsonContent = readFileSync(packageJsonPath, 'utf8')
const packageJson = JSON.parse(packageJsonContent)
const usesNextLint = Object.values(packageJson.scripts || {}).some(
(script) => typeof script === 'string' && script.includes('next lint')
)
if (!usesNextLint) {
console.log(
'Skipping migration: no next lint script found in package.json.'
)
return
}
const isTypeScript = detectTypeScript(projectRoot)
console.log('Migrating from next lint to the ESLint CLI...')
......@@ -1144,7 +1156,6 @@ export default function transformer(
}
}
const packageJsonContent = readFileSync(packageJsonPath, 'utf8')
const result = updatePackageJsonScripts(packageJsonContent)
if (result.updated) {
......
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