mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Refactor micromark helper getTokenTextByType to be more efficient, remove tokenIfType helper for being redundant.
This commit is contained in:
parent
b749d9fbb4
commit
2ea3f95fd1
6 changed files with 82 additions and 120 deletions
|
|
@ -1352,8 +1352,12 @@ function getTokenParentOfType(token, types) {
|
||||||
* @returns {string | null} Text of token.
|
* @returns {string | null} Text of token.
|
||||||
*/
|
*/
|
||||||
function getTokenTextByType(tokens, type) {
|
function getTokenTextByType(tokens, type) {
|
||||||
const filtered = tokens.filter((token) => token.type === type);
|
for (const token of tokens) {
|
||||||
return (filtered.length > 0) ? filtered[0].text : null;
|
if (token.type === type) {
|
||||||
|
return token.text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1381,17 +1385,6 @@ function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the specified token iff it is of the desired type.
|
|
||||||
*
|
|
||||||
* @param {Token} token Micromark token candidate.
|
|
||||||
* @param {TokenType} type Desired type.
|
|
||||||
* @returns {Token | null} Token instance.
|
|
||||||
*/
|
|
||||||
function tokenIfType(token, type) {
|
|
||||||
return (token && (token.type === type)) ? token : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set containing token types that do not contain content.
|
* Set containing token types that do not contain content.
|
||||||
*
|
*
|
||||||
|
|
@ -1424,8 +1417,7 @@ module.exports = {
|
||||||
inHtmlFlow,
|
inHtmlFlow,
|
||||||
isHtmlFlowComment,
|
isHtmlFlowComment,
|
||||||
matchAndGetTokensByType,
|
matchAndGetTokensByType,
|
||||||
nonContentTokens,
|
nonContentTokens
|
||||||
tokenIfType
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -5448,7 +5440,7 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { tokenIfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { getDescendantsByType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
||||||
|
|
||||||
const leftSpaceRe = /^\s(?:[^`]|$)/;
|
const leftSpaceRe = /^\s(?:[^`]|$)/;
|
||||||
|
|
@ -5474,17 +5466,12 @@ module.exports = {
|
||||||
"function": function MD038(params, onError) {
|
"function": function MD038(params, onError) {
|
||||||
const codeTexts = filterByTypesCached([ "codeText" ]);
|
const codeTexts = filterByTypesCached([ "codeText" ]);
|
||||||
for (const codeText of codeTexts) {
|
for (const codeText of codeTexts) {
|
||||||
const { children } = codeText;
|
const sequences = getDescendantsByType(codeText, [ "codeTextSequence" ]);
|
||||||
const first = 0;
|
const startSequence = sequences[0];
|
||||||
const last = children.length - 1;
|
const endSequence = sequences[sequences.length - 1];
|
||||||
const startSequence = tokenIfType(children[first], "codeTextSequence");
|
const datas = getDescendantsByType(codeText, [ "codeTextData" ]);
|
||||||
const endSequence = tokenIfType(children[last], "codeTextSequence");
|
const startData = datas[0];
|
||||||
const startData =
|
const endData = datas[datas.length - 1];
|
||||||
tokenIfType(children[first + 1], "codeTextData") ||
|
|
||||||
tokenIfType(children[first + 2], "codeTextData");
|
|
||||||
const endData =
|
|
||||||
tokenIfType(children[last - 1], "codeTextData") ||
|
|
||||||
tokenIfType(children[last - 2], "codeTextData");
|
|
||||||
if (startSequence && endSequence && startData && endData) {
|
if (startSequence && endSequence && startData && endData) {
|
||||||
const spaceLeft = leftSpaceRe.test(startData.text);
|
const spaceLeft = leftSpaceRe.test(startData.text);
|
||||||
const spaceRight = rightSpaceRe.test(endData.text);
|
const spaceRight = rightSpaceRe.test(endData.text);
|
||||||
|
|
@ -5655,7 +5642,7 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
const { addError, addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addError, addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { getTokenTextByType, tokenIfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { getDescendantsByType, getTokenTextByType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
|
|
@ -5671,8 +5658,7 @@ module.exports = {
|
||||||
const languageOnly = !!params.config.language_only;
|
const languageOnly = !!params.config.language_only;
|
||||||
const fencedCodes = filterByTypesCached([ "codeFenced" ]);
|
const fencedCodes = filterByTypesCached([ "codeFenced" ]);
|
||||||
for (const fencedCode of fencedCodes) {
|
for (const fencedCode of fencedCodes) {
|
||||||
const openingFence = tokenIfType(fencedCode.children[0], "codeFencedFence");
|
const openingFence = getDescendantsByType(fencedCode, [ "codeFencedFence" ])[0];
|
||||||
if (openingFence) {
|
|
||||||
const { children, startLine, text } = openingFence;
|
const { children, startLine, text } = openingFence;
|
||||||
const info = getTokenTextByType(children, "codeFencedFenceInfo");
|
const info = getTokenTextByType(children, "codeFencedFenceInfo");
|
||||||
if (!info) {
|
if (!info) {
|
||||||
|
|
@ -5685,7 +5671,6 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -6190,7 +6175,7 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
const { addErrorDetailIf, fencedCodeBlockStyleFor } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addErrorDetailIf, fencedCodeBlockStyleFor } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { tokenIfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { getDescendantsByType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
|
|
@ -6205,10 +6190,8 @@ module.exports = {
|
||||||
let expectedStyle = style;
|
let expectedStyle = style;
|
||||||
const codeFenceds = filterByTypesCached([ "codeFenced" ]);
|
const codeFenceds = filterByTypesCached([ "codeFenced" ]);
|
||||||
for (const codeFenced of codeFenceds) {
|
for (const codeFenced of codeFenceds) {
|
||||||
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
|
const codeFencedFenceSequence =
|
||||||
if (codeFencedFence) {
|
getDescendantsByType(codeFenced, [ "codeFencedFence", "codeFencedFenceSequence" ])[0];
|
||||||
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
|
|
||||||
if (codeFencedFenceSequence) {
|
|
||||||
const { startLine, text } = codeFencedFenceSequence;
|
const { startLine, text } = codeFencedFenceSequence;
|
||||||
if (expectedStyle === "consistent") {
|
if (expectedStyle === "consistent") {
|
||||||
expectedStyle = fencedCodeBlockStyleFor(text);
|
expectedStyle = fencedCodeBlockStyleFor(text);
|
||||||
|
|
@ -6221,8 +6204,6 @@ module.exports = {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -6240,7 +6221,7 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
const { addError, emphasisOrStrongStyleFor } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addError, emphasisOrStrongStyleFor } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { filterByPredicate, tokenIfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { filterByPredicate, getDescendantsByType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
|
|
||||||
const intrawordRe = /^\w$/;
|
const intrawordRe = /^\w$/;
|
||||||
|
|
||||||
|
|
@ -6262,9 +6243,9 @@ const impl =
|
||||||
(token) => ((token.type === "htmlFlow") ? [] : token.children)
|
(token) => ((token.type === "htmlFlow") ? [] : token.children)
|
||||||
);
|
);
|
||||||
for (const token of emphasisTokens) {
|
for (const token of emphasisTokens) {
|
||||||
const { children } = token;
|
const sequences = getDescendantsByType(token, [ typeSequence ]);
|
||||||
const startSequence = tokenIfType(children[0], typeSequence);
|
const startSequence = sequences[0];
|
||||||
const endSequence = tokenIfType(children[children.length - 1], typeSequence);
|
const endSequence = sequences[sequences.length - 1];
|
||||||
if (startSequence && endSequence) {
|
if (startSequence && endSequence) {
|
||||||
const markupStyle = emphasisOrStrongStyleFor(startSequence.text);
|
const markupStyle = emphasisOrStrongStyleFor(startSequence.text);
|
||||||
if (style === "consistent") {
|
if (style === "consistent") {
|
||||||
|
|
|
||||||
|
|
@ -467,8 +467,12 @@ function getTokenParentOfType(token, types) {
|
||||||
* @returns {string | null} Text of token.
|
* @returns {string | null} Text of token.
|
||||||
*/
|
*/
|
||||||
function getTokenTextByType(tokens, type) {
|
function getTokenTextByType(tokens, type) {
|
||||||
const filtered = tokens.filter((token) => token.type === type);
|
for (const token of tokens) {
|
||||||
return (filtered.length > 0) ? filtered[0].text : null;
|
if (token.type === type) {
|
||||||
|
return token.text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -496,17 +500,6 @@ function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the specified token iff it is of the desired type.
|
|
||||||
*
|
|
||||||
* @param {Token} token Micromark token candidate.
|
|
||||||
* @param {TokenType} type Desired type.
|
|
||||||
* @returns {Token | null} Token instance.
|
|
||||||
*/
|
|
||||||
function tokenIfType(token, type) {
|
|
||||||
return (token && (token.type === type)) ? token : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set containing token types that do not contain content.
|
* Set containing token types that do not contain content.
|
||||||
*
|
*
|
||||||
|
|
@ -539,6 +532,5 @@ module.exports = {
|
||||||
inHtmlFlow,
|
inHtmlFlow,
|
||||||
isHtmlFlowComment,
|
isHtmlFlowComment,
|
||||||
matchAndGetTokensByType,
|
matchAndGetTokensByType,
|
||||||
nonContentTokens,
|
nonContentTokens
|
||||||
tokenIfType
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
19
lib/md038.js
19
lib/md038.js
|
|
@ -3,7 +3,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorContext } = require("../helpers");
|
const { addErrorContext } = require("../helpers");
|
||||||
const { tokenIfType } = require("../helpers/micromark.cjs");
|
const { getDescendantsByType } = require("../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = require("./cache");
|
const { filterByTypesCached } = require("./cache");
|
||||||
|
|
||||||
const leftSpaceRe = /^\s(?:[^`]|$)/;
|
const leftSpaceRe = /^\s(?:[^`]|$)/;
|
||||||
|
|
@ -29,17 +29,12 @@ module.exports = {
|
||||||
"function": function MD038(params, onError) {
|
"function": function MD038(params, onError) {
|
||||||
const codeTexts = filterByTypesCached([ "codeText" ]);
|
const codeTexts = filterByTypesCached([ "codeText" ]);
|
||||||
for (const codeText of codeTexts) {
|
for (const codeText of codeTexts) {
|
||||||
const { children } = codeText;
|
const sequences = getDescendantsByType(codeText, [ "codeTextSequence" ]);
|
||||||
const first = 0;
|
const startSequence = sequences[0];
|
||||||
const last = children.length - 1;
|
const endSequence = sequences[sequences.length - 1];
|
||||||
const startSequence = tokenIfType(children[first], "codeTextSequence");
|
const datas = getDescendantsByType(codeText, [ "codeTextData" ]);
|
||||||
const endSequence = tokenIfType(children[last], "codeTextSequence");
|
const startData = datas[0];
|
||||||
const startData =
|
const endData = datas[datas.length - 1];
|
||||||
tokenIfType(children[first + 1], "codeTextData") ||
|
|
||||||
tokenIfType(children[first + 2], "codeTextData");
|
|
||||||
const endData =
|
|
||||||
tokenIfType(children[last - 1], "codeTextData") ||
|
|
||||||
tokenIfType(children[last - 2], "codeTextData");
|
|
||||||
if (startSequence && endSequence && startData && endData) {
|
if (startSequence && endSequence && startData && endData) {
|
||||||
const spaceLeft = leftSpaceRe.test(startData.text);
|
const spaceLeft = leftSpaceRe.test(startData.text);
|
||||||
const spaceRight = rightSpaceRe.test(endData.text);
|
const spaceRight = rightSpaceRe.test(endData.text);
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError, addErrorContext } = require("../helpers");
|
const { addError, addErrorContext } = require("../helpers");
|
||||||
const { getTokenTextByType, tokenIfType } = require("../helpers/micromark.cjs");
|
const { getDescendantsByType, getTokenTextByType } = require("../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = require("./cache");
|
const { filterByTypesCached } = require("./cache");
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
|
|
@ -19,8 +19,7 @@ module.exports = {
|
||||||
const languageOnly = !!params.config.language_only;
|
const languageOnly = !!params.config.language_only;
|
||||||
const fencedCodes = filterByTypesCached([ "codeFenced" ]);
|
const fencedCodes = filterByTypesCached([ "codeFenced" ]);
|
||||||
for (const fencedCode of fencedCodes) {
|
for (const fencedCode of fencedCodes) {
|
||||||
const openingFence = tokenIfType(fencedCode.children[0], "codeFencedFence");
|
const openingFence = getDescendantsByType(fencedCode, [ "codeFencedFence" ])[0];
|
||||||
if (openingFence) {
|
|
||||||
const { children, startLine, text } = openingFence;
|
const { children, startLine, text } = openingFence;
|
||||||
const info = getTokenTextByType(children, "codeFencedFenceInfo");
|
const info = getTokenTextByType(children, "codeFencedFenceInfo");
|
||||||
if (!info) {
|
if (!info) {
|
||||||
|
|
@ -33,5 +32,4 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
10
lib/md048.js
10
lib/md048.js
|
|
@ -3,7 +3,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
|
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
|
||||||
const { tokenIfType } = require("../helpers/micromark.cjs");
|
const { getDescendantsByType } = require("../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = require("./cache");
|
const { filterByTypesCached } = require("./cache");
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
|
|
@ -18,10 +18,8 @@ module.exports = {
|
||||||
let expectedStyle = style;
|
let expectedStyle = style;
|
||||||
const codeFenceds = filterByTypesCached([ "codeFenced" ]);
|
const codeFenceds = filterByTypesCached([ "codeFenced" ]);
|
||||||
for (const codeFenced of codeFenceds) {
|
for (const codeFenced of codeFenceds) {
|
||||||
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
|
const codeFencedFenceSequence =
|
||||||
if (codeFencedFence) {
|
getDescendantsByType(codeFenced, [ "codeFencedFence", "codeFencedFenceSequence" ])[0];
|
||||||
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
|
|
||||||
if (codeFencedFenceSequence) {
|
|
||||||
const { startLine, text } = codeFencedFenceSequence;
|
const { startLine, text } = codeFencedFenceSequence;
|
||||||
if (expectedStyle === "consistent") {
|
if (expectedStyle === "consistent") {
|
||||||
expectedStyle = fencedCodeBlockStyleFor(text);
|
expectedStyle = fencedCodeBlockStyleFor(text);
|
||||||
|
|
@ -34,6 +32,4 @@ module.exports = {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError, emphasisOrStrongStyleFor } = require("../helpers");
|
const { addError, emphasisOrStrongStyleFor } = require("../helpers");
|
||||||
const { filterByPredicate, tokenIfType } = require("../helpers/micromark.cjs");
|
const { filterByPredicate, getDescendantsByType } = require("../helpers/micromark.cjs");
|
||||||
|
|
||||||
const intrawordRe = /^\w$/;
|
const intrawordRe = /^\w$/;
|
||||||
|
|
||||||
|
|
@ -25,9 +25,9 @@ const impl =
|
||||||
(token) => ((token.type === "htmlFlow") ? [] : token.children)
|
(token) => ((token.type === "htmlFlow") ? [] : token.children)
|
||||||
);
|
);
|
||||||
for (const token of emphasisTokens) {
|
for (const token of emphasisTokens) {
|
||||||
const { children } = token;
|
const sequences = getDescendantsByType(token, [ typeSequence ]);
|
||||||
const startSequence = tokenIfType(children[0], typeSequence);
|
const startSequence = sequences[0];
|
||||||
const endSequence = tokenIfType(children[children.length - 1], typeSequence);
|
const endSequence = sequences[sequences.length - 1];
|
||||||
if (startSequence && endSequence) {
|
if (startSequence && endSequence) {
|
||||||
const markupStyle = emphasisOrStrongStyleFor(startSequence.text);
|
const markupStyle = emphasisOrStrongStyleFor(startSequence.text);
|
||||||
if (style === "consistent") {
|
if (style === "consistent") {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue