Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for eslint 8 #353

Merged
merged 2 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"build": "run-p build:*",
"build:r": "r -f esm,es2015",
"build:r": "r -f es2015",
"build:ts": "tsc -b",
"clean": "rimraf packages/*/{lib,*.tsbuildinfo} node_modules/@1stg/eslint-config/node_modules",
"lint": "run-p lint:*",
Expand All @@ -30,7 +30,7 @@
"@types/eslint-plugin-markdown": "^2.0.0",
"@types/jest": "^27.0.2",
"@types/node": "^16.10.9",
"@types/react": "^17.0.29",
"@types/react": "^17.0.30",
"@types/unist": "^2.0.6",
"lerna": "^4.0.0",
"npm-run-all": "^4.1.5",
Expand All @@ -44,6 +44,12 @@
"yarn-deduplicate": "^3.1.0"
},
"resolutions": {
"@typescript-eslint/eslint-plugin": "^5.0.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These dependencies have not been upgraded in my @1stg/eslint-config due to eslint-plugin-mdx.

"@typescript-eslint/eslint-plugin-tslint": "^5.0.0",
"@typescript-eslint/experimental-utils": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-plugin-unicorn": "^37.0.0",
"prettier": "^2.4.1"
},
"commitlint": {
Expand All @@ -63,6 +69,7 @@
"eslint/lib/linter/linter"
],
"moduleNameMapper": {
"^eslint/use-at-your-own-risk$": "<rootDir>/node_modules/eslint/lib/unsupported-api.js",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it should be supported by jest itself.

"^eslint-mdx$": "<rootDir>/packages/eslint-mdx/src",
"^eslint-plugin-mdx$": "<rootDir>/packages/eslint-plugin-mdx/src"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/eslint-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
"engines": {
"node": ">=10.0.0"
},
"main": "lib",
"module": "lib/esm",
"es2015": "lib/es2015",
"main": "lib/index.js",
"module": "lib/index.es2015.mjs",
"types": "lib",
"files": [
"lib",
Expand Down
5 changes: 2 additions & 3 deletions packages/eslint-plugin-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
"engines": {
"node": ">=10.0.0"
},
"main": "lib",
"module": "lib/esm",
"es2015": "lib/es2015",
"main": "lib/index.js",
"module": "lib/index.es2015.mjs",
"types": "lib",
"files": [
"lib",
Expand Down
26 changes: 26 additions & 0 deletions packages/eslint-plugin-mdx/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Rule } from 'eslint'

export const getGlobals = <
T extends Record<string, unknown> | string[],
G extends Record<string, boolean>,
Expand All @@ -18,3 +20,27 @@ export const getGlobals = <
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
initialGlobals as R,
)

const PRIVATE_API_VERSION = 8

export const getBuiltinRule = (ruleId: string) => {
// TODO: Remove this when we drop support for ESLint < 8
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const { version: eslintVersion } = require('eslint/package.json') as {
version: string
}

const majorVersion = Number.parseInt(eslintVersion.split('.')[0], 10)

/* istanbul ignore next */
if (majorVersion < PRIVATE_API_VERSION) {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
return require(`eslint/lib/rules/${ruleId}`) as Rule.RuleModule
}

// prettier-ignore
return (
// eslint-disable-next-line @typescript-eslint/consistent-type-imports, @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
require('eslint/use-at-your-own-risk') as typeof import('eslint/use-at-your-own-risk')
).builtinRules.get(ruleId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import type { Rule } from 'eslint'
import { isJsxNode } from 'eslint-mdx'
import esLintNoUnusedExpressions from 'eslint/lib/rules/no-unused-expressions'

import { getBuiltinRule } from '../helpers'

import type { ExpressionStatementWithParent } from './types'

const esLintNoUnusedExpressions = getBuiltinRule('no-unused-expressions')

export const noUnusedExpressions: Rule.RuleModule = {
...esLintNoUnusedExpressions,
create(context) {
Expand Down
6 changes: 6 additions & 0 deletions packages/eslint-plugin-mdx/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ declare module 'remark-mdx' {
const mdx: unified.Attacher
export = mdx
}

declare module 'eslint/use-at-your-own-risk' {
import type { Rule } from 'eslint'

export const builtinRules: Map<string, Rule.RuleModule>
}
14 changes: 8 additions & 6 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,14 @@ Array [
exports[`fixtures should match all snapshots: comments.mdx 1`] = `
Array [
Object {
"column": 2,
"line": 1,
"message": "Unused eslint-disable directive (no problems were reported from 'no-console').",
"nodeType": null,
"ruleId": null,
"severity": 2,
"column": 1,
"endColumn": 8,
"endLine": 9,
"line": 9,
"message": "Don’t use multiple top level headings (1:1)",
"nodeType": "Program",
"ruleId": "remark-lint-no-multiple-toplevel-headings",
"severity": 1,
},
]
`;
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/comments.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<!-- eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary. -->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Header
<!-- eslint-disable mdx/remark -- Here's a description about why this configuration is necessary. -->
# Header
<!-- eslint-enable mdx/remark -->
# Title
Loading