Skip to content

Commit

Permalink
Change to replace getter/setters with raw data
Browse files Browse the repository at this point in the history
When you make extensions to this, get and set stuff on `this.data`
directly, instead of using `this.getData` and `this.setData`.
  • Loading branch information
wooorm committed Jul 9, 2023
1 parent 18f4bb0 commit 03581b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 71 deletions.
95 changes: 30 additions & 65 deletions dev/lib/index.js
Expand Up @@ -109,22 +109,20 @@
* Stack of nodes.
* @property {Array<TokenTuple>} tokenStack
* Stack of tokens.
* @property {<Key extends keyof CompileData>(key: Key) => CompileData[Key]} getData
* Get data from the key/value store.
* @property {<Key extends keyof CompileData>(key: Key, value?: CompileData[Key]) => undefined} setData
* Set data into the key/value store.
* @property {(this: CompileContext) => undefined} buffer
* Capture some of the output data.
* @property {(this: CompileContext) => string} resume
* Stop capturing and access the output data.
* @property {(this: CompileContext, node: Nodes, token: Token, onError?: OnEnterError) => undefined} enter
* Enter a token.
* Enter a node.
* @property {(this: CompileContext, token: Token, onError?: OnExitError) => undefined} exit
* Exit a token.
* Exit a node.
* @property {TokenizeContext['sliceSerialize']} sliceSerialize
* Get the string value of a token.
* @property {Config} config
* Configuration.
* @property {CompileData} data
* Info passed around; key/value store.
*
* @typedef FromMarkdownOptions
* Configuration for how to build mdast.
Expand All @@ -135,8 +133,6 @@
* Configuration.
*/

// To do: next major: remove setter/getter.

import {ok as assert} from 'devlop'
import {toString} from 'mdast-util-to-string'
import {parse, postprocess, preprocess} from 'micromark'
Expand Down Expand Up @@ -317,8 +313,7 @@ function compiler(options) {
exit,
buffer,
resume,
setData,
getData
data
}
/** @type {Array<number>} */
const listStack = []
Expand Down Expand Up @@ -538,36 +533,6 @@ function compiler(options) {
return length
}

/**
* Set data.
*
* @template {keyof CompileData} Key
* Field type.
* @param {Key} key
* Key of field.
* @param {CompileData[Key] | undefined} [value]
* New value.
* @returns {undefined}
* Nothing.
*/
function setData(key, value) {
data[key] = value
}

/**
* Get data.
*
* @template {keyof CompileData} Key
* Field type.
* @param {Key} key
* Key of field.
* @returns {CompileData[Key]}
* Value.
*/
function getData(key) {
return data[key]
}

/**
* Create an opener handle.
*
Expand Down Expand Up @@ -704,23 +669,23 @@ function compiler(options) {
* @type {Handle}
*/
function onenterlistordered() {
setData('expectingFirstListItemValue', true)
this.data.expectingFirstListItemValue = true
}

/**
* @this {CompileContext}
* @type {Handle}
*/
function onenterlistitemvalue(token) {
if (getData('expectingFirstListItemValue')) {
if (this.data.expectingFirstListItemValue) {
const ancestor = this.stack[this.stack.length - 2]
assert(ancestor, 'expected nodes on stack')
assert(ancestor.type === 'list', 'expected list on stack')
ancestor.start = Number.parseInt(
this.sliceSerialize(token),
constants.numericBaseDecimal
)
setData('expectingFirstListItemValue')
this.data.expectingFirstListItemValue = undefined
}
}

Expand Down Expand Up @@ -754,9 +719,9 @@ function compiler(options) {
*/
function onexitcodefencedfence() {
// Exit if this is the closing fence.
if (getData('flowCodeInside')) return
if (this.data.flowCodeInside) return
this.buffer()
setData('flowCodeInside', true)
this.data.flowCodeInside = true
}

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

node.value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
setData('flowCodeInside')
this.data.flowCodeInside = undefined
}

/**
Expand Down Expand Up @@ -859,7 +824,7 @@ function compiler(options) {
* @type {Handle}
*/
function onexitsetextheadingtext() {
setData('setextHeadingSlurpLineEnding', true)
this.data.setextHeadingSlurpLineEnding = true
}

/**
Expand All @@ -880,7 +845,7 @@ function compiler(options) {
* @type {Handle}
*/
function onexitsetextheading() {
setData('setextHeadingSlurpLineEnding')
this.data.setextHeadingSlurpLineEnding = undefined
}

/**
Expand Down Expand Up @@ -935,17 +900,17 @@ function compiler(options) {
assert(context, 'expected `node`')

// If we’re at a hard break, include the line ending in there.
if (getData('atHardBreak')) {
if (this.data.atHardBreak) {
assert('children' in context, 'expected `parent`')
const tail = context.children[context.children.length - 1]
assert(tail.position, 'expected tail to have a starting position')
tail.position.end = point(token.end)
setData('atHardBreak')
this.data.atHardBreak = undefined
return
}

if (
!getData('setextHeadingSlurpLineEnding') &&
!this.data.setextHeadingSlurpLineEnding &&
config.canContainEols.includes(context.type)
) {
onenterdata.call(this, token)
Expand All @@ -959,7 +924,7 @@ function compiler(options) {
*/

function onexithardbreak() {
setData('atHardBreak', true)
this.data.atHardBreak = true
}

/**
Expand Down Expand Up @@ -1018,9 +983,9 @@ function compiler(options) {
// These are used / cleaned here.

// To do: clean.
if (getData('inReference')) {
if (this.data.inReference) {
/** @type {ReferenceType} */
const referenceType = getData('referenceType') || 'shortcut'
const referenceType = this.data.referenceType || 'shortcut'

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

setData('referenceType')
this.data.referenceType = undefined
}

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

