Last active
January 16, 2025 08:57
-
-
Save xfournet/ed9f2f02e411b86188bad4aeb9b1bddb to your computer and use it in GitHub Desktop.
airbnb compat with ESLint 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fixupConfigRules } from '@eslint/compat'; | |
import { FlatCompat } from '@eslint/eslintrc'; | |
import stylistic from '@stylistic/eslint-plugin'; | |
import path from 'path'; | |
import { fileURLToPath } from 'url'; | |
const filename = fileURLToPath(import.meta.url); | |
const dirname = path.dirname(filename); | |
const compat = new FlatCompat({ | |
baseDirectory: dirname, | |
}); | |
const legacyRules = { | |
'@typescript-eslint/no-throw-literal': '@typescript-eslint/only-throw-error', | |
...Object.fromEntries([ | |
'brace-style', | |
'comma-dangle', | |
'comma-spacing', | |
'func-call-spacing', | |
'indent', | |
'keyword-spacing', | |
'lines-between-class-members', | |
'no-extra-semi', | |
'space-before-blocks', | |
'quotes', | |
'semi', | |
'space-before-function-paren', | |
'space-infix-ops', | |
'object-curly-spacing', | |
].map((name) => [`@typescript-eslint/${name}`, `stylistic/${name}`])), | |
}; | |
const fixupLegacyRules = (configs) => { | |
configs.forEach((config) => { | |
if (config.plugins) { | |
// eslint-disable-next-line no-param-reassign | |
config.plugins = {}; | |
} | |
const configRules = config.rules; | |
if (configRules) { | |
Object.entries(legacyRules).forEach(([legacyRule, newRule]) => { | |
const ruleConfig = configRules[legacyRule]; | |
if (ruleConfig) { | |
delete configRules[legacyRule]; | |
if (!configRules[newRule]) { | |
configRules[newRule] = ruleConfig; | |
} | |
} | |
}); | |
} | |
}); | |
return configs; | |
}; | |
export const config = [ | |
{ | |
plugins: { | |
stylistic, | |
}, | |
}, | |
...fixupLegacyRules(fixupConfigRules(compat.extends( | |
'airbnb', | |
'airbnb/hooks', | |
'airbnb-typescript' | |
))), | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment