mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 14:30:12 +01:00
Refactor to remove micromark helper getSiblingTokens for simpler code and better performance.
This commit is contained in:
parent
4436d84b55
commit
996d88a9b4
4 changed files with 88 additions and 140 deletions
|
|
@ -1473,28 +1473,6 @@ function filterByTypes(tokens, types) {
|
||||||
return filterByPredicate(tokens, predicate);
|
return filterByPredicate(tokens, predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets all sibling token groups for a list of Micromark tokens.
|
|
||||||
*
|
|
||||||
* @param {Token[]} tokens Micromark tokens.
|
|
||||||
* @returns {Token[][]} Sibling token groups.
|
|
||||||
*/
|
|
||||||
function getSiblingTokens(tokens) {
|
|
||||||
const result = [];
|
|
||||||
const queue = [ tokens ];
|
|
||||||
// eslint-disable-next-line init-declarations
|
|
||||||
let current;
|
|
||||||
while ((current = queue.shift())) {
|
|
||||||
result.push(current);
|
|
||||||
for (const token of current) {
|
|
||||||
if (token.children.length > 0) {
|
|
||||||
queue.push(token.children);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the heading level of a Micromark heading tokan.
|
* Gets the heading level of a Micromark heading tokan.
|
||||||
*
|
*
|
||||||
|
|
@ -1619,7 +1597,6 @@ module.exports = {
|
||||||
getHeadingLevel,
|
getHeadingLevel,
|
||||||
getHtmlTagInfo,
|
getHtmlTagInfo,
|
||||||
getMicromarkEvents,
|
getMicromarkEvents,
|
||||||
getSiblingTokens,
|
|
||||||
getTokenParentOfType,
|
getTokenParentOfType,
|
||||||
getTokenTextByType,
|
getTokenTextByType,
|
||||||
inHtmlFlow,
|
inHtmlFlow,
|
||||||
|
|
@ -4544,35 +4521,32 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { getSiblingTokens } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": ["MD027", "no-multiple-space-blockquote"],
|
"names": ["MD027", "no-multiple-space-blockquote"],
|
||||||
"description": "Multiple spaces after blockquote symbol",
|
"description": "Multiple spaces after blockquote symbol",
|
||||||
"tags": ["blockquote", "whitespace", "indentation"],
|
"tags": ["blockquote", "whitespace", "indentation"],
|
||||||
"function": function MD027(params, onError) {
|
"function": function MD027(params, onError) {
|
||||||
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
|
const { tokens } = params.parsers.micromark;
|
||||||
let previousType = null;
|
for (const token of filterByTypes(tokens, [ "linePrefix" ])) {
|
||||||
for (const token of siblings) {
|
const siblings = token.parent?.children || tokens;
|
||||||
const { type } = token;
|
if (siblings[siblings.indexOf(token) - 1]?.type === "blockQuotePrefix") {
|
||||||
if ((type === "linePrefix") && (previousType === "blockQuotePrefix")) {
|
const { startColumn, startLine, text } = token;
|
||||||
const { startColumn, startLine, text } = token;
|
const { length } = text;
|
||||||
const { length } = text;
|
const line = params.lines[startLine - 1];
|
||||||
const line = params.lines[startLine - 1];
|
addErrorContext(
|
||||||
addErrorContext(
|
onError,
|
||||||
onError,
|
startLine,
|
||||||
startLine,
|
line,
|
||||||
line,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
[ startColumn, length ],
|
||||||
[ startColumn, length ],
|
{
|
||||||
{
|
"editColumn": startColumn,
|
||||||
"editColumn": startColumn,
|
"deleteCount": length
|
||||||
"deleteCount": length
|
}
|
||||||
}
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
previousType = type;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4593,36 +4567,36 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
const { addError } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addError } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { getSiblingTokens } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
|
|
||||||
|
const ignoreTypes = new Set([ "lineEnding", "listItemIndent", "linePrefix" ]);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD028", "no-blanks-blockquote" ],
|
"names": [ "MD028", "no-blanks-blockquote" ],
|
||||||
"description": "Blank line inside blockquote",
|
"description": "Blank line inside blockquote",
|
||||||
"tags": [ "blockquote", "whitespace" ],
|
"tags": [ "blockquote", "whitespace" ],
|
||||||
"function": function MD028(params, onError) {
|
"function": function MD028(params, onError) {
|
||||||
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
|
const { tokens } = params.parsers.micromark;
|
||||||
let errorLineNumbers = null;
|
for (const token of filterByTypes(tokens, [ "blockQuote" ])) {
|
||||||
for (const sibling of siblings) {
|
const errorLineNumbers = [];
|
||||||
switch (sibling.type) {
|
const siblings = token.parent?.children || tokens;
|
||||||
case "blockQuote":
|
for (let i = siblings.indexOf(token) + 1; i < siblings.length; i++) {
|
||||||
for (const lineNumber of (errorLineNumbers || [])) {
|
const sibling = siblings[i];
|
||||||
addError(onError, lineNumber);
|
const { startLine, type } = sibling;
|
||||||
}
|
if (type === "lineEndingBlank") {
|
||||||
errorLineNumbers = [];
|
// Possible blank between blockquotes
|
||||||
break;
|
errorLineNumbers.push(startLine);
|
||||||
case "lineEnding":
|
} else if (ignoreTypes.has(type)) {
|
||||||
case "linePrefix":
|
// Ignore invisible formatting
|
||||||
case "listItemIndent":
|
} else if (type === "blockQuote") {
|
||||||
// Ignore
|
// Blockquote followed by blockquote
|
||||||
break;
|
for (const lineNumber of errorLineNumbers) {
|
||||||
case "lineEndingBlank":
|
addError(onError, lineNumber);
|
||||||
if (errorLineNumbers) {
|
}
|
||||||
errorLineNumbers.push(sibling.startLine);
|
break;
|
||||||
}
|
} else {
|
||||||
break;
|
// Blockquote not followed by blockquote
|
||||||
default:
|
break;
|
||||||
errorLineNumbers = null;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -295,28 +295,6 @@ function filterByTypes(tokens, types) {
|
||||||
return filterByPredicate(tokens, predicate);
|
return filterByPredicate(tokens, predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets all sibling token groups for a list of Micromark tokens.
|
|
||||||
*
|
|
||||||
* @param {Token[]} tokens Micromark tokens.
|
|
||||||
* @returns {Token[][]} Sibling token groups.
|
|
||||||
*/
|
|
||||||
function getSiblingTokens(tokens) {
|
|
||||||
const result = [];
|
|
||||||
const queue = [ tokens ];
|
|
||||||
// eslint-disable-next-line init-declarations
|
|
||||||
let current;
|
|
||||||
while ((current = queue.shift())) {
|
|
||||||
result.push(current);
|
|
||||||
for (const token of current) {
|
|
||||||
if (token.children.length > 0) {
|
|
||||||
queue.push(token.children);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the heading level of a Micromark heading tokan.
|
* Gets the heading level of a Micromark heading tokan.
|
||||||
*
|
*
|
||||||
|
|
@ -441,7 +419,6 @@ module.exports = {
|
||||||
getHeadingLevel,
|
getHeadingLevel,
|
||||||
getHtmlTagInfo,
|
getHtmlTagInfo,
|
||||||
getMicromarkEvents,
|
getMicromarkEvents,
|
||||||
getSiblingTokens,
|
|
||||||
getTokenParentOfType,
|
getTokenParentOfType,
|
||||||
getTokenTextByType,
|
getTokenTextByType,
|
||||||
inHtmlFlow,
|
inHtmlFlow,
|
||||||
|
|
|
||||||
43
lib/md027.js
43
lib/md027.js
|
|
@ -3,35 +3,32 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorContext } = require("../helpers");
|
const { addErrorContext } = require("../helpers");
|
||||||
const { getSiblingTokens } = require("../helpers/micromark.cjs");
|
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": ["MD027", "no-multiple-space-blockquote"],
|
"names": ["MD027", "no-multiple-space-blockquote"],
|
||||||
"description": "Multiple spaces after blockquote symbol",
|
"description": "Multiple spaces after blockquote symbol",
|
||||||
"tags": ["blockquote", "whitespace", "indentation"],
|
"tags": ["blockquote", "whitespace", "indentation"],
|
||||||
"function": function MD027(params, onError) {
|
"function": function MD027(params, onError) {
|
||||||
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
|
const { tokens } = params.parsers.micromark;
|
||||||
let previousType = null;
|
for (const token of filterByTypes(tokens, [ "linePrefix" ])) {
|
||||||
for (const token of siblings) {
|
const siblings = token.parent?.children || tokens;
|
||||||
const { type } = token;
|
if (siblings[siblings.indexOf(token) - 1]?.type === "blockQuotePrefix") {
|
||||||
if ((type === "linePrefix") && (previousType === "blockQuotePrefix")) {
|
const { startColumn, startLine, text } = token;
|
||||||
const { startColumn, startLine, text } = token;
|
const { length } = text;
|
||||||
const { length } = text;
|
const line = params.lines[startLine - 1];
|
||||||
const line = params.lines[startLine - 1];
|
addErrorContext(
|
||||||
addErrorContext(
|
onError,
|
||||||
onError,
|
startLine,
|
||||||
startLine,
|
line,
|
||||||
line,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
[ startColumn, length ],
|
||||||
[ startColumn, length ],
|
{
|
||||||
{
|
"editColumn": startColumn,
|
||||||
"editColumn": startColumn,
|
"deleteCount": length
|
||||||
"deleteCount": length
|
}
|
||||||
}
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
previousType = type;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
48
lib/md028.js
48
lib/md028.js
|
|
@ -3,36 +3,36 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError } = require("../helpers");
|
const { addError } = require("../helpers");
|
||||||
const { getSiblingTokens } = require("../helpers/micromark.cjs");
|
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||||
|
|
||||||
|
const ignoreTypes = new Set([ "lineEnding", "listItemIndent", "linePrefix" ]);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD028", "no-blanks-blockquote" ],
|
"names": [ "MD028", "no-blanks-blockquote" ],
|
||||||
"description": "Blank line inside blockquote",
|
"description": "Blank line inside blockquote",
|
||||||
"tags": [ "blockquote", "whitespace" ],
|
"tags": [ "blockquote", "whitespace" ],
|
||||||
"function": function MD028(params, onError) {
|
"function": function MD028(params, onError) {
|
||||||
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
|
const { tokens } = params.parsers.micromark;
|
||||||
let errorLineNumbers = null;
|
for (const token of filterByTypes(tokens, [ "blockQuote" ])) {
|
||||||
for (const sibling of siblings) {
|
const errorLineNumbers = [];
|
||||||
switch (sibling.type) {
|
const siblings = token.parent?.children || tokens;
|
||||||
case "blockQuote":
|
for (let i = siblings.indexOf(token) + 1; i < siblings.length; i++) {
|
||||||
for (const lineNumber of (errorLineNumbers || [])) {
|
const sibling = siblings[i];
|
||||||
addError(onError, lineNumber);
|
const { startLine, type } = sibling;
|
||||||
}
|
if (type === "lineEndingBlank") {
|
||||||
errorLineNumbers = [];
|
// Possible blank between blockquotes
|
||||||
break;
|
errorLineNumbers.push(startLine);
|
||||||
case "lineEnding":
|
} else if (ignoreTypes.has(type)) {
|
||||||
case "linePrefix":
|
// Ignore invisible formatting
|
||||||
case "listItemIndent":
|
} else if (type === "blockQuote") {
|
||||||
// Ignore
|
// Blockquote followed by blockquote
|
||||||
break;
|
for (const lineNumber of errorLineNumbers) {
|
||||||
case "lineEndingBlank":
|
addError(onError, lineNumber);
|
||||||
if (errorLineNumbers) {
|
}
|
||||||
errorLineNumbers.push(sibling.startLine);
|
break;
|
||||||
}
|
} else {
|
||||||
break;
|
// Blockquote not followed by blockquote
|
||||||
default:
|
break;
|
||||||
errorLineNumbers = null;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue