Add "// @ts-check" for better VS Code experience, address corresponding issues.

This commit is contained in:
David Anson 2017-12-15 22:55:51 -08:00
parent 1184281c87
commit 3cecb86f9b
3 changed files with 72 additions and 58 deletions

View file

@ -1,3 +1,5 @@
// @ts-check
"use strict"; "use strict";
var fs = require("fs"); var fs = require("fs");
@ -228,37 +230,42 @@ function lintContent(
"range": range || null "range": range || null
}); });
} }
errors.add = function add(lineNumber) { var errorHelpers = {
addError(lineNumber); "add": function add(lineNumber) {
addError(lineNumber);
},
"addDetail": function addDetail(lineNumber, detail) {
addError(lineNumber, detail);
},
"addDetailIf":
function addDetailIf(lineNumber, expected, actual, detail, range) {
if (expected !== actual) {
addError(
lineNumber,
"Expected: " + expected + "; Actual: " + actual +
(detail ? "; " + detail : ""),
null,
range);
}
},
"addContext":
function addContext(lineNumber, context, left, right, range) {
if (context.length <= 30) {
// Nothing to do
} else if (left && right) {
context = context.substr(0, 15) + "..." + context.substr(-15);
} else if (right) {
context = "..." + context.substr(-30);
} else {
context = context.substr(0, 30) + "...";
}
addError(lineNumber, null, context, range);
},
"count": function count() {
return errors.length;
}
}; };
errors.addDetail = function addDetail(lineNumber, detail) { rule.func(params, errorHelpers);
addError(lineNumber, detail);
};
errors.addDetailIf =
function addDetailIf(lineNumber, expected, actual, detail, range) {
if (expected !== actual) {
addError(
lineNumber,
"Expected: " + expected + "; Actual: " + actual +
(detail ? "; " + detail : ""),
null,
range);
}
};
errors.addContext =
function addContext(lineNumber, context, left, right, range) {
if (context.length <= 30) {
// Nothing to do
} else if (left && right) {
context = context.substr(0, 15) + "..." + context.substr(-15);
} else if (right) {
context = "..." + context.substr(-30);
} else {
context = context.substr(0, 30) + "...";
}
addError(lineNumber, null, context, range);
};
rule.func(params, errors);
// Record any errors (significant performance benefit from length check) // Record any errors (significant performance benefit from length check)
if (errors.length) { if (errors.length) {
errors.sort(lineNumberComparison); errors.sort(lineNumberComparison);
@ -336,22 +343,7 @@ function lintFile(
} }
} }
// Callback used as a sentinel by markdownlintSync function lintInput(options, synchronous, callback) {
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) {
// Normalize inputs // Normalize inputs
options = options || {}; options = options || {};
callback = callback || function noop() {}; callback = callback || function noop() {};
@ -368,7 +360,6 @@ function markdownlint(options, callback) {
var noInlineConfig = !!options.noInlineConfig; var noInlineConfig = !!options.noInlineConfig;
var resultVersion = (options.resultVersion === undefined) ? var resultVersion = (options.resultVersion === undefined) ?
1 : options.resultVersion; 1 : options.resultVersion;
var synchronous = (callback === markdownlintSynchronousCallback);
var results = new Results(); var results = new Results();
// Helper to lint the next file in the array // Helper to lint the next file in the array
function lintFilesArray() { function lintFilesArray() {
@ -405,10 +396,17 @@ function markdownlint(options, callback) {
}); });
// Lint files // Lint files
lintFilesArray(); 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. * @returns {Object} Result object.
*/ */
function markdownlintSync(options) { 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 // Export a/synchronous APIs
markdownlint.sync = markdownlintSync;
markdownlint.readConfig = readConfig;
markdownlint.readConfigSync = readConfigSync;
module.exports = markdownlint; module.exports = markdownlint;
module.exports.sync = markdownlintSync;
module.exports.readConfig = readConfig;
module.exports.readConfigSync = readConfigSync;

View file

@ -1,3 +1,5 @@
// @ts-check
"use strict"; "use strict";
var shared = require("./shared"); var shared = require("./shared");
@ -942,24 +944,28 @@ module.exports = [
!re.test(t.children[1].content)) { !re.test(t.children[1].content)) {
errors.addContext(t.lineNumber, t.children[1].content); errors.addContext(t.lineNumber, t.children[1].content);
} }
return base;
}; };
} else if (token.type === "blockquote_open") { } else if (token.type === "blockquote_open") {
return function inBlockquote(t) { return function inBlockquote(t) {
if (t.type !== "blockquote_close") { if (t.type !== "blockquote_close") {
return inBlockquote; return inBlockquote;
} }
return base;
}; };
} else if (token.type === "list_item_open") { } else if (token.type === "list_item_open") {
return function inListItem(t) { return function inListItem(t) {
if (t.type !== "list_item_close") { if (t.type !== "list_item_close") {
return inListItem; return inListItem;
} }
return base;
}; };
} }
return base;
} }
var state = base; var state = base;
params.tokens.forEach(function forToken(token) { params.tokens.forEach(function forToken(token) {
state = state(token) || base; state = state(token);
}); });
} }
}, },
@ -1147,7 +1153,7 @@ module.exports = [
var i = 0; var i = 0;
var optional = false; var optional = false;
forEachHeading(params, function forHeading(heading, content) { forEachHeading(params, function forHeading(heading, content) {
if (!errors.length) { if (!errors.count()) {
var actual = levels[heading.tag] + " " + content; var actual = levels[heading.tag] + " " + content;
var expected = requiredHeaders[i++] || "[None]"; var expected = requiredHeaders[i++] || "[None]";
if (expected === "*") { 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]); errors.addContext(params.lines.length, requiredHeaders[i]);
} }
} }

View file

@ -1,3 +1,5 @@
// @ts-check
"use strict"; "use strict";
// Regular expression for matching common newline characters // Regular expression for matching common newline characters