Skip to content

Commit 9061a51

Browse files
authored
Better display of rule option default for array option (#503)
1 parent 7cbab78 commit 9061a51

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

lib/rule-options-list.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ const COLUMN_TYPE_DEFAULT_PRESENCE_AND_ORDERING: {
4545
[COLUMN_TYPE.DEPRECATED]: true,
4646
};
4747

48+
/**
49+
* Output could look like:
50+
* - `[]`
51+
* - [`hello world`, `1`, `2`, `true`]
52+
*/
53+
function arrayToString(arr: readonly unknown[]): string {
54+
return `${arr.length === 0 ? '`' : ''}[${arr.length > 0 ? '`' : ''}${arr.join(
55+
'`, `'
56+
)}${arr.length > 0 ? '`' : ''}]${arr.length === 0 ? '`' : ''}`;
57+
}
58+
4859
function ruleOptionToColumnValues(ruleOption: RuleOption): {
4960
[key in COLUMN_TYPE]: string | undefined;
5061
} {
@@ -55,6 +66,8 @@ function ruleOptionToColumnValues(ruleOption: RuleOption): {
5566
[COLUMN_TYPE.DEFAULT]:
5667
ruleOption.default === undefined
5768
? undefined
69+
: Array.isArray(ruleOption.default)
70+
? arrayToString(ruleOption.default)
5871
: `\`${String(ruleOption.default)}\``,
5972
[COLUMN_TYPE.DEPRECATED]: ruleOption.deprecated ? 'Yes' : undefined,
6073
[COLUMN_TYPE.DESCRIPTION]: ruleOption.description,

test/lib/generate/__snapshots__/rule-options-list-test.ts.snap

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ exports[`generate (rule options list) basic generates the documentation 1`] = `
77
## Options
88
<!-- begin auto-generated rule options list -->
99
10-
| Name | Description | Type | Choices | Default | Required | Deprecated |
11-
| :------------------------- | :---------------------------- | :------------------ | :---------------- | :------- | :------- | :--------- |
12-
| \`arr1\` | | Array | | | | |
13-
| \`arrWithArrType\` | | String, Boolean | | | | |
14-
| \`arrWithArrTypeSingleItem\` | | String | | | | |
15-
| \`arrWithItemsArrayType\` | | (String, Boolean)[] | | | | |
16-
| \`arrWithItemsType\` | | String[] | | | | |
17-
| \`bar\` | Choose how to use the rule. | String | \`always\`, \`never\` | \`always\` | Yes | |
18-
| \`baz\` | | | | \`true\` | Yes | |
19-
| \`biz\` | | | | | | |
20-
| \`foo\` | Enable some kind of behavior. | Boolean | | \`false\` | | Yes |
10+
| Name | Description | Type | Choices | Default | Required | Deprecated |
11+
| :------------------------- | :---------------------------- | :------------------ | :---------------- | :------------------------------------- | :------- | :--------- |
12+
| \`arr1\` | | Array | | | | |
13+
| \`arrWithArrType\` | | String, Boolean | | | | |
14+
| \`arrWithArrTypeSingleItem\` | | String | | | | |
15+
| \`arrWithDefault\` | | Array | | [\`hello world\`, \`1\`, \`2\`, \`3\`, \`true\`] | | |
16+
| \`arrWithDefaultEmpty\` | | Array | | \`[]\` | | |
17+
| \`arrWithItemsArrayType\` | | (String, Boolean)[] | | | | |
18+
| \`arrWithItemsType\` | | String[] | | | | |
19+
| \`bar\` | Choose how to use the rule. | String | \`always\`, \`never\` | \`always\` | Yes | |
20+
| \`baz\` | | | | \`true\` | Yes | |
21+
| \`biz\` | | | | | | |
22+
| \`foo\` | Enable some kind of behavior. | Boolean | | \`false\` | | Yes |
2123
2224
<!-- end auto-generated rule options list -->"
2325
`;

test/lib/generate/rule-options-list-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ describe('generate (rule options list)', function () {
6666
type: ["string", "boolean"]
6767
}
6868
},
69+
arrWithDefaultEmpty: {
70+
type: "array",
71+
default: [],
72+
},
73+
arrWithDefault: {
74+
type: "array",
75+
default: ['hello world', 1, 2, 3, true],
76+
},
6977
},
7078
required: ["bar"],
7179
additionalProperties: false

0 commit comments

Comments
 (0)