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 10833e6 commit f3d4ff4
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
101 changes: 74 additions & 27 deletions packages/eslint-plugin-mdx/src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -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 `[email protected]` 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 f3d4ff4

Please sign in to comment.