Skip to content

Commit fe6a7f0

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent bcda18d commit fe6a7f0

File tree

5 files changed

+143
-55
lines changed

5 files changed

+143
-55
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
coverage/
22
node_modules/
3-
index.d.ts
4-
test.d.ts
3+
*.d.ts
54
*.log
65
.DS_Store
76
yarn.lock
7+
!/complex-types.d.ts
8+
!/index.d.ts

complex-types.d.ts

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
1-
import type {Literal} from 'mdast'
2-
import type {Program} from 'estree-jsx'
3-
4-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
5-
export interface MdxFlowExpression extends Literal {
6-
type: 'mdxFlowExpression'
7-
data?: {
8-
estree?: Program
9-
} & Literal['data']
10-
}
11-
12-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
13-
export interface MdxTextExpression extends Literal {
14-
type: 'mdxTextExpression'
15-
data?: {
16-
estree?: Program
17-
} & Literal['data']
18-
}
19-
20-
declare module 'mdast' {
21-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
22-
interface StaticPhrasingContentMap {
23-
mdxTextExpression: MdxTextExpression
24-
}
25-
26-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
27-
interface BlockContentMap {
28-
mdxFlowExpression: MdxFlowExpression
29-
}
30-
}
31-
32-
declare module 'hast' {
33-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
34-
interface RootContentMap {
35-
mdxTextExpression: MdxTextExpression
36-
mdxFlowExpression: MdxFlowExpression
37-
}
38-
39-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
40-
interface ElementContentMap {
41-
mdxTextExpression: MdxTextExpression
42-
mdxFlowExpression: MdxFlowExpression
43-
}
44-
}
1+
// To do: next major: remove this file.
2+
export type {
3+
MdxFlowExpression,
4+
MdxTextExpression,
5+
MDXFlowExpression,
6+
MDXTextExpression
7+
} from './index.js'
8+
9+
/// <reference types="./index.js" />

index.d.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import type {Literal} from 'mdast'
2+
import type {Program} from 'estree-jsx'
3+
4+
export {
5+
mdxExpressionFromMarkdown,
6+
mdxExpressionToMarkdown
7+
} from './lib/index.js'
8+
9+
/**
10+
* MDX expression node, occurring in flow (block).
11+
*/
12+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
13+
export interface MdxFlowExpression extends Literal {
14+
/**
15+
* Node type.
16+
*/
17+
type: 'mdxFlowExpression'
18+
19+
/**
20+
* Data.
21+
*/
22+
data?: {
23+
/**
24+
* Program node from estree.
25+
*/
26+
// eslint-disable-next-line @typescript-eslint/ban-types
27+
estree?: Program | null | undefined
28+
} & Literal['data']
29+
}
30+
31+
/**
32+
* MDX expression node, occurring in text (phrasing).
33+
*/
34+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
35+
export interface MdxTextExpression extends Literal {
36+
/**
37+
* Node type.
38+
*/
39+
type: 'mdxTextExpression'
40+
41+
/**
42+
* Data.
43+
*/
44+
data?: {
45+
/**
46+
* Program node from estree.
47+
*/
48+
// eslint-disable-next-line @typescript-eslint/ban-types
49+
estree?: Program | null | undefined
50+
} & Literal['data']
51+
}
52+
53+
/**
54+
* Deprecated: use `MdxFlowExpression`.
55+
*/
56+
// eslint-disable-next-line @typescript-eslint/naming-convention
57+
export type MDXFlowExpression = MdxFlowExpression
58+
59+
/**
60+
* Deprecated: use `MdxTextExpression`.
61+
*/
62+
// eslint-disable-next-line @typescript-eslint/naming-convention
63+
export type MDXTextExpression = MdxTextExpression
64+
65+
// Add nodes to mdast content.
66+
declare module 'mdast' {
67+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
68+
interface StaticPhrasingContentMap {
69+
/**
70+
* MDX expression node, occurring in text (phrasing).
71+
*/
72+
mdxTextExpression: MdxTextExpression
73+
}
74+
75+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
76+
interface BlockContentMap {
77+
/**
78+
* MDX expression node, occurring in flow (block).
79+
*/
80+
mdxFlowExpression: MdxFlowExpression
81+
}
82+
}
83+
84+
declare module 'hast' {
85+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
86+
interface RootContentMap {
87+
/**
88+
* MDX expression node, occurring in flow (block).
89+
*/
90+
mdxFlowExpression: MdxFlowExpression
91+
/**
92+
* MDX expression node, occurring in text (phrasing).
93+
*/
94+
mdxTextExpression: MdxTextExpression
95+
}
96+
97+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
98+
interface ElementContentMap {
99+
/**
100+
* MDX expression node, occurring in flow (block).
101+
*/
102+
mdxFlowExpression: MdxFlowExpression
103+
/**
104+
* MDX expression node, occurring in text (phrasing).
105+
*/
106+
mdxTextExpression: MdxTextExpression
107+
}
108+
}

