Skip to content

Commit 94fcb4c

Browse files
authored
feat: resolve custom remark config on parsing (#337)
1 parent 0feb2d3 commit 94fcb4c

File tree

17 files changed

+234
-205
lines changed

17 files changed

+234
-205
lines changed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,24 @@
2929
"@1stg/tslint-config": "^2.0.0",
3030
"@types/eslint": "^7.28.0",
3131
"@types/eslint-plugin-markdown": "^2.0.0",
32-
"@types/jest": "^26.0.24",
33-
"@types/node": "^16.3.1",
34-
"@types/react": "^17.0.14",
35-
"@types/unist": "^2.0.5",
32+
"@types/jest": "^27.0.1",
33+
"@types/node": "^16.7.1",
34+
"@types/react": "^17.0.19",
35+
"@types/unist": "^2.0.6",
3636
"lerna": "^4.0.0",
3737
"npm-run-all": "^4.1.5",
3838
"react": "^17.0.2",
39+
"remark-frontmatter": "2",
3940
"remark-validate-links": "^10.0.4",
40-
"ts-jest": "^27.0.3",
41-
"ts-node": "^10.1.0",
41+
"ts-jest": "^27.0.5",
42+
"ts-node": "^10.2.1",
4243
"tslint": "^6.1.3",
4344
"type-coverage": "^2.18.0",
4445
"typescript": "^4.3.5",
4546
"yarn-deduplicate": "^3.1.0"
4647
},
4748
"resolutions": {
48-
"@babel/core": "^7.14.6",
49-
"prettier": "^2.3.2",
50-
"tslib": "^2.3.0"
49+
"prettier": "^2.3.2"
5150
},
5251
"commitlint": {
5352
"extends": [
@@ -83,6 +82,7 @@
8382
"prettier": "@1stg/prettier-config",
8483
"remarkConfig": {
8584
"plugins": [
85+
"remark-frontmatter",
8686
"@1stg/remark-config",
8787
[
8888
"lint-no-duplicate-headings",

packages/eslint-mdx/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
"eslint": ">=5.0.0"
3333
},
3434
"dependencies": {
35+
"cosmiconfig": "^7.0.0",
3536
"remark-mdx": "^1.6.22",
3637
"remark-parse": "^8.0.3",
37-
"tslib": "^2.3.0",
38+
"remark-stringify": "^8.1.1",
39+
"tslib": "^2.3.1",
3840
"unified": "^9.2.1"
3941
}
4042
}

packages/eslint-mdx/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './helpers'
22
export * from './parser'
3+
export * from './processor'
34
export * from './regexp'
45
export * from './traverse'
56
export * from './types'

packages/eslint-mdx/src/parser.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import path from 'path'
22

33
import type { ExpressionStatement } from '@babel/types'
44
import type { AST, Linter } from 'eslint'
5-
import remarkMdx from 'remark-mdx'
6-
import remarkParse from 'remark-parse'
7-
import unified from 'unified'
85

96
import {
107
arrayify,
@@ -16,6 +13,7 @@ import {
1613
normalizePosition,
1714
restoreNodeLocation,
1815
} from './helpers'
16+
import { getPhysicalFilename, getRemarkProcessor } from './processor'
1917
import {
2018
COMMENT_CONTENT_REGEX,
2119
COMMENT_CONTENT_REGEX_GLOBAL,
@@ -32,9 +30,6 @@ import type {
3230
ParserServices,
3331
} from './types'
3432

35-
export const mdProcessor = unified().use(remarkParse).freeze()
36-
export const mdxProcessor = mdProcessor().use(remarkMdx).freeze()
37-
3833
export const AST_PROPS = ['body', 'comments', 'tokens'] as const
3934
export const ES_NODE_TYPES: readonly string[] = ['export', 'import', 'jsx']
4035

@@ -43,6 +38,8 @@ export const LOC_ERROR_PROPERTIES = ['column', 'lineNumber'] as const
4338
export const DEFAULT_EXTENSIONS: readonly string[] = ['.mdx']
4439
export const MARKDOWN_EXTENSIONS: readonly string[] = ['.md']
4540

41+
export const PLACEHOLDER_FILE_PATH = '__placeholder__.mdx'
42+
4643
export const DEFAULT_PARSER_OPTIONS: ParserOptions = {
4744
comment: true,
4845
ecmaFeatures: {
@@ -52,7 +49,7 @@ export const DEFAULT_PARSER_OPTIONS: ParserOptions = {
5249
new Date().getUTCFullYear() as Linter.ParserOptions['ecmaVersion'],
5350
sourceType: 'module',
5451
tokens: true,
55-
filePath: '__placeholder__.mdx',
52+
filePath: PLACEHOLDER_FILE_PATH,
5653
// required for @typescript-eslint/parser
5754
// reference: https://github.com/typescript-eslint/typescript-eslint/pull/2028
5855
loc: true,
@@ -170,7 +167,10 @@ export class Parser {
170167
return this._eslintParse(code, options)
171168
}
172169

173-
const root = (isMdx ? mdxProcessor : mdProcessor).parse(code) as Parent
170+
const root = getRemarkProcessor(
171+
getPhysicalFilename(options.filePath),
172+
isMdx,
173+
).parse(code) as Parent
174174

175175
this._ast = {
176176
...normalizePosition(root.position),

packages/eslint-plugin-mdx/src/rules/helpers.ts renamed to packages/eslint-mdx/src/processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import fs from 'fs'
22
import path from 'path'
33

44
import { cosmiconfigSync } from 'cosmiconfig'
5-
import { arrayify } from 'eslint-mdx'
65
import remarkMdx from 'remark-mdx'
76
import remarkParse from 'remark-parse'
87
import remarkStringify from 'remark-stringify'
98
import type { FrozenProcessor } from 'unified'
109
import unified from 'unified'
1110

11+
import { arrayify } from './helpers'
1212
import type { RemarkConfig, RemarkPlugin } from './types'
1313

1414
export const requirePkg = <T>(

packages/eslint-mdx/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { JSXElement, JSXFragment } from '@babel/types'
22
import type { AST, Linter } from 'eslint'
3+
import type { Attacher } from 'unified'
34
import type { Node as _Node, Parent as _Parent, Point } from 'unist'
45

56
export interface Node<T = string> extends _Node {
@@ -70,3 +71,10 @@ export interface Comment {
7071
export interface ParserServices {
7172
JSXElementsWithHTMLComments: Node[]
7273
}
74+
75+
export type RemarkPlugin = Attacher | string
76+
77+
export interface RemarkConfig {
78+
settings: Record<string, string>
79+
plugins: Array<RemarkPlugin | [RemarkPlugin, ...unknown[]]>
80+
}

packages/eslint-plugin-mdx/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,10 @@
3232
"eslint": ">=5.0.0"
3333
},
3434
"dependencies": {
35-
"cosmiconfig": "^7.0.0",
3635
"eslint-mdx": "^1.14.1",
3736
"eslint-plugin-markdown": "^2.2.0",
38-
"remark-mdx": "^1.6.22",
39-
"remark-parse": "^8.0.3",
40-
"remark-stringify": "^8.1.1",
4137
"synckit": "^0.3.4",
42-
"tslib": "^2.3.0",
43-
"unified": "^9.2.1",
38+
"tslib": "^2.3.1",
4439
"vfile": "^4.2.1"
4540
}
4641
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { noJsxHtmlComments } from './no-jsx-html-comments'
44
import { noUnusedExpressions } from './no-unused-expressions'
55
import { remark } from './remark'
66

7-
export * from './helpers'
87
export * from './types'
98

109
export { noJsxHtmlComments, noUnusedExpressions, remark }

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import path from 'path'
22

33
import type { Rule } from 'eslint'
44
import type { ParserOptions } from 'eslint-mdx'
5-
import { DEFAULT_EXTENSIONS, MARKDOWN_EXTENSIONS } from 'eslint-mdx'
5+
import {
6+
DEFAULT_EXTENSIONS,
7+
MARKDOWN_EXTENSIONS,
8+
getPhysicalFilename,
9+
getRemarkProcessor,
10+
} from 'eslint-mdx'
611
import { createSyncFn } from 'synckit'
712
import type { FrozenProcessor } from 'unified'
813
import type { VFile, VFileOptions } from 'vfile'
914
import vfile from 'vfile'
1015

11-
import { getPhysicalFilename, getRemarkProcessor } from './helpers'
1216
import type { RemarkLintMessage } from './types'
1317

1418
const workerPath = require.resolve('../worker')

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Linter } from 'eslint'
22
import type { ExpressionStatement, Node } from 'estree'
3-
import type { Attacher } from 'unified'
43

54
export interface WithParent {
65
parent: NodeWithParent
@@ -12,13 +11,6 @@ export interface ExpressionStatementWithParent
1211
extends ExpressionStatement,
1312
WithParent {}
1413

15-
export type RemarkPlugin = Attacher | string
16-
17-
export interface RemarkConfig {
18-
settings: Record<string, string>
19-
plugins: Array<RemarkPlugin | [RemarkPlugin, ...unknown[]]>
20-
}
21-
2214
export interface RemarkLintMessage {
2315
reason: string
2416
source: string

0 commit comments

Comments
 (0)