Skip to content

Commit

Permalink
Refactor functions for generating and splitting the rules list (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Dec 19, 2022
1 parent b8844c9 commit 1bb2280
Showing 1 changed file with 79 additions and 73 deletions.
152 changes: 79 additions & 73 deletions lib/rule-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ function getConfigurationColumnValueForRule(
}

function buildRuleRow(
columnsEnabled: Record<COLUMN_TYPE, boolean>,
ruleName: string,
rule: RuleModule,
columnsEnabled: Record<COLUMN_TYPE, boolean>,
configsToRules: ConfigsToRules,
pluginPrefix: string,
pathPlugin: string,
Expand Down Expand Up @@ -171,8 +171,8 @@ function buildRuleRow(
}

function generateRulesListMarkdown(
columns: Record<COLUMN_TYPE, boolean>,
ruleNamesAndRules: RuleNamesAndRules,
columns: Record<COLUMN_TYPE, boolean>,
configsToRules: ConfigsToRules,
pluginPrefix: string,
pathPlugin: string,
Expand Down Expand Up @@ -201,9 +201,9 @@ function generateRulesListMarkdown(
listHeaderRow,
...ruleNamesAndRules.map(([name, rule]) =>
buildRuleRow(
columns,
name,
rule,
columns,
configsToRules,
pluginPrefix,
pathPlugin,
Expand All @@ -219,24 +219,60 @@ function generateRulesListMarkdown(
);
}

/**
* Generate multiple rule lists given the `ruleListSplit` property.
*/
function generateRulesListMarkdownWithRuleListSplit(
type RulesAndHeaders = { header?: string; rules: RuleNamesAndRules }[];
type RulesAndHeadersReadOnly = readonly {
header?: string;
rules: RuleNamesAndRules;
}[];

function generateRuleListMarkdownForRulesAndHeaders(
rulesAndHeaders: RulesAndHeadersReadOnly,
headerLevel: number,
columns: Record<COLUMN_TYPE, boolean>,
ruleNamesAndRules: RuleNamesAndRules,
plugin: Plugin,
configsToRules: ConfigsToRules,
pluginPrefix: string,
pathPlugin: string,
pathRuleDoc: string,
pathRuleList: string,
configEmojis: ConfigEmojis,
ignoreConfig: readonly string[],
ruleListSplit: string,
headerLevel: number,
urlRuleDoc?: string
): string {
const parts: string[] = [];

for (const { header, rules } of rulesAndHeaders) {
if (header) {
parts.push(`${'#'.repeat(headerLevel)} ${header}`);
}
parts.push(
generateRulesListMarkdown(
rules,
columns,
configsToRules,
pluginPrefix,
pathPlugin,
pathRuleDoc,
pathRuleList,
configEmojis,
ignoreConfig,
urlRuleDoc
)
);
}

return parts.join('\n\n');
}

/**
* Get the pairs of rules and headers for a given split property.
*/
function getRulesAndHeadersForSplit(
ruleNamesAndRules: RuleNamesAndRules,
plugin: Plugin,
ruleListSplit: string
): RulesAndHeadersReadOnly {
const rulesAndHeaders: RulesAndHeaders = [];

const values = new Set(
ruleNamesAndRules.map(([name]) =>
getPropertyFromRule(plugin, name, ruleListSplit)
Expand All @@ -250,27 +286,15 @@ function generateRulesListMarkdownWithRuleListSplit(
);
}

const parts: string[] = [];

// Show any rules that don't have a value for this rule-list-split property first, or for which the boolean property is off.
if (valuesAll.some((val) => isConsideredFalse(val))) {
const rulesForThisValue = ruleNamesAndRules.filter(([name]) =>
isConsideredFalse(getPropertyFromRule(plugin, name, ruleListSplit))
);
parts.push(
generateRulesListMarkdown(
columns,
rulesForThisValue,
configsToRules,
pluginPrefix,
pathPlugin,
pathRuleDoc,
pathRuleList,
configEmojis,
ignoreConfig,
urlRuleDoc
)
);

rulesAndHeaders.push({
rules: rulesForThisValue,
});
}

// For each possible non-disabled value, show a header and list of corresponding rules.
Expand Down Expand Up @@ -301,26 +325,13 @@ function generateRulesListMarkdownWithRuleListSplit(
transform: (str) => capitalizeOnlyFirstLetter(str),
});

parts.push(
`${'#'.repeat(headerLevel)} ${
isBooleanableTrue(value) ? ruleListSplitTitle : value // eslint-disable-line @typescript-eslint/restrict-template-expressions -- TODO: better handling to ensure value is a string.
}`,
generateRulesListMarkdown(
columns,
rulesForThisValue,
configsToRules,
pluginPrefix,
pathPlugin,
pathRuleDoc,
pathRuleList,
configEmojis,
ignoreConfig,
urlRuleDoc
)
);
rulesAndHeaders.push({
header: String(isBooleanableTrue(value) ? ruleListSplitTitle : value),
rules: rulesForThisValue,
});
}

return parts.join('\n\n');
return rulesAndHeaders;
}

export function updateRulesList(
Expand Down Expand Up @@ -401,35 +412,30 @@ export function updateRulesList(
urlConfigs
);

// Determine the pairs of rules and headers based on any split property.
const rulesAndHeaders: RulesAndHeaders = [];
if (ruleListSplit) {
rulesAndHeaders.push(
...getRulesAndHeadersForSplit(ruleNamesAndRules, plugin, ruleListSplit)
);
} else {
rulesAndHeaders.push({ rules: ruleNamesAndRules });
}

// New rule list.
const list = ruleListSplit
? generateRulesListMarkdownWithRuleListSplit(
columns,
ruleNamesAndRules,
plugin,
configsToRules,
pluginPrefix,
pathPlugin,
pathRuleDoc,
pathRuleList,
configEmojis,
ignoreConfig,
ruleListSplit,
ruleListSplitHeaderLevel,
urlRuleDoc
)
: generateRulesListMarkdown(
columns,
ruleNamesAndRules,
configsToRules,
pluginPrefix,
pathPlugin,
pathRuleDoc,
pathRuleList,
configEmojis,
ignoreConfig,
urlRuleDoc
);
const list = generateRuleListMarkdownForRulesAndHeaders(
rulesAndHeaders,
ruleListSplitHeaderLevel,
columns,
configsToRules,
pluginPrefix,
pathPlugin,
pathRuleDoc,
pathRuleList,
configEmojis,
ignoreConfig,
urlRuleDoc
);

const newContent = `${legend ? `${legend}\n\n` : ''}${list}`;

Expand Down

0 comments on commit 1bb2280

Please sign in to comment.