mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Refactor lintInput to share code between sync/async, support an async path for strings, and process files first for better concurrency.
This commit is contained in:
parent
53e5e4272e
commit
e531bd6359
2 changed files with 101 additions and 119 deletions
|
|
@ -1608,65 +1608,63 @@ function lintInput(options, synchronous, callback) {
|
||||||
var fs = options.fs || __webpack_require__(/*! fs */ "?ec0a");
|
var fs = options.fs || __webpack_require__(/*! fs */ "?ec0a");
|
||||||
var results = newResults(ruleList);
|
var results = newResults(ruleList);
|
||||||
var done = false;
|
var done = false;
|
||||||
// Linting of strings is always synchronous
|
|
||||||
var syncItem = null;
|
|
||||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
||||||
function syncCallback(err, result) {
|
|
||||||
if (err) {
|
|
||||||
done = true;
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
results[syncItem] = result;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
while (!done && (syncItem = stringsKeys.shift())) {
|
|
||||||
lintContent(ruleList, syncItem, strings[syncItem] || "", md, config, frontMatter, handleRuleFailures, noInlineConfig, resultVersion, syncCallback);
|
|
||||||
}
|
|
||||||
if (synchronous) {
|
|
||||||
// Lint files synchronously
|
|
||||||
while (!done && (syncItem = files.shift())) {
|
|
||||||
lintFile(ruleList, syncItem, md, config, frontMatter, handleRuleFailures, noInlineConfig, resultVersion, fs, synchronous, syncCallback);
|
|
||||||
}
|
|
||||||
return done || callback(null, results);
|
|
||||||
}
|
|
||||||
// Lint files asynchronously
|
|
||||||
var concurrency = 0;
|
var concurrency = 0;
|
||||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||||
function lintConcurrently() {
|
function lintWorker() {
|
||||||
var asyncItem = files.shift();
|
var currentItem = null;
|
||||||
if (done) {
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||||
// Nothing to do
|
function lintWorkerCallback(err, result) {
|
||||||
}
|
|
||||||
else if (asyncItem) {
|
|
||||||
concurrency++;
|
|
||||||
lintFile(ruleList, asyncItem, md, config, frontMatter, handleRuleFailures, noInlineConfig, resultVersion, fs, synchronous, function (err, result) {
|
|
||||||
concurrency--;
|
concurrency--;
|
||||||
if (err) {
|
if (err) {
|
||||||
done = true;
|
done = true;
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
results[asyncItem] = result;
|
results[currentItem] = result;
|
||||||
lintConcurrently();
|
if (!synchronous) {
|
||||||
|
lintWorker();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
});
|
}
|
||||||
|
if (done) {
|
||||||
|
// Abort for error or nothing left to do
|
||||||
|
}
|
||||||
|
else if (files.length > 0) {
|
||||||
|
// Lint next file
|
||||||
|
concurrency++;
|
||||||
|
currentItem = files.shift();
|
||||||
|
lintFile(ruleList, currentItem, md, config, frontMatter, handleRuleFailures, noInlineConfig, resultVersion, fs, synchronous, lintWorkerCallback);
|
||||||
|
}
|
||||||
|
else if (stringsKeys.length > 0) {
|
||||||
|
// Lint next string
|
||||||
|
concurrency++;
|
||||||
|
currentItem = stringsKeys.shift();
|
||||||
|
lintContent(ruleList, currentItem, strings[currentItem] || "", md, config, frontMatter, handleRuleFailures, noInlineConfig, resultVersion, lintWorkerCallback);
|
||||||
}
|
}
|
||||||
else if (concurrency === 0) {
|
else if (concurrency === 0) {
|
||||||
|
// Finish
|
||||||
done = true;
|
done = true;
|
||||||
return callback(null, results);
|
return callback(null, results);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (synchronous) {
|
||||||
|
while (!done) {
|
||||||
|
lintWorker();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
// Testing on a Raspberry Pi 4 Model B with an artificial 5ms file access
|
// Testing on a Raspberry Pi 4 Model B with an artificial 5ms file access
|
||||||
// delay suggests that a concurrency factor of 8 can eliminate the impact
|
// delay suggests that a concurrency factor of 8 can eliminate the impact
|
||||||
// of that delay (i.e., total time is the same as with no delay).
|
// of that delay (i.e., total time is the same as with no delay).
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -749,98 +749,82 @@ function lintInput(options, synchronous, callback) {
|
||||||
const fs = options.fs || require("fs");
|
const fs = options.fs || require("fs");
|
||||||
const results = newResults(ruleList);
|
const results = newResults(ruleList);
|
||||||
let done = false;
|
let done = false;
|
||||||
// Linting of strings is always synchronous
|
|
||||||
let syncItem = null;
|
|
||||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
||||||
function syncCallback(err, result) {
|
|
||||||
if (err) {
|
|
||||||
done = true;
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
results[syncItem] = result;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
while (!done && (syncItem = stringsKeys.shift())) {
|
|
||||||
lintContent(
|
|
||||||
ruleList,
|
|
||||||
syncItem,
|
|
||||||
strings[syncItem] || "",
|
|
||||||
md,
|
|
||||||
config,
|
|
||||||
frontMatter,
|
|
||||||
handleRuleFailures,
|
|
||||||
noInlineConfig,
|
|
||||||
resultVersion,
|
|
||||||
syncCallback
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (synchronous) {
|
|
||||||
// Lint files synchronously
|
|
||||||
while (!done && (syncItem = files.shift())) {
|
|
||||||
lintFile(
|
|
||||||
ruleList,
|
|
||||||
syncItem,
|
|
||||||
md,
|
|
||||||
config,
|
|
||||||
frontMatter,
|
|
||||||
handleRuleFailures,
|
|
||||||
noInlineConfig,
|
|
||||||
resultVersion,
|
|
||||||
fs,
|
|
||||||
synchronous,
|
|
||||||
syncCallback
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return done || callback(null, results);
|
|
||||||
}
|
|
||||||
// Lint files asynchronously
|
|
||||||
let concurrency = 0;
|
let concurrency = 0;
|
||||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||||
function lintConcurrently() {
|
function lintWorker() {
|
||||||
const asyncItem = files.shift();
|
let currentItem = null;
|
||||||
if (done) {
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||||
// Nothing to do
|
function lintWorkerCallback(err, result) {
|
||||||
} else if (asyncItem) {
|
|
||||||
concurrency++;
|
|
||||||
lintFile(
|
|
||||||
ruleList,
|
|
||||||
asyncItem,
|
|
||||||
md,
|
|
||||||
config,
|
|
||||||
frontMatter,
|
|
||||||
handleRuleFailures,
|
|
||||||
noInlineConfig,
|
|
||||||
resultVersion,
|
|
||||||
fs,
|
|
||||||
synchronous,
|
|
||||||
(err, result) => {
|
|
||||||
concurrency--;
|
concurrency--;
|
||||||
if (err) {
|
if (err) {
|
||||||
done = true;
|
done = true;
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
results[asyncItem] = result;
|
results[currentItem] = result;
|
||||||
lintConcurrently();
|
if (!synchronous) {
|
||||||
|
lintWorker();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (done) {
|
||||||
|
// Abort for error or nothing left to do
|
||||||
|
} else if (files.length > 0) {
|
||||||
|
// Lint next file
|
||||||
|
concurrency++;
|
||||||
|
currentItem = files.shift();
|
||||||
|
lintFile(
|
||||||
|
ruleList,
|
||||||
|
currentItem,
|
||||||
|
md,
|
||||||
|
config,
|
||||||
|
frontMatter,
|
||||||
|
handleRuleFailures,
|
||||||
|
noInlineConfig,
|
||||||
|
resultVersion,
|
||||||
|
fs,
|
||||||
|
synchronous,
|
||||||
|
lintWorkerCallback
|
||||||
|
);
|
||||||
|
} else if (stringsKeys.length > 0) {
|
||||||
|
// Lint next string
|
||||||
|
concurrency++;
|
||||||
|
currentItem = stringsKeys.shift();
|
||||||
|
lintContent(
|
||||||
|
ruleList,
|
||||||
|
currentItem,
|
||||||
|
strings[currentItem] || "",
|
||||||
|
md,
|
||||||
|
config,
|
||||||
|
frontMatter,
|
||||||
|
handleRuleFailures,
|
||||||
|
noInlineConfig,
|
||||||
|
resultVersion,
|
||||||
|
lintWorkerCallback
|
||||||
);
|
);
|
||||||
} else if (concurrency === 0) {
|
} else if (concurrency === 0) {
|
||||||
|
// Finish
|
||||||
done = true;
|
done = true;
|
||||||
return callback(null, results);
|
return callback(null, results);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (synchronous) {
|
||||||
|
while (!done) {
|
||||||
|
lintWorker();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// Testing on a Raspberry Pi 4 Model B with an artificial 5ms file access
|
// Testing on a Raspberry Pi 4 Model B with an artificial 5ms file access
|
||||||
// delay suggests that a concurrency factor of 8 can eliminate the impact
|
// delay suggests that a concurrency factor of 8 can eliminate the impact
|
||||||
// of that delay (i.e., total time is the same as with no delay).
|
// of that delay (i.e., total time is the same as with no delay).
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
lintConcurrently();
|
lintWorker();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue