mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add "// @ts-check" for better VS Code experience, address corresponding issues.
This commit is contained in:
parent
1184281c87
commit
3cecb86f9b
3 changed files with 72 additions and 58 deletions
|
@ -1,3 +1,5 @@
|
|||
// @ts-check
|
||||
|
||||
"use strict";
|
||||
|
||||
var fs = require("fs");
|
||||
|
@ -228,13 +230,14 @@ function lintContent(
|
|||
"range": range || null
|
||||
});
|
||||
}
|
||||
errors.add = function add(lineNumber) {
|
||||
var errorHelpers = {
|
||||
"add": function add(lineNumber) {
|
||||
addError(lineNumber);
|
||||
};
|
||||
errors.addDetail = function addDetail(lineNumber, detail) {
|
||||
},
|
||||
"addDetail": function addDetail(lineNumber, detail) {
|
||||
addError(lineNumber, detail);
|
||||
};
|
||||
errors.addDetailIf =
|
||||
},
|
||||
"addDetailIf":
|
||||
function addDetailIf(lineNumber, expected, actual, detail, range) {
|
||||
if (expected !== actual) {
|
||||
addError(
|
||||
|
@ -244,8 +247,8 @@ function lintContent(
|
|||
null,
|
||||
range);
|
||||
}
|
||||
};
|
||||
errors.addContext =
|
||||
},
|
||||
"addContext":
|
||||
function addContext(lineNumber, context, left, right, range) {
|
||||
if (context.length <= 30) {
|
||||
// Nothing to do
|
||||
|
@ -257,8 +260,12 @@ function lintContent(
|
|||
context = context.substr(0, 30) + "...";
|
||||
}
|
||||
addError(lineNumber, null, context, range);
|
||||
},
|
||||
"count": function count() {
|
||||
return errors.length;
|
||||
}
|
||||
};
|
||||
rule.func(params, errors);
|
||||
rule.func(params, errorHelpers);
|
||||
// Record any errors (significant performance benefit from length check)
|
||||
if (errors.length) {
|
||||
errors.sort(lineNumberComparison);
|
||||
|
@ -336,22 +343,7 @@ function lintFile(
|
|||
}
|
||||
}
|
||||
|
||||
// Callback used as a sentinel by markdownlintSync
|
||||
function markdownlintSynchronousCallback() {
|
||||
// Unreachable; no code path in the synchronous case passes err
|
||||
// if (err) {
|
||||
// throw err; // Synchronous APIs throw
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* Lint specified Markdown files.
|
||||
*
|
||||
* @param {Object} options Configuration options.
|
||||
* @param {Function} callback Callback (err, result) function.
|
||||
* @returns {void}
|
||||
*/
|
||||
function markdownlint(options, callback) {
|
||||
function lintInput(options, synchronous, callback) {
|
||||
// Normalize inputs
|
||||
options = options || {};
|
||||
callback = callback || function noop() {};
|
||||
|
@ -368,7 +360,6 @@ function markdownlint(options, callback) {
|
|||
var noInlineConfig = !!options.noInlineConfig;
|
||||
var resultVersion = (options.resultVersion === undefined) ?
|
||||
1 : options.resultVersion;
|
||||
var synchronous = (callback === markdownlintSynchronousCallback);
|
||||
var results = new Results();
|
||||
// Helper to lint the next file in the array
|
||||
function lintFilesArray() {
|
||||
|
@ -405,10 +396,17 @@ function markdownlint(options, callback) {
|
|||
});
|
||||
// Lint files
|
||||
lintFilesArray();
|
||||
// Return results
|
||||
if (synchronous) {
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lint specified Markdown files.
|
||||
*
|
||||
* @param {Object} options Configuration options.
|
||||
* @param {Function} callback Callback (err, result) function.
|
||||
* @returns {void}
|
||||
*/
|
||||
function markdownlint(options, callback) {
|
||||
return lintInput(options, false, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -418,7 +416,15 @@ function markdownlint(options, callback) {
|
|||
* @returns {Object} Result object.
|
||||
*/
|
||||
function markdownlintSync(options) {
|
||||
return markdownlint(options, markdownlintSynchronousCallback);
|
||||
var results = null;
|
||||
lintInput(options, true, function callback(error, res) {
|
||||
// Unreachable; no code path in the synchronous case passes error
|
||||
// if (error) {
|
||||
// throw error;
|
||||
// }
|
||||
results = res;
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -477,7 +483,7 @@ function readConfigSync(file) {
|
|||
}
|
||||
|
||||
// Export a/synchronous APIs
|
||||
markdownlint.sync = markdownlintSync;
|
||||
markdownlint.readConfig = readConfig;
|
||||
markdownlint.readConfigSync = readConfigSync;
|
||||
module.exports = markdownlint;
|
||||
module.exports.sync = markdownlintSync;
|
||||
module.exports.readConfig = readConfig;
|
||||
module.exports.readConfigSync = readConfigSync;
|
||||
|
|
12
lib/rules.js
12
lib/rules.js
|
@ -1,3 +1,5 @@
|
|||
// @ts-check
|
||||
|
||||
"use strict";
|
||||
|
||||
var shared = require("./shared");
|
||||
|
@ -942,24 +944,28 @@ module.exports = [
|
|||
!re.test(t.children[1].content)) {
|
||||
errors.addContext(t.lineNumber, t.children[1].content);
|
||||
}
|
||||
return base;
|
||||
};
|
||||
} else if (token.type === "blockquote_open") {
|
||||
return function inBlockquote(t) {
|
||||
if (t.type !== "blockquote_close") {
|
||||
return inBlockquote;
|
||||
}
|
||||
return base;
|
||||
};
|
||||
} else if (token.type === "list_item_open") {
|
||||
return function inListItem(t) {
|
||||
if (t.type !== "list_item_close") {
|
||||
return inListItem;
|
||||
}
|
||||
return base;
|
||||
};
|
||||
}
|
||||
return base;
|
||||
}
|
||||
var state = base;
|
||||
params.tokens.forEach(function forToken(token) {
|
||||
state = state(token) || base;
|
||||
state = state(token);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
@ -1147,7 +1153,7 @@ module.exports = [
|
|||
var i = 0;
|
||||
var optional = false;
|
||||
forEachHeading(params, function forHeading(heading, content) {
|
||||
if (!errors.length) {
|
||||
if (!errors.count()) {
|
||||
var actual = levels[heading.tag] + " " + content;
|
||||
var expected = requiredHeaders[i++] || "[None]";
|
||||
if (expected === "*") {
|
||||
|
@ -1161,7 +1167,7 @@ module.exports = [
|
|||
}
|
||||
}
|
||||
});
|
||||
if ((i < requiredHeaders.length) && !errors.length) {
|
||||
if ((i < requiredHeaders.length) && !errors.count()) {
|
||||
errors.addContext(params.lines.length, requiredHeaders[i]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// @ts-check
|
||||
|
||||
"use strict";
|
||||
|
||||
// Regular expression for matching common newline characters
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue