@pob/eslint-plugin 56.1.0 → 57.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/CHANGELOG.md +14 -0
- package/README.md +28 -0
- package/{configs → lib/configs}/base.js +1 -3
- package/{configs → lib/configs}/react.js +1 -3
- package/lib/index.js +13 -0
- package/lib/loadRules.js +46 -0
- package/lib/rules/forbid-non-native-fetch-import.js +42 -0
- package/{rules → lib/rules}/forbid-non-native-fetch-import.test.js +2 -4
- package/lib/rules/react-function-component-return-react-node.js +66 -0
- package/{rules → lib/rules}/react-function-component-return-react-node.test.js +5 -7
- package/lib/rules/react-named-import.js +38 -0
- package/{rules → lib/rules}/react-named-import.test.js +5 -7
- package/lib/rules/test-utils/RuleTester.js +11 -0
- package/package.json +23 -4
- package/.eslintrc.json +0 -4
- package/docs/rules/forbid-non-native-fetch-import.md +0 -21
- package/docs/rules/react-function-return-react-node.md +0 -27
- package/docs/rules/react-named-import.md +0 -21
- package/index.js +0 -15
- package/loadRules.js +0 -42
- package/rules/forbid-non-native-fetch-import.js +0 -42
- package/rules/react-function-component-return-react-node.js +0 -66
- package/rules/react-named-import.js +0 -38
- package/rules/test-utils/RuleTester.js +0 -12
- /package/{fixtures → lib/fixtures}/file.ts +0 -0
- /package/{fixtures → lib/fixtures}/react.tsx +0 -0
- /package/{fixtures → lib/fixtures}/tsconfig.json +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,20 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [57.0.0](https://github.com/christophehurpeau/eslint-config-pob/compare/v56.1.0...v57.0.0) (2024-08-11)
|
|
7
|
+
|
|
8
|
+
### ⚠ BREAKING CHANGES
|
|
9
|
+
|
|
10
|
+
* use flat config
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* use flat config ([b0bda47](https://github.com/christophehurpeau/eslint-config-pob/commit/b0bda47976ec40a30679988c603d425f9b75346f))
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* fix error message ([a1abcef](https://github.com/christophehurpeau/eslint-config-pob/commit/a1abcef0a1e6711d121a567f045d7fcaf8516a6c))
|
|
19
|
+
|
|
6
20
|
## [56.1.0](https://github.com/christophehurpeau/eslint-config-pob/compare/v56.0.0...v56.1.0) (2024-07-27)
|
|
7
21
|
|
|
8
22
|
### Features
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<h3 align="center">
|
|
2
|
+
@pob/eslint-plugin
|
|
3
|
+
</h3>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
eslint plugin for react
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://npmjs.org/package/@pob/eslint-plugin"><img src="https://img.shields.io/npm/v/@pob/eslint-plugin.svg?style=flat-square"></a>
|
|
11
|
+
<a href="https://npmjs.org/package/@pob/eslint-plugin"><img src="https://img.shields.io/npm/dw/@pob/eslint-plugin.svg?style=flat-square"></a>
|
|
12
|
+
<a href="https://npmjs.org/package/@pob/eslint-plugin"><img src="https://img.shields.io/node/v/@pob/eslint-plugin.svg?style=flat-square"></a>
|
|
13
|
+
<a href="https://npmjs.org/package/@pob/eslint-plugin"><img src="https://img.shields.io/npm/types/@pob/eslint-plugin.svg?style=flat-square"></a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install --save @pob/eslint-plugin
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import pobEslintPlugin from "@pob/eslint-plugin";
|
|
26
|
+
|
|
27
|
+
console.log(pobEslintPlugin);
|
|
28
|
+
```
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import baseConfig from "./configs/base.js";
|
|
2
|
+
import reactConfig from "./configs/react.js";
|
|
3
|
+
import { loadRules } from "./loadRules.js";
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
rules: {
|
|
7
|
+
...(await loadRules()),
|
|
8
|
+
},
|
|
9
|
+
configs: {
|
|
10
|
+
base: baseConfig,
|
|
11
|
+
react: reactConfig,
|
|
12
|
+
},
|
|
13
|
+
};
|
package/lib/loadRules.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import forbidNonNativeFetchImportRule from "./rules/forbid-non-native-fetch-import.js";
|
|
4
|
+
import reactFunctionComponentReturnReactNodeRule from "./rules/react-function-component-return-react-node.js";
|
|
5
|
+
import reactNamedImportRule from "./rules/react-named-import.js";
|
|
6
|
+
|
|
7
|
+
const pkg = JSON.parse(
|
|
8
|
+
// eslint-disable-next-line unicorn/prefer-json-parse-buffer
|
|
9
|
+
fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const repoUrl = "https://github.com/christophehurpeau/eslint-config-pob";
|
|
13
|
+
const packagePath = "@pob/eslint-plugin";
|
|
14
|
+
|
|
15
|
+
const getDocumentationUrl = (filename) => {
|
|
16
|
+
const ruleName = path.basename(filename, ".js");
|
|
17
|
+
return `${repoUrl}/blob/v${pkg.version}/${packagePath}/docs/rules/${ruleName}.md`;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function loadRule(ruleName, rule) {
|
|
21
|
+
return {
|
|
22
|
+
meta: {
|
|
23
|
+
schema: [],
|
|
24
|
+
...rule.meta,
|
|
25
|
+
docs: {
|
|
26
|
+
...rule.meta.docs,
|
|
27
|
+
url: getDocumentationUrl(ruleName),
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
create: rule.create,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function loadRules() {
|
|
35
|
+
return {
|
|
36
|
+
"forbid-non-native-fetch-import": loadRule(
|
|
37
|
+
"forbid-non-native-fetch-import",
|
|
38
|
+
forbidNonNativeFetchImportRule,
|
|
39
|
+
),
|
|
40
|
+
"react-function-component-return-react-node": loadRule(
|
|
41
|
+
"react-function-component-return-react-node",
|
|
42
|
+
reactFunctionComponentReturnReactNodeRule,
|
|
43
|
+
),
|
|
44
|
+
"react-named-import": loadRule("react-named-import", reactNamedImportRule),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const fetchLibs = ["node-fetch", "cross-fetch", "isomorphic-fetch"];
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "problem",
|
|
6
|
+
|
|
7
|
+
docs: {
|
|
8
|
+
description:
|
|
9
|
+
"Forbid node-fetch import now that fetch is available in node and browsers",
|
|
10
|
+
category: "Best Practices",
|
|
11
|
+
recommended: true,
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
messages: {
|
|
15
|
+
forbidden:
|
|
16
|
+
'import from "{{value}}" is forbidden. Fetch is available in browsers and node since 18.0.0.',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
create: (context) => {
|
|
21
|
+
const checkNode = (node) => {
|
|
22
|
+
const importSource = node.source.value.trim();
|
|
23
|
+
|
|
24
|
+
if (fetchLibs.includes(importSource)) {
|
|
25
|
+
context.report({
|
|
26
|
+
node,
|
|
27
|
+
messageId: "forbidden",
|
|
28
|
+
data: { value: importSource },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
ImportDeclaration: checkNode,
|
|
34
|
+
ExportNamedDeclaration(node) {
|
|
35
|
+
if (node.source) {
|
|
36
|
+
checkNode(node);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
ExportAllDeclaration: checkNode,
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { RuleTester } = require("eslint");
|
|
4
|
-
const rule = require("./forbid-non-native-fetch-import");
|
|
1
|
+
import { RuleTester } from "eslint";
|
|
2
|
+
import rule from "./forbid-non-native-fetch-import.js";
|
|
5
3
|
|
|
6
4
|
const parserOptions = {
|
|
7
5
|
ecmaVersion: 2018,
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import Components from "eslint-plugin-react/lib/util/Components.js";
|
|
2
|
+
|
|
3
|
+
function getSuggestionFixer(node, range, context) {
|
|
4
|
+
return (fixer) => {
|
|
5
|
+
return fixer.replaceTextRange(range, "ReactNode");
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function checkFunctionReturnType(node, onError) {
|
|
10
|
+
const { returnType } = node;
|
|
11
|
+
if (returnType == null) return;
|
|
12
|
+
|
|
13
|
+
const { typeAnnotation } = returnType;
|
|
14
|
+
if (typeAnnotation == null) return;
|
|
15
|
+
|
|
16
|
+
const { typeName } = typeAnnotation;
|
|
17
|
+
if (typeName == null) return;
|
|
18
|
+
|
|
19
|
+
if (typeName.name !== "ReactNode") {
|
|
20
|
+
onError(typeName, typeName.name, typeName.range);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default {
|
|
25
|
+
meta: {
|
|
26
|
+
type: "problem",
|
|
27
|
+
|
|
28
|
+
docs: {
|
|
29
|
+
description:
|
|
30
|
+
"Forbid returning ReactElement or ReactElement | null in function components",
|
|
31
|
+
category: "Best Practices",
|
|
32
|
+
recommended: true,
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
messages: {
|
|
36
|
+
invalidReturnType:
|
|
37
|
+
'Returning "{{value}}" is forbidden. Return "ReactNode" instead.',
|
|
38
|
+
replaceReturnType: 'Replace "{{value}}" by "ReactNode"',
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
hasSuggestions: true,
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
create: Components.detect((context, components) => {
|
|
45
|
+
return {
|
|
46
|
+
FunctionDeclaration(node) {
|
|
47
|
+
if (!components.get(node)) return;
|
|
48
|
+
|
|
49
|
+
checkFunctionReturnType(node, (typeName, currentName, range) =>
|
|
50
|
+
context.report({
|
|
51
|
+
node: typeName,
|
|
52
|
+
messageId: "invalidReturnType",
|
|
53
|
+
data: { value: currentName },
|
|
54
|
+
suggest: [
|
|
55
|
+
{
|
|
56
|
+
messageId: "replaceReturnType",
|
|
57
|
+
data: { value: currentName },
|
|
58
|
+
fix: getSuggestionFixer(node, range, context),
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}),
|
|
66
|
+
};
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const rule = require("./react-function-component-return-react-node");
|
|
5
|
-
const { RuleTester } = require("./test-utils/RuleTester");
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import rule from "./react-function-component-return-react-node.js";
|
|
3
|
+
import { RuleTester } from "./test-utils/RuleTester.js";
|
|
6
4
|
|
|
7
5
|
const ruleTester = new RuleTester({
|
|
8
|
-
parser:
|
|
6
|
+
parser: "@typescript-eslint/parser",
|
|
9
7
|
parserOptions: {
|
|
10
|
-
tsconfigRootDir:
|
|
8
|
+
tsconfigRootDir: join(import.meta.dirname, "../fixtures"),
|
|
11
9
|
project: "./tsconfig.json",
|
|
12
10
|
ecmaFeatures: {
|
|
13
11
|
jsx: true,
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
|
|
5
|
+
docs: {
|
|
6
|
+
description: "Use named react import as suggested in the doc",
|
|
7
|
+
category: "Best Practices",
|
|
8
|
+
recommended: true,
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
messages: {
|
|
12
|
+
namespace:
|
|
13
|
+
'import * as React from "react" is forbidden. Use named imports instead.',
|
|
14
|
+
default:
|
|
15
|
+
'import React from "react" is forbidden. Use named imports instead.',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
create(context) {
|
|
20
|
+
const checkNode = (node, messageId) => {
|
|
21
|
+
const importDeclaration = node.parent;
|
|
22
|
+
const importSource = importDeclaration.source.value.trim();
|
|
23
|
+
if (importSource !== "react") return;
|
|
24
|
+
context.report({
|
|
25
|
+
node,
|
|
26
|
+
messageId,
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
ImportNamespaceSpecifier: (node) => {
|
|
31
|
+
checkNode(node, "namespace");
|
|
32
|
+
},
|
|
33
|
+
ImportDefaultSpecifier: (node) => {
|
|
34
|
+
checkNode(node, "default");
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
};
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const rule = require("./react-named-import");
|
|
5
|
-
const { RuleTester } = require("./test-utils/RuleTester");
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import rule from "./react-named-import.js";
|
|
3
|
+
import { RuleTester } from "./test-utils/RuleTester.js";
|
|
6
4
|
|
|
7
5
|
const ruleTester = new RuleTester({
|
|
8
|
-
parser:
|
|
6
|
+
parser: "@typescript-eslint/parser",
|
|
9
7
|
parserOptions: {
|
|
10
|
-
tsconfigRootDir:
|
|
8
|
+
tsconfigRootDir: join(import.meta.dirname, "../fixtures"),
|
|
11
9
|
project: "./tsconfig.json",
|
|
12
10
|
ecmaFeatures: {
|
|
13
11
|
jsx: true,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { after, describe, it } from "node:test";
|
|
2
|
+
// eslint-disable-next-line import/no-unresolved -- missing main field
|
|
3
|
+
import { RuleTester } from "@typescript-eslint/rule-tester";
|
|
4
|
+
|
|
5
|
+
RuleTester.afterAll = after;
|
|
6
|
+
RuleTester.describe = describe;
|
|
7
|
+
RuleTester.it = it;
|
|
8
|
+
RuleTester.itOnly = it.only;
|
|
9
|
+
|
|
10
|
+
// eslint-disable-next-line unicorn/prefer-export-from -- because we modify it, it is clearer if we export the modified version even if it has no impact.
|
|
11
|
+
export { RuleTester };
|
package/package.json
CHANGED
|
@@ -1,19 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pob/eslint-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "57.0.0",
|
|
4
4
|
"description": "eslint plugin for react",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"author": "Christophe Hurpeau <christophe@hurpeau.com> (https://christophe.hurpeau.com)",
|
|
5
7
|
"license": "ISC",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/christophehurpeau/eslint-config-pob.git",
|
|
11
|
+
"directory": "@pob/eslint-plugin"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/christophehurpeau/eslint-config-pob",
|
|
14
|
+
"type": "module",
|
|
6
15
|
"engines": {
|
|
7
16
|
"node": ">=20.10.0"
|
|
8
17
|
},
|
|
9
|
-
"
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"main": "./lib/index.js",
|
|
10
20
|
"exports": {
|
|
11
21
|
"./package.json": "./package.json",
|
|
12
|
-
".": "./index.js"
|
|
22
|
+
".": "./lib/index.js"
|
|
13
23
|
},
|
|
14
24
|
"publishConfig": {
|
|
15
25
|
"access": "public"
|
|
16
26
|
},
|
|
27
|
+
"files": [
|
|
28
|
+
"lib"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"lint": "yarn run lint:eslint",
|
|
32
|
+
"lint:eslint": "yarn ../.. run eslint --quiet @pob/eslint-plugin"
|
|
33
|
+
},
|
|
34
|
+
"pob": {},
|
|
35
|
+
"prettier": "@pob/root/prettier-config",
|
|
17
36
|
"peerDependencies": {
|
|
18
37
|
"eslint": "^8.39.0",
|
|
19
38
|
"typescript": ">=5.2.2"
|
|
@@ -31,6 +50,6 @@
|
|
|
31
50
|
"@eslint/eslintrc": "2.1.4",
|
|
32
51
|
"@typescript-eslint/rule-tester": "6.21.0",
|
|
33
52
|
"eslint": "8.57.0",
|
|
34
|
-
"typescript": "5.4
|
|
53
|
+
"typescript": "5.5.4"
|
|
35
54
|
}
|
|
36
55
|
}
|
package/.eslintrc.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# Forbid importing libraries replacing fetch which is now available in native, browser and node.
|
|
2
|
-
|
|
3
|
-
💼 This rule is enabled in the ✅ `base` config.
|
|
4
|
-
|
|
5
|
-
## Fail
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import fetch from "node-fetch";
|
|
9
|
-
|
|
10
|
-
fetch("/api").then(console.log);
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Pass
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
fetch("/api").then(console.log);
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Options
|
|
20
|
-
|
|
21
|
-
None
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# Return type of react function component should be ReactNode
|
|
2
|
-
|
|
3
|
-
💼 This rule is enabled in the ✅ `react` config.
|
|
4
|
-
|
|
5
|
-
## Fail
|
|
6
|
-
|
|
7
|
-
```tsx
|
|
8
|
-
function Component(): ReactElement {
|
|
9
|
-
return <div />;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function Component(): ReactElement | null {
|
|
13
|
-
return <div />;
|
|
14
|
-
}
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Pass
|
|
18
|
-
|
|
19
|
-
```tsx
|
|
20
|
-
function Component(): ReactNode {
|
|
21
|
-
return <div />;
|
|
22
|
-
}
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Options
|
|
26
|
-
|
|
27
|
-
None
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# Importing react should use named imports
|
|
2
|
-
|
|
3
|
-
💼 This rule is enabled in the ✅ `react` config.
|
|
4
|
-
|
|
5
|
-
## Fail
|
|
6
|
-
|
|
7
|
-
```tsx
|
|
8
|
-
import React from "react";
|
|
9
|
-
import * as React from "react";
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
## Pass
|
|
13
|
-
|
|
14
|
-
```tsx
|
|
15
|
-
import type { ReactNode } from "react";
|
|
16
|
-
import { useState } from "react";
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Options
|
|
20
|
-
|
|
21
|
-
None
|
package/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const baseConfig = require("./configs/base");
|
|
4
|
-
const reactConfig = require("./configs/react");
|
|
5
|
-
const loadRules = require("./loadRules");
|
|
6
|
-
|
|
7
|
-
module.exports = {
|
|
8
|
-
rules: {
|
|
9
|
-
...loadRules(),
|
|
10
|
-
},
|
|
11
|
-
configs: {
|
|
12
|
-
base: baseConfig,
|
|
13
|
-
react: reactConfig,
|
|
14
|
-
},
|
|
15
|
-
};
|
package/loadRules.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("node:fs");
|
|
4
|
-
const path = require("node:path");
|
|
5
|
-
const pkg = require("./package.json");
|
|
6
|
-
|
|
7
|
-
const repoUrl = "https://github.com/christophehurpeau/eslint-config-pob";
|
|
8
|
-
const packagePath = "@pob/eslint-plugin";
|
|
9
|
-
|
|
10
|
-
const getDocumentationUrl = (filename) => {
|
|
11
|
-
const ruleName = path.basename(filename, ".js");
|
|
12
|
-
return `${repoUrl}/blob/v${pkg.version}/${packagePath}/docs/rules/${ruleName}.md`;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
function loadRule(ruleId) {
|
|
16
|
-
// eslint-disable-next-line import/no-dynamic-require
|
|
17
|
-
const rule = require(path.resolve(__dirname, `./rules/${ruleId}`));
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
meta: {
|
|
21
|
-
schema: [],
|
|
22
|
-
...rule.meta,
|
|
23
|
-
docs: {
|
|
24
|
-
...rule.meta.docs,
|
|
25
|
-
url: getDocumentationUrl(ruleId),
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
create: rule.create,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
module.exports = function loadRules() {
|
|
33
|
-
return Object.fromEntries(
|
|
34
|
-
fs
|
|
35
|
-
.readdirSync(path.resolve(__dirname, "./rules"), { withFileTypes: true })
|
|
36
|
-
.filter((file) => file.isFile() && !file.name.endsWith(".test.js"))
|
|
37
|
-
.map((file) => {
|
|
38
|
-
const ruleId = path.basename(file.name, ".js");
|
|
39
|
-
return [ruleId, loadRule(ruleId)];
|
|
40
|
-
}),
|
|
41
|
-
);
|
|
42
|
-
};
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.meta = {
|
|
4
|
-
type: "problem",
|
|
5
|
-
|
|
6
|
-
docs: {
|
|
7
|
-
description:
|
|
8
|
-
"Forbid node-fetch import now that fetch is available in node and browsers",
|
|
9
|
-
category: "Best Practices",
|
|
10
|
-
recommended: true,
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
messages: {
|
|
14
|
-
forbidden:
|
|
15
|
-
'import from "{{value}}" is forbidden. Fetch is available in browsers and node since 18.0.0.',
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const fetchLibs = ["node-fetch", "cross-fetch", "isomorphic-fetch"];
|
|
20
|
-
|
|
21
|
-
exports.create = (context) => {
|
|
22
|
-
const checkNode = (node) => {
|
|
23
|
-
const importSource = node.source.value.trim();
|
|
24
|
-
|
|
25
|
-
if (fetchLibs.includes(importSource)) {
|
|
26
|
-
context.report({
|
|
27
|
-
node,
|
|
28
|
-
messageId: "forbidden",
|
|
29
|
-
data: { value: importSource },
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
return {
|
|
34
|
-
ImportDeclaration: checkNode,
|
|
35
|
-
ExportNamedDeclaration(node) {
|
|
36
|
-
if (node.source) {
|
|
37
|
-
checkNode(node);
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
ExportAllDeclaration: checkNode,
|
|
41
|
-
};
|
|
42
|
-
};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const Components = require("eslint-plugin-react/lib/util/Components");
|
|
4
|
-
|
|
5
|
-
exports.meta = {
|
|
6
|
-
type: "problem",
|
|
7
|
-
|
|
8
|
-
docs: {
|
|
9
|
-
description:
|
|
10
|
-
"Forbid returning ReactElement or ReactElement | null in function components",
|
|
11
|
-
category: "Best Practices",
|
|
12
|
-
recommended: true,
|
|
13
|
-
},
|
|
14
|
-
|
|
15
|
-
messages: {
|
|
16
|
-
invalidReturnType:
|
|
17
|
-
'Returning "{{value}}" is forbidden. Return "ReactNode" instead.',
|
|
18
|
-
replaceReturnType: 'Replace "{{value}}" by "ReactNode"',
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
hasSuggestions: true,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function getSuggestionFixer(node, range, context) {
|
|
25
|
-
return (fixer) => {
|
|
26
|
-
return fixer.replaceTextRange(range, "ReactNode");
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function checkFunctionReturnType(node, onError) {
|
|
31
|
-
const { returnType } = node;
|
|
32
|
-
if (returnType == null) return;
|
|
33
|
-
|
|
34
|
-
const { typeAnnotation } = returnType;
|
|
35
|
-
if (typeAnnotation == null) return;
|
|
36
|
-
|
|
37
|
-
const { typeName } = typeAnnotation;
|
|
38
|
-
if (typeName == null) return;
|
|
39
|
-
|
|
40
|
-
if (typeName.name !== "ReactNode") {
|
|
41
|
-
onError(typeName, typeName.name, typeName.range);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
exports.create = Components.detect((context, components) => {
|
|
46
|
-
return {
|
|
47
|
-
FunctionDeclaration(node) {
|
|
48
|
-
if (!components.get(node)) return;
|
|
49
|
-
|
|
50
|
-
checkFunctionReturnType(node, (typeName, currentName, range) =>
|
|
51
|
-
context.report({
|
|
52
|
-
node: typeName,
|
|
53
|
-
messageId: "invalidReturnType",
|
|
54
|
-
data: { value: currentName },
|
|
55
|
-
suggest: [
|
|
56
|
-
{
|
|
57
|
-
messageId: "replaceReturnType",
|
|
58
|
-
data: { value: currentName },
|
|
59
|
-
fix: getSuggestionFixer(node, range, context),
|
|
60
|
-
},
|
|
61
|
-
],
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.meta = {
|
|
4
|
-
type: "problem",
|
|
5
|
-
|
|
6
|
-
docs: {
|
|
7
|
-
description: "Use named react import as suggested in the doc",
|
|
8
|
-
category: "Best Practices",
|
|
9
|
-
recommended: true,
|
|
10
|
-
},
|
|
11
|
-
|
|
12
|
-
messages: {
|
|
13
|
-
namespace:
|
|
14
|
-
'import * as React from "{{value}}" is forbidden. Use named imports instead.',
|
|
15
|
-
default:
|
|
16
|
-
'import React from "{{value}}" is forbidden. Use named imports instead.',
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
exports.create = (context) => {
|
|
21
|
-
const checkNode = (node, messageId) => {
|
|
22
|
-
const importDeclaration = node.parent;
|
|
23
|
-
const importSource = importDeclaration.source.value.trim();
|
|
24
|
-
if (importSource !== "react") return;
|
|
25
|
-
context.report({
|
|
26
|
-
node,
|
|
27
|
-
messageId,
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
return {
|
|
31
|
-
ImportNamespaceSpecifier: (node) => {
|
|
32
|
-
checkNode(node, "namespace");
|
|
33
|
-
},
|
|
34
|
-
ImportDefaultSpecifier: (node) => {
|
|
35
|
-
checkNode(node, "default");
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { after, describe, it } = require("node:test");
|
|
4
|
-
// eslint-disable-next-line import/no-unresolved -- missing main field
|
|
5
|
-
const { RuleTester } = require("@typescript-eslint/rule-tester");
|
|
6
|
-
|
|
7
|
-
RuleTester.afterAll = after;
|
|
8
|
-
RuleTester.describe = describe;
|
|
9
|
-
RuleTester.it = it;
|
|
10
|
-
RuleTester.itOnly = it.only;
|
|
11
|
-
|
|
12
|
-
exports.RuleTester = RuleTester;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|