|
| 1 | +// Helpers for the parts of checkPlugin.ts that decide a plugin's ESLint config |
| 2 | +// and pnpm pin. They only touch the files they are asked about, so the backend |
| 3 | +// tests can run them against a scratch directory without installing anything. |
| 4 | + |
| 5 | +import fs from 'node:fs'; |
| 6 | +import path from 'node:path'; |
| 7 | +import semver from 'semver'; |
| 8 | + |
| 9 | +// File names ESLint 9 reads as a flat config. |
| 10 | +export const FLAT_CONFIG_NAMES = [ |
| 11 | + 'eslint.config.js', 'eslint.config.mjs', 'eslint.config.cjs', |
| 12 | + 'eslint.config.ts', 'eslint.config.mts', 'eslint.config.cts', |
| 13 | +]; |
| 14 | + |
| 15 | +// Legacy (eslintrc) config file names. ESLint 9 ignores all of them. |
| 16 | +export const LEGACY_CONFIG_NAMES = [ |
| 17 | + '.eslintrc', '.eslintrc.js', '.eslintrc.cjs', '.eslintrc.json', '.eslintrc.yml', '.eslintrc.yaml', |
| 18 | +]; |
| 19 | + |
| 20 | +// The .eslintrc.cjs that checkPlugin used to write into every plugin. A plugin |
| 21 | +// whose legacy config is exactly this can be moved to the flat config with no |
| 22 | +// loss; anything else was customised and has to be migrated by hand. |
| 23 | +export const STOCK_LEGACY_CONFIG = `'use strict'; |
| 24 | +
|
| 25 | +// This is a workaround for https://github.com/eslint/eslint/issues/3458 |
| 26 | +require('eslint-config-etherpad/patch/modern-module-resolution'); |
| 27 | +
|
| 28 | +module.exports = { |
| 29 | + root: true, |
| 30 | + extends: 'etherpad/plugin', |
| 31 | +}; |
| 32 | +`; |
| 33 | + |
| 34 | +// devDependencies a plugin needs for the flat config in lib/eslint.config.mjs. |
| 35 | +export const FLAT_CONFIG_DEV_DEPS = { |
| 36 | + 'eslint': '^9.39.5', |
| 37 | + 'eslint-config-etherpad': '^5.0.4', |
| 38 | +}; |
| 39 | + |
| 40 | +type Logger = {info: (m: string) => void, warn: (m: string) => void}; |
| 41 | + |
| 42 | +export type EslintConfigResult = { |
| 43 | + // Files that were (or, without autofix, would be) removed. |
| 44 | + removed: string[], |
| 45 | + // True when eslint.config.mjs was (or would be) written from the template. |
| 46 | + wroteFlatConfig: boolean, |
| 47 | + // True when the plugin lints with a flat config afterwards, so it needs at |
| 48 | + // least FLAT_CONFIG_DEV_DEPS. False while a customised legacy config waits |
| 49 | + // to be migrated by hand: moving that plugin to ESLint 9 would stop it |
| 50 | + // reading its own config. |
| 51 | + usesFlatConfig: boolean, |
| 52 | +}; |
| 53 | + |
| 54 | +const normalize = (s: string) => s.replace(/\r\n/g, '\n').trim(); |
| 55 | + |
| 56 | +// Make sure the plugin uses an ESLint flat config: |
| 57 | +// - a plugin that already has one keeps it, and a leftover stock legacy config |
| 58 | +// is removed because ESLint 9 ignores it; |
| 59 | +// - a plugin with only the stock legacy config, or none at all, gets |
| 60 | +// lib/eslint.config.mjs; |
| 61 | +// - a customised legacy config is never removed, only warned about. |
| 62 | +// The caller raises the ESLint devDependencies when usesFlatConfig is true. |
| 63 | +export const syncEslintConfig = ( |
| 64 | + pluginPath: string, flatTemplate: string, autoFix: boolean, logger: Logger, |
| 65 | +): EslintConfigResult => { |
| 66 | + const exists = (f: string) => fs.existsSync(path.join(pluginPath, f)); |
| 67 | + const flat = FLAT_CONFIG_NAMES.filter(exists); |
| 68 | + const legacy = LEGACY_CONFIG_NAMES.filter(exists); |
| 69 | + const custom = legacy.filter((f) => normalize( |
| 70 | + fs.readFileSync(path.join(pluginPath, f), 'utf8')) !== normalize(STOCK_LEGACY_CONFIG)); |
| 71 | + const stock = legacy.filter((f) => !custom.includes(f)); |
| 72 | + const result: EslintConfigResult = {removed: [], wroteFlatConfig: false, usesFlatConfig: false}; |
| 73 | + |
| 74 | + const remove = (f: string, why: string) => { |
| 75 | + logger.warn(`${f} ${why}; ${autoFix ? 'removing it' : 'please remove it'}`); |
| 76 | + if (autoFix) fs.rmSync(path.join(pluginPath, f)); |
| 77 | + result.removed.push(f); |
| 78 | + }; |
| 79 | + |
| 80 | + if (flat.length > 0) { |
| 81 | + for (const f of stock) remove(f, `is the stock legacy ESLint config and ${flat[0]} exists`); |
| 82 | + if (custom.length > 0) { |
| 83 | + logger.warn(`${custom.join(', ')} is a customised legacy ESLint config that ESLint 9 ` + |
| 84 | + `ignores; move any rules it still needs into ${flat[0]} and delete it by hand`); |
| 85 | + } |
| 86 | + result.usesFlatConfig = true; |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + if (custom.length > 0) { |
| 91 | + logger.warn(`${custom.join(', ')} is a customised legacy ESLint config; ` + |
| 92 | + 'move it to eslint.config.mjs by hand (ESLint 9 ignores it)'); |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + for (const f of stock) remove(f, 'is the stock legacy ESLint config'); |
| 97 | + logger.warn(`eslint.config.mjs not found; ${autoFix ? 'adding' : 'please add'} the template copy`); |
| 98 | + if (autoFix) fs.writeFileSync(path.join(pluginPath, 'eslint.config.mjs'), flatTemplate); |
| 99 | + result.wroteFlatConfig = true; |
| 100 | + result.usesFlatConfig = true; |
| 101 | + return result; |
| 102 | +}; |
| 103 | + |
| 104 | +// The devDependencies a flat-config plugin is missing or has below |
| 105 | +// FLAT_CONFIG_DEV_DEPS. Ranges that are not valid semver are left alone. |
| 106 | +export const flatConfigDevDepBumps = (devDeps: {[pkg: string]: string} = {}) => { |
| 107 | + const want: {[pkg: string]: string} = {}; |
| 108 | + for (const [pkg, ver] of Object.entries(FLAT_CONFIG_DEV_DEPS)) { |
| 109 | + const got = devDeps[pkg]; |
| 110 | + if (got == null) { |
| 111 | + want[pkg] = ver; |
| 112 | + } else if (semver.validRange(got) != null && |
| 113 | + semver.lt(semver.minVersion(got)!, semver.minVersion(ver)!)) { |
| 114 | + want[pkg] = ver; |
| 115 | + } |
| 116 | + } |
| 117 | + return want; |
| 118 | +}; |
| 119 | + |
| 120 | +// Plugin CI takes its pnpm version from core's package.json, so a plugin's own |
| 121 | +// pnpm pin only makes the plugin run an older (or different) pnpm than core. |
| 122 | +// Returns true if the pin was found (and, with autoFix, removed from pkg). |
| 123 | +export const dropPnpmPin = (pkg: any, autoFix: boolean, logger: Logger): boolean => { |
| 124 | + let found = false; |
| 125 | + if (typeof pkg.packageManager === 'string' && pkg.packageManager.startsWith('pnpm@')) { |
| 126 | + logger.warn(`package.json pins ${pkg.packageManager} via packageManager; plugin CI uses ` + |
| 127 | + `core's pnpm, so ${autoFix ? 'removing it' : 'please remove it'}`); |
| 128 | + if (autoFix) delete pkg.packageManager; |
| 129 | + found = true; |
| 130 | + } |
| 131 | + if (pkg.devEngines?.packageManager?.name === 'pnpm') { |
| 132 | + // devEngines can carry other settings, so only point it out. |
| 133 | + logger.warn('package.json pins pnpm via devEngines.packageManager; plugin CI uses ' + |
| 134 | + "core's pnpm, please remove it by hand"); |
| 135 | + } |
| 136 | + return found; |
| 137 | +}; |
0 commit comments