diff --git a/.changeset/fifty-windows-sin.md b/.changeset/fifty-windows-sin.md new file mode 100644 index 00000000..e56bdcb1 --- /dev/null +++ b/.changeset/fifty-windows-sin.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-mdx": minor +--- + +feat: only enable `prettier/prettier` rule for `eslint-plugin-prettier` `<5.1.2` diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bddafb56..3e87f024 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,7 +35,6 @@ jobs: run: | yarn lint yarn test - continue-on-error: ${{ matrix.os == 'windows-latest' }} env: EFF_NO_LINK_RULES: true PARSER_NO_WATCH: true diff --git a/packages/eslint-plugin-mdx/src/configs/recommended.ts b/packages/eslint-plugin-mdx/src/configs/recommended.ts index 9c8eb24c..9242fcc0 100644 --- a/packages/eslint-plugin-mdx/src/configs/recommended.ts +++ b/packages/eslint-plugin-mdx/src/configs/recommended.ts @@ -1,4 +1,7 @@ -import type { Linter } from 'eslint' +import { createRequire } from 'node:module' +import path from 'node:path' + +import type { ESLint, Linter } from 'eslint' import { base } from './base' @@ -18,31 +21,75 @@ export const recommended: Linter.Config = { overrides, } -try { - require.resolve('prettier') - require.resolve('eslint-plugin-prettier') - overrides.push( - { - files: '*.md', - rules: { - 'prettier/prettier': [ - 'error', - { - parser: 'markdown', - }, - ], +/* istanbul ignore next */ +// hack to bypass syntax error for commonjs +const getImportMetaUrl = () => { + try { + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func + return new Function('return import.meta.url')() as string + } catch { + // if `--experimental-vm-modules` is not enabled, the following error would be thrown: + // `SyntaxError: Cannot use 'import.meta' outside a module`, + // then we fallback to `process.cwd()` resolution which could fail actually, + // but we're only trying to resolve `prettier` and `eslint-plugin-prettier` dependencies, + // it would be fine enough + return path.resolve(process.cwd(), '__test__.js') + } +} + +/* istanbul ignore next */ +const cjsRequire = + typeof require === 'undefined' ? createRequire(getImportMetaUrl()) : require + +const addPrettierRules = () => { + try { + cjsRequire.resolve('prettier') + + const { meta } = cjsRequire('eslint-plugin-prettier') as ESLint.Plugin + + /* istanbul ignore next */ + const version = meta?.version || '' + + /** + * @see https://github.com/prettier/eslint-plugin-prettier/releases/tag/v5.1.2 + */ + const [major, minor, patch] = version.split('.') + + /* istanbul ignore if -- We're using `eslint-plugin-prettier@4.2.1` for now */ + if ( + /* istanbul ignore next */ + +major > 5 || + (+major === 5 && + (+minor > 1 || (+minor === 1 && Number.parseInt(patch) >= 2))) + ) { + return + } + + overrides.push( + { + files: '*.md', + rules: { + 'prettier/prettier': [ + 'error', + { + parser: 'markdown', + }, + ], + }, }, - }, - { - files: '*.mdx', - rules: { - 'prettier/prettier': [ - 'error', - { - parser: 'mdx', - }, - ], + { + files: '*.mdx', + rules: { + 'prettier/prettier': [ + 'error', + { + parser: 'mdx', + }, + ], + }, }, - }, - ) -} catch {} + ) + } catch {} +} + +addPrettierRules()