@pob/eslint-plugin 56.1.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 +4 -0
- package/CHANGELOG.md +11 -0
- package/configs/base.js +7 -0
- package/configs/react.js +8 -0
- package/docs/rules/forbid-non-native-fetch-import.md +21 -0
- package/docs/rules/react-function-return-react-node.md +27 -0
- package/docs/rules/react-named-import.md +21 -0
- package/fixtures/file.ts +0 -0
- package/fixtures/react.tsx +0 -0
- package/fixtures/tsconfig.json +10 -0
- package/index.js +15 -0
- package/loadRules.js +42 -0
- package/package.json +36 -0
- package/rules/forbid-non-native-fetch-import.js +42 -0
- package/rules/forbid-non-native-fetch-import.test.js +35 -0
- package/rules/react-function-component-return-react-node.js +66 -0
- package/rules/react-function-component-return-react-node.test.js +58 -0
- package/rules/react-named-import.js +38 -0
- package/rules/react-named-import.test.js +65 -0
- package/rules/test-utils/RuleTester.js +12 -0
package/.eslintrc.json
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [56.1.0](https://github.com/christophehurpeau/eslint-config-pob/compare/v56.0.0...v56.1.0) (2024-07-27)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* pob plugin ([fd99ec8](https://github.com/christophehurpeau/eslint-config-pob/commit/fd99ec8711409b3bc9b960f7eb2e0b2e67984381))
|
|
11
|
+
|
package/configs/base.js
ADDED
package/configs/react.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
|
@@ -0,0 +1,21 @@
|
|
|
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/fixtures/file.ts
ADDED
|
File without changes
|
|
File without changes
|
package/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pob/eslint-plugin",
|
|
3
|
+
"version": "56.1.0",
|
|
4
|
+
"description": "eslint plugin for react",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20.10.0"
|
|
8
|
+
},
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"exports": {
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": "./index.js"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"eslint": "^8.39.0",
|
|
19
|
+
"typescript": ">=5.2.2"
|
|
20
|
+
},
|
|
21
|
+
"peerDependenciesMeta": {
|
|
22
|
+
"typescript": {
|
|
23
|
+
"optional": true
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@typescript-eslint/utils": "6.21.0",
|
|
28
|
+
"eslint-plugin-react": "^7.33.2"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@eslint/eslintrc": "2.1.4",
|
|
32
|
+
"@typescript-eslint/rule-tester": "6.21.0",
|
|
33
|
+
"eslint": "8.57.0",
|
|
34
|
+
"typescript": "5.4.5"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { RuleTester } = require("eslint");
|
|
4
|
+
const rule = require("./forbid-non-native-fetch-import");
|
|
5
|
+
|
|
6
|
+
const parserOptions = {
|
|
7
|
+
ecmaVersion: 2018,
|
|
8
|
+
sourceType: "module",
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const ruleTester = new RuleTester({ parserOptions });
|
|
12
|
+
|
|
13
|
+
ruleTester.run("forbid-non-native-fetch-import", rule, {
|
|
14
|
+
valid: [
|
|
15
|
+
{
|
|
16
|
+
code: `
|
|
17
|
+
fetch('https://example.com');
|
|
18
|
+
`,
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
invalid: [
|
|
22
|
+
{
|
|
23
|
+
code: `
|
|
24
|
+
import fetch from 'node-fetch';
|
|
25
|
+
fetch('https://example.com');
|
|
26
|
+
`,
|
|
27
|
+
errors: [
|
|
28
|
+
{
|
|
29
|
+
messageId: "forbidden",
|
|
30
|
+
data: { value: "node-fetch" },
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const rule = require("./react-function-component-return-react-node");
|
|
5
|
+
const { RuleTester } = require("./test-utils/RuleTester");
|
|
6
|
+
|
|
7
|
+
const ruleTester = new RuleTester({
|
|
8
|
+
parser: require.resolve("@typescript-eslint/parser"),
|
|
9
|
+
parserOptions: {
|
|
10
|
+
tsconfigRootDir: path.join(__dirname, "../fixtures"),
|
|
11
|
+
project: "./tsconfig.json",
|
|
12
|
+
ecmaFeatures: {
|
|
13
|
+
jsx: true,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
ruleTester.run("react-function-component-return-react-node", rule, {
|
|
19
|
+
valid: [
|
|
20
|
+
{
|
|
21
|
+
code: `
|
|
22
|
+
import type { ReactNode } from 'react';
|
|
23
|
+
export function SomeComponent(): ReactNode {
|
|
24
|
+
return <div />;
|
|
25
|
+
}
|
|
26
|
+
`,
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
invalid: [
|
|
30
|
+
{
|
|
31
|
+
code: `
|
|
32
|
+
import type { ReactElement } from 'react';
|
|
33
|
+
export function SomeComponent(): ReactElement {
|
|
34
|
+
return <div />;
|
|
35
|
+
}`,
|
|
36
|
+
errors: [
|
|
37
|
+
{
|
|
38
|
+
messageId: "invalidReturnType",
|
|
39
|
+
data: { value: "ReactElement" },
|
|
40
|
+
line: 3,
|
|
41
|
+
column: 34,
|
|
42
|
+
endColumn: 46,
|
|
43
|
+
suggestions: [
|
|
44
|
+
{
|
|
45
|
+
messageId: "replaceReturnType",
|
|
46
|
+
data: { value: "ReactElement" },
|
|
47
|
+
output: `
|
|
48
|
+
import type { ReactElement } from 'react';
|
|
49
|
+
export function SomeComponent(): ReactNode {
|
|
50
|
+
return <div />;
|
|
51
|
+
}`,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const rule = require("./react-named-import");
|
|
5
|
+
const { RuleTester } = require("./test-utils/RuleTester");
|
|
6
|
+
|
|
7
|
+
const ruleTester = new RuleTester({
|
|
8
|
+
parser: require.resolve("@typescript-eslint/parser"),
|
|
9
|
+
parserOptions: {
|
|
10
|
+
tsconfigRootDir: path.join(__dirname, "../fixtures"),
|
|
11
|
+
project: "./tsconfig.json",
|
|
12
|
+
ecmaFeatures: {
|
|
13
|
+
jsx: true,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
ruleTester.run("react-named-import", rule, {
|
|
18
|
+
valid: [
|
|
19
|
+
{
|
|
20
|
+
code: `
|
|
21
|
+
import type { ReactNode } from 'react';
|
|
22
|
+
|
|
23
|
+
export const simpleElement: ReactNode = <p>paragraph</p>;
|
|
24
|
+
`,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
invalid: [
|
|
28
|
+
{
|
|
29
|
+
code: `
|
|
30
|
+
import React from 'react';
|
|
31
|
+
|
|
32
|
+
export const simpleElement: React.ReactNode = <p>paragraph</p>;
|
|
33
|
+
`,
|
|
34
|
+
errors: [
|
|
35
|
+
{
|
|
36
|
+
messageId: "default",
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
code: `
|
|
42
|
+
import * as React from 'react';
|
|
43
|
+
|
|
44
|
+
export const simpleElement: React.ReactNode = <p>paragraph</p>;
|
|
45
|
+
`,
|
|
46
|
+
errors: [
|
|
47
|
+
{
|
|
48
|
+
messageId: "namespace",
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
code: `
|
|
54
|
+
import type * as React from 'react';
|
|
55
|
+
|
|
56
|
+
export const simpleElement: React.ReactNode = <p>paragraph</p>;
|
|
57
|
+
`,
|
|
58
|
+
errors: [
|
|
59
|
+
{
|
|
60
|
+
messageId: "namespace",
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
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;
|