Add Promise-based APIs for markdownlint and readConfig, update declaration file.

This commit is contained in:
David Anson 2020-09-13 12:58:09 -07:00
parent 1f6a2cdc96
commit e9d63a6284
5 changed files with 201 additions and 45 deletions

51
lib/markdownlint.d.ts vendored
View file

@ -8,7 +8,7 @@ export = markdownlint;
*/
declare function markdownlint(options: Options, callback: LintCallback): void;
declare namespace markdownlint {
export { markdownlintSync as sync, readConfig, readConfigSync, RuleFunction, RuleParams, MarkdownItToken, RuleOnError, RuleOnErrorInfo, RuleOnErrorFixInfo, Rule, Options, Plugin, ToStringCallback, LintResults, LintError, FixInfo, LintCallback, Configuration, RuleConfiguration, ConfigurationParser, ReadConfigCallback };
export { markdownlintSync as sync, readConfig, readConfigSync, promises, RuleFunction, RuleParams, MarkdownItToken, RuleOnError, RuleOnErrorInfo, RuleOnErrorFixInfo, Rule, Options, Plugin, ToStringCallback, LintResults, LintError, FixInfo, LintCallback, Configuration, RuleConfiguration, ConfigurationParser, ReadConfigCallback };
}
/**
* Configuration options.
@ -17,7 +17,7 @@ type Options = {
/**
* Files to lint.
*/
files?: string | string[];
files?: string[] | string;
/**
* Strings to lint.
*/
@ -27,13 +27,11 @@ type Options = {
/**
* Configuration object.
*/
config?: {
[x: string]: any;
};
config?: Configuration;
/**
* Custom rules.
*/
customRules?: Rule | Rule[];
customRules?: Rule[] | Rule;
/**
* Front matter pattern.
*/
@ -58,18 +56,14 @@ type Options = {
/**
* Called with the result of the lint operation.
*/
type LintCallback = (err: Error, results?: {
[x: string]: LintError[];
}) => void;
type LintCallback = (err: Error | null, results?: LintResults) => void;
/**
* Lint specified Markdown files synchronously.
*
* @param {Options} options Configuration options.
* @returns {LintResults} Results object.
*/
declare function markdownlintSync(options: Options): {
[x: string]: LintError[];
};
declare function markdownlintSync(options: Options): LintResults;
/**
* Read specified configuration file.
*
@ -87,9 +81,11 @@ declare function readConfig(file: string, parsers: ConfigurationParser[] | ReadC
* @param {ConfigurationParser[]} [parsers] Parsing function(s).
* @returns {Configuration} Configuration object.
*/
declare function readConfigSync(file: string, parsers?: ConfigurationParser[]): {
[x: string]: any;
};
declare function readConfigSync(file: string, parsers?: ConfigurationParser[]): Configuration;
declare namespace promises {
export { markdownlintPromise as markdownlint };
export { readConfigPromise as readConfig };
}
/**
* Function to implement rule logic.
*/
@ -117,7 +113,7 @@ type RuleParams = {
/**
* Rule configuration.
*/
config: any;
config: RuleConfiguration;
};
/**
* Markdown-It token.
@ -341,12 +337,23 @@ type RuleConfiguration = any;
/**
* Parses a configuration string and returns a configuration object.
*/
type ConfigurationParser = (text: string) => {
[x: string]: any;
};
type ConfigurationParser = (text: string) => Configuration;
/**
* Called with the result of the readConfig operation.
*/
type ReadConfigCallback = (err: Error, config?: {
[x: string]: any;
}) => void;
type ReadConfigCallback = (err: Error | null, config?: Configuration) => void;
/**
* Lint specified Markdown files.
*
* @param {Options} options Configuration options.
* @returns {Promise<LintResults>} Results object.
*/
declare function markdownlintPromise(options: Options): Promise<LintResults>;
/**
* Read specified configuration file.
*
* @param {string} file Configuration file name.
* @param {ConfigurationParser[]} [parsers] Parsing function(s).
* @returns {Promise<Configuration>} Configuration object.
*/
declare function readConfigPromise(file: string, parsers?: ConfigurationParser[]): Promise<Configuration>;

View file

@ -4,6 +4,7 @@
const fs = require("fs");
const path = require("path");
const util = require("util");
const markdownIt = require("markdown-it");
const rules = require("./rules");
const helpers = require("../helpers");
@ -11,7 +12,6 @@ const cache = require("./cache");
const deprecatedRuleNames = [ "MD002", "MD006" ];
/**
* Validate the list of rules for structure and reuse.
*
@ -834,6 +834,18 @@ function markdownlint(options, callback) {
return lintInput(options, false, callback);
}
const markdownlintPromisify = util.promisify(markdownlint);
/**
* Lint specified Markdown files.
*
* @param {Options} options Configuration options.
* @returns {Promise<LintResults>} Results object.
*/
function markdownlintPromise(options) {
return markdownlintPromisify(options);
}
/**
* Lint specified Markdown files synchronously.
*
@ -928,6 +940,20 @@ function readConfig(file, parsers, callback) {
});
}
const readConfigPromisify = util.promisify(readConfig);
/**
* Read specified configuration file.
*
* @param {string} file Configuration file name.
* @param {ConfigurationParser[]} [parsers] Parsing function(s).
* @returns {Promise<Configuration>} Configuration object.
*/
function readConfigPromise(file, parsers) {
// @ts-ignore
return readConfigPromisify(file, parsers);
}
/**
* Read specified configuration file synchronously.
*
@ -959,10 +985,14 @@ function readConfigSync(file, parsers) {
return config;
}
// Export a/synchronous APIs
// Export a/synchronous/Promise APIs
markdownlint.sync = markdownlintSync;
markdownlint.readConfig = readConfig;
markdownlint.readConfigSync = readConfigSync;
markdownlint.promises = {
"markdownlint": markdownlintPromise,
"readConfig": readConfigPromise
};
module.exports = markdownlint;
// Type declarations
@ -1081,11 +1111,8 @@ module.exports = markdownlint;
* Lint results (for resultVersion 3).
*
* @typedef {Object.<string, LintError[]>} LintResults
* @property {ToStringCallback} toString String representation.
*/
// The following should be part of the LintResults typedef, but that causes
// TypeScript to "forget" about the string map.
// * @property {ToStringCallback} toString String representation.
// https://github.com/microsoft/TypeScript/issues/34911
/**
* Lint error.