eslint-plugin-function-rule 0.0.16 → 0.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/LICENSE +1 -1
- package/README.md +47 -32
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/package.json +24 -19
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
ESLint plugin to write custom rules with JavaScript functions.
|
|
2
|
-
|
|
3
|
-
> [!WARNING]
|
|
4
|
-
> This package is a work in progress and is not yet ready for production use.
|
|
1
|
+
An ESLint plugin to write custom rules with JavaScript functions.
|
|
5
2
|
|
|
6
3
|
## Index
|
|
7
4
|
|
|
@@ -21,12 +18,12 @@ npm install --save-dev eslint-plugin-function-rule
|
|
|
21
18
|
|
|
22
19
|
## Write function rules inline
|
|
23
20
|
|
|
24
|
-
```
|
|
21
|
+
```ts
|
|
25
22
|
// eslint.config.ts
|
|
26
23
|
|
|
27
24
|
import type { Rule } from "eslint";
|
|
28
|
-
import { defineConfig } from "eslint/config";
|
|
29
25
|
import { functionRule } from "eslint-plugin-function-rule";
|
|
26
|
+
import { defineConfig } from "eslint/config";
|
|
30
27
|
|
|
31
28
|
export default defineConfig(
|
|
32
29
|
{
|
|
@@ -56,38 +53,38 @@ export default defineConfig(
|
|
|
56
53
|
|
|
57
54
|
## Or import function rules from modules
|
|
58
55
|
|
|
59
|
-
```
|
|
60
|
-
//
|
|
56
|
+
```ts
|
|
57
|
+
// no-null.ts
|
|
61
58
|
|
|
62
59
|
import type { Rule } from "eslint";
|
|
63
|
-
import { functionRuleListener } from "eslint-plugin-function-rule";
|
|
64
|
-
|
|
65
|
-
// Define and document function rule options
|
|
66
|
-
export interface noDebuggerOptions {}
|
|
67
60
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
61
|
+
export function noNull(options?: { enableAutoFix?: boolean; enableSuggest?: boolean }) {
|
|
62
|
+
const { enableAutoFix = false, enableSuggest = false } = options ?? {};
|
|
63
|
+
return (context: Rule.RuleContext): Rule.RuleListener => {
|
|
64
|
+
return {
|
|
65
|
+
Literal(node) {
|
|
66
|
+
if (node.raw !== "null" || node.parent.type !== "BinaryExpression") return;
|
|
67
|
+
function fix(fixer: Rule.RuleFixer) {
|
|
68
|
+
return fixer.replaceText(node, "undefined");
|
|
69
|
+
}
|
|
70
|
+
context.report({
|
|
71
|
+
node: node.parent,
|
|
72
|
+
message: "Avoid using 'null'; Use 'undefined' instead.",
|
|
73
|
+
...enableAutoFix ? { fix } : {},
|
|
74
|
+
...enableSuggest ? { suggest: [{ fix, desc: "Replace with 'undefined'." }] } : {},
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
};
|
|
82
79
|
}
|
|
83
80
|
```
|
|
84
81
|
|
|
85
|
-
```
|
|
82
|
+
```ts
|
|
86
83
|
// eslint.config.ts
|
|
87
84
|
|
|
88
85
|
import { functionRule } from "eslint-plugin-function-rule";
|
|
89
86
|
import { defineConfig } from "eslint/config";
|
|
90
|
-
import {
|
|
87
|
+
import { noNull } from "./no-null.ts";
|
|
91
88
|
|
|
92
89
|
export default defineConfig(
|
|
93
90
|
{
|
|
@@ -96,7 +93,7 @@ export default defineConfig(
|
|
|
96
93
|
"function-rule/function-rule": "error",
|
|
97
94
|
},
|
|
98
95
|
plugins: {
|
|
99
|
-
"function-rule": functionRule(
|
|
96
|
+
"function-rule": functionRule(noNull({ enableAutoFix: true })),
|
|
100
97
|
},
|
|
101
98
|
},
|
|
102
99
|
);
|
|
@@ -104,7 +101,7 @@ export default defineConfig(
|
|
|
104
101
|
|
|
105
102
|
## Define multiple function rules with custom prefix
|
|
106
103
|
|
|
107
|
-
```
|
|
104
|
+
```ts
|
|
108
105
|
// eslint.config.ts
|
|
109
106
|
|
|
110
107
|
import { functionRule } from "eslint-plugin-function-rule";
|
|
@@ -119,10 +116,28 @@ export default defineConfig(
|
|
|
119
116
|
},
|
|
120
117
|
plugins: {
|
|
121
118
|
"custom-1": functionRule((context) => {
|
|
122
|
-
return {
|
|
119
|
+
return {
|
|
120
|
+
CallExpression(node) {
|
|
121
|
+
if (context.sourceCode.getText(node.callee) === "Date.now") {
|
|
122
|
+
context.report({
|
|
123
|
+
node,
|
|
124
|
+
message: "Don't use 'Date.now'.",
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
};
|
|
123
129
|
}),
|
|
124
130
|
"custom-2": functionRule((context) => {
|
|
125
|
-
return {
|
|
131
|
+
return {
|
|
132
|
+
CallExpression(node) {
|
|
133
|
+
if (context.sourceCode.getText(node.callee) === "Math.random") {
|
|
134
|
+
context.report({
|
|
135
|
+
node,
|
|
136
|
+
message: "Don't use 'Math.random()'.",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
};
|
|
126
141
|
}),
|
|
127
142
|
},
|
|
128
143
|
},
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,20 @@ import { Plugin } from "@eslint/core";
|
|
|
2
2
|
import { Rule } from "eslint";
|
|
3
3
|
|
|
4
4
|
//#region src/index.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Wraps an ESLint rule's create function as an ESLint Plugin with a single rule named "function-rule".
|
|
8
|
+
* The rule is fixable and supports suggestions.
|
|
9
|
+
* @param create The rule's listener create function.
|
|
10
|
+
* @returns ESLint Plugin object with "function-rule".
|
|
11
|
+
*/
|
|
5
12
|
declare function functionRule(create: Rule.RuleModule["create"]): Plugin;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a copy of the given rule listener,
|
|
15
|
+
* but prepends an increasing number of spaces to each event key name for uniqueness.
|
|
16
|
+
* @param ruleListener ESLint rule listener object (mapping event name to handler).
|
|
17
|
+
* @returns New rule listener object with modified keys for uniqueness.
|
|
18
|
+
*/
|
|
6
19
|
declare function defineRuleListener(ruleListener: Rule.RuleListener): Rule.RuleListener;
|
|
7
20
|
//#endregion
|
|
8
21
|
export { defineRuleListener, functionRule };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
//#region src/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* Wraps an ESLint rule's create function as an ESLint Plugin with a single rule named "function-rule".
|
|
4
|
+
* The rule is fixable and supports suggestions.
|
|
5
|
+
* @param create The rule's listener create function.
|
|
6
|
+
* @returns ESLint Plugin object with "function-rule".
|
|
7
|
+
*/
|
|
2
8
|
function functionRule(create) {
|
|
3
9
|
return { rules: { "function-rule": {
|
|
4
10
|
meta: {
|
|
@@ -9,6 +15,12 @@ function functionRule(create) {
|
|
|
9
15
|
} } };
|
|
10
16
|
}
|
|
11
17
|
let id = 1;
|
|
18
|
+
/**
|
|
19
|
+
* Returns a copy of the given rule listener,
|
|
20
|
+
* but prepends an increasing number of spaces to each event key name for uniqueness.
|
|
21
|
+
* @param ruleListener ESLint rule listener object (mapping event name to handler).
|
|
22
|
+
* @returns New rule listener object with modified keys for uniqueness.
|
|
23
|
+
*/
|
|
12
24
|
function defineRuleListener(ruleListener) {
|
|
13
25
|
const listener = {};
|
|
14
26
|
for (const key of Object.keys(ruleListener)) listener[" ".repeat(id++) + key] = ruleListener[key];
|
package/package.json
CHANGED
|
@@ -1,39 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-function-rule",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "An ESLint plugin to write custom rules with JavaScript functions.",
|
|
5
|
+
"homepage": "https://github.com/Rel1cx/dx",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/Rel1cx/dx/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/Rel1cx/dx.git"
|
|
12
|
+
},
|
|
4
13
|
"license": "MIT",
|
|
14
|
+
"author": "Rel1cx<rel1cx@proton.me>",
|
|
5
15
|
"sideEffects": false,
|
|
6
16
|
"type": "module",
|
|
7
17
|
"exports": {
|
|
8
18
|
".": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
"default": "./dist/index.js"
|
|
12
|
-
}
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
13
21
|
},
|
|
14
22
|
"./package.json": "./package.json"
|
|
15
23
|
},
|
|
24
|
+
"main": "./dist/index.js",
|
|
16
25
|
"module": "./dist/index.js",
|
|
17
26
|
"types": "./dist/index.d.ts",
|
|
18
27
|
"files": [
|
|
19
28
|
"dist",
|
|
20
|
-
"package.json"
|
|
29
|
+
"./package.json"
|
|
21
30
|
],
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"@eslint/core": "^1.0.0"
|
|
24
|
-
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"dprint": "^0.50.2",
|
|
27
|
-
"tsdown": "^0.16.4"
|
|
28
|
-
},
|
|
29
31
|
"peerDependencies": {
|
|
30
|
-
"eslint": "^
|
|
31
|
-
"
|
|
32
|
+
"@eslint/core": "^1.0.0",
|
|
33
|
+
"eslint": "^9.39.1"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=20.0.0"
|
|
32
37
|
},
|
|
33
38
|
"scripts": {
|
|
34
|
-
"build": "tsdown",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
39
|
+
"build": "tsdown --dts-resolve",
|
|
40
|
+
"build:docs": "typedoc",
|
|
41
|
+
"lint:publish": "publint",
|
|
42
|
+
"lint:ts": "tsc --noEmit"
|
|
38
43
|
}
|
|
39
44
|
}
|