-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support auto-generated config lists
- Loading branch information
Showing
7 changed files
with
599 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
BEGIN_CONFIG_LIST_MARKER, | ||
END_CONFIG_LIST_MARKER, | ||
} from './comment-markers.js'; | ||
import { markdownTable } from 'markdown-table'; | ||
import type { ConfigsToRules, ConfigEmojis, Plugin } from './types.js'; | ||
import { ConfigFormat, configNameToDisplay } from './config-format.js'; | ||
|
||
function generateConfigListMarkdown( | ||
plugin: Plugin, | ||
configsToRules: ConfigsToRules, | ||
pluginPrefix: string, | ||
configEmojis: ConfigEmojis, | ||
configFormat: ConfigFormat, | ||
ignoreConfig: readonly string[] | ||
): string { | ||
/* istanbul ignore next -- configs are sure to exist at this point */ | ||
const configs = Object.values(plugin.configs || {}); | ||
const hasDescription = configs.some( | ||
// @ts-expect-error -- description is not an official config property. | ||
(config) => config.description | ||
); | ||
const listHeaderRow = ['', 'Name']; | ||
if (hasDescription) { | ||
listHeaderRow.push('Description'); | ||
} | ||
|
||
return markdownTable( | ||
[ | ||
listHeaderRow, | ||
...Object.keys(configsToRules) | ||
.filter((configName) => !ignoreConfig.includes(configName)) | ||
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())) | ||
.map((configName) => { | ||
return [ | ||
configEmojis.find((obj) => obj.config === configName)?.emoji || '', | ||
`\`${configNameToDisplay( | ||
configName, | ||
configFormat, | ||
pluginPrefix | ||
)}\``, | ||
hasDescription | ||
? // @ts-expect-error -- description is not an official config property. | ||
(plugin.configs?.[configName]?.description as | ||
| string | ||
| undefined) || '' | ||
: undefined, | ||
].filter((col) => col !== undefined); | ||
}), | ||
], | ||
{ align: 'l' } // Left-align headers. | ||
); | ||
} | ||
|
||
export function updateConfigsList( | ||
markdown: string, | ||
plugin: Plugin, | ||
configsToRules: ConfigsToRules, | ||
pluginPrefix: string, | ||
configEmojis: ConfigEmojis, | ||
configFormat: ConfigFormat, | ||
ignoreConfig: readonly string[] | ||
): string { | ||
const listStartIndex = markdown.indexOf(BEGIN_CONFIG_LIST_MARKER); | ||
let listEndIndex = markdown.indexOf(END_CONFIG_LIST_MARKER); | ||
|
||
if (listStartIndex === -1 || listEndIndex === -1) { | ||
// No config list found. | ||
return markdown; | ||
} | ||
|
||
if ( | ||
Object.keys(configsToRules).filter( | ||
(configName) => !ignoreConfig.includes(configName) | ||
).length === 0 | ||
) { | ||
// No non-ignored configs found. | ||
return markdown; | ||
} | ||
|
||
// Account for length of pre-existing marker. | ||
listEndIndex += END_CONFIG_LIST_MARKER.length; | ||
|
||
const preList = markdown.slice(0, Math.max(0, listStartIndex)); | ||
const postList = markdown.slice(Math.max(0, listEndIndex)); | ||
|
||
// New config list. | ||
const list = generateConfigListMarkdown( | ||
plugin, | ||
configsToRules, | ||
pluginPrefix, | ||
configEmojis, | ||
configFormat, | ||
ignoreConfig | ||
); | ||
|
||
return `${preList}${BEGIN_CONFIG_LIST_MARKER}\n\n${list}\n\n${END_CONFIG_LIST_MARKER}${postList}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
test/lib/generate/__snapshots__/configs-list-test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`generate (configs list) basic generates the documentation 1`] = ` | ||
"## Rules | ||
<!-- begin auto-generated rules list --> | ||
| Name | Description | | ||
| :----------------------------- | :--------------------- | | ||
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | ||
<!-- end auto-generated rules list --> | ||
## Configs | ||
<!-- begin auto-generated configs list --> | ||
| | Name | | ||
| :- | :------------ | | ||
| ✅ | \`recommended\` | | ||
<!-- end auto-generated configs list -->" | ||
`; | ||
|
||
exports[`generate (configs list) when a config exports a description generates the documentation 1`] = ` | ||
"## Rules | ||
<!-- begin auto-generated rules list --> | ||
| Name | Description | | ||
| :----------------------------- | :--------------------- | | ||
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | ||
<!-- end auto-generated rules list --> | ||
## Configs | ||
<!-- begin auto-generated configs list --> | ||
| | Name | Description | | ||
| :- | :------------ | :--------------------------------------- | | ||
| | \`foo\` | | | ||
| ✅ | \`recommended\` | This config has the recommended rules... | | ||
<!-- end auto-generated configs list -->" | ||
`; | ||
|
||
exports[`generate (configs list) when all configs are ignored generates the documentation 1`] = ` | ||
"## Rules | ||
<!-- begin auto-generated rules list --> | ||
| Name | Description | | ||
| :----------------------------- | :--------------------- | | ||
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | ||
<!-- end auto-generated rules list --> | ||
## Configs | ||
<!-- begin auto-generated configs list --> | ||
<!-- end auto-generated configs list -->" | ||
`; | ||
|
||
exports[`generate (configs list) when there are no configs generates the documentation 1`] = ` | ||
"## Rules | ||
<!-- begin auto-generated rules list --> | ||
| Name | Description | | ||
| :----------------------------- | :--------------------- | | ||
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | ||
<!-- end auto-generated rules list --> | ||
## Configs | ||
<!-- begin auto-generated configs list --> | ||
<!-- end auto-generated configs list -->" | ||
`; | ||
|
||
exports[`generate (configs list) with --config-format generates the documentation 1`] = ` | ||
"## Rules | ||
<!-- begin auto-generated rules list --> | ||
| Name | Description | | ||
| :----------------------------- | :--------------------- | | ||
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | ||
<!-- end auto-generated rules list --> | ||
## Configs | ||
<!-- begin auto-generated configs list --> | ||
| | Name | | ||
| :- | :----------------- | | ||
| ✅ | \`test/recommended\` | | ||
<!-- end auto-generated configs list -->" | ||
`; | ||
|
||
exports[`generate (configs list) with --ignore-config generates the documentation 1`] = ` | ||
"## Rules | ||
<!-- begin auto-generated rules list --> | ||
| Name | Description | | ||
| :----------------------------- | :--------------------- | | ||
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | ||
<!-- end auto-generated rules list --> | ||
## Configs | ||
<!-- begin auto-generated configs list --> | ||
| | Name | | ||
| :- | :------------ | | ||
| ✅ | \`recommended\` | | ||
<!-- end auto-generated configs list -->" | ||
`; | ||
|
||
exports[`generate (configs list) with configs not defined in alphabetical order generates the documentation 1`] = ` | ||
"## Rules | ||
<!-- begin auto-generated rules list --> | ||
| Name | Description | | ||
| :----------------------------- | :--------------------- | | ||
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | ||
<!-- end auto-generated rules list --> | ||
## Configs | ||
<!-- begin auto-generated configs list --> | ||
| | Name | | ||
| :- | :------------ | | ||
| | \`foo\` | | ||
| ✅ | \`recommended\` | | ||
<!-- end auto-generated configs list -->" | ||
`; |
Oops, something went wrong.