Skip to content

Commit 0f39ad0

Browse files
authored
feat: only enable prettier/prettier rule for eslint-plugin-prettier <5.1.2 (#497)
1 parent 8707ca6 commit 0f39ad0

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

.changeset/fifty-windows-sin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-mdx": minor
3+
---
4+
5+
feat: only enable `prettier/prettier` rule for `eslint-plugin-prettier` `<5.1.2`

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ jobs:
3434
run: |
3535
yarn lint
3636
yarn test
37-
continue-on-error: ${{ matrix.os == 'windows-latest' }}
3837
env:
3938
EFF_NO_LINK_RULES: true
4039
PARSER_NO_WATCH: true
Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { Linter } from 'eslint'
1+
import { createRequire } from 'node:module'
2+
import path from 'node:path'
3+
4+
import type { ESLint, Linter } from 'eslint'
25

36
import { base } from './base'
47

@@ -18,31 +21,75 @@ export const recommended: Linter.Config = {
1821
overrides,
1922
}
2023

21-
try {
22-
require.resolve('prettier')
23-
require.resolve('eslint-plugin-prettier')
24-
overrides.push(
25-
{
26-
files: '*.md',
27-
rules: {
28-
'prettier/prettier': [
29-
'error',
30-
{
31-
parser: 'markdown',
32-
},
33-
],
24+
/* istanbul ignore next */
25+
// hack to bypass syntax error for commonjs
26+
const getImportMetaUrl = () => {
27+
try {
28+
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
29+
return new Function('return import.meta.url')() as string
30+
} catch {
31+
// if `--experimental-vm-modules` is not enabled, the following error would be thrown:
32+
// `SyntaxError: Cannot use 'import.meta' outside a module`,
33+
// then we fallback to `process.cwd()` resolution which could fail actually,
34+
// but we're only trying to resolve `prettier` and `eslint-plugin-prettier` dependencies,
35+
// it would be fine enough
36+
return path.resolve(process.cwd(), '__test__.js')
37+
}
38+
}
39+
40+
/* istanbul ignore next */
41+
const cjsRequire =
42+
typeof require === 'undefined' ? createRequire(getImportMetaUrl()) : require
43+
44+
const addPrettierRules = () => {
45+
try {
46+
cjsRequire.resolve('prettier')
47+
48+
const { meta } = cjsRequire('eslint-plugin-prettier') as ESLint.Plugin
49+
50+
/* istanbul ignore next */
51+
const version = meta?.version || ''
52+
53+
/**
54+
* @see https://github.com/prettier/eslint-plugin-prettier/releases/tag/v5.1.2
55+
*/
56+
const [major, minor, patch] = version.split('.')
57+
58+
/* istanbul ignore if -- We're using `[email protected]` for now */
59+
if (
60+
/* istanbul ignore next */
61+
+major > 5 ||
62+
(+major === 5 &&
63+
(+minor > 1 || (+minor === 1 && Number.parseInt(patch) >= 2)))
64+
) {
65+
return
66+
}
67+
68+
overrides.push(
69+
{
70+
files: '*.md',
71+
rules: {
72+
'prettier/prettier': [
73+
'error',
74+
{
75+
parser: 'markdown',
76+
},
77+
],
78+
},
3479
},
35-
},
36-
{
37-
files: '*.mdx',
38-
rules: {
39-
'prettier/prettier': [
40-
'error',
41-
{
42-
parser: 'mdx',
43-
},
44-
],
80+
{
81+
files: '*.mdx',
82+
rules: {
83+
'prettier/prettier': [
84+
'error',
85+
{
86+
parser: 'mdx',
87+
},
88+
],
89+
},
4590
},
46-
},
47-
)
48-
} catch {}
91+
)
92+
} catch {}
93+
}
94+
95+
addPrettierRules()

0 commit comments

Comments
 (0)