Cache result of flattenLists for ~8% time reduction.

This commit is contained in:
David Anson 2015-06-11 18:33:40 -07:00
parent 2a2371a8cb
commit 096f4d7afd

View file

@ -85,42 +85,43 @@ 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) {
var lists = []; if (!params.flattenLists) {
var stack = []; var lists = [];
var current = null; var stack = [];
var lastWithMap = { "map": [ 0, 1 ] }; var current = null;
tokens.forEach(function forToken(token) { var lastWithMap = { "map": [ 0, 1 ] };
if ((token.type === "bullet_list_open") || params.tokens.forEach(function forToken(token) {
(token.type === "ordered_list_open")) { if ((token.type === "bullet_list_open") ||
// Save current context and start a new one (token.type === "ordered_list_open")) {
stack.push(current); // Save current context and start a new one
current = { stack.push(current);
"ordered": (token.type === "ordered_list_open"), current = {
"open": token, "ordered": (token.type === "ordered_list_open"),
"items": [], "open": token,
"nesting": stack.length - 1, "items": [],
"lastLineIndex": -1, "nesting": stack.length - 1,
"insert": lists.length "lastLineIndex": -1,
}; "insert": lists.length
} else if ((token.type === "bullet_list_close") || };
(token.type === "ordered_list_close")) { } else if ((token.type === "bullet_list_close") ||
// Finalize current context and restore previous (token.type === "ordered_list_close")) {
current.lastLineIndex = lastWithMap.map[1]; // Finalize current context and restore previous
if ((filterBy === undefined) || (filterBy === current.ordered)) { current.lastLineIndex = lastWithMap.map[1];
lists.splice(current.insert, 0, current); lists.splice(current.insert, 0, current);
delete current.insert; delete current.insert;
current = stack.pop();
} else if (token.type === "list_item_open") {
// Add list item
current.items.push(token);
} else if (token.map) {
// Track last token with map
lastWithMap = token;
} }
current = stack.pop(); });
} else if (token.type === "list_item_open") { params.flattenLists = lists;
// Add list item }
current.items.push(token); return params.flattenLists;
} else if (token.map) {
// Track last token with map
lastWithMap = token;
}
});
return lists;
} }
module.exports = [ module.exports = [
@ -180,15 +181,17 @@ 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 (style === "consistent") { if (!list.ordered) {
style = unorderedListStyleFor(list.items[0]); if (style === "consistent") {
} style = unorderedListStyleFor(list.items[0]);
list.items.forEach(function forItem(item) {
if (unorderedListStyleFor(item) !== style) {
errors.push(item.lineNumber);
} }
}); list.items.forEach(function forItem(item) {
if (unorderedListStyleFor(item) !== style) {
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) {
var indent = indentFor(list.open); if (!list.ordered) {
if ((indent > prevIndent) && var indent = indentFor(list.open);
((indent - prevIndent) !== optionsIndent)) { if ((indent > prevIndent) &&
errors.push(list.open.lineNumber); ((indent - prevIndent) !== optionsIndent)) {
errors.push(list.open.lineNumber);
}
prevIndent = indent;
} }
prevIndent = indent;
}); });
} }
}, },
@ -531,17 +536,19 @@ 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) {
var number = 1; if (list.ordered) {
list.items.forEach(function forItem(item) { var number = 1;
var re = new RegExp("^\\s*" + String(number) + "\\."); list.items.forEach(function forItem(item) {
if (!re.test(item.line)) { var re = new RegExp("^\\s*" + String(number) + "\\.");
errors.push(item.lineNumber); if (!re.test(item.line)) {
} errors.push(item.lineNumber);
if (style === "ordered") { }
number++; if (style === "ordered") {
} 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 ?