eslint-plugin-hyper-fetch 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +11 -0
- package/README.md +87 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +15121 -0
- package/dist/index.js.map +7 -0
- package/package.json +66 -0
- package/src/client-generic-types/README.md +21 -0
- package/src/client-generic-types/client-generic-types.ts +81 -0
- package/src/client-generic-types/index.ts +1 -0
- package/src/index.ts +17 -0
- package/src/request-generic-types/README.md +21 -0
- package/src/request-generic-types/index.ts +1 -0
- package/src/request-generic-types/request-generic-types.ts +82 -0
- package/src/utils/generic-utilities.types.ts +81 -0
- package/src/utils/is-hf-declaration.types.ts +58 -0
- package/tsconfig.json +9 -0
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-hyper-fetch",
|
|
3
|
+
"version": "7.0.0",
|
|
4
|
+
"description": "Eslint plugin for HyperFetch",
|
|
5
|
+
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
|
+
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"source": "src/index.ts",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"clean": "npx rimraf dist",
|
|
14
|
+
"test": "jest --watchAll --maxWorkers=3 --forceExit",
|
|
15
|
+
"test-pipeline": "jest --watchAll=false --maxWorkers=1 --no-cache --forceExit",
|
|
16
|
+
"start": "yarn build && npx run-when-changed --watch 'src/**/*.ts' --exec 'yarn build'",
|
|
17
|
+
"build": "yarn clean && node ../../scripts/build.js",
|
|
18
|
+
"postbuild": "rollup -c '../../rollup.config.js'",
|
|
19
|
+
"lint": "eslint . --fix",
|
|
20
|
+
"format": "prettier --write .",
|
|
21
|
+
"typescheck": "tsc --noEmit --emitDeclarationOnly false",
|
|
22
|
+
"tests": "yarn lint-staged",
|
|
23
|
+
"release": "yarn semantic-release --extends ../../release.config.js -t 'eslint-hyper-fetch-v${version}'"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@hyper-fetch/core": "*",
|
|
27
|
+
"@hyper-fetch/sockets": "*",
|
|
28
|
+
"react": ">= 16.8.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"@hyper-fetch/sockets": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"assert": "2.1.0",
|
|
37
|
+
"path": "0.12.7"
|
|
38
|
+
},
|
|
39
|
+
"lint-staged": {
|
|
40
|
+
"*.{js,jsx,ts,tsx}": [
|
|
41
|
+
"yarn jest --passWithNoTests"
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
"eslintConfig": {
|
|
45
|
+
"extends": [
|
|
46
|
+
"react",
|
|
47
|
+
"react/jest"
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public",
|
|
52
|
+
"registry": "https://registry.npmjs.com/"
|
|
53
|
+
},
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+https://github.com/BetterTyped/hyper-fetch.git",
|
|
57
|
+
"directory": "packages/react"
|
|
58
|
+
},
|
|
59
|
+
"bugs": {
|
|
60
|
+
"url": "https://github.com/BetterTyped/hyper-fetch/issues"
|
|
61
|
+
},
|
|
62
|
+
"keywords": [
|
|
63
|
+
"eslint",
|
|
64
|
+
"hyper-fetch"
|
|
65
|
+
]
|
|
66
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# client-generic-types
|
|
2
|
+
|
|
3
|
+
This rule extends typescript functionality in tracing the issues with object-like generic types.
|
|
4
|
+
|
|
5
|
+
## Errors ⛔️
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { createClient } from "@hyper-fetch/core";
|
|
9
|
+
|
|
10
|
+
const someClient = createClient<{ nonExistingGenericType: any }>({ url: "http://localhost:3000" }); // Error: Unexpected generic type(s) found: nonExistingGenericType
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { createClient } from "@hyper-fetch/core";
|
|
15
|
+
const someClient = createClient<{}>({ url: "http://localhost:3000" }); // Error: Generic type provided to createClient is empty
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
import { createClient } from "@hyper-fetch/core";
|
|
20
|
+
const someClient = createClient<unknown>({ url: "http://localhost:3000" }); // Error: Generic type provided to createClient is not matching the expected object-like format
|
|
21
|
+
```
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
3
|
+
|
|
4
|
+
import { getIsHyperFetchDeclaration } from "utils/is-hf-declaration.types";
|
|
5
|
+
import { getEmptyGenerics, getNotMatchingGeneric, getUnexpectedGenerics } from "utils/generic-utilities.types";
|
|
6
|
+
|
|
7
|
+
// The Rule creator returns a function that is used to create a well-typed ESLint rule
|
|
8
|
+
// The parameter passed into RuleCreator is a URL generator function.
|
|
9
|
+
export const createRule = ESLintUtils.RuleCreator((name) => `https://my-website.io/eslint/${name}`);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Rule for extending typescript possibilities for generic types with HyperFetch
|
|
13
|
+
* @test https://astexplorer.net/
|
|
14
|
+
*/
|
|
15
|
+
export const clientGenericTypes = createRule({
|
|
16
|
+
name: "client-generic-types",
|
|
17
|
+
meta: {
|
|
18
|
+
docs: {
|
|
19
|
+
recommended: "error",
|
|
20
|
+
description: "Rules extending HyperFetch possibilities for generic types",
|
|
21
|
+
},
|
|
22
|
+
type: "problem",
|
|
23
|
+
messages: {
|
|
24
|
+
emptyGeneric: "Generic type provided to createClient is empty",
|
|
25
|
+
unexpectedGenerics: "Unexpected generic type(s) found: {{ items }}",
|
|
26
|
+
notMatchingGenerics: "Generic type provided to createClient is not matching the expected object-like format",
|
|
27
|
+
},
|
|
28
|
+
schema: [],
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
const { isClientDeclaration, hooks } = getIsHyperFetchDeclaration();
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
...hooks,
|
|
36
|
+
CallExpression(node) {
|
|
37
|
+
if (isClientDeclaration(node.callee)) {
|
|
38
|
+
const typeParameters = "typeParameters" in node ? node.typeParameters : undefined;
|
|
39
|
+
const notMatchingGeneric = getNotMatchingGeneric({ typeParameters });
|
|
40
|
+
|
|
41
|
+
if (notMatchingGeneric) {
|
|
42
|
+
// skip other checks if format is incorrect
|
|
43
|
+
return context.report({
|
|
44
|
+
node,
|
|
45
|
+
messageId: "notMatchingGenerics",
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeParameters?.params[0].type === "TSTypeReference") {
|
|
50
|
+
// doSomething<RequestType> are considered matching.
|
|
51
|
+
// Do not check for unexpected generics in this case.
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const unexpectedGenericElements = getUnexpectedGenerics({
|
|
56
|
+
typeParameters,
|
|
57
|
+
allowedGenerics: ["error"],
|
|
58
|
+
});
|
|
59
|
+
const isEmpty = getEmptyGenerics({ typeParameters });
|
|
60
|
+
|
|
61
|
+
if (unexpectedGenericElements.length) {
|
|
62
|
+
context.report({
|
|
63
|
+
node,
|
|
64
|
+
messageId: "unexpectedGenerics",
|
|
65
|
+
data: {
|
|
66
|
+
items: unexpectedGenericElements.join(", "),
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (isEmpty) {
|
|
72
|
+
context.report({
|
|
73
|
+
node,
|
|
74
|
+
messageId: "emptyGeneric",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./client-generic-types";
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { clientGenericTypes } from "client-generic-types";
|
|
2
|
+
import { requestGenericTypes } from "./request-generic-types/request-generic-types";
|
|
3
|
+
|
|
4
|
+
export const configs = {
|
|
5
|
+
recommended: {
|
|
6
|
+
plugins: ["hyper-fetch"],
|
|
7
|
+
rules: {
|
|
8
|
+
"hyper-fetch/request-generic-types": "error",
|
|
9
|
+
"hyper-fetch/client-generic-types": "error",
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const rules = {
|
|
15
|
+
"request-generic-types": requestGenericTypes,
|
|
16
|
+
"client-generic-types": clientGenericTypes,
|
|
17
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# request-generic-types
|
|
2
|
+
|
|
3
|
+
This rule extends typescript functionality in tracing the issues with object-like generic types.
|
|
4
|
+
|
|
5
|
+
## Errors ⛔️
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { client } from "./client";
|
|
9
|
+
|
|
10
|
+
const request = client.createRequest<{ nonExistingGenericType: any }>({ endpoint: "/api/users" }); // Error: Unexpected generic type(s) found: nonExistingGenericType
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { client } from "./client";
|
|
15
|
+
const request = client.createRequest<{}>({ endpoint: "/api/users" }); // Error: Generic type provided to createRequest is empty
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
import { client } from "./client";
|
|
20
|
+
const request = client.createRequest<unknown>({ endpoint: "/api/users" }); // Error: Generic type provided to createRequest is not matching the expected object-like format
|
|
21
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./request-generic-types";
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
3
|
+
|
|
4
|
+
import { getEmptyGenerics, getNotMatchingGeneric, getUnexpectedGenerics } from "utils/generic-utilities.types";
|
|
5
|
+
|
|
6
|
+
// The Rule creator returns a function that is used to create a well-typed ESLint rule
|
|
7
|
+
// The parameter passed into RuleCreator is a URL generator function.
|
|
8
|
+
export const createRule = ESLintUtils.RuleCreator((name) => `https://my-website.io/eslint/${name}`);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Rule for extending typescript possibilities for generic types with HyperFetch
|
|
12
|
+
* @test https://astexplorer.net/
|
|
13
|
+
*/
|
|
14
|
+
export const requestGenericTypes = createRule({
|
|
15
|
+
name: "request-generic-types",
|
|
16
|
+
meta: {
|
|
17
|
+
docs: {
|
|
18
|
+
recommended: "error",
|
|
19
|
+
description: "Rules extending HyperFetch possibilities for generic types",
|
|
20
|
+
},
|
|
21
|
+
type: "problem",
|
|
22
|
+
messages: {
|
|
23
|
+
emptyGeneric: "Generic type provided to createRequest is empty",
|
|
24
|
+
unexpectedGenerics: "Unexpected generic type(s) found: {{ items }}",
|
|
25
|
+
notMatchingGenerics: "Generic type provided to createRequest is not matching the expected object-like format",
|
|
26
|
+
},
|
|
27
|
+
schema: [],
|
|
28
|
+
},
|
|
29
|
+
defaultOptions: [],
|
|
30
|
+
create(context) {
|
|
31
|
+
return {
|
|
32
|
+
CallExpression(node) {
|
|
33
|
+
if (
|
|
34
|
+
node.callee &&
|
|
35
|
+
"property" in node.callee &&
|
|
36
|
+
"name" in node.callee.property &&
|
|
37
|
+
node.callee.property.name === "createRequest"
|
|
38
|
+
) {
|
|
39
|
+
const typeParameters = "typeParameters" in node ? node.typeParameters : undefined;
|
|
40
|
+
const notMatchingGeneric = getNotMatchingGeneric({ typeParameters });
|
|
41
|
+
|
|
42
|
+
if (notMatchingGeneric) {
|
|
43
|
+
// skip other checks if format is incorrect
|
|
44
|
+
return context.report({
|
|
45
|
+
node,
|
|
46
|
+
messageId: "notMatchingGenerics",
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (typeParameters?.params[0].type === "TSTypeReference") {
|
|
51
|
+
// doSomething<RequestType> are considered matching.
|
|
52
|
+
// Do not check for unexpected generics in this case.
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const unexpectedGenericElements = getUnexpectedGenerics({
|
|
57
|
+
typeParameters,
|
|
58
|
+
allowedGenerics: ["response", "queryParams", "error", "payload", "endpoint"],
|
|
59
|
+
});
|
|
60
|
+
const isEmpty = getEmptyGenerics({ typeParameters });
|
|
61
|
+
|
|
62
|
+
if (unexpectedGenericElements.length) {
|
|
63
|
+
context.report({
|
|
64
|
+
node,
|
|
65
|
+
messageId: "unexpectedGenerics",
|
|
66
|
+
data: {
|
|
67
|
+
items: unexpectedGenericElements.join(", "),
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (isEmpty) {
|
|
73
|
+
context.report({
|
|
74
|
+
node,
|
|
75
|
+
messageId: "emptyGeneric",
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
},
|
|
82
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { NewExpression } from "@typescript-eslint/types/dist/generated/ast-spec";
|
|
2
|
+
|
|
3
|
+
export function getUnexpectedGenerics({
|
|
4
|
+
typeParameters,
|
|
5
|
+
allowedGenerics,
|
|
6
|
+
}: {
|
|
7
|
+
typeParameters: NewExpression["typeParameters"] | undefined;
|
|
8
|
+
allowedGenerics: string[];
|
|
9
|
+
}) {
|
|
10
|
+
/**
|
|
11
|
+
* This is valid: doSomething({ endpoint: "/ping" });
|
|
12
|
+
*/
|
|
13
|
+
if (!typeParameters) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// <{ ... }>
|
|
18
|
+
const mainGenericParam = typeParameters.params[0];
|
|
19
|
+
|
|
20
|
+
if (mainGenericParam && "members" in mainGenericParam) {
|
|
21
|
+
return mainGenericParam.members
|
|
22
|
+
.map((member) => {
|
|
23
|
+
if ("key" in member) {
|
|
24
|
+
if ("name" in member.key) {
|
|
25
|
+
return member.key.name;
|
|
26
|
+
}
|
|
27
|
+
return member.type;
|
|
28
|
+
}
|
|
29
|
+
return member.type;
|
|
30
|
+
})
|
|
31
|
+
.filter((key) => !allowedGenerics.includes(key));
|
|
32
|
+
}
|
|
33
|
+
return [mainGenericParam.type];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function getEmptyGenerics({ typeParameters }: { typeParameters: NewExpression["typeParameters"] | undefined }) {
|
|
37
|
+
/**
|
|
38
|
+
* This is valid: doSomething({ endpoint: "/ping" });
|
|
39
|
+
*/
|
|
40
|
+
if (!typeParameters) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// <{ ... }>
|
|
45
|
+
const mainGenericParam = typeParameters.params[0];
|
|
46
|
+
|
|
47
|
+
if (mainGenericParam && "members" in mainGenericParam) {
|
|
48
|
+
return !mainGenericParam.members.length;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function getNotMatchingGeneric({
|
|
54
|
+
typeParameters,
|
|
55
|
+
}: {
|
|
56
|
+
typeParameters: NewExpression["typeParameters"] | undefined;
|
|
57
|
+
}) {
|
|
58
|
+
/**
|
|
59
|
+
* This is valid: doSomething({ endpoint: "/ping" });
|
|
60
|
+
*/
|
|
61
|
+
if (!typeParameters) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// <{ ... }>
|
|
66
|
+
const mainGenericParam = typeParameters.params[0];
|
|
67
|
+
|
|
68
|
+
// doSomething<{ ... }> is a TSTypeLiteral, which is the valid case.
|
|
69
|
+
if (mainGenericParam.type === "TSTypeLiteral") {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// type RequestType = { queryParams: string }
|
|
74
|
+
// doSomething<RequestType> are considered matching.
|
|
75
|
+
if (mainGenericParam.type === "TSTypeReference") {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// All other types, such as `doSomething<string>` (TSStringKeyword) are considered not matching.
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { CallExpression, ImportDeclaration } from "@typescript-eslint/types/dist/generated/ast-spec";
|
|
2
|
+
|
|
3
|
+
const importsToLookFor: string[] = ["@hyper-fetch/core"];
|
|
4
|
+
|
|
5
|
+
export const getIsHyperFetchDeclaration = () => {
|
|
6
|
+
const defaultImports = new Set<string>();
|
|
7
|
+
const clientImports = new Set<string>();
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
isClientDeclaration: (node: CallExpression["callee"]) => {
|
|
11
|
+
/**
|
|
12
|
+
* import * as HF from "@hyper-fetch/core";
|
|
13
|
+
*
|
|
14
|
+
* const client = HF.createClient();
|
|
15
|
+
*/
|
|
16
|
+
const isAccessedViaDefaultImport =
|
|
17
|
+
node.type === "MemberExpression" &&
|
|
18
|
+
node.object.type === "Identifier" &&
|
|
19
|
+
defaultImports.has(node.object.name) &&
|
|
20
|
+
node.property.type === "Identifier" &&
|
|
21
|
+
node.property.name === "createClient";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* import { createClient } HF from "@hyper-fetch/core";
|
|
25
|
+
*
|
|
26
|
+
* const client = createClient();
|
|
27
|
+
*/
|
|
28
|
+
const isDirectlyImported = node.type === "Identifier" && clientImports.has(node.name);
|
|
29
|
+
|
|
30
|
+
return isAccessedViaDefaultImport || isDirectlyImported;
|
|
31
|
+
},
|
|
32
|
+
hooks: {
|
|
33
|
+
ImportDeclaration: (node: ImportDeclaration) => {
|
|
34
|
+
if (node.source.type !== "Literal" || typeof node.source.value !== "string") {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!importsToLookFor.includes(node.source.value)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
node.specifiers.forEach((specifier) => {
|
|
43
|
+
if (specifier.type === "ImportDefaultSpecifier" || specifier.type === "ImportNamespaceSpecifier") {
|
|
44
|
+
defaultImports.add(specifier.local.name);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (specifier.type === "ImportSpecifier" && specifier.imported.name === "createClient") {
|
|
48
|
+
clientImports.add(specifier.local.name);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
"Program:exit": () => {
|
|
53
|
+
defaultImports.clear();
|
|
54
|
+
clientImports.clear();
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
};
|