Skip to content

Commit 03581b3

Browse files
committed
Change to replace getter/setters with raw data
When you make extensions to this, get and set stuff on `this.data` directly, instead of using `this.getData` and `this.setData`.
1 parent 18f4bb0 commit 03581b3

File tree

2 files changed

+34
-71
lines changed

2 files changed

+34
-71
lines changed

dev/lib/index.js

Lines changed: 30 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,20 @@
109109
* Stack of nodes.
110110
* @property {Array<TokenTuple>} tokenStack
111111
* Stack of tokens.
112-
* @property {<Key extends keyof CompileData>(key: Key) => CompileData[Key]} getData
113-
* Get data from the key/value store.
114-
* @property {<Key extends keyof CompileData>(key: Key, value?: CompileData[Key]) => undefined} setData
115-
* Set data into the key/value store.
116112
* @property {(this: CompileContext) => undefined} buffer
117113
* Capture some of the output data.
118114
* @property {(this: CompileContext) => string} resume
119115
* Stop capturing and access the output data.
120116
* @property {(this: CompileContext, node: Nodes, token: Token, onError?: OnEnterError) => undefined} enter
121-
* Enter a token.
117+
* Enter a node.
122118
* @property {(this: CompileContext, token: Token, onError?: OnExitError) => undefined} exit
123-
* Exit a token.
119+
* Exit a node.
124120
* @property {TokenizeContext['sliceSerialize']} sliceSerialize
125121
* Get the string value of a token.
126122
* @property {Config} config
127123
* Configuration.
124+
* @property {CompileData} data
125+
* Info passed around; key/value store.
128126
*
129127
* @typedef FromMarkdownOptions
130128
* Configuration for how to build mdast.
@@ -135,8 +133,6 @@
135133
* Configuration.
136134
*/
137135

138-
// To do: next major: remove setter/getter.
139-
140136
import {ok as assert} from 'devlop'
141137
import {toString} from 'mdast-util-to-string'
142138
import {parse, postprocess, preprocess} from 'micromark'
@@ -317,8 +313,7 @@ function compiler(options) {
317313
exit,
318314
buffer,
319315
resume,
320-
setData,
321-
getData
316+
data
322317
}
323318
/** @type {Array<number>} */
324319
const listStack = []
@@ -538,36 +533,6 @@ function compiler(options) {
538533
return length
539534
}
540535

541-
/**
542-
* Set data.
543-
*
544-
* @template {keyof CompileData} Key
545-
* Field type.
546-
* @param {Key} key
547-
* Key of field.
548-
* @param {CompileData[Key] | undefined} [value]
549-
* New value.
550-
* @returns {undefined}
551-
* Nothing.
552-
*/
553-
function setData(key, value) {
554-
data[key] = value
555-
}
556-
557-
/**
558-
* Get data.
559-
*
560-
* @template {keyof CompileData} Key
561-
* Field type.
562-
* @param {Key} key
563-
* Key of field.
564-
* @returns {CompileData[Key]}
565-
* Value.
566-
*/
567-
function getData(key) {
568-
return data[key]
569-
}
570-
571536
/**
572537
* Create an opener handle.
573538
*
@@ -704,23 +669,23 @@ function compiler(options) {
704669
* @type {Handle}
705670
*/
706671
function onenterlistordered() {
707-
setData('expectingFirstListItemValue', true)
672+
this.data.expectingFirstListItemValue = true
708673
}
709674

710675
/**
711676
* @this {CompileContext}
712677
* @type {Handle}
713678
*/
714679
function onenterlistitemvalue(token) {
715-
if (getData('expectingFirstListItemValue')) {
680+
if (this.data.expectingFirstListItemValue) {
716681
const ancestor = this.stack[this.stack.length - 2]
717682
assert(ancestor, 'expected nodes on stack')
718683
assert(ancestor.type === 'list', 'expected list on stack')
719684
ancestor.start = Number.parseInt(
720685
this.sliceSerialize(token),
721686
constants.numericBaseDecimal
722687
)
723-
setData('expectingFirstListItemValue')
688+
this.data.expectingFirstListItemValue = undefined
724689
}
725690
}
726691

@@ -754,9 +719,9 @@ function compiler(options) {
754719
*/
755720
function onexitcodefencedfence() {
756721
// Exit if this is the closing fence.
757-
if (getData('flowCodeInside')) return
722+
if (this.data.flowCodeInside) return
758723
this.buffer()
759-
setData('flowCodeInside', true)
724+
this.data.flowCodeInside = true
760725
}
761726

762727
/**
@@ -770,7 +735,7 @@ function compiler(options) {
770735
assert(node.type === 'code', 'expected code on stack')
771736

772737
node.value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
773-
setData('flowCodeInside')
738+
this.data.flowCodeInside = undefined
774739
}
775740

776741
/**
@@ -859,7 +824,7 @@ function compiler(options) {
859824
* @type {Handle}
860825
*/
861826
function onexitsetextheadingtext() {
862-
setData('setextHeadingSlurpLineEnding', true)
827+
this.data.setextHeadingSlurpLineEnding = true
863828
}
864829

865830
/**
@@ -880,7 +845,7 @@ function compiler(options) {
880845
* @type {Handle}
881846
*/
882847
function onexitsetextheading() {
883-
setData('setextHeadingSlurpLineEnding')
848+
this.data.setextHeadingSlurpLineEnding = undefined
884849
}
885850

886851
/**
@@ -935,17 +900,17 @@ function compiler(options) {
935900
assert(context, 'expected `node`')
936901

937902
// If we’re at a hard break, include the line ending in there.
938-
if (getData('atHardBreak')) {
903+
if (this.data.atHardBreak) {
939904
assert('children' in context, 'expected `parent`')
940905
const tail = context.children[context.children.length - 1]
941906
assert(tail.position, 'expected tail to have a starting position')
942907
tail.position.end = point(token.end)
943-
setData('atHardBreak')
908+
this.data.atHardBreak = undefined
944909
return
945910
}
946911

947912
if (
948-
!getData('setextHeadingSlurpLineEnding') &&
913+
!this.data.setextHeadingSlurpLineEnding &&
949914
config.canContainEols.includes(context.type)
950915
) {
951916
onenterdata.call(this, token)
@@ -959,7 +924,7 @@ function compiler(options) {
959924
*/
960925

961926
function onexithardbreak() {
962-
setData('atHardBreak', true)
927+
this.data.atHardBreak = true
963928
}
964929

965930
/**
@@ -1018,9 +983,9 @@ function compiler(options) {
1018983
// These are used / cleaned here.
1019984

1020985
// To do: clean.
1021-
if (getData('inReference')) {
986+
if (this.data.inReference) {
1022987
/** @type {ReferenceType} */
1023-
const referenceType = getData('referenceType') || 'shortcut'
988+
const referenceType = this.data.referenceType || 'shortcut'
1024989

1025990
node.type += 'Reference'
1026991
// @ts-expect-error: mutate.
@@ -1035,7 +1000,7 @@ function compiler(options) {
10351000
delete node.label
10361001
}
10371002

1038-
setData('referenceType')
1003+
this.data.referenceType = undefined
10391004
}
10401005

10411006
/**
@@ -1052,9 +1017,9 @@ function compiler(options) {
10521017
// These are used / cleaned here.
10531018

10541019
// To do: clean.
1055-
if (getData('inReference')) {
1020+
if (this.data.inReference) {
10561021
/** @type {ReferenceType} */
1057-
const referenceType = getData('referenceType') || 'shortcut'
1022+
const referenceType = this.data.referenceType || 'shortcut'
10581023

10591024
node.type += 'Reference'
10601025
// @ts-expect-error: mutate.
@@ -1069,7 +1034,7 @@ function compiler(options) {
10691034
delete node.label
10701035
}
10711036

1072-
setData('referenceType')
1037+
this.data.referenceType = undefined
10731038
}
10741039

