mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Cache result of forEachLine for ~4% time reduction.
This commit is contained in:
parent
3e208b7bc5
commit
483597c82a
2 changed files with 31 additions and 24 deletions
|
|
@ -12,7 +12,7 @@
|
||||||
"rules": {
|
"rules": {
|
||||||
"no-alert": 2,
|
"no-alert": 2,
|
||||||
"no-array-constructor": 2,
|
"no-array-constructor": 2,
|
||||||
"no-bitwise": 2,
|
"no-bitwise": 0,
|
||||||
"no-caller": 2,
|
"no-caller": 2,
|
||||||
"no-catch-shadow": 2,
|
"no-catch-shadow": 2,
|
||||||
"no-comma-dangle": 2,
|
"no-comma-dangle": 2,
|
||||||
|
|
|
||||||
53
lib/rules.js
53
lib/rules.js
|
|
@ -38,22 +38,32 @@ function filterTokens(params, type, callback) {
|
||||||
|
|
||||||
// Calls the provided function for each line (with context)
|
// Calls the provided function for each line (with context)
|
||||||
function forEachLine(params, callback) {
|
function forEachLine(params, callback) {
|
||||||
// Identify lines in code blocks
|
if (!params.forEachLine) {
|
||||||
var codeLines = [];
|
var lineMetadata = new Array(params.lines.length);
|
||||||
filterTokens(params, "code_block", function forToken(token) {
|
var inFence = false;
|
||||||
for (var i = token.map[0]; i < token.map[1]; i++) {
|
// Find fenced code by pattern (parser ignores "``` close fence")
|
||||||
codeLines.push(i);
|
params.lines.forEach(function forLine(line, lineIndex) {
|
||||||
}
|
var metadata = 0;
|
||||||
});
|
if (/^(```|~~~)/.test(line)) {
|
||||||
// Identify lines in code fences (with info about transitions)
|
metadata = inFence ? -2 : 2;
|
||||||
var inFence = false;
|
inFence = !inFence;
|
||||||
|
} else if (inFence) {
|
||||||
|
metadata = 1;
|
||||||
|
}
|
||||||
|
lineMetadata[lineIndex] = metadata;
|
||||||
|
});
|
||||||
|
// Find code blocks normally
|
||||||
|
filterTokens(params, "code_block", function forToken(token) {
|
||||||
|
for (var i = token.map[0]; i < token.map[1]; i++) {
|
||||||
|
lineMetadata[i] = 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
params.forEachLine = lineMetadata;
|
||||||
|
}
|
||||||
|
// Invoke callback
|
||||||
params.lines.forEach(function forLine(line, lineIndex) {
|
params.lines.forEach(function forLine(line, lineIndex) {
|
||||||
var onFence = /^(```|~~~)/.test(line);
|
var metadata = params.forEachLine[lineIndex];
|
||||||
if (onFence) {
|
callback(line, lineIndex, !!metadata, (metadata >> 1));
|
||||||
inFence = !inFence;
|
|
||||||
}
|
|
||||||
var inCodeBlock = (codeLines.indexOf(lineIndex) !== -1);
|
|
||||||
callback(line, lineIndex, inFence || inCodeBlock, onFence);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -581,13 +591,11 @@ module.exports = [
|
||||||
"desc": "Fenced code blocks should be surrounded by blank lines",
|
"desc": "Fenced code blocks should be surrounded by blank lines",
|
||||||
"tags": [ "code", "blank_lines" ],
|
"tags": [ "code", "blank_lines" ],
|
||||||
"func": function MD031(params, errors) {
|
"func": function MD031(params, errors) {
|
||||||
forEachLine(params, function forLine(line, lineIndex, inCode, onFence) {
|
var lines = params.lines;
|
||||||
if (onFence &&
|
forEachLine(params, function forLine(line, i, inCode, onFence) {
|
||||||
((inCode && (lineIndex - 1 >= 0) &&
|
if (((onFence > 0) && (i - 1 >= 0) && lines[i - 1].length) ||
|
||||||
params.lines[lineIndex - 1].length) ||
|
((onFence < 0) && (i + 1 < lines.length) && lines[i + 1].length)) {
|
||||||
(!inCode && (lineIndex + 1 < params.lines.length) &&
|
errors.push(i + 1);
|
||||||
params.lines[lineIndex + 1].length))) {
|
|
||||||
errors.push(lineIndex + 1);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -610,7 +618,6 @@ module.exports = [
|
||||||
}
|
}
|
||||||
inList = listMarker;
|
inList = listMarker;
|
||||||
}
|
}
|
||||||
inList = inList && !onFence;
|
|
||||||
prevLine = line;
|
prevLine = line;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue