-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: only enable
prettier/prettier
rule for `eslint-plugin-prettie…
…r` `<5.1.2` (#497)
- Loading branch information
Showing
3 changed files
with
79 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
||
|
@@ -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() |