@onrails/eslint-plugin 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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -0
  3. package/package.json +60 -0
  4. package/src/index.js +105 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alan R. Soares
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 ADDED
@@ -0,0 +1,57 @@
1
+ # @onrails/eslint-plugin
2
+
3
+ ESLint rules for `@onrails/result` boundaries. Replaces the unmaintained `eslint-plugin-neverthrow`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ bun add -d @onrails/eslint-plugin
9
+ ```
10
+
11
+ ## Flat config (ESLint 9+)
12
+
13
+ ```js
14
+ // eslint.config.js
15
+ import onrails from "@onrails/eslint-plugin";
16
+
17
+ export default [
18
+ {
19
+ plugins: { "@onrails": onrails },
20
+ rules: {
21
+ "@onrails/no-promise-result": "warn",
22
+ "@onrails/no-unsafe-unwrap": "warn",
23
+ },
24
+ },
25
+ ];
26
+ ```
27
+
28
+ Or use the recommended preset:
29
+
30
+ ```js
31
+ import onrails from "@onrails/eslint-plugin";
32
+
33
+ export default [
34
+ {
35
+ plugins: { "@onrails": onrails },
36
+ rules: {
37
+ ...onrails.configs.recommended.rules,
38
+ },
39
+ },
40
+ ];
41
+ ```
42
+
43
+ ## Rules
44
+
45
+ | Rule | What it does |
46
+ |---|---|
47
+ | `@onrails/no-promise-result` | Flags `Promise<Result<…>>` in source — public APIs should return `ResultAsync` |
48
+ | `@onrails/no-unsafe-unwrap` | Flags `_unsafeUnwrap*` outside test files |
49
+
50
+ ## Caveats (v0)
51
+
52
+ - Current rules are **text-scan only**. They catch the common cases but miss `Promise<Result<…>>` hidden inside complex generics. A `@typescript-eslint`-powered AST rule is planned.
53
+ - Spec files (`*.spec.ts`) are exempted from `no-unsafe-unwrap`.
54
+
55
+ ## License
56
+
57
+ MIT
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@onrails/eslint-plugin",
3
+ "version": "0.1.0",
4
+ "description": "ESLint rules for @onrails/result boundaries — flags Promise<Result<…>> in public APIs and _unsafeUnwrap*. Replaces unmaintained eslint-plugin-neverthrow.",
5
+ "license": "MIT",
6
+ "author": "Alan R. Soares",
7
+ "homepage": "https://github.com/alanrsoares/onrails/tree/main/packages/eslint-plugin#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/alanrsoares/onrails.git",
11
+ "directory": "packages/eslint-plugin"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/alanrsoares/onrails/issues"
15
+ },
16
+ "keywords": [
17
+ "eslint",
18
+ "eslint-plugin",
19
+ "result",
20
+ "neverthrow",
21
+ "onrails",
22
+ "boundary",
23
+ "lint"
24
+ ],
25
+ "engines": {
26
+ "node": ">=18"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "provenance": true
31
+ },
32
+ "type": "module",
33
+ "sideEffects": false,
34
+ "main": "./src/index.js",
35
+ "exports": {
36
+ ".": "./src/index.js"
37
+ },
38
+ "files": [
39
+ "src",
40
+ "README.md",
41
+ "LICENSE"
42
+ ],
43
+ "peerDependencies": {
44
+ "eslint": "^10.4.0"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "@typescript-eslint/parser": {
48
+ "optional": true
49
+ }
50
+ },
51
+ "scripts": {
52
+ "typecheck": "echo 'noop (js plugin)'",
53
+ "test": "bun test",
54
+ "check": "bun test"
55
+ },
56
+ "devDependencies": {
57
+ "@typescript-eslint/parser": "^8.60.0",
58
+ "eslint": "^10.4.0"
59
+ }
60
+ }
package/src/index.js ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * @onrails/eslint-plugin — boundary rules for `@onrails/result`.
3
+ *
4
+ * Rules are written against the TypeScript-ESLint AST (`TSTypeReference`,
5
+ * `MemberExpression`, …). Consumers must use `@typescript-eslint/parser`
6
+ * for TypeScript files; without it the rules never fire.
7
+ */
8
+
9
+ const isIdentifierNamed = (node, name) =>
10
+ node && node.type === "Identifier" && node.name === name;
11
+
12
+ const noPromiseResult = {
13
+ meta: {
14
+ type: "problem",
15
+ fixable: "code",
16
+ docs: {
17
+ description:
18
+ "Disallow Promise<Result<…>> in public signatures — return ResultAsync<T, E> instead.",
19
+ },
20
+ schema: [],
21
+ messages: {
22
+ use: "Promise<Result<{{ok}}, {{err}}>> in public API — return ResultAsync<{{ok}}, {{err}}> and use fromAsync()/tryAsync() at the boundary.",
23
+ },
24
+ },
25
+ create(context) {
26
+ const sourceCode = context.sourceCode;
27
+ return {
28
+ TSTypeReference(node) {
29
+ if (!isIdentifierNamed(node.typeName, "Promise")) return;
30
+ const args = node.typeArguments?.params;
31
+ const inner = args?.[0];
32
+ if (!inner || inner.type !== "TSTypeReference") return;
33
+ if (!isIdentifierNamed(inner.typeName, "Result")) return;
34
+
35
+ const innerArgs = inner.typeArguments?.params;
36
+ const okT = innerArgs?.[0];
37
+ const errT = innerArgs?.[1];
38
+ const okText = okT ? sourceCode.getText(okT) : "T";
39
+ const errText = errT ? sourceCode.getText(errT) : "E";
40
+
41
+ context.report({
42
+ node,
43
+ messageId: "use",
44
+ data: { ok: okText, err: errText },
45
+ fix: (fixer) => fixer.replaceText(node, `ResultAsync<${okText}, ${errText}>`),
46
+ });
47
+ },
48
+ };
49
+ },
50
+ };
51
+
52
+ const noUnsafeUnwrap = {
53
+ meta: {
54
+ type: "suggestion",
55
+ docs: {
56
+ description: "Discourage _unsafeUnwrap / _unsafeUnwrapErr outside tests.",
57
+ },
58
+ schema: [],
59
+ messages: {
60
+ unsafe: "Avoid _unsafeUnwrap* — use match(), resolve(), or yieldResult() in tryGen.",
61
+ },
62
+ },
63
+ create(context) {
64
+ const filename = context.filename.replace(/\\/g, "/");
65
+ if (filename.includes(".spec.") || filename.includes(".test.")) {
66
+ return {};
67
+ }
68
+ return {
69
+ MemberExpression(node) {
70
+ if (
71
+ node.property.type === "Identifier" &&
72
+ (node.property.name === "_unsafeUnwrap" || node.property.name === "_unsafeUnwrapErr")
73
+ ) {
74
+ context.report({ node, messageId: "unsafe" });
75
+ }
76
+ },
77
+ };
78
+ },
79
+ };
80
+
81
+ /** @type {import('eslint').ESLint.Plugin} */
82
+ const plugin = {
83
+ meta: {
84
+ name: "@onrails/eslint-plugin",
85
+ version: "0.0.0",
86
+ },
87
+ rules: {
88
+ "no-promise-result": noPromiseResult,
89
+ "no-unsafe-unwrap": noUnsafeUnwrap,
90
+ },
91
+ };
92
+
93
+ export default plugin;
94
+
95
+ export const configs = {
96
+ recommended: {
97
+ plugins: {
98
+ "@onrails/result": plugin,
99
+ },
100
+ rules: {
101
+ "@onrails/result/no-promise-result": "warn",
102
+ "@onrails/result/no-unsafe-unwrap": "warn",
103
+ },
104
+ },
105
+ };