mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Refactor to move parseConfiguration function into a separate file, add stronger typing, add first test case.
This commit is contained in:
parent
c4d15e0d2a
commit
1f237e6c54
4 changed files with 72 additions and 38 deletions
42
lib/parse-configuration.mjs
Normal file
42
lib/parse-configuration.mjs
Normal 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
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue