Skip to content

Commit d47af33

Browse files
authored
feat: add support for eslint 8 (#353)
1 parent 24cfe78 commit d47af33

File tree

9 files changed

+231
-235
lines changed

9 files changed

+231
-235
lines changed

package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"scripts": {
1313
"build": "run-p build:*",
14-
"build:r": "r -f esm,es2015",
14+
"build:r": "r -f es2015",
1515
"build:ts": "tsc -b",
1616
"clean": "rimraf packages/*/{lib,*.tsbuildinfo} node_modules/@1stg/eslint-config/node_modules",
1717
"lint": "run-p lint:*",
@@ -30,7 +30,7 @@
3030
"@types/eslint-plugin-markdown": "^2.0.0",
3131
"@types/jest": "^27.0.2",
3232
"@types/node": "^16.10.9",
33-
"@types/react": "^17.0.29",
33+
"@types/react": "^17.0.30",
3434
"@types/unist": "^2.0.6",
3535
"lerna": "^4.0.0",
3636
"npm-run-all": "^4.1.5",
@@ -44,6 +44,12 @@
4444
"yarn-deduplicate": "^3.1.0"
4545
},
4646
"resolutions": {
47+
"@typescript-eslint/eslint-plugin": "^5.0.0",
48+
"@typescript-eslint/eslint-plugin-tslint": "^5.0.0",
49+
"@typescript-eslint/experimental-utils": "^5.0.0",
50+
"@typescript-eslint/parser": "^5.0.0",
51+
"eslint": "^8.0.1",
52+
"eslint-plugin-unicorn": "^37.0.0",
4753
"prettier": "^2.4.1"
4854
},
4955
"commitlint": {
@@ -63,6 +69,7 @@
6369
"eslint/lib/linter/linter"
6470
],
6571
"moduleNameMapper": {
72+
"^eslint/use-at-your-own-risk$": "<rootDir>/node_modules/eslint/lib/unsupported-api.js",
6673
"^eslint-mdx$": "<rootDir>/packages/eslint-mdx/src",
6774
"^eslint-plugin-mdx$": "<rootDir>/packages/eslint-plugin-mdx/src"
6875
},

packages/eslint-mdx/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
"engines": {
1414
"node": ">=10.0.0"
1515
},
16-
"main": "lib",
17-
"module": "lib/esm",
18-
"es2015": "lib/es2015",
16+
"main": "lib/index.js",
17+
"module": "lib/index.es2015.mjs",
1918
"types": "lib",
2019
"files": [
2120
"lib",

packages/eslint-plugin-mdx/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
"engines": {
1414
"node": ">=10.0.0"
1515
},
16-
"main": "lib",
17-
"module": "lib/esm",
18-
"es2015": "lib/es2015",
16+
"main": "lib/index.js",
17+
"module": "lib/index.es2015.mjs",
1918
"types": "lib",
2019
"files": [
2120
"lib",

packages/eslint-plugin-mdx/src/helpers.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Rule } from 'eslint'
2+
13
export const getGlobals = <
24
T extends Record<string, unknown> | string[],
35
G extends Record<string, boolean>,
@@ -18,3 +20,27 @@ export const getGlobals = <
1820
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
1921
initialGlobals as R,
2022
)
23+
24+
const PRIVATE_API_VERSION = 8
25+
26+
export const getBuiltinRule = (ruleId: string) => {
27+
// TODO: Remove this when we drop support for ESLint < 8
28+
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
29+
const { version: eslintVersion } = require('eslint/package.json') as {
30+
version: string
31+
}
32+
33+
const majorVersion = Number.parseInt(eslintVersion.split('.')[0], 10)
34+
35+
/* istanbul ignore next */
36+
if (majorVersion < PRIVATE_API_VERSION) {
37+
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
38+
return require(`eslint/lib/rules/${ruleId}`) as Rule.RuleModule
39+
}
40+
41+
// prettier-ignore
42+
return (
43+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports, @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
44+
require('eslint/use-at-your-own-risk') as typeof import('eslint/use-at-your-own-risk')
45+
).builtinRules.get(ruleId)
46+
}

packages/eslint-plugin-mdx/src/rules/no-unused-expressions.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import type { Rule } from 'eslint'
44
import { isJsxNode } from 'eslint-mdx'
5-
import esLintNoUnusedExpressions from 'eslint/lib/rules/no-unused-expressions'
5+
6+
import { getBuiltinRule } from '../helpers'
67

78
import type { ExpressionStatementWithParent } from './types'
89

10+
const esLintNoUnusedExpressions = getBuiltinRule('no-unused-expressions')
11+
912
export const noUnusedExpressions: Rule.RuleModule = {
1013
...esLintNoUnusedExpressions,
1114
create(context) {

packages/eslint-plugin-mdx/typings.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ declare module 'remark-mdx' {
44
const mdx: unified.Attacher
55
export = mdx
66
}
7+
8+
declare module 'eslint/use-at-your-own-risk' {
9+
import type { Rule } from 'eslint'
10+
11+
export const builtinRules: Map<string, Rule.RuleModule>
12+
}

test/__snapshots__/fixtures.test.ts.snap

+8-6
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,14 @@ Array [
172172
exports[`fixtures should match all snapshots: comments.mdx 1`] = `
173173
Array [
174174
Object {
175-
"column": 2,
176-
"line": 1,
177-
"message": "Unused eslint-disable directive (no problems were reported from 'no-console').",
178-
"nodeType": null,
179-
"ruleId": null,
180-
"severity": 2,
175+
"column": 1,
176+
"endColumn": 8,
177+
"endLine": 9,
178+
"line": 9,
179+
"message": "Don’t use multiple top level headings (1:1)",
180+
"nodeType": "Program",
181+
"ruleId": "remark-lint-no-multiple-toplevel-headings",
182+
"severity": 1,
181183
},
182184
]
183185
`;

test/fixtures/comments.mdx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<!-- eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary. -->
1+
# Header
2+
<!-- eslint-disable mdx/remark -- Here's a description about why this configuration is necessary. -->
3+
# Header
4+
<!-- eslint-enable mdx/remark -->
5+
# Title

0 commit comments

Comments
 (0)