From d6054fa080274cbfa2a910511ade928f055135f7 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 1 Oct 2023 15:23:49 -0400 Subject: [PATCH] refactor(website): auto-inject ban-types default options --- .../eslint-plugin/docs/rules/ban-types.md | 58 +------------------ .../website/plugins/generated-rule-docs.ts | 27 +++++++++ 2 files changed, 28 insertions(+), 57 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/ban-types.md b/packages/eslint-plugin/docs/rules/ban-types.md index 7109e24e8456..f52b24cf7e9f 100644 --- a/packages/eslint-plugin/docs/rules/ban-types.md +++ b/packages/eslint-plugin/docs/rules/ban-types.md @@ -75,63 +75,7 @@ The default options provide a set of "best practices", intended to provide safet
Default Options -```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` instead.', - ].join('\n'), - suggest: ['object', 'unknown', 'NonNullable'], - }, - '{}': { - 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` instead.', - '- If you really want a type meaning "any non-nullish value", you probably want `NonNullable` instead.', - ].join('\n'), - suggest: [ - 'object', - 'unknown', - 'Record', - 'NonNullable', - ], - }, -}; -``` +
diff --git a/packages/website/plugins/generated-rule-docs.ts b/packages/website/plugins/generated-rule-docs.ts index 9f4d013d5455..bc2c065cd15d 100644 --- a/packages/website/plugins/generated-rule-docs.ts +++ b/packages/website/plugins/generated-rule-docs.ts @@ -299,6 +299,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).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( {