Skip to content

Commit b718574

Browse files
committed
feat: support custom parser like babel-eslint
1 parent 9bebe61 commit b718574

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ module.exports = {
2828
{
2929
files: ['*.mdx'],
3030
extends: ['plugin:@rxts/mdx/recommended'],
31+
parserOptions: {
32+
parser: 'babel-eslint',
33+
},
3134
},
3235
{
3336
files: ['*.ts', '*.tsx'],

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rxts/eslint-plugin-mdx",
3-
"version": "0.1.3",
3+
"version": "0.2.0",
44
"description": "ESLint Parser/Plugin for MDX",
55
"repository": "[email protected]:rx-ts/eslint-plugin-mdx.git",
66
"author": "JounQin <[email protected]>",
@@ -14,14 +14,13 @@
1414
"prepublishOnly": "yarn build",
1515
"build": "tsc -P src",
1616
"test": "jest",
17-
"lint": "eslint . --ext mdx,ts,tsx"
17+
"lint": "eslint . --ext js,mdx,ts,tsx"
1818
},
1919
"peerDependencies": {
2020
"eslint": ">=6.0.0"
2121
},
2222
"dependencies": {
2323
"@mdx-js/mdx": "^1.1.0",
24-
"eslint-visitor-keys": "^1.0.0",
2524
"remark-mdx": "^1.1.0",
2625
"remark-parse": "^7.0.0",
2726
"remark-stringify": "^7.0.1",
@@ -31,16 +30,15 @@
3130
"@commitlint/config-conventional": "^8.1.0",
3231
"@rxts/eslint-plugin-mdx": "file:config",
3332
"@types/eslint": "^4.16.6",
34-
"@types/eslint-visitor-keys": "^1.0.0",
35-
"@types/estree": "^0.0.39",
3633
"@types/node": "^12.6.8",
3734
"@types/react": "^16.8.23",
3835
"@types/unist": "^2.0.3",
36+
"babel-eslint": "^10.0.2",
3937
"commitlint": "^8.1.0",
4038
"eslint": "^6.1.0",
4139
"eslint-config-1stg": "~5.4.1",
4240
"eslint-plugin-jest": "^22.14.0",
43-
"husky": "^3.0.1",
41+
"husky": "^3.0.2",
4442
"jest": "^24.8.0",
4543
"lint-staged": "^9.2.1",
4644
"prettier": "1.18.2",

src/parser.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ export const parseForESLint = (
3232
code: string,
3333
options: Linter.ParserOptions,
3434
): Linter.ESLintParseResult => {
35+
let { parser } = options
36+
37+
if (parser) {
38+
if (typeof parser === 'string') {
39+
parser = require(parser).parse
40+
} else {
41+
if (typeof parser === 'object') {
42+
parser = parser.parseForESLint || parser.parse
43+
}
44+
if (typeof parser !== 'function') {
45+
throw new Error(
46+
`Invalid custom parser for \`eslint-plugin-mdx\`: ${parser}`,
47+
)
48+
}
49+
}
50+
} else {
51+
parser = esParse
52+
}
53+
3554
const root = unified()
3655
.use<any>(remarkParse)
3756
.use<any>(remarkStringify)
@@ -53,7 +72,7 @@ export const parseForESLint = (
5372
let program: AST.Program
5473

5574
try {
56-
program = esParse(rawText, options) as AST.Program
75+
program = parser(rawText, options)
5776
} catch (e) {
5877
if (e instanceof SyntaxError) {
5978
e.index += node.start

yarn.lock

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
esutils "^2.0.2"
8787
js-tokens "^4.0.0"
8888

89-
"@babel/parser@^7.1.0", "@babel/parser@^7.4.3", "@babel/parser@^7.4.4", "@babel/parser@^7.5.5":
89+
"@babel/parser@^7.0.0", "@babel/parser@^7.1.0", "@babel/parser@^7.4.3", "@babel/parser@^7.4.4", "@babel/parser@^7.5.5":
9090
version "7.5.5"
9191
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.5.5.tgz#02f077ac8817d3df4a832ef59de67565e71cca4b"
9292
integrity sha512-E5BN68cqR7dhKan1SfqgPGhQ178bkVKpXTPEXnFJBrEt8/DKRZlybmy+IgYLTeN7tp1R5Ccmbm2rBk17sHYU3g==
@@ -122,7 +122,7 @@
122122
"@babel/parser" "^7.4.4"
123123
"@babel/types" "^7.4.4"
124124

125-
"@babel/traverse@^7.1.0", "@babel/traverse@^7.4.3", "@babel/traverse@^7.5.5":
125+
"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0", "@babel/traverse@^7.4.3", "@babel/traverse@^7.5.5":
126126
version "7.5.5"
127127
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.5.5.tgz#f664f8f368ed32988cd648da9f72d5ca70f165bb"
128128
integrity sha512-MqB0782whsfffYfSjH4TM+LMjrJnhCNEDMDIjeTpl+ASaUvxcjoiVCo/sM1GhS1pHOXYfWVCYneLjMckuUxDaQ==
@@ -536,7 +536,7 @@
536536
"@types/estree" "*"
537537
"@types/json-schema" "*"
538538

539-
"@types/estree@*", "@types/estree@^0.0.39":
539+
"@types/estree@*":
540540
version "0.0.39"
541541
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
542542
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
@@ -897,6 +897,18 @@ aws4@^1.8.0:
897897
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
898898
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==
899899

900+
babel-eslint@^10.0.2:
901+
version "10.0.2"
902+
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-10.0.2.tgz#182d5ac204579ff0881684b040560fdcc1558456"
903+
integrity sha512-UdsurWPtgiPgpJ06ryUnuaSXC2s0WoSZnQmEpbAH65XZSdwowgN5MvyP7e88nW07FYXv72erVtpBkxyDVKhH1Q==
904+
dependencies:
905+
"@babel/code-frame" "^7.0.0"
906+
"@babel/parser" "^7.0.0"
907+
"@babel/traverse" "^7.0.0"
908+
"@babel/types" "^7.0.0"
909+
eslint-scope "3.7.1"
910+
eslint-visitor-keys "^1.0.0"
911+
900912
babel-jest@^24.8.0:
901913
version "24.8.0"
902914
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-24.8.0.tgz#5c15ff2b28e20b0f45df43fe6b7f2aae93dba589"
@@ -2016,6 +2028,14 @@ eslint-plugin-vue@^5.2.3:
20162028
dependencies:
20172029
vue-eslint-parser "^5.0.0"
20182030

2031+
2032+
version "3.7.1"
2033+
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-3.7.1.tgz#3d63c3edfda02e06e01a452ad88caacc7cdcb6e8"
2034+
integrity sha1-PWPD7f2gLgbgGkUq2IyqzHzctug=
2035+
dependencies:
2036+
esrecurse "^4.1.0"
2037+
estraverse "^4.1.1"
2038+
20192039
eslint-scope@^4.0.0:
20202040
version "4.0.3"
20212041
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.3.tgz#ca03833310f6889a3264781aa82e63eb9cfe7848"
@@ -2816,10 +2836,10 @@ http-signature@~1.2.0:
28162836
jsprim "^1.2.2"
28172837
sshpk "^1.7.0"
28182838

2819-
husky@^3.0.1:
2820-
version "3.0.1"
2821-
resolved "https://registry.yarnpkg.com/husky/-/husky-3.0.1.tgz#06152c28e129622b05fa09c494209de8cf2dfb59"
2822-
integrity sha512-PXBv+iGKw23GHUlgELRlVX9932feFL407/wHFwtsGeArp0dDM4u+/QusSQwPKxmNgjpSL+ustbOdQ2jetgAZbA==
2839+
husky@^3.0.2:
2840+
version "3.0.2"
2841+
resolved "https://registry.yarnpkg.com/husky/-/husky-3.0.2.tgz#e78fd2ae16edca59fc88e56aeb8d70acdcc1c082"
2842+
integrity sha512-WXCtaME2x0o4PJlKY4ap8BzLA+D0zlvefqAvLCPriOOu+x0dpO5uc5tlB7CY6/0SE2EESmoZsj4jW5D09KrJoA==
28232843
dependencies:
28242844
chalk "^2.4.2"
28252845
cosmiconfig "^5.2.1"

0 commit comments

Comments
 (0)