lib/index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
/**
2+
* @typedef {import('estree-jsx').Program} Program
3+
*
24
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
35
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
46
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
7+
*
58
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
69
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
7-
* @typedef {import('estree-jsx').Program} Program
8-
* @typedef {import('../complex-types.js').MdxFlowExpression} MdxFlowExpression
9-
* @typedef {import('../complex-types.js').MdxTextExpression} MdxTextExpression
10+
*
11+
* @typedef {import('../index.js').MdxFlowExpression} MdxFlowExpression
12+
* @typedef {import('../index.js').MdxTextExpression} MdxTextExpression
1013
*/
1114

12-
/** @type {FromMarkdownExtension} */
15+
/**
16+
* Extension for `mdast-util-from-markdown` to enable MDX expressions.
17+
*
18+
* When using the syntax extension with `addResult`, nodes will have a
19+
* `data.estree` field set to an ESTree `Program` node.
20+
*
21+
* @type {FromMarkdownExtension}
22+
*/
1323
export const mdxExpressionFromMarkdown = {
1424
enter: {
1525
mdxFlowExpression: enterMdxFlowExpression,
@@ -23,7 +33,11 @@ export const mdxExpressionFromMarkdown = {
2333
}
2434
}
2535

26-
/** @type {ToMarkdownExtension} */
36+
/**
37+
* Extension for `mdast-util-to-markdown` to enable MDX ESM.
38+
*
39+
* @type {ToMarkdownExtension}
40+
*/
2741
export const mdxExpressionToMarkdown = {
2842
handlers: {
2943
mdxFlowExpression: handleMdxExpression,
@@ -59,10 +73,10 @@ function enterMdxTextExpression(token) {
5973
*/
6074
function exitMdxExpression(token) {
6175
const value = this.resume()
62-
/** @type {Program|undefined} */
76+
/** @type {Program | undefined} */
6377
// @ts-expect-error: estree.
6478
const estree = token.estree
65-
const node = /** @type {MdxFlowExpression|MdxTextExpression} */ (
79+
const node = /** @type {MdxFlowExpression | MdxTextExpression} */ (
6680
this.exit(token)
6781
)
6882
node.value = value
@@ -83,7 +97,7 @@ function exitMdxExpressionData(token) {
8397

8498
/**
8599
* @type {ToMarkdownHandle}
86-
* @param {MdxFlowExpression|MdxTextExpression} node
100+
* @param {MdxFlowExpression | MdxTextExpression} node
87101
*/
88102
function handleMdxExpression(node) {
89103
const value = node.value || ''

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"include": ["**/*.js"],
2+
"include": ["**/*.js", "complex-types.d.ts", "index.d.ts"],
33
"exclude": ["coverage/", "node_modules/"],
44
"compilerOptions": {
55
"checkJs": true,

0 commit comments

Comments
 (0)