From 03581b3ede92d3874a6689a96b8ae0a7c17b86af Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sun, 9 Jul 2023 11:56:47 +0200 Subject: [PATCH] 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`. --- dev/lib/index.js | 95 +++++++++++++++--------------------------------- readme.md | 10 ++--- 2 files changed, 34 insertions(+), 71 deletions(-) diff --git a/dev/lib/index.js b/dev/lib/index.js index 12d213f..df7f81b 100644 --- a/dev/lib/index.js +++ b/dev/lib/index.js @@ -109,22 +109,20 @@ * Stack of nodes. * @property {Array} tokenStack * Stack of tokens. - * @property {(key: Key) => CompileData[Key]} getData - * Get data from the key/value store. - * @property {(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. @@ -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' @@ -317,8 +313,7 @@ function compiler(options) { exit, buffer, resume, - setData, - getData + data } /** @type {Array} */ const listStack = [] @@ -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. * @@ -704,7 +669,7 @@ function compiler(options) { * @type {Handle} */ function onenterlistordered() { - setData('expectingFirstListItemValue', true) + this.data.expectingFirstListItemValue = true } /** @@ -712,7 +677,7 @@ function compiler(options) { * @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') @@ -720,7 +685,7 @@ function compiler(options) { this.sliceSerialize(token), constants.numericBaseDecimal ) - setData('expectingFirstListItemValue') + this.data.expectingFirstListItemValue = undefined } } @@ -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 } /** @@ -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 } /** @@ -859,7 +824,7 @@ function compiler(options) { * @type {Handle} */ function onexitsetextheadingtext() { - setData('setextHeadingSlurpLineEnding', true) + this.data.setextHeadingSlurpLineEnding = true } /** @@ -880,7 +845,7 @@ function compiler(options) { * @type {Handle} */ function onexitsetextheading() { - setData('setextHeadingSlurpLineEnding') + this.data.setextHeadingSlurpLineEnding = undefined } /** @@ -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) @@ -959,7 +924,7 @@ function compiler(options) { */ function onexithardbreak() { - setData('atHardBreak', true) + this.data.atHardBreak = true } /** @@ -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. @@ -1035,7 +1000,7 @@ function compiler(options) { delete node.label } - setData('referenceType') + this.data.referenceType = undefined } /** @@ -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. @@ -1069,7 +1034,7 @@ function compiler(options) { delete node.label } - setData('referenceType') + this.data.referenceType = undefined } /** @@ -1111,7 +1076,7 @@ function compiler(options) { ) // Assume a reference. - setData('inReference', true) + this.data.inReference = true if (node.type === 'link') { /** @type {Array} */ @@ -1161,7 +1126,7 @@ function compiler(options) { */ function onexitresource() { - setData('inReference') + this.data.inReference = undefined } /** @@ -1170,7 +1135,7 @@ function compiler(options) { */ function onenterreference() { - setData('referenceType', 'collapsed') + this.data.referenceType = 'collapsed' } /** @@ -1194,7 +1159,7 @@ function compiler(options) { node.identifier = normalizeIdentifier( this.sliceSerialize(token) ).toLowerCase() - setData('referenceType', 'full') + this.data.referenceType = 'full' } /** @@ -1207,7 +1172,7 @@ function compiler(options) { token.type === 'characterReferenceMarkerNumeric' || token.type === 'characterReferenceMarkerHexadecimal' ) - setData('characterReferenceType', token.type) + this.data.characterReferenceType = token.type } /** @@ -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 @@ -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') diff --git a/readme.md b/readme.md index 00ad14e..1a1e28e 100644 --- a/readme.md +++ b/readme.md @@ -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`)