Skip to content

Commit 9a57d62

Browse files
committed
Refactor code-style
1 parent 2800858 commit 9a57d62

File tree

5 files changed

+914
-752
lines changed

5 files changed

+914
-752
lines changed

index.d.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77

88
export {directiveFromMarkdown, directiveToMarkdown} from './lib/index.js'
99

10-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
1110
interface DirectiveFields {
1211
/**
1312
* Directive name.
@@ -17,15 +16,13 @@ interface DirectiveFields {
1716
/**
1817
* Directive attributes.
1918
*/
20-
// eslint-disable-next-line @typescript-eslint/ban-types
2119
attributes?: Record<string, string | null | undefined> | null | undefined
2220
}
2321

2422
/**
2523
* Directive in flow content (such as in the root document, or block
2624
* quotes), which contains further flow content.
2725
*/
28-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
2926
export interface ContainerDirective extends Parent, DirectiveFields {
3027
/**
3128
* Node type.
@@ -42,7 +39,6 @@ export interface ContainerDirective extends Parent, DirectiveFields {
4239
* Directive in flow content (such as in the root document, or block
4340
* quotes), which contains nothing.
4441
*/
45-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
4642
export interface LeafDirective extends Parent, DirectiveFields {
4743
/**
4844
* Node type.
@@ -58,7 +54,6 @@ export interface LeafDirective extends Parent, DirectiveFields {
5854
/**
5955
* Directive in phrasing content (such as in paragraphs, headings).
6056
*/
61-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
6257
export interface TextDirective extends Parent, DirectiveFields {
6358
/**
6459
* Node type.
@@ -78,7 +73,6 @@ export type Directive = ContainerDirective | LeafDirective | TextDirective
7873

7974
// Add custom data tracked to turn markdown into a tree.
8075
declare module 'mdast-util-from-markdown' {
81-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
8276
interface CompileData {
8377
/**
8478
* Attributes for current directive.
@@ -89,7 +83,6 @@ declare module 'mdast-util-from-markdown' {
8983

9084
// Add custom data tracked to turn a syntax tree into markdown.
9185
declare module 'mdast-util-to-markdown' {
92-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
9386
interface ConstructNameMap {
9487
/**
9588
* Whole container directive.
@@ -156,10 +149,9 @@ declare module 'mdast-util-to-markdown' {
156149
}
157150
}
158151

159-
// Add nodes to content.
152+
// Add nodes to content, register `data` on paragraph.
160153
declare module 'mdast' {
161-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
162-
interface RootContentMap {
154+
interface BlockContentMap {
163155
/**
164156
* Directive in flow content (such as in the root document, or block
165157
* quotes), which contains further flow content.
@@ -171,23 +163,31 @@ declare module 'mdast' {
171163
* quotes), which contains nothing.
172164
*/
173165
leafDirective: LeafDirective
166+
}
174167

168+
interface ParagraphData {
175169
/**
176-
* Directive in phrasing content (such as in paragraphs, headings).
170+
* Field set on the first paragraph which is a child of a container
171+
* directive.
172+
* When this is `true`, that means the paragraph represents the *label*:
173+
*
174+
* ```markdown
175+
* :::a[This is the label]
176+
* This is further things.
177+
* :::
178+
* ```
177179
*/
178-
textDirective: TextDirective
180+
directiveLabel?: boolean | null | undefined
179181
}
180182

181-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
182183
interface PhrasingContentMap {
183184
/**
184185
* Directive in phrasing content (such as in paragraphs, headings).
185186
*/
186187
textDirective: TextDirective
187188
}
188189

189-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
190-
interface BlockContentMap {
190+
interface RootContentMap {
191191
/**
192192
* Directive in flow content (such as in the root document, or block
193193
* quotes), which contains further flow content.
@@ -199,22 +199,10 @@ declare module 'mdast' {
199199
* quotes), which contains nothing.
200200
*/
201201
leafDirective: LeafDirective
202-
}
203202

204-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
205-
interface ParagraphData {
206203
/**
207-
* Field set on the first paragraph which is a child of a container
208-
* directive.
209-
* When this is `true`, that means the paragraph represents the *label*:
210-
*
211-
* ```markdown
212-
* :::a[This is the label]
213-
* This is further things.
214-
* :::
215-
* ```
204+
* Directive in phrasing content (such as in paragraphs, headings).
216205
*/
217-
// eslint-disable-next-line @typescript-eslint/ban-types
218-
directiveLabel?: boolean | null | undefined
206+
textDirective: TextDirective
219207
}
220208
}

lib/index.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
* @typedef {import('mdast').BlockContent} BlockContent
3-
* @typedef {import('mdast').DefinitionContent} DefinitionContent
2+
* @typedef {import('mdast').Nodes} Nodes
43
* @typedef {import('mdast').Paragraph} Paragraph
54
*
65
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
@@ -13,11 +12,12 @@
1312
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
1413
* @typedef {import('mdast-util-to-markdown').State} State
1514
*
15+
* @typedef {import('../index.js').Directive} Directive
1616
* @typedef {import('../index.js').LeafDirective} LeafDirective
1717
* @typedef {import('../index.js').TextDirective} TextDirective
18-
* @typedef {import('../index.js').Directive} Directive
1918
*/
2019

20+
import {ok as assert} from 'devlop'
2121
import {parseEntities} from 'parse-entities'
2222
import {stringifyEntitiesLight} from 'stringify-entities'
2323
import {visitParents} from 'unist-util-visit-parents'
@@ -144,7 +144,12 @@ function enter(type, token) {
144144
* @param {Token} token
145145
*/
146146
function exitName(token) {
147-
const node = /** @type {Directive} */ (this.stack[this.stack.length - 1])
147+
const node = this.stack[this.stack.length - 1]
148+
assert(
149+
node.type === 'containerDirective' ||
150+
node.type === 'leafDirective' ||
151+
node.type === 'textDirective'
152+
)
148153
node.name = this.sliceSerialize(token)
149154
}
150155

@@ -181,9 +186,8 @@ function enterAttributes() {
181186
* @type {FromMarkdownHandle}
182187
*/
183188
function exitAttributeIdValue(token) {
184-
const list = /** @type {Array<[string, string]>} */ (
185-
this.data.directiveAttributes
186-
)
189+
const list = this.data.directiveAttributes
190+
assert(list, 'expected `directiveAttributes`')
187191
list.push([
188192
'id',
189193
parseEntities(this.sliceSerialize(token), {
@@ -197,9 +201,8 @@ function exitAttributeIdValue(token) {
197201
* @type {FromMarkdownHandle}
198202
*/
199203
function exitAttributeClassValue(token) {
200-
const list = /** @type {Array<[string, string]>} */ (
201-
this.data.directiveAttributes
202-
)
204+
const list = this.data.directiveAttributes
205+
assert(list, 'expected `directiveAttributes`')
203206
list.push([
204207
'class',
205208
parseEntities(this.sliceSerialize(token), {
@@ -213,9 +216,8 @@ function exitAttributeClassValue(token) {
213216
* @type {FromMarkdownHandle}
214217
*/
215218
function exitAttributeValue(token) {
216-
const list = /** @type {Array<[string, string]>} */ (
217-
this.data.directiveAttributes
218-
)
219+
const list = this.data.directiveAttributes
220+
assert(list, 'expected `directiveAttributes`')
219221
list[list.length - 1][1] = parseEntities(this.sliceSerialize(token), {
220222
attribute: true
221223
})
@@ -226,9 +228,8 @@ function exitAttributeValue(token) {
226228
* @type {FromMarkdownHandle}
227229
*/
228230
function exitAttributeName(token) {
229-
const list = /** @type {Array<[string, string]>} */ (
230-
this.data.directiveAttributes
231-
)
231+
const list = this.data.directiveAttributes
232+
assert(list, 'expected `directiveAttributes`')
232233

233234
// Attribute names in CommonMark are significantly limited, so character
234235
// references can’t exist.
@@ -240,9 +241,8 @@ function exitAttributeName(token) {
240241
* @type {FromMarkdownHandle}
241242
*/
242243
function exitAttributes() {
243-
const list = /** @type {Array<[string, string]>} */ (
244-
this.data.directiveAttributes
245-
)
244+
const list = this.data.directiveAttributes
245+
assert(list, 'expected `directiveAttributes`')
246246
/** @type {Record<string, string>} */
247247
const cleaned = {}
248248
let index = -1
@@ -259,7 +259,12 @@ function exitAttributes() {
259259

260260
this.data.directiveAttributes = undefined
261261
this.resume() // Drop EOLs
262-
const node = /** @type {Directive} */ (this.stack[this.stack.length - 1])
262+
const node = this.stack[this.stack.length - 1]
263+
assert(
264+
node.type === 'containerDirective' ||
265+
node.type === 'leafDirective' ||
266+
node.type === 'textDirective'
267+
)
263268
node.attributes = cleaned
264269
}
265270

@@ -275,12 +280,12 @@ function exit(token) {
275280
* @type {ToMarkdownHandle}
276281
* @param {Directive} node
277282
*/
278-
function handleDirective(node, _, state, safeOptions) {
279-
const tracker = state.createTracker(safeOptions)
283+
function handleDirective(node, _, state, info) {
284+
const tracker = state.createTracker(info)
280285
const sequence = fence(node)
281286
const exit = state.enter(node.type)
282287
let value = tracker.move(sequence + (node.name || ''))
283-
/** @type {Paragraph | LeafDirective | TextDirective | undefined} */
288+
/** @type {LeafDirective | Paragraph | TextDirective | undefined} */
284289
let label
285290

286291
if (node.type === 'containerDirective') {
@@ -424,7 +429,7 @@ function attributes(node, state) {
424429
}
425430

426431
/**
427-
* @param {BlockContent | DefinitionContent} node
432+
* @param {Nodes} node
428433
* @returns {node is Paragraph & {data: {directiveLabel: true}}}
429434
*/
430435
function inlineDirectiveLabel(node) {

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
"types": "index.d.ts",
3333
"files": [
3434
"lib/",
35-
"complex-types.d.ts",
3635
"index.d.ts",
3736
"index.js"
3837
],
3938
"dependencies": {
4039
"@types/mdast": "^4.0.0",
4140
"@types/unist": "^3.0.0",
41+
"devlop": "^1.0.0",
4242
"mdast-util-from-markdown": "^2.0.0",
4343
"mdast-util-to-markdown": "^2.0.0",
4444
"parse-entities": "^4.0.0",
@@ -85,6 +85,17 @@
8585
"strict": true
8686
},
8787
"xo": {
88+
"overrides": [
89+
{
90+
"files": [
91+
"**/*.ts"
92+
],
93+
"rules": {
94+
"@typescript-eslint/ban-types": "off",
95+
"@typescript-eslint/consistent-type-definitions": "off"
96+
}
97+
}
98+
],
8899
"prettier": true
89100
}
90101
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ import {visit} from 'unist-util-visit'
427427
/** @type {import('mdast').Root} */
428428
const tree = getMdastNodeSomeHow()
429429

430-
visit(tree, (node) => {
430+
visit(tree, function (node) {
431431
// `node` can now be one of the nodes for directives.
432432
})
433433
```

0 commit comments

Comments
 (0)