Skip to content

Commit

Permalink
Better variable name for rule details (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Dec 14, 2022
1 parent 8b909dd commit 9d3380f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
8 changes: 4 additions & 4 deletions lib/generator.ts
Expand Up @@ -145,7 +145,7 @@ export async function generate(path: string, options?: GenerateOptions) {
options?.urlRuleDoc ?? OPTION_DEFAULTS[OPTION_TYPE.URL_RULE_DOC];

// Gather details about rules.
const details: readonly RuleDetails[] = Object.entries(plugin.rules)
const ruleDetails: readonly RuleDetails[] = Object.entries(plugin.rules)
.map(([name, rule]): RuleDetails => {
return typeof rule === 'object'
? // Object-style rule.
Expand Down Expand Up @@ -176,15 +176,15 @@ export async function generate(path: string, options?: GenerateOptions) {
})
.filter(
// Filter out deprecated rules from being checked, displayed, or updated if the option is set.
(details) => !ignoreDeprecatedRules || !details.deprecated
(ruleDetails) => !ignoreDeprecatedRules || !ruleDetails.deprecated
)
.sort(({ name: a }, { name: b }) =>
a.toLowerCase().localeCompare(b.toLowerCase())
);

// Update rule doc for each rule.
let initializedRuleDoc = false;
for (const { name, description, schema } of details) {
for (const { name, description, schema } of ruleDetails) {
const pathToDoc = replaceRulePlaceholder(join(path, pathRuleDoc), name);

if (!existsSync(pathToDoc)) {
Expand Down Expand Up @@ -284,7 +284,7 @@ export async function generate(path: string, options?: GenerateOptions) {
const fileContents = readFileSync(pathToFile, 'utf8');
const fileContentsNew = await postprocess(
updateRulesList(
details,
ruleDetails,
fileContents,
plugin,
configsToRules,
Expand Down
43 changes: 26 additions & 17 deletions lib/rule-list-columns.ts
Expand Up @@ -19,14 +19,16 @@ import type { RuleDetails, ConfigsToRules, Plugin } from './types.js';
export const COLUMN_HEADER: {
[key in COLUMN_TYPE]:
| string
| ((data: { details: readonly RuleDetails[] }) => string);
| ((data: { ruleDetails: readonly RuleDetails[] }) => string);
} = {
[COLUMN_TYPE.NAME]: ({ details }) => {
const ruleNames = details.map((detail) => detail.name);
[COLUMN_TYPE.NAME]: ({ ruleDetails }) => {
const ruleNames = ruleDetails.map((ruleDetail) => ruleDetail.name);
const longestRuleNameLength = Math.max(
...ruleNames.map(({ length }) => length)
);
const ruleDescriptions = details.map((detail) => detail.description);
const ruleDescriptions = ruleDetails.map(
(ruleDetail) => ruleDetail.description
);
const longestRuleDescriptionLength = Math.max(
...ruleDescriptions.map((description) =>
description ? description.length : 0
Expand Down Expand Up @@ -67,7 +69,7 @@ export const COLUMN_HEADER: {
*/
export function getColumns(
plugin: Plugin,
details: readonly RuleDetails[],
ruleDetails: readonly RuleDetails[],
configsToRules: ConfigsToRules,
ruleListColumns: readonly COLUMN_TYPE[],
pluginPrefix: string,
Expand Down Expand Up @@ -101,23 +103,30 @@ export function getColumns(
ignoreConfig,
SEVERITY_TYPE.warn
).length > 0,
[COLUMN_TYPE.DEPRECATED]: details.some((detail) => detail.deprecated),
[COLUMN_TYPE.DESCRIPTION]: details.some((detail) => detail.description),
[COLUMN_TYPE.FIXABLE]: details.some((detail) => detail.fixable),
[COLUMN_TYPE.FIXABLE_AND_HAS_SUGGESTIONS]: details.some(
(detail) => detail.fixable || detail.hasSuggestions
[COLUMN_TYPE.DEPRECATED]: ruleDetails.some(
(ruleDetail) => ruleDetail.deprecated
),
[COLUMN_TYPE.DESCRIPTION]: ruleDetails.some(
(ruleDetail) => ruleDetail.description
),
[COLUMN_TYPE.HAS_SUGGESTIONS]: details.some(
(detail) => detail.hasSuggestions
[COLUMN_TYPE.FIXABLE]: ruleDetails.some((ruleDetail) => ruleDetail.fixable),
[COLUMN_TYPE.FIXABLE_AND_HAS_SUGGESTIONS]: ruleDetails.some(
(ruleDetail) => ruleDetail.fixable || ruleDetail.hasSuggestions
),
[COLUMN_TYPE.HAS_SUGGESTIONS]: ruleDetails.some(
(ruleDetail) => ruleDetail.hasSuggestions
),
[COLUMN_TYPE.NAME]: true,
[COLUMN_TYPE.OPTIONS]: details.some((detail) => hasOptions(detail.schema)),
[COLUMN_TYPE.REQUIRES_TYPE_CHECKING]: details.some(
(detail) => detail.requiresTypeChecking
[COLUMN_TYPE.OPTIONS]: ruleDetails.some((ruleDetail) =>
hasOptions(ruleDetail.schema)
),
[COLUMN_TYPE.REQUIRES_TYPE_CHECKING]: ruleDetails.some(
(ruleDetail) => ruleDetail.requiresTypeChecking
),
// Show type column only if we found at least one rule with a standard type.
[COLUMN_TYPE.TYPE]: details.some(
(detail) => detail.type && RULE_TYPES.includes(detail.type as any) // eslint-disable-line @typescript-eslint/no-explicit-any
[COLUMN_TYPE.TYPE]: ruleDetails.some(
(ruleDetail) =>
ruleDetail.type && RULE_TYPES.includes(ruleDetail.type as any) // eslint-disable-line @typescript-eslint/no-explicit-any
),
};

Expand Down
34 changes: 20 additions & 14 deletions lib/rule-list.ts
Expand Up @@ -171,7 +171,7 @@ function buildRuleRow(

function generateRulesListMarkdown(
columns: Record<COLUMN_TYPE, boolean>,
details: readonly RuleDetails[],
ruleDetails: readonly RuleDetails[],
configsToRules: ConfigsToRules,
pluginPrefix: string,
pathPlugin: string,
Expand All @@ -190,15 +190,15 @@ function generateRulesListMarkdown(
const headerStrOrFn = COLUMN_HEADER[columnType];
return [
typeof headerStrOrFn === 'function'
? headerStrOrFn({ details })
? headerStrOrFn({ ruleDetails })
: headerStrOrFn,
];
});

return markdownTable(
[
listHeaderRow,
...details.map((rule: RuleDetails) =>
...ruleDetails.map((rule: RuleDetails) =>
buildRuleRow(
columns,
rule,
Expand All @@ -222,7 +222,7 @@ function generateRulesListMarkdown(
*/
function generateRulesListMarkdownWithRuleListSplit(
columns: Record<COLUMN_TYPE, boolean>,
details: readonly RuleDetails[],
ruleDetails: readonly RuleDetails[],
plugin: Plugin,
configsToRules: ConfigsToRules,
pluginPrefix: string,
Expand All @@ -236,8 +236,8 @@ function generateRulesListMarkdownWithRuleListSplit(
urlRuleDoc?: string
): string {
const values = new Set(
details.map((detail) =>
getPropertyFromRule(plugin, detail.name, ruleListSplit)
ruleDetails.map((ruleDetail) =>
getPropertyFromRule(plugin, ruleDetail.name, ruleListSplit)
)
);
const valuesAll = [...values.values()];
Expand All @@ -252,8 +252,10 @@ function generateRulesListMarkdownWithRuleListSplit(

// 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 = details.filter((detail) =>
isConsideredFalse(getPropertyFromRule(plugin, detail.name, ruleListSplit))
const rulesForThisValue = ruleDetails.filter((ruleDetail) =>
isConsideredFalse(
getPropertyFromRule(plugin, ruleDetail.name, ruleListSplit)
)
);
parts.push(
generateRulesListMarkdown(
Expand Down Expand Up @@ -283,8 +285,12 @@ function generateRulesListMarkdownWithRuleListSplit(
for (const value of valuesNew.sort((a, b) =>
String(a).toLowerCase().localeCompare(String(b).toLowerCase())
)) {
const rulesForThisValue = details.filter((detail) => {
const property = getPropertyFromRule(plugin, detail.name, ruleListSplit);
const rulesForThisValue = ruleDetails.filter((ruleDetail) => {
const property = getPropertyFromRule(
plugin,
ruleDetail.name,
ruleListSplit
);
return (
property === value || (value === true && isBooleanableTrue(property))
);
Expand Down Expand Up @@ -322,7 +328,7 @@ function generateRulesListMarkdownWithRuleListSplit(
}

export function updateRulesList(
details: readonly RuleDetails[],
ruleDetails: readonly RuleDetails[],
markdown: string,
plugin: Plugin,
configsToRules: ConfigsToRules,
Expand Down Expand Up @@ -381,7 +387,7 @@ export function updateRulesList(
// Determine columns to include in the rules list.
const columns = getColumns(
plugin,
details,
ruleDetails,
configsToRules,
ruleListColumns,
pluginPrefix,
Expand All @@ -403,7 +409,7 @@ export function updateRulesList(
const list = ruleListSplit
? generateRulesListMarkdownWithRuleListSplit(
columns,
details,
ruleDetails,
plugin,
configsToRules,
pluginPrefix,
Expand All @@ -418,7 +424,7 @@ export function updateRulesList(
)
: generateRulesListMarkdown(
columns,
details,
ruleDetails,
configsToRules,
pluginPrefix,
pathPlugin,
Expand Down

0 comments on commit 9d3380f

Please sign in to comment.