eslint-plugin-function-rule 0.0.3 → 0.0.6

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 REL1CX
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,15 +1,135 @@
1
1
  # eslint-plugin-function-rule
2
2
 
3
- To install dependencies:
3
+ ESLint plugin to write custom rules with JavaScript functions.
4
4
 
5
- ```bash
6
- bun install
5
+ > [!WARNING]
6
+ > This package is a work in progress and is not yet ready for production use.
7
+
8
+ ## Installation
9
+
10
+ ```sh
11
+ # npm
12
+ npm install --save-dev eslint-plugin-function-rule
7
13
  ```
8
14
 
9
- To run:
15
+ ## Configure ESLint
16
+
17
+ ### Write function rules inline
18
+
19
+ ```js
20
+ // eslint.config.ts
21
+
22
+ import eslintJs from "@eslint/js";
23
+ import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
24
+ import functionRule from "eslint-plugin-function-rule";
25
+ import { defineConfig } from "eslint/config";
26
+ import tseslint from "typescript-eslint";
10
27
 
11
- ```bash
12
- bun run index.ts
28
+ export default defineConfig(
29
+ {
30
+ files: ["**/*.ts", "**/*.tsx"],
31
+ extends: [
32
+ eslintJs.configs.recommended,
33
+ tseslint.configs.recommended,
34
+ ],
35
+ languageOptions: {
36
+ parser: tseslint.parser,
37
+ parserOptions: {
38
+ projectService: true,
39
+ tsconfigRootDir: import.meta.dirname,
40
+ },
41
+ },
42
+ },
43
+ {
44
+ files: ["**/*.ts"],
45
+ rules: {
46
+ "function-rule/function-rule": "error",
47
+ },
48
+ plugins: {
49
+ "function-rule": functionRule((context) => {
50
+ return {
51
+ DebuggerStatement(node) {
52
+ context.report({
53
+ node,
54
+ message: "Remove 'debugger' from code.",
55
+
56
+ fix(fixer) {
57
+ return fixer.remove(node);
58
+ },
59
+ });
60
+ },
61
+ } satisfies RuleListener;
62
+ }),
63
+ },
64
+ },
65
+ );
13
66
  ```
14
67
 
15
- This project was created using `bun init` in bun v1.3.2. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
68
+ ### Import function rules from modules
69
+
70
+ ```js
71
+ // noDebugger.ts
72
+
73
+ import type { RuleDefinition } from "@eslint/core";
74
+ import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
75
+ import { defineRuleVisitor } from "eslint-plugin-function-rule";
76
+
77
+ // Define and document function rule options
78
+ export interface noDebuggerOptions {
79
+ /**
80
+ * @deprecated Use bar instead
81
+ */
82
+ foo: string
83
+ bar: string
84
+ }
85
+
86
+ // Define and document function rule
87
+ /**
88
+ * Remove debugger from code
89
+ *
90
+ * @param options The rule options
91
+ * @returns RuleFunction
92
+ */
93
+ export function noDebugger(options?: noDebuggerOptions): RuleDefinition["create"] {
94
+ return (context) => {
95
+ return defineRuleVisitor({
96
+ DebuggerStatement(node) {
97
+ context.report({
98
+ node,
99
+ message: "Remove 'debugger' from code.",
100
+
101
+ fix(fixer) {
102
+ return fixer.remove(node);
103
+ },
104
+ });
105
+ },
106
+ });
107
+ };
108
+ }
109
+ ```
110
+
111
+ ```js
112
+ // Import and use function rule
113
+
114
+ // ...
115
+ import { noDebugger } from "./noDebugger.ts";
116
+
117
+ const noDebuggerRule = noDebugger({ bar: "pass rule options" });
118
+
119
+ export default defineConfig(
120
+ // ...
121
+ {
122
+ files: ["**/*.ts"],
123
+ rules: {
124
+ "function-rule/function-rule": "error",
125
+ },
126
+ plugins: {
127
+ "function-rule": functionRule((context) => {
128
+ return {
129
+ ...noDebuggerRule(context)
130
+ }
131
+ }),
132
+ },
133
+ },
134
+ );
135
+ ```
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import * as _eslint_core0 from "@eslint/core";
2
- import { RuleDefinition } from "@eslint/core";
2
+ import { RuleDefinition, RuleVisitor } from "@eslint/core";
3
3
 
4
4
  //#region index.d.ts
5
+ declare function defineRuleVisitor(ruleVisitor: RuleVisitor): RuleVisitor;
5
6
  declare function functionRule(create: RuleDefinition["create"]): {
6
7
  readonly rules: {
7
8
  readonly "function-rule": {
@@ -20,9 +21,9 @@ declare function functionRule(create: RuleDefinition["create"]): {
20
21
  RuleOptions: unknown[];
21
22
  Node: unknown;
22
23
  MessageIds: string;
23
- }>) => _eslint_core0.RuleVisitor;
24
+ }>) => RuleVisitor;
24
25
  };
25
26
  };
26
27
  };
27
28
  //#endregion
28
- export { functionRule as default };
29
+ export { functionRule as default, defineRuleVisitor };
package/dist/index.js CHANGED
@@ -1,4 +1,10 @@
1
1
  //#region index.ts
2
+ const id = 0;
3
+ function defineRuleVisitor(ruleVisitor) {
4
+ const visitor = {};
5
+ for (const key of Object.keys(ruleVisitor)) visitor[key + `[type!=${id}]`] = ruleVisitor[key];
6
+ return visitor;
7
+ }
2
8
  function functionRule(create) {
3
9
  return { rules: { "function-rule": {
4
10
  meta: {
@@ -10,4 +16,4 @@ function functionRule(create) {
10
16
  }
11
17
 
12
18
  //#endregion
13
- export { functionRule as default };
19
+ export { functionRule as default, defineRuleVisitor };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-function-rule",
3
- "version": "0.0.3",
3
+ "version": "0.0.6",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -15,6 +15,7 @@
15
15
  "package.json"
16
16
  ],
17
17
  "dependencies": {
18
+ "string-ts": "^2.2.1",
18
19
  "@eslint/core": "^0.17.0"
19
20
  },
20
21
  "devDependencies": {