// To do: clean.
if (getData('inReference')) {
if (this.data.inReference) {
/** @type {ReferenceType} */
const referenceType = getData('referenceType') || 'shortcut'
const referenceType = this.data.referenceType || 'shortcut'

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

setData('referenceType')
this.data.referenceType = undefined
}

/**
Expand Down Expand Up @@ -1111,7 +1076,7 @@ function compiler(options) {
)

// Assume a reference.
setData('inReference', true)
this.data.inReference = true

if (node.type === 'link') {
/** @type {Array<PhrasingContent>} */
Expand Down Expand Up @@ -1161,7 +1126,7 @@ function compiler(options) {
*/

function onexitresource() {
setData('inReference')
this.data.inReference = undefined
}

/**
Expand All @@ -1170,7 +1135,7 @@ function compiler(options) {
*/

function onenterreference() {
setData('referenceType', 'collapsed')
this.data.referenceType = 'collapsed'
}

/**
Expand All @@ -1194,7 +1159,7 @@ function compiler(options) {
node.identifier = normalizeIdentifier(
this.sliceSerialize(token)
).toLowerCase()
setData('referenceType', 'full')
this.data.referenceType = 'full'
}

/**
Expand All @@ -1207,7 +1172,7 @@ function compiler(options) {
token.type === 'characterReferenceMarkerNumeric' ||
token.type === 'characterReferenceMarkerHexadecimal'
)
setData('characterReferenceType', token.type)
this.data.characterReferenceType = token.type
}

/**
Expand All @@ -1216,7 +1181,7 @@ function compiler(options) {
*/
function onexitcharacterreferencevalue(token) {
const data = this.sliceSerialize(token)
const type = getData('characterReferenceType')
const type = this.data.characterReferenceType
/** @type {string} */
let value

Expand All @@ -1227,7 +1192,7 @@ function compiler(options) {
? constants.numericBaseDecimal
: constants.numericBaseHexadecimal
)
setData('characterReferenceType')
this.data.characterReferenceType = undefined
} else {
const result = decodeNamedCharacterReference(data)
assert(result !== false, 'expected reference to decode')
Expand Down
10 changes: 4 additions & 6 deletions readme.md
Expand Up @@ -169,18 +169,16 @@ mdast compiler context (TypeScript type).
— stack of nodes
* `tokenStack` (`Array<[Token, OnEnterError | undefined]>`)
— stack of tokens
* `getData` (`(key: string) => unknown`)
— get data from the key/value store (see [`CompileData`][api-compile-data])
* `setData` (`(key: string, value?: unknown) => undefined`)
— set data into the key/value store (see [`CompileData`][api-compile-data])
* `data` ([`CompileData`][api-compile-data])
— info passed around; key/value store
* `buffer` (`() => undefined`)
— capture some of the output data
* `resume` (`() => string`)
— stop capturing and access the output data
* `enter` (`(node: Node, token: Token, onError?: OnEnterError) => undefined`)
— enter a token
— enter a node
* `exit` (`(token: Token, onError?: OnExitError) => undefined`)
— exit a token
— exit a node
* `sliceSerialize` (`(token: Token, expandTabs?: boolean) => string`)
— get the string value of a token
* `config` (`Required<Extension>`)
Expand Down

0 comments on commit 03581b3

Please sign in to comment.