mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 14:30:12 +01:00
Cache result of flattenLists for ~8% time reduction.
This commit is contained in:
parent
2a2371a8cb
commit
096f4d7afd
1 changed files with 69 additions and 62 deletions
31
lib/rules.js
31
lib/rules.js
|
|
@ -85,12 +85,13 @@ function forEachHeading(params, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns (nested) lists as a flat array (in order)
|
// Returns (nested) lists as a flat array (in order)
|
||||||
function flattenLists(tokens, filterBy) {
|
function flattenLists(params) {
|
||||||
|
if (!params.flattenLists) {
|
||||||
var lists = [];
|
var lists = [];
|
||||||
var stack = [];
|
var stack = [];
|
||||||
var current = null;
|
var current = null;
|
||||||
var lastWithMap = { "map": [ 0, 1 ] };
|
var lastWithMap = { "map": [ 0, 1 ] };
|
||||||
tokens.forEach(function forToken(token) {
|
params.tokens.forEach(function forToken(token) {
|
||||||
if ((token.type === "bullet_list_open") ||
|
if ((token.type === "bullet_list_open") ||
|
||||||
(token.type === "ordered_list_open")) {
|
(token.type === "ordered_list_open")) {
|
||||||
// Save current context and start a new one
|
// Save current context and start a new one
|
||||||
|
|
@ -107,10 +108,8 @@ function flattenLists(tokens, filterBy) {
|
||||||
(token.type === "ordered_list_close")) {
|
(token.type === "ordered_list_close")) {
|
||||||
// Finalize current context and restore previous
|
// Finalize current context and restore previous
|
||||||
current.lastLineIndex = lastWithMap.map[1];
|
current.lastLineIndex = lastWithMap.map[1];
|
||||||
if ((filterBy === undefined) || (filterBy === current.ordered)) {
|
|
||||||
lists.splice(current.insert, 0, current);
|
lists.splice(current.insert, 0, current);
|
||||||
delete current.insert;
|
delete current.insert;
|
||||||
}
|
|
||||||
current = stack.pop();
|
current = stack.pop();
|
||||||
} else if (token.type === "list_item_open") {
|
} else if (token.type === "list_item_open") {
|
||||||
// Add list item
|
// Add list item
|
||||||
|
|
@ -120,7 +119,9 @@ function flattenLists(tokens, filterBy) {
|
||||||
lastWithMap = token;
|
lastWithMap = token;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return lists;
|
params.flattenLists = lists;
|
||||||
|
}
|
||||||
|
return params.flattenLists;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
|
|
@ -180,7 +181,8 @@ module.exports = [
|
||||||
"tags": [ "bullet", "ul" ],
|
"tags": [ "bullet", "ul" ],
|
||||||
"func": function MD004(params, errors) {
|
"func": function MD004(params, errors) {
|
||||||
var style = params.options.style || "consistent";
|
var style = params.options.style || "consistent";
|
||||||
flattenLists(params.tokens, false).forEach(function forList(list) {
|
flattenLists(params).forEach(function forList(list) {
|
||||||
|
if (!list.ordered) {
|
||||||
if (style === "consistent") {
|
if (style === "consistent") {
|
||||||
style = unorderedListStyleFor(list.items[0]);
|
style = unorderedListStyleFor(list.items[0]);
|
||||||
}
|
}
|
||||||
|
|
@ -189,6 +191,7 @@ module.exports = [
|
||||||
errors.push(item.lineNumber);
|
errors.push(item.lineNumber);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -198,7 +201,7 @@ module.exports = [
|
||||||
"desc": "Inconsistent indentation for list items at the same level",
|
"desc": "Inconsistent indentation for list items at the same level",
|
||||||
"tags": [ "bullet", "ul", "indentation" ],
|
"tags": [ "bullet", "ul", "indentation" ],
|
||||||
"func": function MD005(params, errors) {
|
"func": function MD005(params, errors) {
|
||||||
flattenLists(params.tokens).forEach(function forList(list) {
|
flattenLists(params).forEach(function forList(list) {
|
||||||
var indent = indentFor(list.items[0]);
|
var indent = indentFor(list.items[0]);
|
||||||
list.items.forEach(function forItem(item) {
|
list.items.forEach(function forItem(item) {
|
||||||
if (indentFor(item) !== indent) {
|
if (indentFor(item) !== indent) {
|
||||||
|
|
@ -214,8 +217,8 @@ module.exports = [
|
||||||
"desc": "Consider starting bulleted lists at the beginning of the line",
|
"desc": "Consider starting bulleted lists at the beginning of the line",
|
||||||
"tags": [ "bullet", "ul", "indentation" ],
|
"tags": [ "bullet", "ul", "indentation" ],
|
||||||
"func": function MD006(params, errors) {
|
"func": function MD006(params, errors) {
|
||||||
flattenLists(params.tokens, false).forEach(function forList(list) {
|
flattenLists(params).forEach(function forList(list) {
|
||||||
if (!list.nesting && indentFor(list.open)) {
|
if (!list.ordered && !list.nesting && indentFor(list.open)) {
|
||||||
errors.push(list.open.lineNumber);
|
errors.push(list.open.lineNumber);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -229,13 +232,15 @@ module.exports = [
|
||||||
"func": function MD007(params, errors) {
|
"func": function MD007(params, errors) {
|
||||||
var optionsIndent = params.options.indent || 2;
|
var optionsIndent = params.options.indent || 2;
|
||||||
var prevIndent = 0;
|
var prevIndent = 0;
|
||||||
flattenLists(params.tokens, false).forEach(function forList(list) {
|
flattenLists(params).forEach(function forList(list) {
|
||||||
|
if (!list.ordered) {
|
||||||
var indent = indentFor(list.open);
|
var indent = indentFor(list.open);
|
||||||
if ((indent > prevIndent) &&
|
if ((indent > prevIndent) &&
|
||||||
((indent - prevIndent) !== optionsIndent)) {
|
((indent - prevIndent) !== optionsIndent)) {
|
||||||
errors.push(list.open.lineNumber);
|
errors.push(list.open.lineNumber);
|
||||||
}
|
}
|
||||||
prevIndent = indent;
|
prevIndent = indent;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -531,7 +536,8 @@ module.exports = [
|
||||||
"tags": [ "ol" ],
|
"tags": [ "ol" ],
|
||||||
"func": function MD029(params, errors) {
|
"func": function MD029(params, errors) {
|
||||||
var style = params.options.style || "one";
|
var style = params.options.style || "one";
|
||||||
flattenLists(params.tokens, true).forEach(function forList(list) {
|
flattenLists(params).forEach(function forList(list) {
|
||||||
|
if (list.ordered) {
|
||||||
var number = 1;
|
var number = 1;
|
||||||
list.items.forEach(function forItem(item) {
|
list.items.forEach(function forItem(item) {
|
||||||
var re = new RegExp("^\\s*" + String(number) + "\\.");
|
var re = new RegExp("^\\s*" + String(number) + "\\.");
|
||||||
|
|
@ -542,6 +548,7 @@ module.exports = [
|
||||||
number++;
|
number++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -555,7 +562,7 @@ module.exports = [
|
||||||
var olSingle = params.options.ol_single || 1;
|
var olSingle = params.options.ol_single || 1;
|
||||||
var ulMulti = params.options.ul_multi || 1;
|
var ulMulti = params.options.ul_multi || 1;
|
||||||
var olMulti = params.options.ol_multi || 1;
|
var olMulti = params.options.ol_multi || 1;
|
||||||
flattenLists(params.tokens).forEach(function forList(list) {
|
flattenLists(params).forEach(function forList(list) {
|
||||||
var lineCount = list.lastLineIndex - list.open.map[0];
|
var lineCount = list.lastLineIndex - list.open.map[0];
|
||||||
var allSingle = lineCount === list.items.length;
|
var allSingle = lineCount === list.items.length;
|
||||||
var expectedSpaces = list.ordered ?
|
var expectedSpaces = list.ordered ?
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue