Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0cf081b

Browse files
Merge branch 'main'
2 parents 185fa5c + 1629ef0 commit 0cf081b

File tree

18 files changed

+1491
-1033
lines changed

18 files changed

+1491
-1033
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# typescript-eslint Examples
22

33
Various examples of working with typescript-eslint. 📝
4-
We may add to this over time, or we may get rid of it in favor of [typescript-eslint.io](https://typescript-eslint.io).
5-
We'll see.
4+
5+
| Example | Use Case |
6+
| --------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
7+
| [`eslint-plugin-example-typed-linting`](./examples/eslint-plugin-example-typed-linting) | ESLint plugin showing typed linting with `@typescript-eslint/utils`. |
8+
| [`flat-config-disable-type-checked`](./examples/flat-config-disable-type-checked) | Linting TypeScript code with type information, disabling typed rules on `*.js` files. |
9+
| [`flat-config-typed`](./examples/flat-config-typed) | Linting TypeScript code with type information using Project Service. |
10+
| [`flat-config-typed-tsconfig`](./examples/flat-config-typed-tsconfig) | Linting TypeScript code with type information and a `tsconfig.eslint.json`. |
11+
| [`flat-config-untyped`](./examples/flat-config-untyped) | Linting TypeScript code without type information. |
12+
| [`node-test-floating-promises`](./examples/node-test-floating-promises) | Using `node:test` along the `@typescript-eslint/no-floating-promises` `allowForKnownSafeCalls` option. |
13+
| [`typed-rule-via-linter`](./examples/typed-rule-via-linter) | Running a single typed rule via ESLint's `Linter` class. |
14+
| [`typescript-estree-standalone`](./examples/typescript-estree-standalone) | Using the `@typescript-eslint/estree` package to generate an AST standalone (without ESLint). |

package-lock.json

Lines changed: 1331 additions & 985 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/eslint-plugin-example-typed-linting/package.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
{
22
"dependencies": {
3-
"@typescript-eslint/utils": "8.18.0"
3+
"@typescript-eslint/utils": "8.32.1"
44
},
55
"devDependencies": {
6-
"@eslint/js": "^9.16.0",
7-
"@types/eslint": "^9.6.1",
8-
"@types/eslint__js": "^8.42.3",
9-
"@types/node": "^22.10.2",
10-
"@typescript-eslint/rule-tester": "8.18.0",
11-
"eslint": "^9.16.0",
12-
"eslint-doc-generator": "^2.0.0",
13-
"eslint-plugin-eslint-plugin": "^6.3.2",
14-
"typescript": "^5.7.2",
15-
"typescript-eslint": "8.18.0",
16-
"vitest": "^2.1.8"
6+
"@eslint/js": "^9.27.0",
7+
"@types/node": "^22.15.21",
8+
"@typescript-eslint/rule-tester": "8.32.1",
9+
"eslint": "^9.27.0",
10+
"eslint-doc-generator": "^2.1.2",
11+
"eslint-plugin-eslint-plugin": "^6.4.0",
12+
"typescript": "^5.8.3",
13+
"typescript-eslint": "8.32.1",
14+
"vitest": "^3.1.4"
1715
},
1816
"exports": {
1917
".": {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Example: `fastify` and Floating Promise Detection
2+
3+
An example of using [`fastify`](https://nodejs.org/api/test.html) along with the [`@typescript-eslint/no-floating-promises` rule](https://typescript-eslint.io/rules/no-floating-promises) enabled.
4+
By default, the rule does not report on Fastify code because Fastify functions return `PromiseLike`s and not `Promise`s.
5+
6+
It uses the [`allowForKnownSafePromises` rule option](https://typescript-eslint.io/rules/no-floating-promises/#allowforknownsafepromises) to not report on calls to `test()`.
7+
8+
## Setup
9+
10+
```shell
11+
npm i
12+
```
13+
14+
## Usage
15+
16+
```shell
17+
npm run lint
18+
```
19+
20+
There should be no lint reports.
21+
22+
If you enable `checkThenables` in `eslint.config.js`, then there will be reports on `response.header` and `fastify.register`:
23+
24+
```plaintext
25+
.../index.ts
26+
7:5 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promises
27+
11:1 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promises
28+
```
29+
30+
If you enable the `allowForKnownSafePromises` option from `eslint.config.js`, there should again be no lint reports.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @ts-check
2+
3+
import eslint from "@eslint/js";
4+
import tseslint from "typescript-eslint";
5+
6+
export default tseslint.config(
7+
eslint.configs.recommended,
8+
tseslint.configs.recommendedTypeChecked,
9+
{
10+
languageOptions: {
11+
parserOptions: {
12+
projectService: {
13+
allowDefaultProject: ['*.config.*']
14+
},
15+
},
16+
},
17+
rules: {
18+
"@typescript-eslint/no-floating-promises": [
19+
"error", {
20+
// 1. Switch this to true to start reporting on Fastify Promise types
21+
"checkThenables": false,
22+
23+
// 2. Un-comment this to allow known safe Fastify Promise types
24+
// "allowForKnownSafePromises": [
25+
// { "from": "package", "name": "FastifyInstance", "package": "fastify" },
26+
// { "from": "package", "name": "FastifyRegister", "package": "fastify" },
27+
// { "from": "package", "name": "FastifyReply", "package": "fastify" },
28+
// ],
29+
}
30+
]
31+
}
32+
}
33+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import FastifyCookie from '@fastify/cookie';
2+
import Fastify from 'fastify';
3+
4+
const fastify = Fastify();
5+
6+
fastify.get('/', (_request, response) => {
7+
response.header('x-example-header', '...');
8+
return { hello: 'world' };
9+
})
10+
11+
fastify.register(FastifyCookie);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "fastify-floating-promises",
3+
"version": "0.0.0",
4+
"description": "Example of using Fastify with `@typescript-eslint/no-floating-promises`'s `allowForKnownSafePromises` option.",
5+
"main": "index.ts",
6+
"repository": {
7+
"directory": "packages/fastify-floating-promises",
8+
"type": "git",
9+
"url": "https://github.com/typescript-eslint/typescript-eslint-examples"
10+
},
11+
"license": "MIT",
12+
"devDependencies": {
13+
"@types/node": "^22.15.21",
14+
"eslint": "^9.27.0",
15+
"typescript": "^5.8.3",
16+
"typescript-eslint": "^8.32.1"
17+
},
18+
"dependencies": {
19+
"@fastify/cookie": "^11.0.2",
20+
"fastify": "^5.3.3"
21+
},
22+
"scripts": {
23+
"lint": "eslint .",
24+
"tsc": "tsc"
25+
},
26+
"type": "module"
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "NodeNext",
4+
"moduleResolution": "NodeNext",
5+
"noEmit": true,
6+
"strict": true
7+
},
8+
}

packages/flat-config-disable-type-checked/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example: Flat Config and disable-type-checked
22

3-
An example of using the [`typescript-eslint`](https://typescript-eslint.io/packages/typescript-eslint) package to lint TypeScript code with type information.
3+
An example of using the [`typescript-eslint`](https://typescript-eslint.io/packages/typescript-eslint) package to lint TypeScript code with type information, disabling type checked rules on `*.js` files.
44

55
## Setup
66

packages/flat-config-disable-type-checked/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
},
1111
"license": "MIT",
1212
"devDependencies": {
13-
"@types/eslint": "^9.6.1",
14-
"eslint": "^9.16.0",
15-
"typescript": "^5.7.2",
16-
"typescript-eslint": "^8.18.0"
13+
"eslint": "^9.27.0",
14+
"typescript": "^5.8.3",
15+
"typescript-eslint": "^8.32.1"
1716
},
1817
"scripts": {
1918
"lint": "eslint .",

packages/flat-config-typed-tsconfig/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
},
1111
"license": "MIT",
1212
"devDependencies": {
13-
"@types/eslint": "^9.6.1",
14-
"@types/eslint__js": "^8.42.3",
15-
"eslint": "^9.16.0",
16-
"typescript": "^5.7.2",
17-
"typescript-eslint": "^8.18.0"
13+
"typescript": "^5.8.3",
14+
"typescript-eslint": "^8.32.1"
1815
},
1916
"scripts": {
2017
"lint": "eslint .",

packages/flat-config-typed/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example: Flat Config (Typed)
22

3-
An example of using the [`typescript-eslint`](https://typescript-eslint.io/packages/typescript-eslint) package to lint TypeScript code with type information.
3+
An example of using the [`typescript-eslint`](https://typescript-eslint.io/packages/typescript-eslint) package to lint TypeScript code with type information using Project Service.
44

55
## Setup
66

packages/flat-config-typed/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
},
1111
"license": "MIT",
1212
"devDependencies": {
13-
"@types/eslint": "^9.6.1",
14-
"eslint": "^9.16.0",
15-
"typescript": "^5.7.2",
16-
"typescript-eslint": "^8.18.0"
13+
"eslint": "^9.27.0",
14+
"typescript": "^5.8.3",
15+
"typescript-eslint": "^8.32.1"
1716
},
1817
"scripts": {
1918
"lint": "eslint src",

packages/flat-config-untyped/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
},
1111
"license": "MIT",
1212
"devDependencies": {
13-
"typescript-eslint": "^8.18.0",
14-
"typescript": "^5.7.2",
15-
"eslint": "^9.16.0"
13+
"typescript-eslint": "^8.32.1",
14+
"typescript": "^5.8.3",
15+
"eslint": "^9.27.0"
1616
},
1717
"scripts": {
1818
"lint": "eslint src",

packages/node-test-floating-promises/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
},
1111
"license": "MIT",
1212
"devDependencies": {
13-
"@types/node": "^22.10.7",
14-
"eslint": "^9.16.0",
15-
"typescript": "^5.7.2",
16-
"typescript-eslint": "^8.18.0"
13+
"@types/node": "^22.15.21",
14+
"eslint": "^9.27.0",
15+
"typescript": "^5.8.3",
16+
"typescript-eslint": "^8.32.1"
1717
},
1818
"scripts": {
1919
"lint": "eslint .",

packages/typed-rule-via-linter/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
},
1111
"license": "MIT",
1212
"dependencies": {
13-
"@typescript-eslint/parser": "^8.18.0",
14-
"@typescript-eslint/utils": "^8.18.0",
15-
"eslint": "^9.16.0",
16-
"typescript": "^5.7.2"
13+
"@typescript-eslint/parser": "^8.32.1",
14+
"@typescript-eslint/utils": "^8.32.1",
15+
"eslint": "^9.27.0",
16+
"typescript": "^5.8.3"
1717
},
1818
"devDependencies": {
19-
"@types/eslint": "^9.6.1",
20-
"tsx": "^4.19.2"
19+
"tsx": "^4.19.4"
2120
},
2221
"scripts": {
2322
"example": "tsx index.ts"

packages/typescript-estree-standalone/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import ts from "typescript";
66
const { ast, services } = tsESTree.parseAndGenerateServices(
77
`const hello = 'world';`,
88
{
9-
EXPERIMENTAL_useProjectService: true,
9+
projectService: {
10+
allowDefaultProject: ['estree.ts']
11+
},
1012
}
1113
);
1214

packages/typescript-estree-standalone/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
},
1111
"license": "MIT",
1212
"dependencies": {
13-
"@typescript-eslint/typescript-estree": "^8.18.0",
14-
"ts-api-utils": "^2.0.0",
15-
"typescript": "^5.7.2"
13+
"@typescript-eslint/typescript-estree": "^8.32.1",
14+
"ts-api-utils": "^2.1.0",
15+
"typescript": "^5.8.3"
1616
},
1717
"devDependencies": {
18-
"@types/node": "^22.10.2",
19-
"tsx": "^4.19.2"
18+
"@types/node": "^22.15.21",
19+
"tsx": "^4.19.4"
2020
},
2121
"scripts": {
2222
"example": "tsx index.ts"

0 commit comments

Comments
 (0)