Skip to content

Commit

Permalink
Better display of rule option default for array option (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Nov 16, 2023
1 parent 7cbab78 commit 9061a51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
13 changes: 13 additions & 0 deletions lib/rule-options-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const COLUMN_TYPE_DEFAULT_PRESENCE_AND_ORDERING: {
[COLUMN_TYPE.DEPRECATED]: true,
};

/**
* Output could look like:
* - `[]`
* - [`hello world`, `1`, `2`, `true`]
*/
function arrayToString(arr: readonly unknown[]): string {
return `${arr.length === 0 ? '`' : ''}[${arr.length > 0 ? '`' : ''}${arr.join(
'`, `'
)}${arr.length > 0 ? '`' : ''}]${arr.length === 0 ? '`' : ''}`;
}

function ruleOptionToColumnValues(ruleOption: RuleOption): {
[key in COLUMN_TYPE]: string | undefined;
} {
Expand All @@ -55,6 +66,8 @@ function ruleOptionToColumnValues(ruleOption: RuleOption): {
[COLUMN_TYPE.DEFAULT]:
ruleOption.default === undefined
? undefined
: Array.isArray(ruleOption.default)
? arrayToString(ruleOption.default)
: `\`${String(ruleOption.default)}\``,
[COLUMN_TYPE.DEPRECATED]: ruleOption.deprecated ? 'Yes' : undefined,
[COLUMN_TYPE.DESCRIPTION]: ruleOption.description,
Expand Down
24 changes: 13 additions & 11 deletions test/lib/generate/__snapshots__/rule-options-list-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ exports[`generate (rule options list) basic generates the documentation 1`] = `
## Options
<!-- begin auto-generated rule options list -->
| Name | Description | Type | Choices | Default | Required | Deprecated |
| :------------------------- | :---------------------------- | :------------------ | :---------------- | :------- | :------- | :--------- |
| \`arr1\` | | Array | | | | |
| \`arrWithArrType\` | | String, Boolean | | | | |
| \`arrWithArrTypeSingleItem\` | | String | | | | |
| \`arrWithItemsArrayType\` | | (String, Boolean)[] | | | | |
| \`arrWithItemsType\` | | String[] | | | | |
| \`bar\` | Choose how to use the rule. | String | \`always\`, \`never\` | \`always\` | Yes | |
| \`baz\` | | | | \`true\` | Yes | |
| \`biz\` | | | | | | |
| \`foo\` | Enable some kind of behavior. | Boolean | | \`false\` | | Yes |
| Name | Description | Type | Choices | Default | Required | Deprecated |
| :------------------------- | :---------------------------- | :------------------ | :---------------- | :------------------------------------- | :------- | :--------- |
| \`arr1\` | | Array | | | | |
| \`arrWithArrType\` | | String, Boolean | | | | |
| \`arrWithArrTypeSingleItem\` | | String | | | | |
| \`arrWithDefault\` | | Array | | [\`hello world\`, \`1\`, \`2\`, \`3\`, \`true\`] | | |
| \`arrWithDefaultEmpty\` | | Array | | \`[]\` | | |
| \`arrWithItemsArrayType\` | | (String, Boolean)[] | | | | |
| \`arrWithItemsType\` | | String[] | | | | |
| \`bar\` | Choose how to use the rule. | String | \`always\`, \`never\` | \`always\` | Yes | |
| \`baz\` | | | | \`true\` | Yes | |
| \`biz\` | | | | | | |
| \`foo\` | Enable some kind of behavior. | Boolean | | \`false\` | | Yes |
<!-- end auto-generated rule options list -->"
`;
Expand Down
8 changes: 8 additions & 0 deletions test/lib/generate/rule-options-list-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ describe('generate (rule options list)', function () {
type: ["string", "boolean"]
}
},
arrWithDefaultEmpty: {
type: "array",
default: [],
},
arrWithDefault: {
type: "array",
default: ['hello world', 1, 2, 3, true],
},
},
required: ["bar"],
additionalProperties: false
Expand Down

0 comments on commit 9061a51

Please sign in to comment.