Created
September 29, 2025 19:22
-
-
Save jbenner-radham/7fb2b99ef17abeabf27565dd881cb94c to your computer and use it in GitHub Desktop.
Detect ESLint w/Stylistic quote style (untested)
This file contains hidden or 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 multimatch from 'multimatch' | |
| import fs from 'node:fs'; | |
| import path from 'node:path'; | |
| import process from 'node:process'; | |
| import { tsImport } from 'tsx/esm/api'; | |
| const QUOTE_STYLES = ['double', 'single', 'backtick']; | |
| function isJavaScriptFileGlob(globs) { | |
| return Boolean(multimatch('test.js', globs).length); | |
| } | |
| function isTypeScriptFileGlob(globs) { | |
| return Boolean(multimatch('test.ts', globs).length); | |
| } | |
| async function detectEslintQuoteStyle(cwd = process.cwd()) { | |
| const configFileCandidates = [ | |
| 'eslint.config.js', | |
| 'eslint.config.mjs', | |
| 'eslint.config.cjs', | |
| 'eslint.config.ts', | |
| 'eslint.config.mts', | |
| 'eslint.config.cts' | |
| ]; | |
| const configFile = configFileCandidates.find(candidate => fs.existsSync(path.join(cwd, candidate))); | |
| if (!configFile) { | |
| return null; | |
| } | |
| const configPath = path.join(cwd, configFile); | |
| const tsFileExtensions = ['.ts', '.mts', '.cts']; | |
| const isTsConfigFile = tsFileExtensions.some(extension => configFile.endsWith(extension)); | |
| const config = isTsConfigFile | |
| ? await tsImport(configPath, import.meta.url) | |
| : await import(configPath); | |
| const relevantConfigSections = config.filter(section => | |
| Boolean( | |
| section?.files?.length && | |
| ( | |
| isTsConfigFile | |
| ? isTypeScriptFileGlob(section.files) | |
| : isJavaScriptFileGlob(section.files) | |
| ) && | |
| section.rules?.['@stylistic/quotes'] | |
| ) | |
| ); | |
| if (relevantConfigSections) { | |
| const quotes = relevantConfigSections.at(-1).rules['@stylistic/quotes']; | |
| const eslintErrorAndWarnStates = ['warn', 1, 'error', 2]; | |
| if (!Array.isArray(quotes) && eslintErrorAndWarnStates.includes(quotes)) { | |
| return 'double'; | |
| } else if (Array.isArray(quotes) && quotes.length >= 2) { | |
| const quoteStyle = quotes.at(1); | |
| return QUOTE_STYLES.includes(quoteStyle) | |
| ? quoteStyle | |
| : null; | |
| } | |
| } | |
| return null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment