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'
2
5
3
6
import { base } from './base'
4
7
@@ -18,31 +21,75 @@ export const recommended: Linter.Config = {
18
21
overrides,
19
22
}
20
23
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
+ } ,
34
79
} ,
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
+ } ,
45
90
} ,
46
- } ,
47
- )
48
- } catch { }
91
+ )
92
+ } catch { }
93
+ }
94
+
95
+ addPrettierRules ( )
0 commit comments