Skip to content

Commit

Permalink
docs: update docs generator
Browse files Browse the repository at this point in the history
  • Loading branch information
roikoren755 committed Nov 11, 2021
1 parent 7d904a2 commit 8d98774
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-shoes-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-es-roikoren': patch
---

docs: update docs generator
34 changes: 19 additions & 15 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,27 @@ For example, to enable only Rest/Spread Properties in ES2018, as similar to lega

This plugin provides the following configs.

<!-- PRESETS_TABLE_START -->
| Config ID | Description |
|:----------|:------------|
| `plugin:es-roikoren/restrict-to-es2019` | disallow new stuff that ES2019 doesn't include.
| `plugin:es-roikoren/restrict-to-es2018` | disallow new stuff that ES2018 doesn't include.
| `plugin:es-roikoren/restrict-to-es2017` | disallow new stuff that ES2017 doesn't include.
| `plugin:es-roikoren/restrict-to-es2016` | disallow new stuff that ES2016 doesn't include.
| `plugin:es-roikoren/restrict-to-es2015` | disallow new stuff that ES2015 doesn't include.
| `plugin:es-roikoren/restrict-to-es5` | disallow new stuff that ES5 doesn't include.
| `plugin:es-roikoren/restrict-to-es3` | disallow new stuff that ES3 doesn't include.
| `plugin:es-roikoren/no-new-in-es2020` | disallow the new stuff in ES2020.
| `plugin:es-roikoren/no-new-in-es2019` | disallow the new stuff in ES2019.
| `plugin:es-roikoren/no-new-in-es2018` | disallow the new stuff in ES2018.
| `plugin:es-roikoren/no-new-in-es2017` | disallow the new stuff in ES2017.
| `plugin:es-roikoren/no-new-in-es2016` | disallow the new stuff in ES2016.
| `plugin:es-roikoren/no-new-in-es2015` | disallow the new stuff in ES2015.
| `plugin:es-roikoren/no-new-in-es5` | disallow the new stuff in ES5.
| `plugin:es-roikoren/no-new-in-esnext` | disallow the new stuff to be planned for the next yearly ECMAScript snapshot.<br>⚠️ This config will be changed in the minor versions of this plugin.
| `plugin:es-roikoren/restrict-to-es2020` | disallow new stuff that ES2020 doesn't include. |
| `plugin:es-roikoren/restrict-to-es2019` | disallow new stuff that ES2019 doesn't include. |
| `plugin:es-roikoren/restrict-to-es2018` | disallow new stuff that ES2018 doesn't include. |
| `plugin:es-roikoren/restrict-to-es2017` | disallow new stuff that ES2017 doesn't include. |
| `plugin:es-roikoren/restrict-to-es2016` | disallow new stuff that ES2016 doesn't include. |
| `plugin:es-roikoren/restrict-to-es2015` | disallow new stuff that ES2015 doesn't include. |
| `plugin:es-roikoren/restrict-to-es5` | disallow new stuff that ES5 doesn't include. |
| `plugin:es-roikoren/restrict-to-es3` | disallow new stuff that ES3 doesn't include. |
| `plugin:es-roikoren/no-new-in-es2021` | disallow the new stuff in ES2021. |
| `plugin:es-roikoren/no-new-in-es2020` | disallow the new stuff in ES2020. |
| `plugin:es-roikoren/no-new-in-es2019` | disallow the new stuff in ES2019. |
| `plugin:es-roikoren/no-new-in-es2018` | disallow the new stuff in ES2018. |
| `plugin:es-roikoren/no-new-in-es2017` | disallow the new stuff in ES2017. |
| `plugin:es-roikoren/no-new-in-es2016` | disallow the new stuff in ES2016. |
| `plugin:es-roikoren/no-new-in-es2015` | disallow the new stuff in ES2015. |
| `plugin:es-roikoren/no-new-in-es5` | disallow the new stuff in ES5. |
| `plugin:es-roikoren/no-new-in-esnext` | disallow the new stuff to be planned for the next yearly ECMAScript snapshot.<br>⚠️ This config will be changed in the minor versions of this plugin. |
<!-- PRESETS_TABLE_END -->

For example:

Expand Down
92 changes: 84 additions & 8 deletions scripts/update-docs-readme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readdirSync, writeFileSync } from 'fs';
import { readFileSync, readdirSync, writeFileSync } from 'fs';
import path from 'path';
import type { ICategory, IRule } from './rules';
import { categories } from './rules';
Expand All @@ -7,7 +7,7 @@ const collator = new Intl.Collator('en', { numeric: true });

