Skip to content

Commit

Permalink
Add manual types to index.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 31, 2023
1 parent 1a79d23 commit 9c53c68
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 83 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
coverage/
node_modules/
index.d.ts
test.d.ts
*.d.ts
*.log
.DS_Store
yarn.lock
!/complex-types.d.ts
!/index.d.ts
80 changes: 80 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type {
Node as MdastNode,
Parent as MdastParent,
Literal as MdastLiteral,
BlockContent,
PhrasingContent
} from 'mdast'
import type {Program} from 'estree-jsx'

// Expose JavaScript API.
export {mdxJsxFromMarkdown, mdxJsxToMarkdown} from './lib/index.js'

// Expose options.
export type {ToMarkdownOptions} from './lib/index.js'

// Expose node types.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxAttributeValueExpression extends MdastLiteral {
type: 'mdxJsxAttributeValueExpression'
data?: {estree?: Program} & MdastLiteral['data']
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxExpressionAttribute extends MdastLiteral {
type: 'mdxJsxExpressionAttribute'
data?: {estree?: Program} & MdastLiteral['data']
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxAttribute extends MdastNode {
type: 'mdxJsxAttribute'
name: string
value?: MdxJsxAttributeValueExpression | string | null | undefined
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface MdxJsxElementFields {
name: string | null
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxFlowElement extends MdxJsxElementFields, MdastParent {
type: 'mdxJsxFlowElement'
children: BlockContent[]
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxTextElement extends MdxJsxElementFields, MdastParent {
type: 'mdxJsxTextElement'
children: PhrasingContent[]
}

// Add nodes to mdast content.
declare module 'mdast' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface StaticPhrasingContentMap {
mdxJsxTextElement: MdxJsxTextElement
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface BlockContentMap {
mdxJsxFlowElement: MdxJsxFlowElement
}
}

// Add nodes to hast content.
declare module 'hast' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface RootContentMap {
mdxJsxTextElement: MdxJsxTextElement
mdxJsxFlowElement: MdxJsxFlowElement
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface ElementContentMap {
mdxJsxTextElement: MdxJsxTextElement
mdxJsxFlowElement: MdxJsxFlowElement
}
}
10 changes: 1 addition & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
/**
* @typedef {import('./lib/index.js').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
* @typedef {import('./lib/index.js').MdxJsxAttribute} MdxJsxAttribute
* @typedef {import('./lib/index.js').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('./lib/index.js').MdxJsxFlowElement} MdxJsxFlowElement
* @typedef {import('./lib/index.js').MdxJsxTextElement} MdxJsxTextElement
* @typedef {import('./lib/index.js').ToMarkdownOptions} ToMarkdownOptions
*/

// Note: types exposed from `index.d.ts`.
export {mdxJsxFromMarkdown, mdxJsxToMarkdown} from './lib/index.js'
77 changes: 11 additions & 66 deletions lib/complex-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,11 @@
import type {Node} from 'unist'
import type {Parent, Literal, BlockContent, PhrasingContent} from 'mdast'
import type {Program} from 'estree-jsx'

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxAttributeValueExpression extends Literal {
type: 'mdxJsxAttributeValueExpression'
data?: {estree?: Program} & Literal['data']
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxAttribute extends Node {
type: 'mdxJsxAttribute'
name: string
value?: MdxJsxAttributeValueExpression | string | null
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxExpressionAttribute extends Literal {
type: 'mdxJsxExpressionAttribute'
data?: {estree?: Program} & Literal['data']
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface MdxJsxElementFields {
name: string | null
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxFlowElement extends MdxJsxElementFields, Parent {
type: 'mdxJsxFlowElement'
children: BlockContent[]
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxTextElement extends MdxJsxElementFields, Parent {
type: 'mdxJsxTextElement'
children: PhrasingContent[]
}

declare module 'mdast' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface StaticPhrasingContentMap {
mdxJsxTextElement: MdxJsxTextElement
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface BlockContentMap {
mdxJsxFlowElement: MdxJsxFlowElement
}
}

declare module 'hast' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface RootContentMap {
mdxJsxTextElement: MdxJsxTextElement
mdxJsxFlowElement: MdxJsxFlowElement
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface ElementContentMap {
mdxJsxTextElement: MdxJsxTextElement
mdxJsxFlowElement: MdxJsxFlowElement
}
}
// To do: next major: remove this file.
export type {
MdxJsxAttributeValueExpression,
MdxJsxAttribute,
MdxJsxExpressionAttribute,
MdxJsxFlowElement,
MdxJsxTextElement,
ToMarkdownOptions
} from '../index.js'

/// <reference types="../index.js" />
10 changes: 5 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
* @typedef {import('mdast-util-to-markdown').Map} ToMarkdownMap
*
* @typedef {import('./complex-types.js').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
* @typedef {import('./complex-types.js').MdxJsxAttribute} MdxJsxAttribute
* @typedef {import('./complex-types.js').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('./complex-types.js').MdxJsxFlowElement} MdxJsxFlowElement
* @typedef {import('./complex-types.js').MdxJsxTextElement} MdxJsxTextElement
* @typedef {import('../index.js').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
* @typedef {import('../index.js').MdxJsxAttribute} MdxJsxAttribute
* @typedef {import('../index.js').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('../index.js').MdxJsxFlowElement} MdxJsxFlowElement
* @typedef {import('../index.js').MdxJsxTextElement} MdxJsxTextElement
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["**/*.js"],
"include": ["**/*.js", "lib/complex-types.d.ts", "index.d.ts"],
"exclude": ["coverage/", "node_modules/"],
"compilerOptions": {
"checkJs": true,
Expand Down

0 comments on commit 9c53c68

Please sign in to comment.