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

@ -8,6 +8,7 @@ import { requireMarkdownItCjs } from "./defer-require.cjs";
import { resolveModule } from "./resolve-module.cjs";
import rules from "./rules.mjs";
import { parse as micromarkParse } from "./micromark-parse.mjs";
import parseConfiguration from "./parse-configuration.mjs";
import * as helpers from "../helpers/helpers.cjs";
/**
@ -268,39 +269,6 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
return effectiveConfig;
}
/**
* Parse the content of a configuration file.
*
* @param {string} name Name of the configuration file.
* @param {string} content Configuration content.
* @param {ConfigurationParser[] | null} [parsers] Parsing function(s).
* @returns {Object} Configuration object and error message.
*/
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
};
}
/**
* Create a mapping of enabled rules per line.
*
@ -309,7 +277,7 @@ function parseConfiguration(name, content, parsers) {
* @param {string[]} frontMatterLines List of front matter lines.
* @param {boolean} noInlineConfig Whether to allow inline configuration.
* @param {Configuration} config Configuration object.
* @param {ConfigurationParser[] | null} configParsers Configuration parsers.
* @param {ConfigurationParser[] | undefined} configParsers Configuration parsers.
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule
* names.
* @returns {Object} Effective configuration and enabled rules per line number.
@ -448,7 +416,7 @@ function getEnabledRulesPerLineNumber(
* @param {string} content Markdown content.
* @param {MarkdownItFactory} markdownItFactory Function to create a markdown-it parser.
* @param {Configuration} config Configuration object.
* @param {ConfigurationParser[] | null} configParsers Configuration parsers.
* @param {ConfigurationParser[] | undefined} configParsers Configuration parsers.
* @param {RegExp | null} frontMatter Regular expression for front matter.
* @param {boolean} handleRuleFailures Whether to handle exceptions in rules.
* @param {boolean} noInlineConfig Whether to allow inline configuration.
@ -776,7 +744,7 @@ function lintContent(
* @param {string} file Path of file to lint.
* @param {MarkdownItFactory} markdownItFactory Function to create a markdown-it parser.
* @param {Configuration} config Configuration object.
* @param {ConfigurationParser[] | null} configParsers Configuration parsers.
* @param {ConfigurationParser[] | undefined} configParsers Configuration parsers.
* @param {RegExp | null} frontMatter Regular expression for front matter.
* @param {boolean} handleRuleFailures Whether to handle exceptions in rules.
* @param {boolean} noInlineConfig Whether to allow inline configuration.
@ -869,7 +837,7 @@ function lintInput(options, synchronous, callback) {
const strings = options.strings || {};
const stringsKeys = Object.keys(strings);
const config = options.config || { "default": true };
const configParsers = options.configParsers || null;
const configParsers = options.configParsers || undefined;
const frontMatter = (options.frontMatter === undefined) ?
helpers.frontMatterRe :
options.frontMatter;

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
};
}