Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/docs/rules/ban-types.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/website/plugins/generated-rule-docs.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -299,6 +299,33 @@ export const generatedRuleDocs: Plugin = () => {
}
}

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

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
MemberAuthor

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.

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
MemberAuthor

@Josh-CenaJosh-CenaOct 9, 2023
edited
Loading

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.

JoshuaKGoldberg reacted with thumbs up emoji
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

[8]ページ先頭

©2009-2025 Movatter.jp