mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-21 21:30:47 +02:00
Remove import of node:util and manually implement promisify (improves browser scenario), improve JSDoc typing.
This commit is contained in:
parent
3599f694ba
commit
789190962c
5 changed files with 62 additions and 25 deletions
|
@ -676,7 +676,7 @@ Asynchronous API:
|
|||
* Read specified configuration file.
|
||||
*
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[] | ReadConfigCallback} parsers Parsing function.
|
||||
* @param {ConfigurationParser[] | ReadConfigCallback} [parsers] Parsing function(s).
|
||||
* @param {Object} [fs] File system implementation.
|
||||
* @param {ReadConfigCallback} [callback] Callback (err, result) function.
|
||||
* @returns {void}
|
||||
|
|
|
@ -54,7 +54,6 @@ function config(options) {
|
|||
"fs": false,
|
||||
"os": false,
|
||||
"path": false,
|
||||
"util": false,
|
||||
"module": require.resolve("./module-stub.cjs")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -463,13 +463,13 @@ declare function markdownlintSync(options: Options | null): LintResults;
|
|||
* Read specified configuration file.
|
||||
*
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[] | ReadConfigCallback} parsers Parsing
|
||||
* @param {ConfigurationParser[] | ReadConfigCallback} [parsers] Parsing
|
||||
* function(s).
|
||||
* @param {Object} [fs] File system implementation.
|
||||
* @param {ReadConfigCallback} [callback] Callback (err, result) function.
|
||||
* @returns {void}
|
||||
*/
|
||||
declare function readConfig(file: string, parsers: ConfigurationParser[] | ReadConfigCallback, fs?: any, callback?: ReadConfigCallback): void;
|
||||
declare function readConfig(file: string, parsers?: ConfigurationParser[] | ReadConfigCallback, fs?: any, callback?: ReadConfigCallback): void;
|
||||
/**
|
||||
* Read specified configuration file synchronously.
|
||||
*
|
||||
|
@ -498,11 +498,11 @@ declare function markdownlintPromise(options: Options): Promise<LintResults>;
|
|||
*
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[]} [parsers] Parsing function(s).
|
||||
* @param {Object} [fs] File system implementation.
|
||||
* @param {ConfigurationParser[] | undefined} parsers Parsing function(s).
|
||||
* @param {Object} fs File system implementation.
|
||||
* @returns {Promise<Configuration>} Configuration object.
|
||||
*/
|
||||
declare function extendConfigPromise(config: Configuration, file: string, parsers?: ConfigurationParser[], fs?: any): Promise<Configuration>;
|
||||
declare function extendConfigPromise(config: Configuration, file: string, parsers: ConfigurationParser[] | undefined, fs: any): Promise<Configuration>;
|
||||
/**
|
||||
* Read specified configuration file.
|
||||
*
|
||||
|
|
|
@ -5,7 +5,6 @@ import { createRequire } from "node:module";
|
|||
const dynamicRequire = createRequire(import.meta.url);
|
||||
import * as os from "node:os";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { initialize as cacheInitialize } from "./cache.mjs";
|
||||
import { version } from "./constants.mjs";
|
||||
import rules from "./rules.mjs";
|
||||
|
@ -959,8 +958,6 @@ function markdownlint(options, callback) {
|
|||
return lintInput(options, false, callback);
|
||||
}
|
||||
|
||||
const markdownlintPromisify = promisify && promisify(markdownlint);
|
||||
|
||||
/**
|
||||
* Lint specified Markdown files.
|
||||
*
|
||||
|
@ -968,8 +965,15 @@ const markdownlintPromisify = promisify && promisify(markdownlint);
|
|||
* @returns {Promise<LintResults>} Results object.
|
||||
*/
|
||||
function markdownlintPromise(options) {
|
||||
// @ts-ignore
|
||||
return markdownlintPromisify(options);
|
||||
return new Promise((resolve, reject) => {
|
||||
markdownlint(options, (error, results) => {
|
||||
if (error || !results) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1054,7 +1058,7 @@ function resolveConfigExtendsSync(configFile, referenceId, fs) {
|
|||
*
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[]} parsers Parsing
|
||||
* @param {ConfigurationParser[] | undefined} parsers Parsing
|
||||
* function(s).
|
||||
* @param {Object} fs File system implementation.
|
||||
* @param {ReadConfigCallback} callback Callback (err, result) function.
|
||||
|
@ -1090,27 +1094,32 @@ function extendConfig(config, file, parsers, fs, callback) {
|
|||
return callback(null, config);
|
||||
}
|
||||
|
||||
const extendConfigPromisify = promisify && promisify(extendConfig);
|
||||
|
||||
/**
|
||||
* Extend specified configuration object.
|
||||
*
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[]} [parsers] Parsing function(s).
|
||||
* @param {Object} [fs] File system implementation.
|
||||
* @param {ConfigurationParser[] | undefined} parsers Parsing function(s).
|
||||
* @param {Object} fs File system implementation.
|
||||
* @returns {Promise<Configuration>} Configuration object.
|
||||
*/
|
||||
function extendConfigPromise(config, file, parsers, fs) {
|
||||
// @ts-ignore
|
||||
return extendConfigPromisify(config, file, parsers, fs);
|
||||
return new Promise((resolve, reject) => {
|
||||
extendConfig(config, file, parsers, fs, (error, results) => {
|
||||
if (error || !results) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Read specified configuration file.
|
||||
*
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[] | ReadConfigCallback} parsers Parsing
|
||||
* @param {ConfigurationParser[] | ReadConfigCallback} [parsers] Parsing
|
||||
* function(s).
|
||||
* @param {Object} [fs] File system implementation.
|
||||
* @param {ReadConfigCallback} [callback] Callback (err, result) function.
|
||||
|
@ -1151,8 +1160,6 @@ function readConfig(file, parsers, fs, callback) {
|
|||
});
|
||||
}
|
||||
|
||||
const readConfigPromisify = promisify && promisify(readConfig);
|
||||
|
||||
/**
|
||||
* Read specified configuration file.
|
||||
*
|
||||
|
@ -1162,8 +1169,15 @@ const readConfigPromisify = promisify && promisify(readConfig);
|
|||
* @returns {Promise<Configuration>} Configuration object.
|
||||
*/
|
||||
function readConfigPromise(file, parsers, fs) {
|
||||
// @ts-ignore
|
||||
return readConfigPromisify(file, parsers, fs);
|
||||
return new Promise((resolve, reject) => {
|
||||
readConfig(file, parsers, fs, (error, results) => {
|
||||
if (error || !results) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
// @ts-check
|
||||
|
||||
import fs from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
const require = createRequire(import.meta.url);
|
||||
import os from "node:os";
|
||||
|
@ -516,7 +517,10 @@ test("extendSinglePromise", (t) => new Promise((resolve) => {
|
|||
t.plan(1);
|
||||
const expected = require("./config/config-child.json");
|
||||
markdownlint.promises.extendConfig(
|
||||
expected, "./test/config/config-child.json"
|
||||
expected,
|
||||
"./test/config/config-child.json",
|
||||
undefined,
|
||||
fs
|
||||
)
|
||||
.then((actual) => {
|
||||
t.deepEqual(actual, expected, "Config object not correct.");
|
||||
|
@ -524,6 +528,26 @@ test("extendSinglePromise", (t) => new Promise((resolve) => {
|
|||
});
|
||||
}));
|
||||
|
||||
test("extendBadPromise", (t) => new Promise((resolve) => {
|
||||
t.plan(2);
|
||||
markdownlint.promises.extendConfig(
|
||||
{
|
||||
"extends": "missing.json"
|
||||
},
|
||||
"./test/config/missing.json",
|
||||
undefined,
|
||||
fs
|
||||
)
|
||||
.then(
|
||||
null,
|
||||
(error) => {
|
||||
t.truthy(error, "Did not get an error for bad input.");
|
||||
t.true(error instanceof Error, "Error not instance of Error.");
|
||||
resolve();
|
||||
}
|
||||
);
|
||||
}));
|
||||
|
||||
test("extendCustomFileSystemPromise", (t) => new Promise((resolve) => {
|
||||
t.plan(4);
|
||||
const file = path.resolve("/dir/file.json");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue