Skip to content

Commit 3413315

Browse files
authored
feat: remark-mdx is not compatible with normal markdown syntaxes (#206)
1 parent 8a6242a commit 3413315

File tree

7 files changed

+47
-51
lines changed

7 files changed

+47
-51
lines changed

packages/eslint-mdx/src/parser.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
ParserOptions,
2525
} from './types'
2626

27-
export const mdxProcessor = unified().use(remarkParse).use(remarkMdx).freeze()
27+
export const mdProcessor = unified().use(remarkParse).freeze()
28+
export const mdxProcessor = mdProcessor().use(remarkMdx).freeze()
2829

2930
export const AST_PROPS = ['body', 'comments', 'tokens'] as const
3031
export const ES_NODE_TYPES: readonly string[] = ['export', 'import', 'jsx']
@@ -161,7 +162,7 @@ export class Parser {
161162
return this._eslintParse(code, options)
162163
}
163164

164-
const root = mdxProcessor.parse(code) as Parent
165+
const root = (isMdx ? mdxProcessor : mdProcessor).parse(code) as Parent
165166

166167
this._ast = {
167168
...normalizePosition(root.position),
@@ -333,12 +334,20 @@ export class Parser {
333334

334335
const value = node.value as string
335336

337+
const { loc, start, end } = normalizePosition(node.position)
338+
336339
// fix #4
337340
if (isComment(value)) {
341+
const comment = COMMENT_CONTENT_REGEX.exec(value)[2]
342+
this._ast.comments.push({
343+
type: 'Block',
344+
value: comment,
345+
loc,
346+
range: [start, end],
347+
})
338348
return
339349
}
340350

341-
const { loc, start } = normalizePosition(node.position)
342351
const startLine = loc.start.line - 1 // ! line is 1-indexed, change to 0-indexed to simplify usage
343352

344353
let program: AST.Program

packages/eslint-plugin-mdx/src/rules/helper.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const requirePkg = <T>(
4141
let searchSync: (searchFrom?: string) => CosmiconfigResult
4242
let remarkProcessor: Processor
4343

44-
export const getRemarkProcessor = (searchFrom: string) => {
44+
export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
4545
if (!searchSync) {
4646
searchSync = cosmiconfigSync('remark', {
4747
packageProp: 'remarkConfig',
@@ -66,6 +66,12 @@ export const getRemarkProcessor = (searchFrom: string) => {
6666
// just ignore if the package does not exist
6767
}
6868

69+
const initProcessor = remarkProcessor().use({ settings }).use(remarkStringify)
70+
71+
if (isMdx) {
72+
initProcessor.use(remarkMdx)
73+
}
74+
6975
return plugins
7076
.reduce((processor, pluginWithSettings) => {
7177
const [plugin, ...pluginSettings] = Array.isArray(pluginWithSettings)
@@ -78,6 +84,6 @@ export const getRemarkProcessor = (searchFrom: string) => {
7884
: plugin,
7985
...pluginSettings,
8086
)
81-
}, remarkProcessor().use({ settings }).use(remarkStringify).use(remarkMdx))
87+
}, initProcessor)
8288
.freeze()
8389
}

packages/eslint-plugin-mdx/src/rules/remark.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ export const remark: Rule.RuleModule = {
2323
const filename = context.getFilename()
2424
const extname = path.extname(filename)
2525
const sourceCode = context.getSourceCode()
26-
const extensions = new Set(
27-
DEFAULT_EXTENSIONS.concat(
28-
context.parserOptions.extensions || [],
29-
MARKDOWN_EXTENSIONS,
30-
context.parserOptions.markdownExtensions || [],
31-
),
26+
const options = context.parserOptions
27+
const isMdx = DEFAULT_EXTENSIONS.concat(options.extensions || []).includes(
28+
extname,
3229
)
30+
const isMarkdown = MARKDOWN_EXTENSIONS.concat(
31+
options.markdownExtensions || [],
32+
).includes(extname)
3333
return {
3434
Program(node) {
3535
/* istanbul ignore if */
36-
if (!extensions.has(extname)) {
36+
if (!isMdx && !isMarkdown) {
3737
return
3838
}
3939
const sourceText = sourceCode.getText(node)
40-
const remarkProcessor = getRemarkProcessor(filename)
40+
const remarkProcessor = getRemarkProcessor(filename, isMdx)
4141
const file = vfile({
4242
path: filename,
4343
contents: sourceText,

test/__snapshots__/fixtures.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,6 @@ Header <header>
116116
"
117117
`;
118118
119+
exports[`fixtures should match all snapshots: remark.md 1`] = `undefined`;
120+
119121
exports[`fixtures should match all snapshots: remark.mdx 1`] = `undefined`;

test/fixtures/remark.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_Hello_, **world**!
2+
3+
<!--lint disable no-literal-urls-->
4+
5+
### Code Splitting
6+
7+
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

test/fixtures/remark.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
_Hello_, **world**!
2+
3+
### Code Splitting
4+
5+
<!-- eslint-disable mdx/remark -->
6+
7+
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

yarn.lock

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,17 +1758,7 @@
17581758
"@types/yargs" "^15.0.0"
17591759
chalk "^3.0.0"
17601760

1761-
"@jest/types@^26.1.0":
1762-
version "26.1.0"
1763-
resolved "https://registry.yarnpkg.com/@jest/types/-/types-26.1.0.tgz#f8afaaaeeb23b5cad49dd1f7779689941dcb6057"
1764-
integrity sha512-GXigDDsp6ZlNMhXQDeuy/iYCDsRIHJabWtDzvnn36+aqFfG14JmFV0e/iXxY4SP9vbXSiPNOWdehU5MeqrYHBQ==
1765-
dependencies:
1766-
"@types/istanbul-lib-coverage" "^2.0.0"
1767-
"@types/istanbul-reports" "^1.1.1"
1768-
"@types/yargs" "^15.0.0"
1769-
chalk "^4.0.0"
1770-
1771-
"@jest/types@^26.2.0":
1761+
"@jest/types@^26.1.0", "@jest/types@^26.2.0":
17721762
version "26.2.0"
17731763
resolved "https://registry.yarnpkg.com/@jest/types/-/types-26.2.0.tgz#b28ca1fb517a4eb48c0addea7fcd9edc4ab45721"
17741764
integrity sha512-lvm3rJvctxd7+wxKSxxbzpDbr4FXDLaC57WEKdUIZ2cjTYuxYSc0zlyD7Z4Uqr5VdKxRUrtwIkiqBuvgf8uKJA==
@@ -3056,12 +3046,7 @@
30563046
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
30573047
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=
30583048

3059-
"@types/node@*", "@types/node@>= 8":
3060-
version "14.0.22"
3061-
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.22.tgz#23ea4d88189cec7d58f9e6b66f786b215eb61bdc"
3062-
integrity sha512-emeGcJvdiZ4Z3ohbmw93E/64jRzUHAItSHt8nF7M4TGgQTiWqFVGB8KNpLGFmUHmHLvjvBgFwVlqNcq+VuGv9g==
3063-
3064-
"@types/node@^14.0.27":
3049+
"@types/node@*", "@types/node@>= 8", "@types/node@^14.0.27":
30653050
version "14.0.27"
30663051
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.27.tgz#a151873af5a5e851b51b3b065c9e63390a9e0eb1"
30673052
integrity sha512-kVrqXhbclHNHGu9ztnAwSncIgJv/FaxmzXJvGXNdcCpV1b8u1/Mi6z6m0vwy0LzKeXFTPLH0NzwmoJ3fNCIq0g==
@@ -3098,15 +3083,7 @@
30983083
dependencies:
30993084
"@types/react" "*"
31003085

3101-
"@types/react@*":
3102-
version "16.9.43"
3103-
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.43.tgz#c287f23f6189666ee3bebc2eb8d0f84bcb6cdb6b"
3104-
integrity sha512-PxshAFcnJqIWYpJbLPriClH53Z2WlJcVZE+NP2etUtWQs2s7yIMj3/LDKZT/5CHJ/F62iyjVCDu2H3jHEXIxSg==
3105-
dependencies:
3106-
"@types/prop-types" "*"
3107-
csstype "^2.2.0"
3108-
3109-
"@types/react@^16.9.44":
3086+
"@types/react@*", "@types/react@^16.9.44":
31103087
version "16.9.44"
31113088
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.44.tgz#da84b179c031aef67dc92c33bd3401f1da2fa3bc"
31123089
integrity sha512-BtLoJrXdW8DVZauKP+bY4Kmiq7ubcJq+H/aCpRfvPF7RAT3RwR73Sg8szdc2YasbAlWBDrQ6Q+AFM0KwtQY+WQ==
@@ -7758,7 +7735,7 @@ jest-snapshot@^26.1.0:
77587735
pretty-format "^26.1.0"
77597736
semver "^7.3.2"
77607737

7761-
7738+
[email protected], jest-util@^26.1.0:
77627739
version "26.2.0"
77637740
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-26.2.0.tgz#0597d2a27c559340957609f106c408c17c1d88ac"
77647741
integrity sha512-YmDwJxLZ1kFxpxPfhSJ0rIkiZOM0PQbRcfH0TzJOhqCisCAsI1WcmoQqO83My9xeVA2k4n+rzg2UuexVKzPpig==
@@ -7770,17 +7747,6 @@ [email protected]:
77707747
is-ci "^2.0.0"
77717748
micromatch "^4.0.2"
77727749

7773-
jest-util@^26.1.0:
7774-
version "26.1.0"
7775-
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-26.1.0.tgz#80e85d4ba820decacf41a691c2042d5276e5d8d8"
7776-
integrity sha512-rNMOwFQevljfNGvbzNQAxdmXQ+NawW/J72dmddsK0E8vgxXCMtwQ/EH0BiWEIxh0hhMcTsxwAxINt7Lh46Uzbg==
7777-
dependencies:
7778-
"@jest/types" "^26.1.0"
7779-
chalk "^4.0.0"
7780-
graceful-fs "^4.2.4"
7781-
is-ci "^2.0.0"
7782-
micromatch "^4.0.2"
7783-
77847750
jest-validate@^26.1.0:
77857751
version "26.1.0"
77867752
resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-26.1.0.tgz#942c85ad3d60f78250c488a7f85d8f11a29788e7"

0 commit comments

Comments
 (0)