Skip to content

Commit

Permalink
Rename --split-by option to --rule-list-split (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Nov 27, 2022
1 parent aa0b21c commit 888ae3c
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ There's also an optional path argument if you need to point the CLI to an ESLint
| `--rule-doc-section-options` | Whether to require an "Options" or "Config" rule doc section and mention of any named options for rules with options. Default: `true`. |
| `--rule-doc-title-format` | The format to use for rule doc titles. Defaults to `desc-parens-prefix-name`. See choices in below [table](#--rule-doc-title-format). |
| `--rule-list-columns` | Ordered, comma-separated list of columns to display in rule list. Empty columns will be hidden. Choices: `configsError`, `configsOff`, `configsWarn`, `deprecated`, `description`, `fixable`, `fixableAndHasSuggestions` (off by default), `hasSuggestions`, `name`, `options` (off by default), `requiresTypeChecking`, `type` (off by default). Default: `name,description,configsError,configsWarn,configsOff,fixable,hasSuggestions,requiresTypeChecking,deprecated`. |
| `--split-by` | Rule property to split the rules list by. A separate list and header will be created for each value. Example: `meta.type`. |
| `--rule-list-split` | Rule property to split the rules list by. A separate list and header will be created for each value. Example: `meta.type`. |
| `--url-configs` | Link to documentation about the ESLint configurations exported by the plugin. |
| `--url-rule-doc` | Link to documentation for each rule. Useful when it differs from the rule doc path on disk (e.g. custom documentation site in use). Use `{name}` placeholder for the rule name. |

Expand Down
4 changes: 2 additions & 2 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function loadConfigFileOptions(): Promise<GenerateOptions> {
ruleDocSectionOptions: { type: 'boolean' },
ruleDocTitleFormat: { type: 'string' },
ruleListColumns: schemaStringArray,
splitBy: { type: 'string' },
ruleListSplit: { type: 'string' },
urlConfigs: { type: 'string' },
urlRuleDoc: { type: 'string' },
};
Expand Down Expand Up @@ -241,7 +241,7 @@ export async function run(
[]
)
.option(
'--split-by <property>',
'--rule-list-split <property>',
'(optional) Rule property to split the rules list by. A separate list and header will be created for each value. Example: `meta.type`.'
)
.option(
Expand Down
7 changes: 4 additions & 3 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export async function generate(path: string, options?: GenerateOptions) {
options?.ruleDocTitleFormat ??
OPTION_DEFAULTS[OPTION_TYPE.RULE_DOC_TITLE_FORMAT];
const ruleListColumns = parseRuleListColumnsOption(options?.ruleListColumns);
const splitBy = options?.splitBy ?? OPTION_DEFAULTS[OPTION_TYPE.SPLIT_BY];
const ruleListSplit =
options?.ruleListSplit ?? OPTION_DEFAULTS[OPTION_TYPE.RULE_LIST_SPLIT];
const urlConfigs =
options?.urlConfigs ?? OPTION_DEFAULTS[OPTION_TYPE.URL_CONFIGS];
const urlRuleDoc =
Expand Down Expand Up @@ -291,9 +292,9 @@ export async function generate(path: string, options?: GenerateOptions) {
configEmojis,
ignoreConfig,
ruleListColumns,
ruleListSplit,
urlConfigs,
urlRuleDoc,
splitBy
urlRuleDoc
),
resolve(pathToFile)
);
Expand Down
2 changes: 1 addition & 1 deletion lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const OPTION_DEFAULTS = {
)
.filter(([_col, enabled]) => enabled)
.map(([col]) => col),
[OPTION_TYPE.SPLIT_BY]: undefined,
[OPTION_TYPE.RULE_LIST_SPLIT]: undefined,
[OPTION_TYPE.URL_CONFIGS]: undefined,
[OPTION_TYPE.URL_RULE_DOC]: undefined,
} satisfies Record<OPTION_TYPE, unknown>; // Satisfies is used to ensure all options are included, but without losing type information.
54 changes: 32 additions & 22 deletions lib/rule-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ function generateRulesListMarkdown(
}

/**
* Generate multiple rule lists given the `splitBy` property.
* Generate multiple rule lists given the `ruleListSplit` property.
*/
function generateRulesListMarkdownWithSplitBy(
function generateRulesListMarkdownWithRuleListSplit(
columns: Record<COLUMN_TYPE, boolean>,
details: RuleDetails[],
plugin: Plugin,
Expand All @@ -226,12 +226,14 @@ function generateRulesListMarkdownWithSplitBy(
pathRuleList: string,
configEmojis: ConfigEmojis,
ignoreConfig: string[],
splitBy: string,
ruleListSplit: string,
headerLevel: number,
urlRuleDoc?: string
): string {
const values = new Set(
details.map((detail) => getPropertyFromRule(plugin, detail.name, splitBy))
details.map((detail) =>
getPropertyFromRule(plugin, detail.name, ruleListSplit)
)
);

// Common values for boolean properties.
Expand All @@ -247,15 +249,19 @@ function generateRulesListMarkdownWithSplitBy(
]);

if (values.size === 1 && DISABLED_VALUES.has([...values.values()][0])) {
throw new Error(`No rules found with --split-by property "${splitBy}".`);
throw new Error(
`No rules found with --rule-list-split property "${ruleListSplit}".`
);
}

const parts: string[] = [];

// Show any rules that don't have a value for this split-by property first, or for which the boolean property is off.
// 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 ([...DISABLED_VALUES.values()].some((val) => values.has(val))) {
const rulesForThisValue = details.filter((detail) =>
DISABLED_VALUES.has(getPropertyFromRule(plugin, detail.name, splitBy))
DISABLED_VALUES.has(
getPropertyFromRule(plugin, detail.name, ruleListSplit)
)
);
parts.push(
generateRulesListMarkdown(
Expand All @@ -278,21 +284,25 @@ function generateRulesListMarkdownWithSplitBy(
.filter((value) => !DISABLED_VALUES.has(value))
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))) {
const rulesForThisValue = details.filter(
(detail) => getPropertyFromRule(plugin, detail.name, splitBy) === value
(detail) =>
getPropertyFromRule(plugin, detail.name, ruleListSplit) === value
);

// Turn splitBy into a title.
// Turn ruleListSplit into a title.
// E.g. meta.docs.requiresTypeChecking to "Requires Type Checking".
// TODO: handle other types of variable casing.
const splitByParts = splitBy.split('.');
const splitByFinalPart = splitByParts[splitByParts.length - 1];
const splitByTitle = isCamelCase(splitByFinalPart)
? camelCaseStringToTitle(splitByParts[splitByParts.length - 1])
: splitByFinalPart;
const ruleListSplitParts = ruleListSplit.split('.');
const ruleListSplitFinalPart =
ruleListSplitParts[ruleListSplitParts.length - 1];
const ruleListSplitTitle = isCamelCase(ruleListSplitFinalPart)
? camelCaseStringToTitle(
ruleListSplitParts[ruleListSplitParts.length - 1]
)
: ruleListSplitFinalPart;

parts.push(
`${'#'.repeat(headerLevel)} ${
ENABLED_VALUES.has(value) ? splitByTitle : value
ENABLED_VALUES.has(value) ? ruleListSplitTitle : value
}`,
generateRulesListMarkdown(
columns,
Expand Down Expand Up @@ -324,9 +334,9 @@ export function updateRulesList(
configEmojis: ConfigEmojis,
ignoreConfig: string[],
ruleListColumns: COLUMN_TYPE[],
ruleListSplit?: string,
urlConfigs?: string,
urlRuleDoc?: string,
splitBy?: string
urlRuleDoc?: string
): string {
let listStartIndex = markdown.indexOf(BEGIN_RULE_LIST_MARKER);
let listEndIndex = markdown.indexOf(END_RULE_LIST_MARKER);
Expand Down Expand Up @@ -365,7 +375,7 @@ export function updateRulesList(

// Determine what header level to use for sub-lists based on the last seen header level.
const preListFinalHeaderLevel = findFinalHeaderLevel(preList);
const splitByHeaderLevel = preListFinalHeaderLevel
const ruleListSplitHeaderLevel = preListFinalHeaderLevel
? preListFinalHeaderLevel + 1
: 1;

Expand All @@ -391,8 +401,8 @@ export function updateRulesList(
);

// New rule list.
const list = splitBy
? generateRulesListMarkdownWithSplitBy(
const list = ruleListSplit
? generateRulesListMarkdownWithRuleListSplit(
columns,
details,
plugin,
Expand All @@ -403,8 +413,8 @@ export function updateRulesList(
pathRuleList,
configEmojis,
ignoreConfig,
splitBy,
splitByHeaderLevel,
ruleListSplit,
ruleListSplitHeaderLevel,
urlRuleDoc
)
: generateRulesListMarkdown(
Expand Down
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export enum OPTION_TYPE {
RULE_DOC_SECTION_OPTIONS = 'ruleDocSectionOptions',
RULE_DOC_TITLE_FORMAT = 'ruleDocTitleFormat',
RULE_LIST_COLUMNS = 'ruleListColumns',
SPLIT_BY = 'splitBy',
RULE_LIST_SPLIT = 'ruleListSplit',
URL_CONFIGS = 'urlConfigs',
URL_RULE_DOC = 'urlRuleDoc',
}
Expand Down Expand Up @@ -161,7 +161,7 @@ export type GenerateOptions = {
* A separate list and header will be created for each value.
* Example: `meta.type`.
*/
splitBy?: string;
ruleListSplit?: string;
/** Link to documentation about the ESLint configurations exported by the plugin. */
urlConfigs?: string;
/**
Expand Down
6 changes: 3 additions & 3 deletions test/lib/__snapshots__/cli-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ exports[`cli all CLI options and all config files options merges correctly, with
"hasSuggestions",
"type",
],
"splitBy": "meta.docs.foo-from-cli",
"ruleListSplit": "meta.docs.foo-from-cli",
"urlConfigs": "https://example.com/configs-url-from-cli",
"urlRuleDoc": "https://example.com/rule-doc-url-from-cli",
},
Expand Down Expand Up @@ -95,7 +95,7 @@ exports[`cli all CLI options, no config file options is called correctly 1`] = `
"ruleListColumns": [
"type",
],
"splitBy": "meta.docs.foo-from-cli",
"ruleListSplit": "meta.docs.foo-from-cli",
"urlConfigs": "https://example.com/configs-url-from-cli",
"urlRuleDoc": "https://example.com/rule-doc-url-from-cli",
},
Expand Down Expand Up @@ -140,7 +140,7 @@ exports[`cli all config files options, no CLI options is called correctly 1`] =
"fixable",
"hasSuggestions",
],
"splitBy": "meta.docs.foo-from-config-file",
"ruleListSplit": "meta.docs.foo-from-config-file",
"urlConfigs": "https://example.com/configs-url-from-config-file",
"urlRuleDoc": "https://example.com/rule-doc-url-from-config-file",
},
Expand Down
7 changes: 5 additions & 2 deletions test/lib/cli-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const configFileOptionsAll: { [key in OPTION_TYPE]: unknown } = {
ruleDocSectionOptions: false,
ruleDocTitleFormat: 'desc',
ruleListColumns: ['fixable', 'hasSuggestions'],
splitBy: 'meta.docs.foo-from-config-file',
ruleListSplit: 'meta.docs.foo-from-config-file',
urlConfigs: 'https://example.com/configs-url-from-config-file',
urlRuleDoc: 'https://example.com/rule-doc-url-from-config-file',
};
Expand Down Expand Up @@ -85,7 +85,10 @@ const cliOptionsAll: { [key in OPTION_TYPE]: string[] } = {

[OPTION_TYPE.RULE_LIST_COLUMNS]: ['--rule-list-columns', 'type'],

[OPTION_TYPE.SPLIT_BY]: ['--split-by', 'meta.docs.foo-from-cli'],
[OPTION_TYPE.RULE_LIST_SPLIT]: [
'--rule-list-split',
'meta.docs.foo-from-cli',
],

[OPTION_TYPE.URL_CONFIGS]: [
'--url-configs',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generate (--split-by) by nested property meta.docs.category splits the list 1`] = `
exports[`generate (--rule-list-split) by nested property meta.docs.category splits the list 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand All @@ -24,7 +24,7 @@ exports[`generate (--split-by) by nested property meta.docs.category splits the
"
`;

exports[`generate (--split-by) by type splits the list 1`] = `
exports[`generate (--rule-list-split) by type splits the list 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand All @@ -49,7 +49,7 @@ exports[`generate (--split-by) by type splits the list 1`] = `
"
`;

exports[`generate (--split-by) ignores case splits the list 1`] = `
exports[`generate (--rule-list-split) ignores case splits the list 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand All @@ -75,7 +75,7 @@ exports[`generate (--split-by) ignores case splits the list 1`] = `
"
`;

exports[`generate (--split-by) with boolean splits the list 1`] = `
exports[`generate (--rule-list-split) with boolean splits the list 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand All @@ -96,7 +96,7 @@ exports[`generate (--split-by) with boolean splits the list 1`] = `
"
`;

exports[`generate (--split-by) with no existing headers in file uses the proper sub-list header level 1`] = `
exports[`generate (--rule-list-split) with no existing headers in file uses the proper sub-list header level 1`] = `
"<!-- begin auto-generated rules list -->
| Name |
Expand All @@ -118,7 +118,7 @@ exports[`generate (--split-by) with no existing headers in file uses the proper
<!-- end auto-generated rules list -->"
`;

exports[`generate (--split-by) with one sub-list having no rules enabled by the config splits the list and still uses recommended config emoji in both lists 1`] = `
exports[`generate (--rule-list-split) with one sub-list having no rules enabled by the config splits the list and still uses recommended config emoji in both lists 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand All @@ -141,7 +141,7 @@ exports[`generate (--split-by) with one sub-list having no rules enabled by the
"
`;

exports[`generate (--split-by) with only a title in the rules file uses the proper sub-list header level 1`] = `
exports[`generate (--rule-list-split) with only a title in the rules file uses the proper sub-list header level 1`] = `
"# Rules
<!-- begin auto-generated rules list -->
Expand All @@ -164,7 +164,7 @@ exports[`generate (--split-by) with only a title in the rules file uses the prop
<!-- end auto-generated rules list -->"
`;

exports[`generate (--split-by) with unknown variable type splits the list but does not attempt to convert variable name to title 1`] = `
exports[`generate (--rule-list-split) with unknown variable type splits the list but does not attempt to convert variable name to title 1`] = `
"## Rules
<!-- begin auto-generated rules list -->
Expand Down

0 comments on commit 888ae3c

Please sign in to comment.