8000 test(eslint-plugin): assert that `ts`/`tsx` code blocks in docs are s… · danvk/typescript-eslint@b7cef73 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit b7cef73

Browse files
auvredJoshuaKGoldberg
authored andcommitted
test(eslint-plugin): assert that ts/tsx code blocks in docs are syntactically valid (typescript-eslint#8142)
* test(eslint-plugin): assert that `ts`/`tsx` code blocks in docs are syntactically valid * revert unintended changes in space-before-blocks.md * chore: shorten examples in consistent-type-assertions.md * refactor: do not parse md file again * docs: more consistent examples for consistent-type-assertions * docs: convert `js` code blocks to `ts` * Update packages/eslint-plugin/tests/docs.test.ts Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com> * chore: use regex instead of startsWith * chore: fix few codesamples --------- Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
1 parent b56976e commit b7cef73

16 files changed

+102
-67
lines changed

packages/eslint-plugin/docs/rules/ban-tslint-comment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Useful when migrating from TSLint to ESLint. Once TSLint has been removed, this
1616

1717
### ❌ Incorrect
1818

19-
```js
19+
```ts
2020
/* tslint:disable */
2121
/* tslint:enable */
2222
/* tslint:disable:rule1 rule2 rule3... */
@@ -28,7 +28,7 @@ someCode(); // tslint:disable-line
2828

2929
### ✅ Correct
3030

31-
```js
31+
```ts
3232
// This is a comment that just happens to mention tslint
3333
/* This is a multiline comment that just happens to mention tslint */
3434
someCode(); // This is a comment that just happens to mention tslint

packages/eslint-plugin/docs/rules/consistent-type-assertions.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,22 @@ Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'neve
5454
#### ❌ Incorrect
5555

5656
```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }'
57-
const x = { ... } as T;
57+
const x = { foo: 1 } as T;
5858

59-
function foo() {
60-
return { ... } as T;
59+
function bar() {
60+
return { foo: 1 } as T;
6161
}
6262
```
6363

6464
#### ✅ Correct
6565

6666
```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }'
67-
const x: T = { ... };
68-
const y = { ... } as any;
69-
const z = { ... } as unknown;
67+
const x: T = { foo: 1 };
68+
const y = { foo: 1 } as any;
69+
const z = { foo: 1 } as unknown;
7070

71-
function foo(): T {
72-
return { ... };
71+
function bar(): T {
72+
return { foo: 1 };
7373
}
7474
```
7575

@@ -82,23 +82,25 @@ Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allo
8282
#### ❌ Incorrect
8383

8484
```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }'
85-
const x = { ... } as T;
85+
const x = { foo: 1 } as T;
8686

87-
function foo() {
88-
return { ... } as T;
87+
function bar() {
88+
return { foo: 1 } as T;
8989
}
9090
```
9191

9292
#### ✅ Correct
9393

9494
```tsx option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }'
95-
const x: T = { ... };
96-
const y = { ... } as any;
97-
const z = { ... } as unknown;
98-
foo({ ... } as T);
99-
new Clazz({ ... } as T);
100-
function foo() { throw { bar: 5 } as Foo }
101-
const foo = <Foo props={{ ... } as Bar}/>;
95+
const x: T = { foo: 1 };
96+
const y = { foo: 1 } as any;
97+
const z = { foo: 1 } as unknown;
98+
bar({ foo: 1 } as T);
99+
new Clazz({ foo: 1 } as T);
100+
function bar() {
101+
throw { foo: 1 } as Foo;
102+
}
103+
const foo = <Foo props={{ bar: 1 } as Bar} />;
102104
```
103105

104106
<!--/tabs-->

packages/eslint-plugin/docs/rules/explicit-function-return-type.md

Lines changed: 4 additions & 8 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ type FuncType = () => string;
143143

144144
let arrowFn: FuncType = () => 'test';
145145

146-
let funcExpr: FuncType = function() {
146+
let funcExpr: FuncType = function () {
147147
return 'test';
148148
};
149149

@@ -163,19 +163,15 @@ let objectPropCast = <ObjectType>{
163163
foo: () => 1,
164164
};
165165

166-
declare functionWithArg(arg: () => number);
166+
declare function functionWithArg(arg: () => number);
167167
functionWithArg(() => 1);
168168

169-
declare functionWithObjectArg(arg: { method: () => number });
169+
declare function functionWithObjectArg(arg: { method: () => number });
170170
functionWithObjectArg({
171171
method() {
172172
return 1;
173173
},
174174
});
175-
176-
const Comp: FC = () => {
177-
return <button onClick={() => {}} />;
178-
};
179175
```
180176

181177
### `allowHigherOrderFunctions`
@@ -271,7 +267,7 @@ function foo<T>(t: T): T {
271267

272268
const bar = <T>(t: T): T => t;
273269

274-
const allowedFunction(x: string) {
270+
function allowedFunction(x: string) {
275271
return x;
276272
}
277273

packages/eslint-plugin/docs/rules/explicit-member-accessibility.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ If you are working on a codebase within which you lint non-TypeScript code (i.e.
4848

4949
This rule in its default state requires no configuration and will enforce that every class member has an accessibility modifier. If you would like to allow for some implicit public members then you have the following options:
5050

51-
```ts
51+
```jsonc
5252
{
53-
accessibility: 'explicit',
54-
overrides: {
55-
accessors: 'explicit',
56-
constructors: 'no-public',
57-
methods: 'explicit',
58-
properties: 'off',
59-
parameterProperties: 'explicit'
53+
"accessibility: "explicit",
54+
"overrides": {
55+
"accessors": "explicit",
56+
"constructors": "no-public",
57+
"methods": "explicit",
58+
"properties": "off",
59+
"parameterProperties": "explicit"
6060
}
6161
}
6262
```

packages/eslint-plugin/docs/rules/member-ordering.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,6 @@ It ignores any member group types completely by specifying `"never"` for `member
969969

970970
```ts option='{ "default": { "memberTypes": "never", "order": "alphabetically" } }'
971971
interface Foo {
972-
static c = 0;
973972
b(): void;
974973
a: boolean;
975974

@@ -985,7 +984,6 @@ interface Foo {
985984
interface Foo {
986985
a: boolean;
987986
b(): void;
988-
static c = 0;
989987

990988
[a: string]: number; // Order doesn't matter (no sortable identifier)
991989
new (): Bar; // Order doesn't matter (no sortable identifier)

packages/eslint-plugin/docs/rules/no-empty-function.md

Lines changed: 0 additions & 3 deletion 2851 s
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ class Foo {
6060
Examples of correct code for the `{ "allow": ["decoratedFunctions"] }` option:
6161

6262
```ts option='{ "allow": ["decoratedFunctions"] }' showPlaygroundButton
63-
@decorator()
64-
function foo() {}
65-
6663
class Foo {
6764
@decorator()
6865
foo() {}

packages/eslint-plugin/docs/rules/no-for-in-array.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You may have confused for-in with for-of, which iterates over the elements of th
2121

2222
### ❌ Incorrect
2323

24-
```js
24+
```ts
2525
declare const array: string[];
2626

2727
for (const i in array) {
@@ -35,7 +35,7 @@ for (const i in array) {
3535

3636
### ✅ Correct
3737

38-
```js
38+
```ts
3939
declare const array: string[];
4040

4141
for (const value of array) {
@@ -48,7 +48,7 @@ for (let i = 0; i < array.length; i += 1) {
4848

4949
array.forEach((value, i) => {
5050
console.log(i, value);
51-
})
51+
});
5252

5353
for (const [i, value] of array.entries()) {
5454
console.log(i, value);

packages/eslint-plugin/docs/rules/no-this-alias.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ or not managing scope well.
1515

1616
### ❌ Incorrect
1717

18-
```js
18+
```ts
1919
const self = this;
2020

2121
setTimeout(function () {
@@ -25,7 +25,7 @@ setTimeout(function () {
2525

2626
### ✅ Correct
2727

28-
```js
28+
```ts
2929
setTimeout(() => {
3030
this.doWork();
3131
});

packages/eslint-plugin/docs/rules/no-type-alias.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,42 @@ In TypeScript, type aliases serve three purposes:
2727
```ts
2828
// this...
2929
type Person = {
30-
firstName: string,
31-
lastName: string,
32-
age: number
30+
firstName: string;
31+
lastName: string;
32+
age: number;
3333
};
3434

35-
function addPerson(person : Person) { ... }
35+
function addPerson(person: Person) {
36+
// ...
37+
}
3638

3739
// is easier to read than this...
38-
function addPerson(person : { firstName: string, lastName: string, age: number}) { ... }
40+
function addPerson(person: {
41+
firstName: string;
42+
lastName: string;
43+
age: number;
44+
}) {
45+
// ...
46+
}
3947
```
4048

4149
- Act sort of like an interface, providing a set of methods and properties that must exist
4250
in the objects implementing the type.
4351

4452
```ts
4553
type Person = {
46-
firstName: string,
47-
lastName: string,
48-
age: number,
49-
walk: () => void,
50-
talk: () => void
54+
firstName: string;
55+
lastName: string;
56+
age: number;
57+
walk: () => void;
58+
talk: () => void;
5159
};
5260

5361
// you know person will have 3 properties and 2 methods,
5462
// because the structure has already been defined.
55-
var person : Person = { ... }
63+
var person: Person = {
64+
// ...
65+
};
5666

5767
// so we can be sure that this will work
5868
person.walk();

packages/eslint-plugin/docs/rules/no-unnecessary-type-arguments.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Type parameters in TypeScript may specify a default value.
1010
For example:
1111

1212
```ts
13-
function f<T = number>(...) {...}
13+
function f<T = number>(/* ... */) {
14+
// ...
15+
}
1416
```
1517

1618
It is redundant to provide an explicit type parameter equal to that default: e.g. calling `f<number>(...)`.

0 commit comments

Comments
 (0)
0