Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle when an option could have multiple types in rule option lists #498

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/rule-options-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { markdownTable } from 'markdown-table';
import type { RuleModule } from './types.js';
import { RuleOption, getAllNamedOptions } from './rule-options.js';
import { capitalizeOnlyFirstLetter, sanitizeMarkdownTable } from './string.js';
import { sanitizeMarkdownTable } from './string.js';

export enum COLUMN_TYPE {
// Alphabetical order.
Expand Down Expand Up @@ -64,9 +64,7 @@ function ruleOptionToColumnValues(ruleOption: RuleOption): {
: undefined,
[COLUMN_TYPE.NAME]: `\`${ruleOption.name}\``,
[COLUMN_TYPE.REQUIRED]: ruleOption.required ? 'Yes' : undefined,
[COLUMN_TYPE.TYPE]: ruleOption.type
? capitalizeOnlyFirstLetter(ruleOption.type)
: undefined,
[COLUMN_TYPE.TYPE]: ruleOption.type || undefined,
};

return columns;
Expand Down
17 changes: 15 additions & 2 deletions lib/rule-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import traverse from 'json-schema-traverse';
import type { JSONSchema } from '@typescript-eslint/utils';
import { capitalizeOnlyFirstLetter } from './string.js';

export type RuleOption = {
name: string;
Expand All @@ -11,6 +12,14 @@ export type RuleOption = {
deprecated?: boolean;
};

function typeToString(
type: JSONSchema.JSONSchema4TypeName[] | JSONSchema.JSONSchema4TypeName
): string {
return Array.isArray(type)
? type.map((item) => capitalizeOnlyFirstLetter(item)).join(', ')
: capitalizeOnlyFirstLetter(type);
}

/**
* Gather a list of named options from a rule schema.
* @param jsonSchema - the JSON schema to check
Expand Down Expand Up @@ -43,9 +52,13 @@ export function getAllNamedOptions(
value.type === 'array' &&
!Array.isArray(value.items) &&
value.items?.type
? `${value.items.type.toString()}[]`
? `${
Array.isArray(value.items.type) && value.items.type.length > 1
? `(${typeToString(value.items.type)})`
: typeToString(value.items.type)
}[]`
: value.type
? value.type.toString()
? typeToString(value.type)
: undefined,
description: value.description,
default: value.default,
Expand Down
19 changes: 11 additions & 8 deletions test/lib/generate/__snapshots__/rule-options-list-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ 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 | | | | |
| \`arr2\` | | 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 | | | | |
| \`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
14 changes: 13 additions & 1 deletion test/lib/generate/rule-options-list-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@ describe('generate (rule options list)', function () {
arr1: {
type: "array",
},
arr2: {
arrWithArrType: {
type: ["string", "boolean"],
},
arrWithArrTypeSingleItem: {
type: ["string"],
},
arrWithItemsType: {
type: "array",
items: {
type: "string"
}
},
arrWithItemsArrayType: {
type: "array",
items: {
type: ["string", "boolean"]
}
},
},
required: ["bar"],
additionalProperties: false
Expand Down
75 changes: 65 additions & 10 deletions test/lib/rule-options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething1",
"required": false,
"type": "boolean",
"type": "Boolean",
},
{
"default": undefined,
Expand All @@ -64,7 +64,7 @@ describe('rule options', function () {
],
"name": "optionToDoSomething2",
"required": false,
"type": "string",
"type": "String",
},
{
"default": undefined,
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething",
"required": false,
"type": "boolean",
"type": "Boolean",
},
]
`);
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething1",
"required": false,
"type": "boolean",
"type": "Boolean",
},
{
"default": false,
Expand All @@ -150,7 +150,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething2",
"required": false,
"type": "boolean",
"type": "Boolean",
},
]
`);
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething",
"required": false,
"type": "boolean",
"type": "Boolean",
},
]
`);
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething",
"required": false,
"type": "boolean",
"type": "Boolean",
},
]
`);
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething1",
"required": false,
"type": "object[]",
"type": "Object[]",
},
{
"default": undefined,
Expand All @@ -264,7 +264,7 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething2",
"required": false,
"type": "array",
"type": "Array",
},
{
"default": false,
Expand All @@ -273,7 +273,62 @@ describe('rule options', function () {
"enum": undefined,
"name": "optionToDoSomething2",
"required": false,
"type": "boolean",
"type": "Boolean",
},
]
`);
});

it('handles when type is an array', function () {
expect(
getAllNamedOptions([
{
type: 'object',
properties: {
optionToDoSomething1: {
type: 'array',
items: {
type: ['boolean', 'string'],
},
},
optionToDoSomething2: {
type: ['boolean', 'string'],
},
optionToDoSomething3: {
type: ['boolean'],
},
},
additionalProperties: false,
},
])
).toMatchInlineSnapshot(`
[
{
"default": undefined,
"deprecated": undefined,
"description": undefined,
"enum": undefined,
"name": "optionToDoSomething1",
"required": false,
"type": "(Boolean, String)[]",
},
{
"default": undefined,
"deprecated": undefined,
"description": undefined,
"enum": undefined,
"name": "optionToDoSomething2",
"required": false,
"type": "Boolean, String",
},
{
"default": undefined,
"deprecated": undefined,
"description": undefined,
"enum": undefined,
"name": "optionToDoSomething3",
"required": false,
"type": "Boolean",
},
]
`);
Expand Down
Loading