@autotelic/plumb 0.3.1
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 +22 -0
- package/README.md +64 -0
- package/package.json +41 -0
- package/src/effect/index.ts +59 -0
- package/src/effect/rules/guarded-op-must-return-effect.ts +236 -0
- package/src/effect/rules/guarded-op-must-return-option.ts +97 -0
- package/src/effect/rules/no-cascading-layer-provide.ts +80 -0
- package/src/effect/rules/no-decode-unknown-option.ts +41 -0
- package/src/effect/rules/no-direct-browser-storage.ts +91 -0
- package/src/effect/rules/no-direct-fetch.ts +44 -0
- package/src/effect/rules/no-manual-field-guards.ts +144 -0
- package/src/effect/rules/no-nested-layer-provide.ts +51 -0
- package/src/effect/rules/no-schema-class-modeling.ts +59 -0
- package/src/effect/rules/no-service-constructor-imports.ts +50 -0
- package/src/effect/rules/no-silent-error-swallow.ts +99 -0
- package/src/effect/rules/no-static-effect-service-forwarders.ts +227 -0
- package/src/effect/rules/no-try-catch.ts +32 -0
- package/src/effect/rules/no-unguarded-json-parse.ts +211 -0
- package/src/effect/rules/option-core-needs-effect-public.ts +81 -0
- package/src/effect/rules/prefer-die-for-precondition-defects.ts +118 -0
- package/src/effect/rules/prefer-effect-gen-for-guard-ladders.ts +76 -0
- package/src/effect/rules/prefer-effect-match.ts +78 -0
- package/src/effect/rules/prefer-effect-predicate-over-boolean-function.ts +79 -0
- package/src/effect/rules/prefer-named-guard-predicate.ts +105 -0
- package/src/effect/rules/prefer-option-pipeline.ts +95 -0
- package/src/effect/rules/prefer-ordering-match.ts +152 -0
- package/src/effect/rules/prefer-tagged-enum.ts +68 -0
- package/src/effect/rules/require-schema-type-derivation.ts +83 -0
- package/src/index.ts +106 -0
- package/src/meta/index.ts +25 -0
- package/src/meta/rules/no-disable-directives.ts +38 -0
- package/src/meta/rules/no-manual-ancestor-walks.ts +27 -0
- package/src/meta/rules/prefer-before-file-scope.ts +57 -0
- package/src/meta/rules/require-create-once.ts +51 -0
- package/src/meta/rules/require-rule-tester.ts +57 -0
- package/src/rules/no-anonymous-wide-tuples.ts +38 -0
- package/src/rules/no-barrel-export-star.ts +30 -0
- package/src/rules/no-boolean-field-signals.ts +305 -0
- package/src/rules/no-builtin-throws.ts +83 -0
- package/src/rules/no-chained-type-assertions.ts +65 -0
- package/src/rules/no-conditional-empty-object-spread.ts +42 -0
- package/src/rules/no-duplicated-literal-union.ts +66 -0
- package/src/rules/no-exported-mutable-state.ts +111 -0
- package/src/rules/no-generic-export-names.ts +149 -0
- package/src/rules/no-impossible-branch-throw.ts +55 -0
- package/src/rules/no-known-value-widening.ts +225 -0
- package/src/rules/no-module-mocking.ts +95 -0
- package/src/rules/no-multiple-function-params.ts +192 -0
- package/src/rules/no-nondeterministic-core.ts +73 -0
- package/src/rules/no-object-parameters.ts +127 -0
- package/src/rules/no-optional-function-parameters.ts +49 -0
- package/src/rules/no-product-of-state-booleans.ts +80 -0
- package/src/rules/no-redundant-derived-field.ts +192 -0
- package/src/rules/no-reflect-apply.ts +32 -0
- package/src/rules/no-reflect-get.ts +32 -0
- package/src/rules/no-reinterpret-cast.ts +78 -0
- package/src/rules/no-runtime-typeof.ts +59 -0
- package/src/rules/no-sentinel-comparison-union.ts +148 -0
- package/src/rules/no-shape-in-symbol-names.ts +45 -0
- package/src/rules/no-single-use-private-functions.ts +329 -0
- package/src/rules/no-stacked-jsdoc-blocks.ts +65 -0
- package/src/rules/no-stray-inline-comments.ts +79 -0
- package/src/rules/no-swappable-primitive-params.ts +109 -0
- package/src/rules/no-tag-ladder-assertions.ts +71 -0
- package/src/rules/no-transposed-field-reads.ts +135 -0
- package/src/rules/no-unit-return-validators.ts +65 -0
- package/src/rules/no-unknown-parameters.ts +97 -0
- package/src/rules/no-unknown-returns.ts +120 -0
- package/src/rules/no-unknown-type-aliases.ts +70 -0
- package/src/rules/no-unsafe-dictionary-type.ts +132 -0
- package/src/rules/no-vague-test-filenames.ts +67 -0
- package/src/rules/no-widen-then-assert.ts +345 -0
- package/src/rules/prefer-payload-brand.ts +67 -0
- package/src/rules/prefer-property-tests.ts +43 -0
- package/src/rules/require-canonical-stringify-for-identity.ts +80 -0
- package/src/rules/require-deprecated-tag-for-legacy-comments.ts +45 -0
- package/src/rules/require-exhaustive-tag-switch.ts +42 -0
- package/src/rules/require-fc-block-predicate.ts +42 -0
- package/src/rules/require-jsdoc-on-exported.ts +74 -0
- package/src/rules/require-published-order.ts +109 -0
- package/src/rules/require-safety-comment-for-type-assertion.ts +61 -0
- package/src/rules/require-sort-comparator.ts +41 -0
- package/src/rules/test-utils.ts +68 -0
- package/src/shared/ancestors.ts +21 -0
- package/src/shared/child-nodes.ts +27 -0
- package/src/shared/dictionary-types.ts +524 -0
- package/src/shared/is-node.ts +13 -0
- package/src/shared/lexical-type-parameters.ts +62 -0
- package/src/shared/reflect-method.ts +41 -0
- package/src/shared/rule-options.ts +23 -0
- package/src/shared/structural.ts +81 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/testable-rule.test.ts +68 -0
- package/src/testing/testable-rule.ts +86 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 autotelic
|
|
4
|
+
Copyright (c) anti-slop contributors (dmmulroy/anti-slop)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# plumb
|
|
2
|
+
|
|
3
|
+
Opinionated [Oxlint](https://oxc.rs) rules that keep TypeScript *true*: a plumb line for your codebase.
|
|
4
|
+
|
|
5
|
+
## Plugins
|
|
6
|
+
|
|
7
|
+
- **`plumb`**: generic rules that reject low-evidence and low-signal implementation patterns (43 rules).
|
|
8
|
+
- **`plumb-effect`**: opt-in rules for Effect service and Layer architecture (16 rules). Enable only in repos that depend on `effect`.
|
|
9
|
+
- **`plumb/testing`**: RuleTester lifecycle adapter (`testableRule`, `wireRuleTester`, `createTester`) so `createOnce` rules with `before`/`after` hooks test correctly under the ESLint-compatible `create` path.
|
|
10
|
+
|
|
11
|
+
## Consuming
|
|
12
|
+
|
|
13
|
+
### 1. Add the dependency (public npm, no tokens or registry config needed)
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm add -D @autotelic/plumb
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### 2. Register the plugins in your oxlint config
|
|
20
|
+
|
|
21
|
+
Point `jsPlugins` straight at the installed package: no copies, updates are just a version bump:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// oxlint.config.ts
|
|
25
|
+
jsPlugins: [
|
|
26
|
+
{ name: "plumb", specifier: "@autotelic/plumb" },
|
|
27
|
+
{ name: "plumb-effect", specifier: "@autotelic/plumb/effect" },
|
|
28
|
+
],
|
|
29
|
+
rules: {
|
|
30
|
+
"plumb/no-builtin-throws": "error",
|
|
31
|
+
"plumb-effect/guarded-op-must-return-effect": "error",
|
|
32
|
+
},
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
If your setup does not resolve package exports for plugin specifiers, point directly at the source files instead:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
jsPlugins: [
|
|
39
|
+
{ name: "plumb", specifier: "./node_modules/@autotelic/plumb/src/index.ts" },
|
|
40
|
+
{ name: "plumb-effect", specifier: "./node_modules/@autotelic/plumb/src/effect/index.ts" },
|
|
41
|
+
],
|
|
42
|
+
ignorePatterns: ["node_modules/**"],
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Updating
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
pnpm up @autotelic/plumb # bump to the latest published version
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Releasing (maintainers)
|
|
52
|
+
|
|
53
|
+
Releases publish to public npm automatically when a GitHub Release is published; the `Publish` workflow runs `pnpm check`, then publishes with provenance via npm trusted publishing (no stored npm token).
|
|
54
|
+
|
|
55
|
+
1. Update `version` in `package.json` and merge.
|
|
56
|
+
2. Create a GitHub Release for `vX.Y.Z`.
|
|
57
|
+
3. The workflow publishes to npmjs; verify on the release page.
|
|
58
|
+
|
|
59
|
+
## Development
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
pnpm install
|
|
63
|
+
pnpm check # lint + typecheck + test + rule/config consistency
|
|
64
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@autotelic/plumb",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Opinionated Oxlint rules that keep TypeScript true: generic and Effect-specific patterns, maintained as one source of truth for the autotelic repos.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"files": [
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./src/index.ts",
|
|
15
|
+
"./effect": "./src/effect/index.ts",
|
|
16
|
+
"./meta": "./src/meta/index.ts",
|
|
17
|
+
"./testing": "./src/testing/index.ts"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"check": "pnpm lint && pnpm typecheck && pnpm test && pnpm check:rule-tests && pnpm check:config",
|
|
21
|
+
"lint": "oxlint src",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"test": "vitest run --passWithNoTests",
|
|
24
|
+
"check:rule-tests": "node scripts/check-rule-tests.mjs",
|
|
25
|
+
"check:config": "node scripts/check-config.mjs"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@oxlint/plugins": "1.79.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "24.13.3",
|
|
32
|
+
"fast-check": "^4.9.0",
|
|
33
|
+
"oxlint": "1.79.0",
|
|
34
|
+
"typescript": "5.9.3",
|
|
35
|
+
"vitest": "^4.1.11"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=20"
|
|
39
|
+
},
|
|
40
|
+
"packageManager": "pnpm@10.33.0"
|
|
41
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import { guardedOpMustReturnEffectRule } from "./rules/guarded-op-must-return-effect.ts";
|
|
4
|
+
import { guardedOpMustReturnOptionRule } from "./rules/guarded-op-must-return-option.ts";
|
|
5
|
+
import { noDecodeUnknownOptionRule } from "./rules/no-decode-unknown-option.ts";
|
|
6
|
+
import { noManualFieldGuardsRule } from "./rules/no-manual-field-guards.ts";
|
|
7
|
+
import { noUnguardedJsonParseRule } from "./rules/no-unguarded-json-parse.ts";
|
|
8
|
+
import { noSchemaClassModelingRule } from "./rules/no-schema-class-modeling.ts";
|
|
9
|
+
import { noServiceConstructorImportsRule } from "./rules/no-service-constructor-imports.ts";
|
|
10
|
+
import { optionCoreNeedsEffectPublicRule } from "./rules/option-core-needs-effect-public.ts";
|
|
11
|
+
import { preferDieForPreconditionDefectsRule } from "./rules/prefer-die-for-precondition-defects.ts";
|
|
12
|
+
import { preferEffectGenForGuardLaddersRule } from "./rules/prefer-effect-gen-for-guard-ladders.ts";
|
|
13
|
+
import { preferEffectPredicateOverBooleanFunctionRule } from "./rules/prefer-effect-predicate-over-boolean-function.ts";
|
|
14
|
+
import { preferNamedGuardPredicateRule } from "./rules/prefer-named-guard-predicate.ts";
|
|
15
|
+
import { preferOrderingMatchRule } from "./rules/prefer-ordering-match.ts";
|
|
16
|
+
import { preferOptionPipelineRule } from "./rules/prefer-option-pipeline.ts";
|
|
17
|
+
import { preferTaggedEnumRule } from "./rules/prefer-tagged-enum.ts";
|
|
18
|
+
import { requireSchemaTypeDerivationRule } from "./rules/require-schema-type-derivation.ts";
|
|
19
|
+
import { noDirectFetchRule } from "./rules/no-direct-fetch.ts";
|
|
20
|
+
import { noDirectBrowserStorageRule } from "./rules/no-direct-browser-storage.ts";
|
|
21
|
+
import { noTryCatchRule } from "./rules/no-try-catch.ts";
|
|
22
|
+
import { noSilentErrorSwallowRule } from "./rules/no-silent-error-swallow.ts";
|
|
23
|
+
import { preferEffectMatchRule } from "./rules/prefer-effect-match.ts";
|
|
24
|
+
import { noNestedLayerProvideRule } from "./rules/no-nested-layer-provide.ts";
|
|
25
|
+
import { noCascadingLayerProvideRule } from "./rules/no-cascading-layer-provide.ts";
|
|
26
|
+
import { noStaticEffectServiceForwardersRule } from "./rules/no-static-effect-service-forwarders.ts";
|
|
27
|
+
|
|
28
|
+
/** Opt-in Oxlint rules for Effect service and Layer architecture. */
|
|
29
|
+
const plumbEffectPlugin = eslintCompatPlugin({
|
|
30
|
+
meta: { name: "plumb-effect" },
|
|
31
|
+
rules: {
|
|
32
|
+
"guarded-op-must-return-effect": guardedOpMustReturnEffectRule,
|
|
33
|
+
"guarded-op-must-return-option": guardedOpMustReturnOptionRule,
|
|
34
|
+
"no-decode-unknown-option": noDecodeUnknownOptionRule,
|
|
35
|
+
"no-manual-field-guards": noManualFieldGuardsRule,
|
|
36
|
+
"no-unguarded-json-parse": noUnguardedJsonParseRule,
|
|
37
|
+
"option-core-needs-effect-public": optionCoreNeedsEffectPublicRule,
|
|
38
|
+
"no-schema-class-modeling": noSchemaClassModelingRule,
|
|
39
|
+
"no-service-constructor-imports": noServiceConstructorImportsRule,
|
|
40
|
+
"prefer-die-for-precondition-defects": preferDieForPreconditionDefectsRule,
|
|
41
|
+
"prefer-effect-gen-for-guard-ladders": preferEffectGenForGuardLaddersRule,
|
|
42
|
+
"prefer-effect-predicate-over-boolean-function": preferEffectPredicateOverBooleanFunctionRule,
|
|
43
|
+
"prefer-named-guard-predicate": preferNamedGuardPredicateRule,
|
|
44
|
+
"prefer-option-pipeline": preferOptionPipelineRule,
|
|
45
|
+
"prefer-ordering-match": preferOrderingMatchRule,
|
|
46
|
+
"prefer-tagged-enum": preferTaggedEnumRule,
|
|
47
|
+
"require-schema-type-derivation": requireSchemaTypeDerivationRule,
|
|
48
|
+
"no-direct-fetch": noDirectFetchRule,
|
|
49
|
+
"no-direct-browser-storage": noDirectBrowserStorageRule,
|
|
50
|
+
"no-try-catch": noTryCatchRule,
|
|
51
|
+
"no-silent-error-swallow": noSilentErrorSwallowRule,
|
|
52
|
+
"prefer-effect-match": preferEffectMatchRule,
|
|
53
|
+
"no-nested-layer-provide": noNestedLayerProvideRule,
|
|
54
|
+
"no-cascading-layer-provide": noCascadingLayerProvideRule,
|
|
55
|
+
"no-static-effect-service-forwarders": noStaticEffectServiceForwardersRule,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export default plumbEffectPlugin;
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
import { ancestorsOf } from "../../shared/ancestors.ts";
|
|
6
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$|\/test\//u;
|
|
7
|
+
|
|
8
|
+
/** Whether the member chain ends in the Option namespace, e.g. `Option` or `O`.
|
|
9
|
+
*
|
|
10
|
+
* @param {ESTree.Expression | ESTree.Super} node - The callee object to inspect.
|
|
11
|
+
* @returns {boolean} True when the object resolves to the Option namespace.
|
|
12
|
+
*/
|
|
13
|
+
function isOptionObject(node: ESTree.Expression | ESTree.Super): boolean {
|
|
14
|
+
if (node.type === "Identifier") return node.name === "Option";
|
|
15
|
+
if (node.type === "MemberExpression" && !node.computed && node.property.type === "Identifier") {
|
|
16
|
+
return node.property.name === "Option";
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The name of the function whose body contains this node, if determinable.
|
|
22
|
+
*
|
|
23
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
24
|
+
* @param {ESTree.Node} node - The node whose enclosing function is sought.
|
|
25
|
+
* @returns {string | null} The enclosing function's name, or null.
|
|
26
|
+
*/
|
|
27
|
+
function enclosingFunctionName(sourceCode: SourceCode, node: ESTree.Node): string | null {
|
|
28
|
+
const chain = ancestorsOf(sourceCode, node);
|
|
29
|
+
for (let index = chain.length - 1; index >= 0; index--) {
|
|
30
|
+
const current = chain[index]!;
|
|
31
|
+
if (current.type === "FunctionDeclaration" && current.id !== null) return current.id.name;
|
|
32
|
+
if (
|
|
33
|
+
(current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") &&
|
|
34
|
+
node !== current
|
|
35
|
+
) {
|
|
36
|
+
const name =
|
|
37
|
+
current.type === "FunctionExpression" && current.id !== null
|
|
38
|
+
? current.id.name
|
|
39
|
+
: variableDeclaratorName(sourceCode, current);
|
|
40
|
+
if (name !== null) return name;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The declared name of the variable a function expression initialises, if any.
|
|
47
|
+
*
|
|
48
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
49
|
+
* @param {ESTree.Node} node - The function expression node.
|
|
50
|
+
* @returns {string | null} The initialising declarator's name, or null.
|
|
51
|
+
*/
|
|
52
|
+
function variableDeclaratorName(sourceCode: SourceCode, node: ESTree.Node): string | null {
|
|
53
|
+
const chain = ancestorsOf(sourceCode, node);
|
|
54
|
+
for (let index = chain.length - 1; index >= 0; index--) {
|
|
55
|
+
const current = chain[index]!;
|
|
56
|
+
if (current.type === "Program") break;
|
|
57
|
+
if (current.type === "VariableDeclarator" && current.id.type === "Identifier") return current.id.name;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The doctrine binds public APIs: walk up to see if this node is exported.
|
|
63
|
+
*
|
|
64
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
65
|
+
* @param {ESTree.Node} node - The node to test for export ancestry.
|
|
66
|
+
* @returns {boolean} True when an export declaration encloses the node.
|
|
67
|
+
*/
|
|
68
|
+
function hasExportAncestor(sourceCode: SourceCode, node: ESTree.Node): boolean {
|
|
69
|
+
const chain = ancestorsOf(sourceCode, node);
|
|
70
|
+
for (let index = chain.length - 1; index >= 0; index--) {
|
|
71
|
+
const current = chain[index]!;
|
|
72
|
+
if (current.type === "ExportNamedDeclaration" || current.type === "ExportDefaultDeclaration") return true;
|
|
73
|
+
if (current.type === "Program") return false;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A return-type annotation naming Option<T>, e.g. `Option.Option<Account>`.
|
|
79
|
+
*
|
|
80
|
+
* @param {ESTree.TSType} type - The type node to inspect.
|
|
81
|
+
* @returns {string | null} The Option spelling used, or null when not an Option reference.
|
|
82
|
+
*/
|
|
83
|
+
function optionTypeReference(type: ESTree.TSType): string | null {
|
|
84
|
+
if (type.type !== "TSTypeReference") return null;
|
|
85
|
+
const name = type.typeName;
|
|
86
|
+
if (name.type === "Identifier" && name.name === "Option") return "Option";
|
|
87
|
+
if (
|
|
88
|
+
name.type === "TSQualifiedName" &&
|
|
89
|
+
name.right.type === "Identifier" &&
|
|
90
|
+
name.right.name === "Option"
|
|
91
|
+
) {
|
|
92
|
+
return "Option.Option";
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Guarded operations must surface failure reasons as tagged MoneyError values
|
|
99
|
+
* through the Effect error channel, not as bare Option.None which conflates
|
|
100
|
+
* every possible why into one silent absence.
|
|
101
|
+
*/
|
|
102
|
+
export const guardedOpMustReturnEffectRule = defineRule({
|
|
103
|
+
meta: {
|
|
104
|
+
type: "problem",
|
|
105
|
+
docs: {
|
|
106
|
+
description:
|
|
107
|
+
"Require guarded operations to fail with typed MoneyError reasons via Effect/Either instead of returning bare Option values.",
|
|
108
|
+
},
|
|
109
|
+
messages: {
|
|
110
|
+
optionReturn:
|
|
111
|
+
"`{{found}}` hides why the operation failed. Return `Effect.Effect<T, MoneyError>` (or `Either<T, MoneyError>` in sync cores) with Schema.TaggedError reasons so callers can match Overflow vs InvalidInput vs ZeroDivisor. Options buried in tuples, records or arrays hide it just as well.",
|
|
112
|
+
bareNone:
|
|
113
|
+
"`Option.none()` here discards the reason for failure. Fail with a tagged MoneyError reason instead, or keep the Option strictly internal while a converted public API owns the error channel.",
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
createOnce(context) {
|
|
117
|
+
return {
|
|
118
|
+
before() {
|
|
119
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/"))) return false;
|
|
120
|
+
},
|
|
121
|
+
ReturnStatement(node) {
|
|
122
|
+
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
123
|
+
if (enclosingFunctionName(context.sourceCode, node)?.endsWith("Option")) return;
|
|
124
|
+
const argument = node.argument;
|
|
125
|
+
if (argument === null || argument === undefined) return;
|
|
126
|
+
let call = argument;
|
|
127
|
+
while (call.type === "ParenthesizedExpression") call = call.expression;
|
|
128
|
+
if (call.type !== "CallExpression") return;
|
|
129
|
+
if (
|
|
130
|
+
call.callee.type === "MemberExpression" &&
|
|
131
|
+
call.callee.property.type === "Identifier" &&
|
|
132
|
+
call.callee.property.name === "none" &&
|
|
133
|
+
isOptionObject(call.callee.object)
|
|
134
|
+
) {
|
|
135
|
+
context.report({ node, messageId: "bareNone" });
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
ArrowFunctionExpression(node) {
|
|
139
|
+
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
140
|
+
const parentId = variableDeclaratorName(context.sourceCode, node);
|
|
141
|
+
if (parentId !== null && parentId.endsWith("Option")) return;
|
|
142
|
+
const annotation = node.returnType?.typeAnnotation;
|
|
143
|
+
if (annotation === undefined) return;
|
|
144
|
+
const found = optionInReturnType(annotation);
|
|
145
|
+
if (found !== null) {
|
|
146
|
+
context.report({ node: node.returnType ?? node, messageId: "optionReturn", data: { found } });
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
FunctionDeclaration(node) {
|
|
150
|
+
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
151
|
+
checkFunctionReturn(context, node, node.id?.name ?? null);
|
|
152
|
+
},
|
|
153
|
+
FunctionExpression(node) {
|
|
154
|
+
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
155
|
+
checkFunctionReturn(
|
|
156
|
+
context,
|
|
157
|
+
node,
|
|
158
|
+
node.id?.name ?? variableDeclaratorName(context.sourceCode, node),
|
|
159
|
+
);
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
function checkFunctionReturn(
|
|
166
|
+
context: { report(diagnostic: { node: ESTree.Node; messageId: string; data?: Record<string, string> }): void },
|
|
167
|
+
node: ESTree.Function,
|
|
168
|
+
idName: string | null,
|
|
169
|
+
): void {
|
|
170
|
+
if (idName !== null && idName.endsWith("Option")) return;
|
|
171
|
+
const annotation = node.returnType?.typeAnnotation;
|
|
172
|
+
if (annotation === undefined) return;
|
|
173
|
+
const found = optionInReturnType(annotation);
|
|
174
|
+
if (found !== null) {
|
|
175
|
+
context.report({ node: node.returnType ?? node, messageId: "optionReturn", data: { found } });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
/** Search a type node for an embedded Option reference (tuples, arrays, members).
|
|
181
|
+
*
|
|
182
|
+
* @param {ESTree.TSType} type - The type node to search.
|
|
183
|
+
* @returns {string | null} The first embedded Option spelling, or null when none.
|
|
184
|
+
*/
|
|
185
|
+
function findOptionType(type: ESTree.TSType): string | null {
|
|
186
|
+
const direct = optionTypeReference(type);
|
|
187
|
+
if (direct !== null) return direct;
|
|
188
|
+
switch (type.type) {
|
|
189
|
+
case "TSTupleType": {
|
|
190
|
+
const render = (element: ESTree.TSTupleElement): string | null => {
|
|
191
|
+
if ("elementType" in element) return render(element.elementType);
|
|
192
|
+
if (element.type === "TSOptionalType" || element.type === "TSRestType") {
|
|
193
|
+
return findOptionType(element.typeAnnotation);
|
|
194
|
+
}
|
|
195
|
+
return findOptionType(element);
|
|
196
|
+
};
|
|
197
|
+
for (const element of type.elementTypes) {
|
|
198
|
+
const found = render(element);
|
|
199
|
+
if (found !== null) return found;
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
case "TSTypeOperator":
|
|
204
|
+
return findOptionType(type.typeAnnotation);
|
|
205
|
+
case "TSArrayType":
|
|
206
|
+
return findOptionType(type.elementType);
|
|
207
|
+
case "TSTypeLiteral":
|
|
208
|
+
for (const member of type.members) {
|
|
209
|
+
if (member.type !== "TSPropertySignature") continue;
|
|
210
|
+
const annotation = member.typeAnnotation?.typeAnnotation;
|
|
211
|
+
if (annotation === undefined || annotation === null) continue;
|
|
212
|
+
const found = findOptionType(annotation);
|
|
213
|
+
if (found !== null) return found;
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
default:
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** A return-type annotation hiding an Option anywhere: unions, tuples, arrays, members.
|
|
222
|
+
*
|
|
223
|
+
* @param {ESTree.TSType} annotation - The return-type annotation to search.
|
|
224
|
+
* @returns {string | null} The Option spelling found, or null when none.
|
|
225
|
+
*/
|
|
226
|
+
function optionInReturnType(annotation: ESTree.TSType): string | null {
|
|
227
|
+
let current = annotation;
|
|
228
|
+
while (current.type === "TSParenthesizedType") current = current.typeAnnotation;
|
|
229
|
+
if (current.type === "TSUnionType") {
|
|
230
|
+
for (const member of current.types) {
|
|
231
|
+
const found = optionTypeReference(member);
|
|
232
|
+
if (found !== null) return found;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return findOptionType(current);
|
|
236
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
4
|
+
import { ancestorsOf } from "../../shared/ancestors.ts";
|
|
5
|
+
|
|
6
|
+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
7
|
+
|
|
8
|
+
type FunctionWithReturnType = ESTree.ArrowFunctionExpression | ESTree.Function;
|
|
9
|
+
|
|
10
|
+
function isNullableKeyword(type: ESTree.TSType): boolean {
|
|
11
|
+
return type.type === "TSNullKeyword" || type.type === "TSUndefinedKeyword";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns whether a union pairs a nullable member with at least one real
|
|
16
|
+
* value type (T | null/undefined, not void | undefined).
|
|
17
|
+
*
|
|
18
|
+
* @param {ESTree.TSUnionType} union - The union annotation to inspect.
|
|
19
|
+
* @returns {boolean} True when the union mixes null/undefined with value types.
|
|
20
|
+
*/
|
|
21
|
+
function nullableWithValue(union: ESTree.TSUnionType): boolean {
|
|
22
|
+
let nullable = false;
|
|
23
|
+
let value = false;
|
|
24
|
+
for (const member of union.types) {
|
|
25
|
+
if (isNullableKeyword(member)) {
|
|
26
|
+
nullable = true;
|
|
27
|
+
} else if (member.type !== "TSVoidKeyword" && member.type !== "TSNeverKeyword") {
|
|
28
|
+
value = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return nullable && value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function asUnion(type: ESTree.TSType): ESTree.TSUnionType | null {
|
|
35
|
+
if (type.type === "TSParenthesizedType") return asUnion(type.typeAnnotation);
|
|
36
|
+
return type.type === "TSUnionType" ? type : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Guarded operations must surface absence as Option, not null/undefined unions or crashes. */
|
|
40
|
+
export const guardedOpMustReturnOptionRule = defineRule({
|
|
41
|
+
meta: {
|
|
42
|
+
type: "problem",
|
|
43
|
+
docs: {
|
|
44
|
+
description:
|
|
45
|
+
"Disallow returning T | null/undefined from guarded operations and getOrThrow escapes; express absence as Option<T> so callers compose with Option.zip/flatMap/filter.",
|
|
46
|
+
},
|
|
47
|
+
messages: {
|
|
48
|
+
nullableReturn:
|
|
49
|
+
"This function returns `{{type}}`, conflating absence with a value. Return Option<{{value}}> and express absence as Option.none().",
|
|
50
|
+
optionEscape:
|
|
51
|
+
"`getOrThrow` turns absence into a runtime crash. Keep the Option in the type and handle None, or parse to a refined value so the empty branch can't exist.",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
createOnce(context) {
|
|
55
|
+
|
|
56
|
+
const checkReturnType = (node: FunctionWithReturnType) => {
|
|
57
|
+
const annotation = node.returnType;
|
|
58
|
+
if (annotation === null || annotation === undefined) return;
|
|
59
|
+
const type = annotation.typeAnnotation;
|
|
60
|
+
const union = asUnion(type);
|
|
61
|
+
if (union === null || !nullableWithValue(union)) return;
|
|
62
|
+
const value = union.types
|
|
63
|
+
.filter((member) => !isNullableKeyword(member) && member.type !== "TSVoidKeyword")
|
|
64
|
+
.map((member) => context.sourceCode.getText(member))
|
|
65
|
+
.join(" | ");
|
|
66
|
+
context.report({
|
|
67
|
+
node: type,
|
|
68
|
+
messageId: "nullableReturn",
|
|
69
|
+
data: { type: context.sourceCode.getText(type), value },
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
before() {
|
|
75
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/"))) return false;
|
|
76
|
+
},
|
|
77
|
+
ArrowFunctionExpression: checkReturnType,
|
|
78
|
+
FunctionDeclaration: checkReturnType,
|
|
79
|
+
FunctionExpression: checkReturnType,
|
|
80
|
+
MemberExpression(node) {
|
|
81
|
+
if (
|
|
82
|
+
!node.computed &&
|
|
83
|
+
node.property.type === "Identifier" &&
|
|
84
|
+
node.property.name === "getOrThrow"
|
|
85
|
+
) {
|
|
86
|
+
context.report({ node, messageId: "optionEscape" });
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
Identifier(node) {
|
|
90
|
+
if (node.name !== "getOrThrow") return;
|
|
91
|
+
const parent = (ancestorsOf(context.sourceCode, node)).at(-1);
|
|
92
|
+
if (parent?.type === "MemberExpression" && parent.property === node) return;
|
|
93
|
+
context.report({ node, messageId: "optionEscape" });
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
import { isString } from "../../shared/structural.ts";
|
|
6
|
+
|
|
7
|
+
const PROVISION_METHODS = new Set(["provide", "provideMerge"]);
|
|
8
|
+
|
|
9
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$|\/test\//u;
|
|
10
|
+
|
|
11
|
+
/** Multiple provision stages in one pipe entangle dependency tiers; combine independent layers or name each stage.
|
|
12
|
+
*
|
|
13
|
+
* Import tracking covers local names bound to the `Layer` export of
|
|
14
|
+
* "effect"; re-exports are not tracked.
|
|
15
|
+
*/
|
|
16
|
+
export const noCascadingLayerProvideRule = defineRule({
|
|
17
|
+
meta: {
|
|
18
|
+
type: "problem",
|
|
19
|
+
docs: {
|
|
20
|
+
description:
|
|
21
|
+
"Disallow multiple Layer.provide/provideMerge stages in one pipe; combine independent dependencies in one Layer.provide([...]) and extract intentional dependency stages as named layers.",
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
cascadingProvide:
|
|
25
|
+
"Multiple `Layer.provide` stages run in this pipe, entangling dependency tiers. Combine independent dependencies into one `Layer.provide([a, b])`; when a layer depends on another layer, extract that configured layer into a named value before providing it.",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
createOnce(context) {
|
|
29
|
+
const layerNames = new Set<string>();
|
|
30
|
+
return {
|
|
31
|
+
before() {
|
|
32
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/"))) return false;
|
|
33
|
+
},
|
|
34
|
+
ImportDeclaration(node) {
|
|
35
|
+
if (node.source.value !== "effect") return;
|
|
36
|
+
for (const specifier of node.specifiers) {
|
|
37
|
+
if (specifier.type === "ImportNamespaceSpecifier" || specifier.type === "ImportDefaultSpecifier") {
|
|
38
|
+
if (specifier.local.name === "Layer") layerNames.add("Layer");
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (specifier.type !== "ImportSpecifier") continue;
|
|
42
|
+
const importedName =
|
|
43
|
+
specifier.imported.type === "Identifier"
|
|
44
|
+
? specifier.imported.name
|
|
45
|
+
: isString(specifier.imported.value)
|
|
46
|
+
? specifier.imported.value
|
|
47
|
+
: undefined;
|
|
48
|
+
if (importedName !== "Layer") continue;
|
|
49
|
+
layerNames.add(specifier.local.name);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
CallExpression(node) {
|
|
53
|
+
const callee = node.callee;
|
|
54
|
+
if (
|
|
55
|
+
callee.type !== "MemberExpression" ||
|
|
56
|
+
callee.computed ||
|
|
57
|
+
callee.property.type !== "Identifier" ||
|
|
58
|
+
callee.property.name !== "pipe"
|
|
59
|
+
) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
let stages = 0;
|
|
63
|
+
for (const argument of node.arguments) {
|
|
64
|
+
if (
|
|
65
|
+
argument.type === "CallExpression" &&
|
|
66
|
+
argument.callee.type === "MemberExpression" &&
|
|
67
|
+
!argument.callee.computed &&
|
|
68
|
+
argument.callee.object.type === "Identifier" &&
|
|
69
|
+
layerNames.has(argument.callee.object.name) &&
|
|
70
|
+
argument.callee.property.type === "Identifier" &&
|
|
71
|
+
PROVISION_METHODS.has(argument.callee.property.name)
|
|
72
|
+
)
|
|
73
|
+
stages += 1;
|
|
74
|
+
}
|
|
75
|
+
if (stages < 2) return;
|
|
76
|
+
context.report({ node, messageId: "cascadingProvide" });
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* decodeUnknownOption collapses every schema mismatch into None, discarding
|
|
9
|
+
* the issue tree that explains why decoding failed. Prefer adapters that keep
|
|
10
|
+
* the issues: SchemaParser.decodeUnknownResult for sync cores,
|
|
11
|
+
* decodeUnknownEffect on the Effect channel.
|
|
12
|
+
*/
|
|
13
|
+
export const noDecodeUnknownOptionRule = defineRule({
|
|
14
|
+
meta: {
|
|
15
|
+
type: "problem",
|
|
16
|
+
docs: {
|
|
17
|
+
description:
|
|
18
|
+
"Disallow Schema.decodeUnknownOption outside tests: it discards mismatch details. Return the schema issues as typed failure evidence instead.",
|
|
19
|
+
},
|
|
20
|
+
messages: {
|
|
21
|
+
noDecodeUnknownOption:
|
|
22
|
+
"`decodeUnknownOption` intentionally discards mismatch details, so callers cannot learn why decoding failed. Use `SchemaParser.decodeUnknownResult(schema)` (sync cores) or `SchemaParser.decodeUnknownEffect(schema)` (Effect channel) and fold the issue into your tagged error's evidence.",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
createOnce(context) {
|
|
26
|
+
return {
|
|
27
|
+
before() {
|
|
28
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/"))) return false;
|
|
29
|
+
},
|
|
30
|
+
MemberExpression(node) {
|
|
31
|
+
if (
|
|
32
|
+
!node.computed &&
|
|
33
|
+
node.property.type === "Identifier" &&
|
|
34
|
+
node.property.name === "decodeUnknownOption"
|
|
35
|
+
) {
|
|
36
|
+
context.report({ node, messageId: "noDecodeUnknownOption" });
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
});
|