Refactor Token.parent() to Token.parent, add validation tests for parent, fix parent in htmlFlow scenario, improve type saftey slightly.

This commit is contained in:
David Anson 2023-10-19 23:01:31 -07:00
parent 06466905a5
commit 2a56f130c1
9 changed files with 73 additions and 372 deletions

View file

@ -87,6 +87,7 @@
"id-length": "off", "id-length": "off",
"indent": ["error", 2, { "SwitchCase": 1 }], "indent": ["error", 2, { "SwitchCase": 1 }],
"linebreak-style": "off", "linebreak-style": "off",
"lines-around-comment": "off",
"logical-assignment-operators": "off", "logical-assignment-operators": "off",
"max-depth": "off", "max-depth": "off",
"max-lines": "off", "max-lines": "off",

View file

@ -1219,14 +1219,7 @@ var flatTokensSymbol = Symbol("flat-tokens");
* @property {number} endColumn End column (1-based). * @property {number} endColumn End column (1-based).
* @property {string} text Token text. * @property {string} text Token text.
* @property {Token[]} children Child tokens. * @property {Token[]} children Child tokens.
* @property {GetTokenParent} parent Parent token. * @property {Token | null} parent Parent token.
*/
/**
* Returns parent Token of a Token.
*
* @typedef {Function} GetTokenParent
* @returns {Token} Parent token.
*/ */
/** /**
@ -1290,17 +1283,26 @@ function getMicromarkEvents(markdown) {
* @param {Object} micromarkOptions Options for micromark. * @param {Object} micromarkOptions Options for micromark.
* @param {boolean} referencesDefined Treat references as defined. * @param {boolean} referencesDefined Treat references as defined.
* @param {number} lineDelta Offset to apply to start/end line. * @param {number} lineDelta Offset to apply to start/end line.
* @param {Token} [ancestor] Parent of top-most tokens.
* @returns {Token[]} Micromark tokens (frozen). * @returns {Token[]} Micromark tokens (frozen).
*/ */
function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined, lineDelta) { function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined, lineDelta, ancestor) {
// Use micromark to parse document into Events // Use micromark to parse document into Events
var events = getMicromarkEvents(markdown, micromarkOptions, referencesDefined); var events = getMicromarkEvents(markdown, micromarkOptions, referencesDefined);
// Create Token objects // Create Token objects
var document = []; var document = [];
var flatTokens = []; var flatTokens = [];
/** @type {Token} */
var root = { var root = {
"children": document "type": "ROOT",
"startLine": -1,
"startColumn": -1,
"endLine": -1,
"endColumn": -1,
"text": "ROOT",
"children": document,
"parent": null
}; };
var history = [root]; var history = [root];
var current = root; var current = root;
@ -1310,7 +1312,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
var _iterator = _createForOfIteratorHelper(events), var _iterator = _createForOfIteratorHelper(events),
_step; _step;
try { try {
var _loop = function _loop() { for (_iterator.s(); !(_step = _iterator.n()).done;) {
var event = _step.value; var event = _step.value;
var _event = _slicedToArray(event, 3), var _event = _slicedToArray(event, 3),
kind = _event[0], kind = _event[0],
@ -1335,13 +1337,10 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
endColumn: endColumn, endColumn: endColumn,
text: text, text: text,
"children": [], "children": [],
"parent": function parent() { "parent": previous === root ? ancestor || null : previous
return previous === root ? null : previous;
}
}; };
previous.children.push(current); previous.children.push(current);
flatTokens.push(current); flatTokens.push(current);
// @ts-ignore
if (current.type === "htmlFlow" && !isHtmlFlowComment(current)) { if (current.type === "htmlFlow" && !isHtmlFlowComment(current)) {
skipHtmlFlowChildren = true; skipHtmlFlowChildren = true;
if (!reparseOptions || !lines) { if (!reparseOptions || !lines) {
@ -1355,7 +1354,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
lines = markdown.split(newLineRe); lines = markdown.split(newLineRe);
} }
var reparseMarkdown = lines.slice(current.startLine - 1, current.endLine).join("\n"); var reparseMarkdown = lines.slice(current.startLine - 1, current.endLine).join("\n");
var tokens = micromarkParseWithOffset(reparseMarkdown, reparseOptions, referencesDefined, current.startLine - 1); var tokens = micromarkParseWithOffset(reparseMarkdown, reparseOptions, referencesDefined, current.startLine - 1, current);
current.children = tokens; current.children = tokens;
// Avoid stack overflow of Array.push(...spread) // Avoid stack overflow of Array.push(...spread)
// eslint-disable-next-line unicorn/prefer-spread // eslint-disable-next-line unicorn/prefer-spread
@ -1372,9 +1371,6 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
current = history.pop(); current = history.pop();
} }
} }
};
for (_iterator.s(); !(_step = _iterator.n()).done;) {
_loop();
} }
// Return document // Return document
@ -3575,6 +3571,11 @@ var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
addErrorDetailIf = _require.addErrorDetailIf; addErrorDetailIf = _require.addErrorDetailIf;
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"), var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
filterByTypes = _require2.filterByTypes; filterByTypes = _require2.filterByTypes;
/**
* @typedef {import("../helpers/micromark.cjs").Token} Token
*/
module.exports = { module.exports = {
"names": ["MD007", "ul-indent"], "names": ["MD007", "ul-indent"],
"description": "Unordered list indentation", "description": "Unordered list indentation",
@ -3591,15 +3592,17 @@ module.exports = {
try { try {
for (_iterator.s(); !(_step = _iterator.n()).done;) { for (_iterator.s(); !(_step = _iterator.n()).done;) {
var token = _step.value; var token = _step.value;
var startColumn = token.startColumn, var parent = token.parent,
startColumn = token.startColumn,
startLine = token.startLine, startLine = token.startLine,
type = token.type; type = token.type;
if (type === "blockQuotePrefix") { if (type === "blockQuotePrefix") {
lastBlockQuotePrefix = token; lastBlockQuotePrefix = token;
} else if (type === "listUnordered") { } else if (type === "listUnordered") {
var nesting = 0; var nesting = 0;
/** @type {Token | null} */
var current = token; var current = token;
while (current = current.parent()) { while (current = current.parent) {
if (current.type === "listUnordered") { if (current.type === "listUnordered") {
nesting++; nesting++;
} else if (current.type === "listOrdered") { } else if (current.type === "listOrdered") {
@ -3614,7 +3617,7 @@ module.exports = {
} }
} else { } else {
// listItemPrefix // listItemPrefix
var _nesting = unorderedListNesting.get(token.parent()); var _nesting = unorderedListNesting.get(parent);
if (_nesting !== undefined) { if (_nesting !== undefined) {
var _lastBlockQuotePrefix; var _lastBlockQuotePrefix;
// listItemPrefix for listUnordered // listItemPrefix for listUnordered

View file

@ -23,14 +23,7 @@ const flatTokensSymbol = Symbol("flat-tokens");
* @property {number} endColumn End column (1-based). * @property {number} endColumn End column (1-based).
* @property {string} text Token text. * @property {string} text Token text.
* @property {Token[]} children Child tokens. * @property {Token[]} children Child tokens.
* @property {GetTokenParent} parent Parent token. * @property {Token | null} parent Parent token.
*/
/**
* Returns parent Token of a Token.
*
* @typedef {Function} GetTokenParent
* @returns {Token} Parent token.
*/ */
/** /**
@ -105,13 +98,15 @@ function getMicromarkEvents(
* @param {Object} micromarkOptions Options for micromark. * @param {Object} micromarkOptions Options for micromark.
* @param {boolean} referencesDefined Treat references as defined. * @param {boolean} referencesDefined Treat references as defined.
* @param {number} lineDelta Offset to apply to start/end line. * @param {number} lineDelta Offset to apply to start/end line.
* @param {Token} [ancestor] Parent of top-most tokens.
* @returns {Token[]} Micromark tokens (frozen). * @returns {Token[]} Micromark tokens (frozen).
*/ */
function micromarkParseWithOffset( function micromarkParseWithOffset(
markdown, markdown,
micromarkOptions, micromarkOptions,
referencesDefined, referencesDefined,
lineDelta lineDelta,
ancestor
) { ) {
// Use micromark to parse document into Events // Use micromark to parse document into Events
const events = getMicromarkEvents( const events = getMicromarkEvents(
@ -121,8 +116,16 @@ function micromarkParseWithOffset(
// Create Token objects // Create Token objects
const document = []; const document = [];
let flatTokens = []; let flatTokens = [];
/** @type {Token} */
const root = { const root = {
"children": document "type": "ROOT",
"startLine": -1,
"startColumn": -1,
"endLine": -1,
"endColumn": -1,
"text": "ROOT",
"children": document,
"parent": null
}; };
const history = [ root ]; const history = [ root ];
let current = root; let current = root;
@ -146,11 +149,10 @@ function micromarkParseWithOffset(
endColumn, endColumn,
text, text,
"children": [], "children": [],
"parent": () => (previous === root ? null : previous) "parent": ((previous === root) ? (ancestor || null) : previous)
}; };
previous.children.push(current); previous.children.push(current);
flatTokens.push(current); flatTokens.push(current);
// @ts-ignore
if ((current.type === "htmlFlow") && !isHtmlFlowComment(current)) { if ((current.type === "htmlFlow") && !isHtmlFlowComment(current)) {
skipHtmlFlowChildren = true; skipHtmlFlowChildren = true;
if (!reparseOptions || !lines) { if (!reparseOptions || !lines) {
@ -173,7 +175,8 @@ function micromarkParseWithOffset(
reparseMarkdown, reparseMarkdown,
reparseOptions, reparseOptions,
referencesDefined, referencesDefined,
current.startLine - 1 current.startLine - 1,
current
); );
current.children = tokens; current.children = tokens;
// Avoid stack overflow of Array.push(...spread) // Avoid stack overflow of Array.push(...spread)

View file

@ -5,6 +5,10 @@
const { addErrorDetailIf } = require("../helpers"); const { addErrorDetailIf } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs"); const { filterByTypes } = require("../helpers/micromark.cjs");
/**
* @typedef {import("../helpers/micromark.cjs").Token} Token
*/
module.exports = { module.exports = {
"names": [ "MD007", "ul-indent" ], "names": [ "MD007", "ul-indent" ],
"description": "Unordered list indentation", "description": "Unordered list indentation",
@ -20,13 +24,14 @@ module.exports = {
[ "blockQuotePrefix", "listItemPrefix", "listUnordered" ] [ "blockQuotePrefix", "listItemPrefix", "listUnordered" ]
); );
for (const token of tokens) { for (const token of tokens) {
const { startColumn, startLine, type } = token; const { parent, startColumn, startLine, type } = token;
if (type === "blockQuotePrefix") { if (type === "blockQuotePrefix") {
lastBlockQuotePrefix = token; lastBlockQuotePrefix = token;
} else if (type === "listUnordered") { } else if (type === "listUnordered") {
let nesting = 0; let nesting = 0;
/** @type {Token | null} */
let current = token; let current = token;
while ((current = current.parent())) { while ((current = current.parent)) {
if (current.type === "listUnordered") { if (current.type === "listUnordered") {
nesting++; nesting++;
} else if (current.type === "listOrdered") { } else if (current.type === "listOrdered") {
@ -41,7 +46,7 @@ module.exports = {
} }
} else { } else {
// listItemPrefix // listItemPrefix
const nesting = unorderedListNesting.get(token.parent()); const nesting = unorderedListNesting.get(parent);
if (nesting !== undefined) { if (nesting !== undefined) {
// listItemPrefix for listUnordered // listItemPrefix for listUnordered
const expectedIndent = const expectedIndent =

View file

@ -1489,7 +1489,11 @@ test("customRulesParamsAreFrozen", (t) => {
t.true(Object.isFrozen(current) || (current === params)); t.true(Object.isFrozen(current) || (current === params));
for (const name of Object.getOwnPropertyNames(current)) { for (const name of Object.getOwnPropertyNames(current)) {
const value = current[name]; const value = current[name];
if (value && (typeof value === "object")) { if (
value &&
(typeof value === "object") &&
(name !== "parent")
) {
pending.push(value); pending.push(value);
} }
} }

View file

@ -17,9 +17,26 @@ const testTokens = new Promise((resolve, reject) => {
testContent.then(parse).then(resolve, reject); testContent.then(parse).then(resolve, reject);
}); });
const cloneToken = (token) => {
for (const child of token.children) {
const expectedParent = (token.type ? token : null);
if (child.parent !== expectedParent) {
throw new Error("Unexpected parent.");
}
}
const clone = { ...token };
delete clone.parent;
clone.children = clone.children.map(cloneToken);
return clone;
};
const cloneTokens = (tokens) => (
cloneToken({ "children": tokens }).children
);
test("parse", async(t) => { test("parse", async(t) => {
t.plan(1); t.plan(1);
t.snapshot(await testTokens, "Unexpected tokens"); t.snapshot(cloneTokens(await testTokens), "Unexpected tokens");
}); });
test("getMicromarkEvents/filterByPredicate", async(t) => { test("getMicromarkEvents/filterByPredicate", async(t) => {

File diff suppressed because it is too large Load diff