Skip to content

Commit

Permalink
chore(website): auto-inject ban-types default options (#7714)
Browse files Browse the repository at this point in the history
refactor(website): auto-inject ban-types default options

Co-authored-by: Josh Goldberg ✨ <[email protected]>
  • Loading branch information
Josh-Cena and JoshuaKGoldberg committed Oct 19, 2023
1 parent 458cd85 commit 76ab373
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 57 deletions.
58 changes: 1 addition & 57 deletions packages/eslint-plugin/docs/rules/ban-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,63 +75,7 @@ The default options provide a set of "best practices", intended to provide safet
<details>
<summary>Default Options</summary>

```ts
const defaultTypes = {
String: {
message: 'Use string instead',
fixWith: 'string',
},
Boolean: {
message: 'Use boolean instead',
fixWith: 'boolean',
},
Number: {
message: 'Use number instead',
fixWith: 'number',
},
Symbol: {
message: 'Use symbol instead',
fixWith: 'symbol',
},
BigInt: {
message: 'Use bigint instead',
fixWith: 'bigint',
},
Function: {
message: [
'The `Function` type accepts any function-like value.',
'It provides no type safety when calling the function, which can be a common source of bugs.',
'It also accepts things like class declarations, which will throw at runtime as they will not be called with `new`.',
'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.',
].join('\n'),
},
// object typing
Object: {
message: [
'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.',
'- If you want a type meaning "any object", you probably want `object` instead.',
'- If you want a type meaning "any value", you probably want `unknown` instead.',
'- If you really want a type meaning "any non-nullish value", you probably want `NonNullable<unknown>` instead.',
].join('\n'),
suggest: ['object', 'unknown', 'NonNullable<unknown>'],
},
'{}': {
message: [
'`{}` actually means "any non-nullish value".',
'- If you want a type meaning "any object", you probably want `object` instead.',
'- If you want a type meaning "any value", you probably want `unknown` instead.',
'- If you want a type meaning "empty object", you probably want `Record<string, never>` instead.',
'- If you really want a type meaning "any non-nullish value", you probably want `NonNullable<unknown>` instead.',
].join('\n'),
suggest: [
'object',
'unknown',
'Record<string, never>',
'NonNullable<unknown>',
],
},
};
```
<!-- Inject default options -->

</details>

Expand Down
27 changes: 27 additions & 0 deletions packages/website/plugins/generated-rule-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,33 @@ export const generatedRuleDocs: Plugin = () => {
}
}

// Insert default rule options for ban-types
if (file.stem === 'ban-types') {
const placeToInsert = children.findIndex(
(node: unist.Node) =>
node.type === 'comment' &&
(node as unist.Literal<string>).value.trim() ===
'Inject default options',
);
if (placeToInsert === -1) {
throw new Error('Could not find default injection site in ban-types');
}
const defaultOptions = fs
.readFileSync(
path.join(eslintPluginDirectory, 'src/rules/ban-types.ts'),
'utf8',
)
.match(/^const defaultTypes.+?^\};$/msu)?.[0];
if (!defaultOptions) {
throw new Error('Could not find default options for ban-types');
}
children.splice(placeToInsert, 1, {
lang: 'ts',
type: 'code',
value: defaultOptions,
} as mdast.Code);
}

// 5. Add a link to view the rule's source and test code
children.push(
{
Expand Down

0 comments on commit 76ab373

Please sign in to comment.