From bc9f74f81de23ad103bc96be1d70e919710d1298 Mon Sep 17 00:00:00 2001 From: auvred Date: Wed, 27 Dec 2023 08:43:43 +0000 Subject: [PATCH 1/9] test(eslint-plugin): assert that `ts`/`tsx` code blocks in docs are syntactically valid --- .../docs/rules/consistent-type-assertions.md | 66 ++++++++++++++----- .../rules/explicit-function-return-type.md | 4 -- .../rules/explicit-member-accessibility.md | 16 ++--- .../docs/rules/member-ordering.md | 2 - .../docs/rules/no-empty-function.md | 3 - .../eslint-plugin/docs/rules/no-type-alias.md | 32 +++++---- .../rules/no-unnecessary-type-arguments.md | 4 +- .../rules/no-unnecessary-type-constraint.md | 2 +- .../docs/rules/prefer-enum-initializers.md | 2 +- .../docs/rules/space-before-blocks.md | 10 +-- packages/eslint-plugin/docs/rules/typedef.md | 6 +- packages/eslint-plugin/tests/docs.test.ts | 29 ++++++++ 12 files changed, 123 insertions(+), 53 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/consistent-type-assertions.md b/packages/eslint-plugin/docs/rules/consistent-type-assertions.md index b453e7201aa6..debfd0bbc152 100644 --- a/packages/eslint-plugin/docs/rules/consistent-type-assertions.md +++ b/packages/eslint-plugin/docs/rules/consistent-type-assertions.md @@ -54,22 +54,34 @@ Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'neve #### ❌ Incorrect ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }' -const x = { ... } as T; +const x = { + // ... +} as T; function foo() { - return { ... } as T; + return { + // ... + } as T; } ``` #### ✅ Correct ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }' -const x: T = { ... }; -const y = { ... } as any; -const z = { ... } as unknown; +const x: T = { + // ... +}; +const y = { + // ... +} as any; +const z = { + // ... +} as unknown; function foo(): T { - return { ... }; + return { + // ... + }; } ``` @@ -82,23 +94,47 @@ Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allo #### ❌ Incorrect ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }' -const x = { ... } as T; +const x = { + // ... +} as T; function foo() { - return { ... } as T; + return { + // ... + } as T; } ``` #### ✅ Correct ```tsx option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }' -const x: T = { ... }; -const y = { ... } as any; -const z = { ... } as unknown; -foo({ ... } as T); -new Clazz({ ... } as T); -function foo() { throw { bar: 5 } as Foo } -const foo = ; +const x: T = { + // ... +}; +const y = { + // ... +} as any; +const z = { + // ... +} as unknown; +foo({ + // ... +} as T); +new Clazz({ + // ... +} as T); +function foo() { + throw { bar: 5 } as Foo; +} +const foo = ( + +); ``` diff --git a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md index ad6b5598e557..ca4d019df906 100644 --- a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md +++ b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md @@ -172,10 +172,6 @@ functionWithObjectArg({ return 1; }, }); - -const Comp: FC = () => { - return