Skip to content

Commit

Permalink
feat: only enable prettier/prettier rule for `eslint-plugin-prettie…
Browse files Browse the repository at this point in the history
…r` `<5.1.2` (#497)
  • Loading branch information
JounQin committed Dec 24, 2023
1 parent 8707ca6 commit 0f39ad0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .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`
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -34,7 +34,6 @@ jobs:
run: |
yarn lint
yarn test
continue-on-error: ${{ matrix.os == 'windows-latest' }}
env:
EFF_NO_LINK_RULES: true
PARSER_NO_WATCH: true
Expand Down
101 changes: 74 additions & 27 deletions 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'

Expand All @@ -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()

0 comments on commit 0f39ad0

Please sign in to comment.