Skip to content

chore(website): auto-inject ban-types default options #7714

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

Merged
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
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 @@ -299,6 +299,33 @@ export const generatedRuleDocs: Plugin = () => {
}
}

// Insert default rule options for ban-types
if (file.stem === 'ban-types') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 I do dislike having such rule-specific things in this already kind of big file... what do you think about having an mdx component like <BanTypesDefaultOptions />?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have file-specific handling for this page. This cannot be an MDX component because it has to work server-side and get statically injected instead of dynamically rendered.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blurgh, I keep forgetting these things. MDX components don't work server-side? Is this mentioned in docs / one of the things blocked by MDX v2? (I searched and can't find it)

Copy link
Member Author

@Josh-Cena Josh-Cena Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MDX components are basically React components. They can get server-rendered (through the same machinery that renders the rest of the page to HTML) but they can't access server data like files.

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