markdownlint/lib/parse-configuration.mjs
David Anson 7beb9fc9d0
Some checks are pending
Checkers / linkcheck (push) Waiting to run
Checkers / spellcheck (push) Waiting to run
CI / build (20, macos-latest) (push) Waiting to run
CI / build (20, ubuntu-latest) (push) Waiting to run
CI / build (20, windows-latest) (push) Waiting to run
CI / build (22, macos-latest) (push) Waiting to run
CI / build (22, ubuntu-latest) (push) Waiting to run
CI / build (22, windows-latest) (push) Waiting to run
CI / build (24, macos-latest) (push) Waiting to run
CI / build (24, ubuntu-latest) (push) Waiting to run
CI / build (24, windows-latest) (push) Waiting to run
CI / pnpm (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
TestRepos / build (latest, ubuntu-latest) (push) Waiting to run
UpdateTestRepos / update (push) Waiting to run
Address new TypeScript warnings in core files, improve type definitions.
2025-10-11 16:48:18 -07:00

47 lines
1.4 KiB
JavaScript

// @ts-check
/**
* Result of a call to parseConfiguration.
*
* @typedef {Object} ParseConfigurationResult
* @property {import("markdownlint").Configuration | null} config Configuration object if successful.
* @property {string | null} message Error message if an error occurred.
*/
/**
* Parse the content of a configuration file.
*
* @param {string} name Name of the configuration file.
* @param {string} content Configuration content.
* @param {import("markdownlint").ConfigurationParser[]} [parsers] Parsing function(s).
* @returns {ParseConfigurationResult} Parse configuration result.
*/
export default function parseConfiguration(name, content, parsers) {
let config = null;
let message = null;
const errors = [];
let index = 0;
// Try each parser
const failed = (parsers || [ JSON.parse ]).every((parser) => {
try {
const result = parser(content);
config = (result && (typeof result === "object") && !Array.isArray(result)) ? result : {};
// Succeeded
return false;
// eslint-disable-next-line jsdoc/reject-any-type
} catch(/** @type {any} */ error) {
errors.push(`Parser ${index++}: ${error?.message}`);
}
// Failed, try the next parser
return true;
});
// Message if unable to parse
if (failed) {
errors.unshift(`Unable to parse '${name}'`);
message = errors.join("; ");
}
return {
config,
message
};
}