Commit e1ce4c2d authored by Ben Gubler's avatar Ben Gubler

fix: legacy .eslintrc processing, monorepo support

parent e5311667
...@@ -83,6 +83,7 @@ export function installPackages( ...@@ -83,6 +83,7 @@ export function installPackages(
packageManager?: PackageManager packageManager?: PackageManager
silent?: boolean silent?: boolean
dev?: boolean dev?: boolean
cwd?: string
} = {} } = {}
) { ) {
if (packageToInstall.length === 0) return if (packageToInstall.length === 0) return
...@@ -91,6 +92,7 @@ export function installPackages( ...@@ -91,6 +92,7 @@ export function installPackages(
packageManager = getPkgManager(process.cwd()), packageManager = getPkgManager(process.cwd()),
silent = false, silent = false,
dev = false, dev = false,
cwd = process.cwd(),
} = options } = options
if (!packageManager) throw new Error('Failed to find package manager') if (!packageManager) throw new Error('Failed to find package manager')
...@@ -107,6 +109,7 @@ export function installPackages( ...@@ -107,6 +109,7 @@ export function installPackages(
// Keeping stderr since it'll likely be relevant later when it fails. // Keeping stderr since it'll likely be relevant later when it fails.
stdio: silent ? ['ignore', 'ignore', 'inherit'] : 'inherit', stdio: silent ? ['ignore', 'ignore', 'inherit'] : 'inherit',
shell: true, shell: true,
cwd,
}) })
} catch (error) { } catch (error) {
throw new Error( throw new Error(
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"next": "15.0.0" "next": "15.0.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^8.0.0", "eslint": "^9",
"eslint-config-next": "15.0.0", "eslint-config-next": "15.0.0",
"@eslint/eslintrc": "^3" "@eslint/eslintrc": "^3"
} }
......
...@@ -58,7 +58,7 @@ describe('next-lint-to-eslint-cli', () => { ...@@ -58,7 +58,7 @@ describe('next-lint-to-eslint-cli', () => {
consoleErrorSpy.mockRestore() consoleErrorSpy.mockRestore()
}) })
test('transforms correctly using existing-eslint data', () => { test('migrates legacy .eslintrc.json to flat config', () => {
// Read input fixture // Read input fixture
const inputPath = path.join(__dirname, '../__testfixtures__/next-lint-to-eslint-cli/existing-eslint.input.json') const inputPath = path.join(__dirname, '../__testfixtures__/next-lint-to-eslint-cli/existing-eslint.input.json')
const expectedOutputPath = path.join(__dirname, '../__testfixtures__/next-lint-to-eslint-cli/existing-eslint.output.json') const expectedOutputPath = path.join(__dirname, '../__testfixtures__/next-lint-to-eslint-cli/existing-eslint.output.json')
...@@ -83,12 +83,49 @@ describe('next-lint-to-eslint-cli', () => { ...@@ -83,12 +83,49 @@ describe('next-lint-to-eslint-cli', () => {
const actualPackageJson = fs.readFileSync(packageJsonPath, 'utf8') const actualPackageJson = fs.readFileSync(packageJsonPath, 'utf8')
expect(JSON.parse(actualPackageJson)).toEqual(JSON.parse(expectedOutput)) expect(JSON.parse(actualPackageJson)).toEqual(JSON.parse(expectedOutput))
// Check that no new eslint.config.mjs was created (existing config should be preserved) // Check that new eslint.config.mjs was created from legacy config
const eslintConfigPath = path.join(tempDir, 'eslint.config.mjs') const eslintConfigPath = path.join(tempDir, 'eslint.config.mjs')
expect(fs.existsSync(eslintConfigPath)).toBe(false) expect(fs.existsSync(eslintConfigPath)).toBe(true)
const migratedConfig = fs.readFileSync(eslintConfigPath, 'utf8')
expect(migratedConfig).toContain('FlatCompat')
expect(migratedConfig).toContain('compat.extends("next/core-web-vitals"')
consoleSpy.mockRestore()
consoleErrorSpy.mockRestore()
})
test('migrates legacy .eslintrc (JSON) to flat config', () => {
const packageJson = {
name: 'app-with-legacy-eslintrc',
scripts: { lint: 'next lint' },
dependencies: { next: '15.0.0' },
}
const legacyEslintrc = {
env: { browser: true, es2021: true },
extends: ['eslint:recommended'],
rules: { semi: ['error', 'always'] },
}
// Check that existing config still exists const packageJsonPath = path.join(tempDir, 'package.json')
expect(fs.existsSync(existingEslintPath)).toBe(true) const legacyPath = path.join(tempDir, '.eslintrc')
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
fs.writeFileSync(legacyPath, JSON.stringify(legacyEslintrc, null, 2))
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
const consoleErrorSpy = jest
.spyOn(console, 'error')
.mockImplementation(() => {})
transformer([tempDir], { skipInstall: true })
const eslintConfigPath = path.join(tempDir, 'eslint.config.mjs')
expect(fs.existsSync(eslintConfigPath)).toBe(true)
const migratedConfig = fs.readFileSync(eslintConfigPath, 'utf8')
expect(migratedConfig).toContain('compat.config(legacy)')
expect(migratedConfig).toContain('next/core-web-vitals')
expect(migratedConfig).toContain('ignores')
consoleSpy.mockRestore() consoleSpy.mockRestore()
consoleErrorSpy.mockRestore() consoleErrorSpy.mockRestore()
...@@ -218,9 +255,9 @@ describe('next-lint-to-eslint-cli', () => { ...@@ -218,9 +255,9 @@ describe('next-lint-to-eslint-cli', () => {
consoleErrorSpy.mockRestore() consoleErrorSpy.mockRestore()
}) })
test('preserves existing eslint dependencies', () => { test('upgrades ESLint to v9 when older version is present', () => {
const packageJson = { const packageJson = {
name: 'app-with-eslint', name: 'app-with-eslint-old',
scripts: { scripts: {
lint: 'next lint' lint: 'next lint'
}, },
...@@ -243,8 +280,9 @@ describe('next-lint-to-eslint-cli', () => { ...@@ -243,8 +280,9 @@ describe('next-lint-to-eslint-cli', () => {
const actualPackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')) const actualPackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
// Should preserve existing eslint versions // Should upgrade ESLint to v9 to support flat config
expect(actualPackageJson.devDependencies.eslint).toBe('^8.57.0') expect(actualPackageJson.devDependencies.eslint).toBe('^9')
// Should preserve existing eslint-config-next
expect(actualPackageJson.devDependencies['eslint-config-next']).toBe('14.2.0') expect(actualPackageJson.devDependencies['eslint-config-next']).toBe('14.2.0')
// Should add missing @eslint/eslintrc // Should add missing @eslint/eslintrc
expect(actualPackageJson.devDependencies['@eslint/eslintrc']).toBe('^3') expect(actualPackageJson.devDependencies['@eslint/eslintrc']).toBe('^3')
...@@ -253,6 +291,47 @@ describe('next-lint-to-eslint-cli', () => { ...@@ -253,6 +291,47 @@ describe('next-lint-to-eslint-cli', () => {
consoleErrorSpy.mockRestore() consoleErrorSpy.mockRestore()
}) })
test('updates existing CommonJS flat config exporting identifier', () => {
const inputPath = path.join(__dirname, '../__testfixtures__/next-lint-to-eslint-cli/existing-flat-config.input.json')
const expectedOutputPath = path.join(__dirname, '../__testfixtures__/next-lint-to-eslint-cli/existing-flat-config.output.json')
const inputContent = fs.readFileSync(inputPath, 'utf8')
const expectedOutput = fs.readFileSync(expectedOutputPath, 'utf8')
const packageJsonPath = path.join(tempDir, 'package.json')
const eslintConfigPath = path.join(tempDir, 'eslint.config.cjs')
fs.writeFileSync(packageJsonPath, inputContent)
const cjsIdentifierConfig = `const eslintConfig = [
{
rules: { quotes: ["error", "single"], semi: ["error", "always"] }
}
]
module.exports = eslintConfig
`
fs.writeFileSync(eslintConfigPath, cjsIdentifierConfig)
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
transformer([tempDir], { skipInstall: true })
// Check package.json was updated
const actualPackageJson = fs.readFileSync(packageJsonPath, 'utf8')
expect(JSON.parse(actualPackageJson)).toEqual(JSON.parse(expectedOutput))
// Check that existing config was updated with Next.js configs and ignores
const updatedConfig = fs.readFileSync(eslintConfigPath, 'utf8')
expect(updatedConfig).toContain('FlatCompat')
expect(updatedConfig).toContain('next/core-web-vitals')
expect(updatedConfig).toContain('ignores')
expect(updatedConfig).toContain('node_modules/**')
expect(updatedConfig).toContain('semi') // Original rule should be preserved
consoleSpy.mockRestore()
consoleErrorSpy.mockRestore()
})
test('handles TypeScript config files', () => { test('handles TypeScript config files', () => {
const packageJson = { const packageJson = {
name: 'app-with-ts-config', name: 'app-with-ts-config',
......
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