const extractCategoryId = (filePath: string): string => {
const basename = path.basename(filePath, '.ts');
const match = /no-new-in-(es\d+)/u.exec(basename);
const match = /no-new-in-(es(?:\d+|next))/u.exec(basename);

return match?.[1].toUpperCase() ?? '';
};
Expand All @@ -33,20 +33,29 @@ export const formatList = (xs: string[]): string => {
}
};

interface IConfig {
id: string;
categoryIds: string[];
kind: 'not-new-in' | 'restrict-to';
es: string;
}

// Analyze configs
const configRoot = path.resolve(__dirname, '../src/configs/');
const configs = readdirSync(configRoot).map((filename) => {
const configs = readdirSync(configRoot).map<IConfig>((filename) => {
const id = `plugin:es-roikoren/${path.basename(filename, '.ts')}`;
const configFile = path.join(configRoot, filename);
const kind = filename.startsWith('no-new-in-') ? 'not-new-in' : 'restrict-to';
const categoryId = extractCategoryId(configFile);
const categoryIds = [
extractCategoryId(configFile),
...(kind === 'not-new-in' ? [categoryId] : []),
// eslint-disable-next-line @typescript-eslint/no-var-requires
...((require(configFile) as { default: { extends?: string[] } }).default.extends ?? []).map((filePath) =>
extractCategoryId(filePath),
),
].filter(Boolean);

return { id, categoryIds };
return { id, categoryIds, kind, es: categoryId.slice(2) || id.slice(id.lastIndexOf('es') + 2) };
});

/**
Expand All @@ -64,9 +73,15 @@ const toTableRow = ({ ruleId, description, fixable }: IRule): string => {
* Create markdown text for a category.
* @param {ICategory} category The category information to convert.
*/
const toTable = ({ rules }: ICategory): string => `| Rule ID | Description | |
const toTable = ({ rules }: ICategory): string => {
if (rules.length === 0) {
return '⚠️ No rules yet. It will be added in the future.';
}

return `| Rule ID | Description | |
|:--------|:------------|:--:|
${rules.map((rule) => toTableRow(rule)).join('\n')}`;
};

/**
* Create markdown text for a category.
Expand All @@ -91,12 +106,12 @@ ${toTable(categories[categoryId])}
`;
};

// Convert categories to README sections
// Convert categories to rules/README sections
const ruleSectionContent = Object.keys(categories)
.map((category) => toSection(category))
.join('\n');

// Write README.md
// Write rules/README.md
writeFileSync(
'docs/rules/README.md',
`# Available Rules
Expand All @@ -108,3 +123,64 @@ This plugin provides the following rules.
${ruleSectionContent}
`,
);

const compareConfigId = (a: IConfig, b: IConfig): number => {
if (a.kind !== b.kind) {
if (a.kind === 'restrict-to') {
return -1;
}

return 1;
}

if (a.es === b.es) {
return 0;
}

if (a.es === 'NEXT') {
return 1;
}

if (b.es === 'NEXT') {
return -1;
}

return Number(b.es) - Number(a.es);
};

/**
* Create markdown table row for a config.
* @param {IConfig} config The config to convert.
* @return string The table row for the config.
*/
const toPresetConfigTableRow = (config: IConfig): string => {
let description = `disallow the new stuff in ES${config.es}.`;

if (config.kind === 'restrict-to') {
description = `disallow new stuff that ES${config.es} doesn't include.`;
} else if (config.es === 'NEXT') {
description =
'disallow the new stuff to be planned for the next yearly ECMAScript snapshot.<br>⚠️ This config will be changed in the minor versions of this plugin.';
}

return `| \`${config.id}\` | ${description} |`;
};

// Convert categories to README presets table
const presetsTableContent = `| Config ID | Description |
|:----------|:------------|
${configs
.sort(compareConfigId)
.map((config) => toPresetConfigTableRow(config))
.join('\n')}`;

// Write README.md
writeFileSync(
'docs/README.md',
readFileSync('docs/README.md', 'utf-8').replace(
/<!--\s*PRESETS_TABLE_START\s*-->[\s\S]*?<!--\s*PRESETS_TABLE_END\s*-->/u,
`<!-- PRESETS_TABLE_START -->
${presetsTableContent}
<!-- PRESETS_TABLE_END -->`,
),
);

0 comments on commit 8d98774

Please sign in to comment.