10751040
/**
@@ -1111,7 +1076,7 @@ function compiler(options) {
11111076
)
11121077

11131078
// Assume a reference.
1114-
setData('inReference', true)
1079+
this.data.inReference = true
11151080

11161081
if (node.type === 'link') {
11171082
/** @type {Array<PhrasingContent>} */
@@ -1161,7 +1126,7 @@ function compiler(options) {
11611126
*/
11621127

11631128
function onexitresource() {
1164-
setData('inReference')
1129+
this.data.inReference = undefined
11651130
}
11661131

11671132
/**
@@ -1170,7 +1135,7 @@ function compiler(options) {
11701135
*/
11711136

11721137
function onenterreference() {
1173-
setData('referenceType', 'collapsed')
1138+
this.data.referenceType = 'collapsed'
11741139
}
11751140

11761141
/**
@@ -1194,7 +1159,7 @@ function compiler(options) {
11941159
node.identifier = normalizeIdentifier(
11951160
this.sliceSerialize(token)
11961161
).toLowerCase()
1197-
setData('referenceType', 'full')
1162+
this.data.referenceType = 'full'
11981163
}
11991164

12001165
/**
@@ -1207,7 +1172,7 @@ function compiler(options) {
12071172
token.type === 'characterReferenceMarkerNumeric' ||
12081173
token.type === 'characterReferenceMarkerHexadecimal'
12091174
)
1210-
setData('characterReferenceType', token.type)
1175+
this.data.characterReferenceType = token.type
12111176
}
12121177

12131178
/**
@@ -1216,7 +1181,7 @@ function compiler(options) {
12161181
*/
12171182
function onexitcharacterreferencevalue(token) {
12181183
const data = this.sliceSerialize(token)
1219-
const type = getData('characterReferenceType')
1184+
const type = this.data.characterReferenceType
12201185
/** @type {string} */
12211186
let value
12221187

@@ -1227,7 +1192,7 @@ function compiler(options) {
12271192
? constants.numericBaseDecimal
12281193
: constants.numericBaseHexadecimal
12291194
)
1230-
setData('characterReferenceType')
1195+
this.data.characterReferenceType = undefined
12311196
} else {
12321197
const result = decodeNamedCharacterReference(data)
12331198
assert(result !== false, 'expected reference to decode')

readme.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,16 @@ mdast compiler context (TypeScript type).
169169
— stack of nodes
170170
* `tokenStack` (`Array<[Token, OnEnterError | undefined]>`)
171171
— stack of tokens
172-
* `getData` (`(key: string) => unknown`)
173-
— get data from the key/value store (see [`CompileData`][api-compile-data])
174-
* `setData` (`(key: string, value?: unknown) => undefined`)
175-
— set data into the key/value store (see [`CompileData`][api-compile-data])
172+
* `data` ([`CompileData`][api-compile-data])
173+
— info passed around; key/value store
176174
* `buffer` (`() => undefined`)
177175
— capture some of the output data
178176
* `resume` (`() => string`)
179177
— stop capturing and access the output data
180178
* `enter` (`(node: Node, token: Token, onError?: OnEnterError) => undefined`)
181-
— enter a token
179+
— enter a node
182180
* `exit` (`(token: Token, onError?: OnExitError) => undefined`)
183-
— exit a token
181+
— exit a node
184182
* `sliceSerialize` (`(token: Token, expandTabs?: boolean) => string`)
185183
— get the string value of a token
186184
* `config` (`Required<Extension>`)

0 commit comments

Comments
 (0)