Refactor to move parseConfiguration function into a separate file, add stronger typing, add first test case.

This commit is contained in:
David Anson 2025-02-28 20:25:44 -08:00
parent c4d15e0d2a
commit 1f237e6c54
4 changed files with 72 additions and 38 deletions

View file

@ -0,0 +1,42 @@
// @ts-check
/**
* Result of a call to parseConfiguration.
*
* @typedef {Object} ParseConfigurationResult
* @property {Object | null} config Configuration object if successful.
* @property {string} 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.mjs").ConfigurationParser[]} [parsers] Parsing function(s).
* @returns {ParseConfigurationResult} Parse configuration result.
*/
export default function parseConfiguration(name, content, parsers) {
let config = null;
let message = "";
const errors = [];
let index = 0;
// Try each parser
(parsers || [ JSON.parse ]).every((parser) => {
try {
config = parser(content);
} catch (error) {
errors.push(`Parser ${index++}: ${error.message}`);
}
return !config;
});
// Message if unable to parse
if (!config) {
errors.unshift(`Unable to parse '${name}'`);
message = errors.join("; ");
}
return {
config,
message
};
}