Skip to content

Commit

Permalink
Merge pull request #205 from bmish/separate-config-cols
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Nov 2, 2022
2 parents 53fdcf3 + 9681c71 commit c60554b
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 573 deletions.
26 changes: 7 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ Generated content in a rule doc (everything above the marker comment) (intention
```md
# Disallow using foo (`test/no-foo`)

✅ This rule is enabled in the `recommended` config.

💼 This rule is enabled in the following configs: ✅ `recommended`, 🎨 `stylistic`.

🎨<sup>⚠️</sup> This rule _warns_ in the `stylistic` config.
⚠️ This rule _warns_ in the 🎨 `stylistic` config.

🎨<sup>🚫</sup> This rule is _disabled_ in the `stylistic` config.
🚫 This rule is _disabled_ in the 🎨 `stylistic` config.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).

Expand Down Expand Up @@ -131,10 +129,10 @@ Generated rules table in `README.md` (everything between the marker comments) (i
<!-- begin auto-generated rules list -->

💼 Configurations enabled in.\
✅ Enabled in the `recommended` configuration.\
✅<sup>⚠️</sup> Warns in the `recommended` configuration.\
<sup>🚫</sup> Disabled in the `recommended` configuration.\
🎨 Enabled in the `stylistic` configuration.\
⚠️ Configurations set to warn in.\
🚫 Configurations disabled in.\
Set in the `recommended` configuration.\
🎨 Set in the `stylistic` configuration.\
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).\
💭 Requires type information.\
Expand Down Expand Up @@ -186,7 +184,7 @@ And how it looks:
| `--rule-doc-section-include` | Required section in each rule doc. Exit with failure if missing. Option can be repeated. |
| `--rule-doc-section-options` | Whether to require an "Options" or "Config" rule doc section and mention of any named options for rules with options (default: `true`). |
| `--rule-doc-title-format` | The format to use for rule doc titles. Defaults to `desc-parens-prefix-name`. See choices in below [table](#--rule-doc-title-format). |
| `--rule-list-columns` | Ordered, comma-separated list of columns to display in rule list. Empty columns will be hidden. Choices: `configs`, `deprecated`, `description`, `fixable`, `hasSuggestions`, `name`, `requiresTypeChecking`, `type` (off by default). Default: `name,description,configs,fixable,hasSuggestions,requiresTypeChecking,deprecated`. |
| `--rule-list-columns` | Ordered, comma-separated list of columns to display in rule list. Empty columns will be hidden. Choices: `configsError`, `configsOff`, `configsWarn`, `deprecated`, `description`, `fixable`, `hasSuggestions`, `name`, `requiresTypeChecking`, `type` (off by default). Default: `name,description,configsError,configsWarn,configsOff,fixable,hasSuggestions,requiresTypeChecking,deprecated`. |
| `--split-by` | Rule property to split the rules list by. A separate list and header will be created for each value. Example: `meta.type`. |
| `--url-configs` | Link to documentation about the ESLint configurations exported by the plugin. |

Expand Down Expand Up @@ -217,16 +215,6 @@ If you have a build step for your code like [Babel](https://babeljs.io/) or [Typ
}
```

### markdownlint

The output of this tool should be compatible with [markdownlint](https://github.com/DavidAnson/markdownlint) which you might use to lint your markdown. However, if any of your ESLint configs disable your rules or set them to warn, you'll need to exempt some elements used for emoji superscripts from [no-inline-html](https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md#md033---inline-html):

```json
{
"no-inline-html": { "allowed_elements": ["br", "sup"] }
}
```

### prettier

If you use [prettier](https://prettier.io/) to format your markdown, you may need to adjust your scripts to run prettier formatting after running this tool:
Expand Down
77 changes: 45 additions & 32 deletions lib/configs.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
import {
EMOJI_CONFIGS,
EMOJI_CONFIG,
EMOJI_CONFIG_WARN,
EMOJI_CONFIG_OFF,
EMOJI_CONFIG_ERROR,
RESERVED_EMOJIS,
} from './emojis.js';
import { SEVERITY_TYPE_TO_SET } from './types.js';
import type {
Plugin,
ConfigsToRules,
ConfigEmojis,
RuleSeverity,
SEVERITY_TYPE,
} from './types.js';

export function getConfigsThatSetARule(
plugin: Plugin,
configsToRules: ConfigsToRules,
pluginPrefix: string,
ignoreConfig: string[],
severityType?: SEVERITY_TYPE
) {
/* istanbul ignore next -- this shouldn't happen */
if (!plugin.rules) {
throw new Error('Missing rules in plugin.');
}
const ruleNames = Object.keys(plugin.rules);
return (
Object.entries(configsToRules)
.filter(([configName]) =>
// Only consider configs that configure at least one of the plugin's rules.
ruleNames.some((ruleName) =>
getConfigsForRule(
ruleName,
configsToRules,
pluginPrefix,
severityType
).includes(configName)
)
)
// Filter out ignored configs.
.filter(([configName]) => !ignoreConfig?.includes(configName))
.map(([configName]) => configName)
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
);
}

/**
* Get config names that a given rule belongs to.
* @param severity - Include configs that set the rule to this severity. Omit to allow any severity.
* @param severityType - Include configs that set the rule to this severity. Omit to allow any severity.
*/
export function getConfigsForRule(
ruleName: string,
configsToRules: ConfigsToRules,
pluginPrefix: string,
severity?: Set<RuleSeverity>
severityType?: SEVERITY_TYPE
) {
const severity = severityType
? SEVERITY_TYPE_TO_SET[severityType]
: undefined;
const configNames: Array<keyof typeof configsToRules> = [];

for (const configName in configsToRules) {
Expand Down Expand Up @@ -75,10 +109,8 @@ export function parseConfigEmojiOptions(
);
}

if (Object.keys(plugin.configs)?.length > 1 && emoji === EMOJI_CONFIG) {
throw new Error(
`Cannot use the general configs emoji ${EMOJI_CONFIG} for an individual config when multiple configs are present.`
);
if (RESERVED_EMOJIS.includes(emoji)) {
throw new Error(`Cannot specify reserved emoji ${EMOJI_CONFIG_ERROR}.`);
}

return [{ config, emoji }];
Expand All @@ -98,29 +130,19 @@ export function parseConfigEmojiOptions(
return configEmojis;
}

function emojiWithSuperscript(emoji: string, superscriptEmoji: string) {
if (emoji === superscriptEmoji) {
// Avoid double emoji.
return emoji;
}
return `${emoji}<sup>${superscriptEmoji}</sup>`;
}

/**
* Find the representation of a config to display.
* @param configEmojis - known list of configs and corresponding emojis
* @param configName - name of the config to find an emoji for
* @param options
* @param options.severity - if present, decorate the config's emoji for the given severity level
* @param options.fallback - if true and no emoji is found, choose whether to fallback to a generic config emoji or a badge
* @param options.fallback - if true and no emoji is found, choose whether to fallback to a badge.
* @returns the string to display for the config
*/
export function findConfigEmoji(
configEmojis: ConfigEmojis,
configName: string,
options?: {
severity?: SEVERITY_TYPE;
fallback?: 'badge' | 'emoji';
fallback?: 'badge';
}
) {
let emoji = configEmojis.find(
Expand All @@ -129,20 +151,11 @@ export function findConfigEmoji(
if (!emoji) {
if (options?.fallback === 'badge') {
emoji = `![${configName}][]`;
} else if (options?.fallback === 'emoji') {
emoji = EMOJI_CONFIG;
} else {
// No fallback.
return undefined; // eslint-disable-line unicorn/no-useless-undefined
}
}

switch (options?.severity) {
case 'warn':
return emojiWithSuperscript(emoji, EMOJI_CONFIG_WARN);
case 'off':
return emojiWithSuperscript(emoji, EMOJI_CONFIG_OFF);
default:
return emoji;
}
return emoji;
}
25 changes: 23 additions & 2 deletions lib/emojis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { SEVERITY_TYPE } from './types.js';
import { EMOJIS_TYPE } from './rule-type.js';

// Default emojis for common configs.
const EMOJI_A11Y = '♿';
const EMOJI_ERROR = '❗';
const EMOJI_STYLE = '🎨';
const EMOJI_TYPESCRIPT = '⌨️';
const EMOJI_WARNING = '⚠️';
const EMOJI_WARNING = '🚸';
export const EMOJI_CONFIGS = {
a11y: EMOJI_A11Y,
accessibility: EMOJI_A11Y,
Expand All @@ -24,9 +27,16 @@ export const EMOJI_CONFIGS = {
};

// General configs.
export const EMOJI_CONFIG = '💼';
export const EMOJI_CONFIG_ERROR = '💼';
export const EMOJI_CONFIG_WARN = '⚠️';
export const EMOJI_CONFIG_OFF = '🚫';
export const EMOJI_CONFIG_FROM_SEVERITY: {
[key in SEVERITY_TYPE]: string;
} = {
[SEVERITY_TYPE.error]: EMOJI_CONFIG_ERROR,
[SEVERITY_TYPE.warn]: EMOJI_CONFIG_WARN,
[SEVERITY_TYPE.off]: EMOJI_CONFIG_OFF,
};

// Fixers.
export const EMOJI_FIXABLE = '🔧';
Expand All @@ -41,3 +51,14 @@ export const EMOJI_TYPE = '🗂️';

// Deprecated.
export const EMOJI_DEPRECATED = '❌';

export const RESERVED_EMOJIS = [
...Object.values(EMOJI_CONFIG_FROM_SEVERITY),
...Object.values(EMOJIS_TYPE),

EMOJI_FIXABLE,
EMOJI_HAS_SUGGESTIONS,
EMOJI_REQUIRES_TYPE_CHECKING,
EMOJI_TYPE,
EMOJI_DEPRECATED,
];

0 comments on commit c60554b

Please sign in to comment.