@sshkeda/check 0.0.0-stage → 1.0.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 +21 -0
- package/README.md +156 -2
- package/docs/how-to-make-check-pass.md +71 -0
- package/package.json +51 -4
- package/src/index.ts +8 -0
- package/src/internal/ban-abbreviated-words-in-declared-names.ts +165 -0
- package/src/internal/ban-checked-members-outside-calls.ts +151 -0
- package/src/internal/ban-comments.ts +23 -0
- package/src/internal/ban-comparing-or-switching-on-tag.ts +114 -0
- package/src/internal/ban-deadlines-inside-tests.ts +41 -0
- package/src/internal/ban-declared-values.ts +70 -0
- package/src/internal/ban-dropped-failures-detached-fibers-sleeps-and-untraced-functions.ts +67 -0
- package/src/internal/ban-failures-collected-as-results.ts +57 -0
- package/src/internal/ban-fallbacks-of-named-recoveries.ts +118 -0
- package/src/internal/ban-function-overloads.ts +107 -0
- package/src/internal/ban-global-error-in-failure-types.ts +146 -0
- package/src/internal/ban-instanceof-in-effect-recovery-callbacks.ts +63 -0
- package/src/internal/ban-let-and-effect-mutable-ref.ts +79 -0
- package/src/internal/ban-logic-in-index-files.ts +178 -0
- package/src/internal/ban-namespaces-used-without-a-member.ts +176 -0
- package/src/internal/ban-node-builtins-that-effect-replaces.ts +42 -0
- package/src/internal/ban-parsing-outside-data-without-a-schema.ts +299 -0
- package/src/internal/ban-re-exports.ts +172 -0
- package/src/internal/ban-reading-the-clock-and-sleeping-outside-effect.ts +239 -0
- package/src/internal/ban-retired-words-in-names.ts +49 -0
- package/src/internal/ban-scanning-directories-with-bun-glob.ts +79 -0
- package/src/internal/ban-shebang-lines.ts +26 -0
- package/src/internal/ban-timer-globals-and-the-global-object.ts +34 -0
- package/src/internal/ban-timer-schedules-without-deadlines.ts +240 -0
- package/src/internal/ban-type-and-interface-declarations.ts +48 -0
- package/src/internal/ban-type-predicates.ts +82 -0
- package/src/internal/ban-unknown-in-function-signatures.ts +143 -0
- package/src/internal/ban-vague-error-class-names.ts +28 -0
- package/src/internal/ban-vague-file-names.ts +51 -0
- package/src/internal/ban-what-a-file-is-not-granted.ts +512 -0
- package/src/internal/ban-while-do-while-and-for-loops.ts +27 -0
- package/src/internal/confine-service-declarations-to-granted-files.ts +116 -0
- package/src/internal/confine-the-stdio-service-to-terminal-output.ts +190 -0
- package/src/internal/define-the-lint-plugin-and-configs.ts +361 -0
- package/src/internal/describe-and-decode-rule-options.ts +22 -0
- package/src/internal/keep-assertions-in-the-test-home-and-ban-test-runners.ts +128 -0
- package/src/internal/keep-effect-testing-helpers-in-test-files.ts +157 -0
- package/src/internal/match-banned-modules-and-globals.ts +71 -0
- package/src/internal/match-effect-service-factories.ts +20 -0
- package/src/internal/require-a-named-const-for-compound-conditions.ts +44 -0
- package/src/internal/require-capital-snake-case-and-as-const-on-literal-constants.ts +141 -0
- package/src/internal/require-service-keys-to-end-with-their-class-name.ts +64 -0
- package/src/internal/resolve-what-an-expression-imports.ts +295 -0
- package/src/internal/sort-type-constituents.ts +82 -0
- package/src/internal/split-names-into-words.ts +12 -0
- package/src/internal/visit-every-declared-name.ts +264 -0
- package/src/lib/tasks/index.ts +1 -0
- package/src/lib/tasks/internal/declare-the-check-task.ts +29 -0
- package/src/lib/tasks/internal/declare-the-checker-queries-task.ts +41 -0
- package/src/lib/tasks/internal/declare-the-typed-checks-task.ts +25 -0
- package/src/lib/tasks/internal/run-checks-over-one-typescript-project.ts +68 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/index.ts +15 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/check-effect-diagnostics-with-effect-tsgo.ts +211 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/read-effect-tsgo-json-report.ts +39 -0
- package/src/lib/tasks/lib/check-import-boundaries/index.ts +1 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-import-boundary-violations.ts +415 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-subpath-import-targets.ts +117 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-what-hides-an-import.ts +65 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/import-boundary-errors.ts +176 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/index.ts +1 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/declared-use-errors.ts +32 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-declared-use-violations.ts +38 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-sync-calls.ts +23 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-type-only-schemas.ts +375 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/index.ts +1 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/find-layer-provides-outside-program-starts.ts +648 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/layer-provide-errors.ts +27 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/resolve-imported-package-apis.ts +262 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/index.ts +1 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/find-ungranted-stdio-uses.ts +219 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/stdio-use-errors.ts +31 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/index.ts +1 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/internal/find-inferrable-annotations.ts +357 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/internal/inferrable-annotation-errors.ts +33 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/index.ts +1 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/internal/find-repeated-service-yields.ts +162 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/internal/repeated-service-yield-errors.ts +25 -0
- package/src/lib/tasks/lib/enforce-file-layout/index.ts +5 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/check-the-layout-of-each-package.ts +9 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/file-layout-errors.ts +417 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-export-path-mismatches.ts +83 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-file-layout-violations.ts +827 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-module-layout-violations.ts +82 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/resolve-task-imports.ts +178 -0
- package/src/lib/tasks/lib/enforce-file-types/index.ts +5 -0
- package/src/lib/tasks/lib/enforce-file-types/internal/file-type-errors.ts +21 -0
- package/src/lib/tasks/lib/enforce-file-types/internal/find-file-type-violations.ts +78 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/index.ts +1 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/check-formatting-with-bun-fmt.ts +157 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/declare-the-formatting-task.ts +66 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-config-violations.ts +16 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-input-violations.ts +74 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-symbolic-links.ts +14 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/unformatted-file-errors.ts +12 -0
- package/src/lib/tasks/lib/judge-index-re-exports/index.ts +1 -0
- package/src/lib/tasks/lib/judge-index-re-exports/internal/find-index-re-export-violations.ts +353 -0
- package/src/lib/tasks/lib/judge-index-re-exports/internal/index-re-export-errors.ts +21 -0
- package/src/lib/tasks/lib/lint-with-oxlint/index.ts +15 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/check-lint-rules-with-oxlint.ts +123 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/check-unsafe-enum-assignments.ts +152 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/find-tsgolint-executable.ts +21 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/read-oxlint-json-report.ts +82 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/read-tsgolint-rule-messages.ts +68 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/index.ts +1 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/export-placement-errors.ts +135 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-export-placement-violations.ts +507 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-misplaced-files.ts +116 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-subdivided-directories.ts +29 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-the-importer-graph.ts +451 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/list-binding-names.ts +49 -0
- package/src/lib/tasks/lib/refuse-disable-directives/index.ts +15 -0
- package/src/lib/tasks/lib/refuse-disable-directives/internal/disable-directive-errors.ts +45 -0
- package/src/lib/tasks/lib/refuse-disable-directives/internal/find-disable-directives.ts +66 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/index.ts +1 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/error-mapping-errors.ts +21 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/find-error-mappings-to-done.ts +135 -0
- package/src/lib/tasks/lib/require-separate-service-layers/index.ts +1 -0
- package/src/lib/tasks/lib/require-separate-service-layers/internal/find-service-layer-violations.ts +170 -0
- package/src/lib/tasks/lib/require-separate-service-layers/internal/service-layer-errors.ts +21 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/index.ts +14 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/find-linted-files-not-type-checked.ts +117 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/linted-file-errors.ts +51 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/index.ts +11 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/check-types-with-bun-check.ts +54 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/read-bun-check-output.ts +73 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/type-check-errors.ts +10 -0
- package/src/lib/tasks/shared/check-each-nested-package.ts +79 -0
- package/src/lib/tasks/shared/classify-files-by-role.ts +115 -0
- package/src/lib/tasks/shared/count-the-line-of-an-offset.ts +2 -0
- package/src/lib/tasks/shared/create-the-module-resolver.ts +38 -0
- package/src/lib/tasks/shared/declare-lint-config-task.ts +13 -0
- package/src/lib/tasks/shared/declare-typescript-config-task.ts +18 -0
- package/src/lib/tasks/shared/encode-compact-json.ts +5 -0
- package/src/lib/tasks/shared/find-installed-package-binaries.ts +62 -0
- package/src/lib/tasks/shared/find-module-requests.ts +482 -0
- package/src/lib/tasks/shared/find-named-package-manifests.ts +94 -0
- package/src/lib/tasks/shared/find-unmet-typescript-config-requirements.ts +60 -0
- package/src/lib/tasks/shared/list-ignored-files-imported-by-source.ts +79 -0
- package/src/lib/tasks/shared/list-linted-files.ts +28 -0
- package/src/lib/tasks/shared/list-owned-files.ts +13 -0
- package/src/lib/tasks/shared/list-package-files.ts +66 -0
- package/src/lib/tasks/shared/load-the-lint-config.ts +176 -0
- package/src/lib/tasks/shared/match-granted-patch-files.ts +88 -0
- package/src/lib/tasks/shared/parse-config-files-that-import-from-check.ts +310 -0
- package/src/lib/tasks/shared/print-failures-and-keep-detail-in-the-trace.ts +99 -0
- package/src/lib/tasks/shared/project-config-and-tool-errors.ts +485 -0
- package/src/lib/tasks/shared/query-the-typescript-project.ts +635 -0
- package/src/lib/tasks/shared/read-named-package-manifests.ts +53 -0
- package/src/lib/tasks/shared/read-package-manifest-object.ts +28 -0
- package/src/lib/tasks/shared/resolve-absolute-paths.ts +4 -0
- package/src/lib/tasks/shared/resolve-module-requests.ts +121 -0
- package/src/lib/tasks/shared/run-tools-and-decode-their-reports.ts +98 -0
- package/src/lib/tasks/shared/tool-file-locations.ts +38 -0
- package/src/shared/bans-each-permission-lifts.ts +454 -0
- package/src/shared/config-file-names.ts +3 -0
- package/src/shared/find-retired-words.ts +17 -0
- package/src/shared/format-reported-failures.ts +31 -0
- package/src/shared/lint-rule-messages-and-patterns.ts +104 -0
- package/src/shared/match-file-names-and-path-prefixes.ts +18 -0
- package/src/shared/permission-grant-errors.ts +42 -0
- package/src/shared/record-and-grant-permissions-by-path.ts +419 -0
- package/src/shared/test-home-directories.ts +4 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
importedExpression,
|
|
6
|
+
isGlobalReference,
|
|
7
|
+
staticKeyName,
|
|
8
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
9
|
+
|
|
10
|
+
const FAILING_TYPES = new Set(["Effect.Effect", "Stream.Stream", "Layer.Layer"]);
|
|
11
|
+
|
|
12
|
+
const FAILURE_TYPE_ARGUMENT = 1;
|
|
13
|
+
|
|
14
|
+
const GLOBAL_ERROR = "Error";
|
|
15
|
+
|
|
16
|
+
const GLOBAL_OBJECT = "globalThis";
|
|
17
|
+
|
|
18
|
+
const UNNAMED = "this type";
|
|
19
|
+
|
|
20
|
+
const typeNameParts = (name: ESTree.Node): readonly string[] => {
|
|
21
|
+
if (name.type === "TSQualifiedName") {
|
|
22
|
+
return [...typeNameParts(name.left), name.right.name];
|
|
23
|
+
}
|
|
24
|
+
return name.type === "Identifier" ? [name.name] : [];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const typeNameRoot = (name: ESTree.Node): ESTree.Node =>
|
|
28
|
+
name.type === "TSQualifiedName" ? typeNameRoot(name.left) : name;
|
|
29
|
+
|
|
30
|
+
const effectModulePath = (module: string) => {
|
|
31
|
+
if (module === "effect") {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
return module.startsWith("effect/") ? [module.slice("effect/".length)] : undefined;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const effectTypePath = (reference: ESTree.TSTypeReference, sourceCode: SourceCode) => {
|
|
38
|
+
const imported = importedExpression(typeNameRoot(reference.typeName), sourceCode);
|
|
39
|
+
const modulePath = imported && effectModulePath(imported.module);
|
|
40
|
+
const isOutsideEffect = imported === undefined || modulePath === undefined;
|
|
41
|
+
if (isOutsideEffect) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
return [...modulePath, ...imported.members, ...typeNameParts(reference.typeName).slice(1)].join(
|
|
45
|
+
".",
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const isGlobalError = (type: ESTree.Node, sourceCode: SourceCode) => {
|
|
50
|
+
if (type.type !== "TSTypeReference") {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const { typeName } = type;
|
|
54
|
+
if (typeName.type === "TSQualifiedName") {
|
|
55
|
+
return (
|
|
56
|
+
typeName.right.name === GLOBAL_ERROR &&
|
|
57
|
+
isGlobalReference(typeName.left, sourceCode, GLOBAL_OBJECT)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return isGlobalReference(typeName, sourceCode, GLOBAL_ERROR);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const failureMembers = (type: ESTree.Node): readonly ESTree.Node[] => {
|
|
64
|
+
if (type.type === "TSParenthesizedType") {
|
|
65
|
+
return failureMembers(type.typeAnnotation);
|
|
66
|
+
}
|
|
67
|
+
return type.type === "TSUnionType" ? type.types.flatMap(failureMembers) : [type];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const keyName = (key: ESTree.Node, computed: boolean) => {
|
|
71
|
+
const isUncomputedIdentifier = !computed && key.type === "Identifier";
|
|
72
|
+
return isUncomputedIdentifier ? key.name : staticKeyName(key);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const declaredName = (node: ESTree.Node): string | undefined => {
|
|
76
|
+
const isAnnotatedBinding =
|
|
77
|
+
node.type === "Identifier" && node.typeAnnotation !== undefined && node.typeAnnotation !== null;
|
|
78
|
+
if (isAnnotatedBinding) {
|
|
79
|
+
return node.name;
|
|
80
|
+
}
|
|
81
|
+
if (node.type === "TSTypeAliasDeclaration") {
|
|
82
|
+
return node.id.name;
|
|
83
|
+
}
|
|
84
|
+
if (node.type === "VariableDeclarator") {
|
|
85
|
+
return node.id.type === "Identifier" ? node.id.name : undefined;
|
|
86
|
+
}
|
|
87
|
+
const isFunction =
|
|
88
|
+
node.type === "FunctionDeclaration" ||
|
|
89
|
+
node.type === "FunctionExpression" ||
|
|
90
|
+
node.type === "TSDeclareFunction";
|
|
91
|
+
if (isFunction) {
|
|
92
|
+
return node.id?.name;
|
|
93
|
+
}
|
|
94
|
+
const isKeyedMember =
|
|
95
|
+
node.type === "TSPropertySignature" ||
|
|
96
|
+
node.type === "TSMethodSignature" ||
|
|
97
|
+
node.type === "PropertyDefinition" ||
|
|
98
|
+
node.type === "MethodDefinition" ||
|
|
99
|
+
node.type === "Property";
|
|
100
|
+
return isKeyedMember ? keyName(node.key, node.computed) : undefined;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const enclosingName = (node: ESTree.Node): string => {
|
|
104
|
+
const name = declaredName(node);
|
|
105
|
+
if (name !== undefined) {
|
|
106
|
+
return name;
|
|
107
|
+
}
|
|
108
|
+
const isStatement = node.type.endsWith("Statement") || node.type === "Program";
|
|
109
|
+
const hasEnclosingNode = node.parent !== null && !isStatement;
|
|
110
|
+
return hasEnclosingNode ? enclosingName(node.parent) : UNNAMED;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const noPlainErrorFailures = defineRule({
|
|
114
|
+
meta: {
|
|
115
|
+
type: "problem",
|
|
116
|
+
docs: {
|
|
117
|
+
description:
|
|
118
|
+
"Name each tagged error an Effect, Stream or Layer fails with instead of the global Error.",
|
|
119
|
+
},
|
|
120
|
+
messages: {
|
|
121
|
+
plainError: "{{name}} fails with Error",
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
create(context) {
|
|
125
|
+
return {
|
|
126
|
+
TSTypeReference(reference) {
|
|
127
|
+
const failure = reference.typeArguments?.params[FAILURE_TYPE_ARGUMENT];
|
|
128
|
+
const isFailingType =
|
|
129
|
+
failure !== undefined &&
|
|
130
|
+
FAILING_TYPES.has(effectTypePath(reference, context.sourceCode) ?? "");
|
|
131
|
+
if (!isFailingType) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
for (const member of failureMembers(failure)) {
|
|
135
|
+
if (isGlobalError(member, context.sourceCode)) {
|
|
136
|
+
context.report({
|
|
137
|
+
node: member,
|
|
138
|
+
messageId: "plainError",
|
|
139
|
+
data: { name: enclosingName(reference) },
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
import { defineRule } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
effectOperation,
|
|
6
|
+
enclosingFunction,
|
|
7
|
+
resolvedVariable,
|
|
8
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
9
|
+
|
|
10
|
+
const innermostMemberObject = (node: ESTree.Node): ESTree.Node =>
|
|
11
|
+
node.type === "MemberExpression" ? innermostMemberObject(node.object) : node;
|
|
12
|
+
|
|
13
|
+
export const noErrorInstanceof = defineRule({
|
|
14
|
+
meta: {
|
|
15
|
+
type: "problem",
|
|
16
|
+
docs: { description: "Use tagged failures in Effect recovery callbacks." },
|
|
17
|
+
messages: {
|
|
18
|
+
tagged: "instanceof tests a value reached from the parameter of Effect.{{operation}}",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
create(context) {
|
|
22
|
+
return {
|
|
23
|
+
BinaryExpression(node) {
|
|
24
|
+
if (node.operator !== "instanceof") {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const callback = enclosingFunction(node);
|
|
28
|
+
if (!callback) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const call = callback.parent;
|
|
32
|
+
if (call.type !== "CallExpression") {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const operation = effectOperation(call.callee, context.sourceCode);
|
|
36
|
+
if (operation === undefined) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const isRecoveryOperation = operation.startsWith("catch") || operation === "mapError";
|
|
40
|
+
if (!isRecoveryOperation) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const subject = innermostMemberObject(node.left);
|
|
44
|
+
if (subject.type !== "Identifier") {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const subjectName = subject.name;
|
|
48
|
+
const parameter = callback.params.find(
|
|
49
|
+
(candidate) => candidate.type === "Identifier" && candidate.name === subjectName,
|
|
50
|
+
);
|
|
51
|
+
if (parameter?.type !== "Identifier") {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const parameterVariable = context.sourceCode
|
|
55
|
+
.getDeclaredVariables(callback)
|
|
56
|
+
.find((variable) => variable.identifiers.includes(parameter));
|
|
57
|
+
if (resolvedVariable(subject, context.sourceCode) === parameterVariable) {
|
|
58
|
+
context.report({ node, messageId: "tagged", data: { operation } });
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import { effectModuleReference } from "./resolve-what-an-expression-imports.ts";
|
|
5
|
+
|
|
6
|
+
const bindingNamesOf = (pattern: ESTree.Node): readonly string[] => {
|
|
7
|
+
if (pattern.type === "Identifier") {
|
|
8
|
+
return [pattern.name];
|
|
9
|
+
}
|
|
10
|
+
if (pattern.type === "ObjectPattern") {
|
|
11
|
+
return pattern.properties.flatMap((property) =>
|
|
12
|
+
bindingNamesOf(property.type === "RestElement" ? property.argument : property.value),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
if (pattern.type === "ArrayPattern") {
|
|
16
|
+
return pattern.elements.flatMap((element) => (element === null ? [] : bindingNamesOf(element)));
|
|
17
|
+
}
|
|
18
|
+
if (pattern.type === "RestElement") {
|
|
19
|
+
return bindingNamesOf(pattern.argument);
|
|
20
|
+
}
|
|
21
|
+
return pattern.type === "AssignmentPattern" ? bindingNamesOf(pattern.left) : [];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const noLet = defineRule({
|
|
25
|
+
meta: {
|
|
26
|
+
type: "suggestion",
|
|
27
|
+
docs: {
|
|
28
|
+
description:
|
|
29
|
+
"Declare every variable with const, including for...of and for...in loop variables, so no name is reassigned. A let that starts a for statement is left to no-while, which reports the whole loop.",
|
|
30
|
+
},
|
|
31
|
+
messages: {
|
|
32
|
+
reassignable: "let declares {{names}}, which can be reassigned",
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
create(context) {
|
|
36
|
+
return {
|
|
37
|
+
VariableDeclaration(node) {
|
|
38
|
+
const isLetNotStartingAForStatement =
|
|
39
|
+
node.kind === "let" && node.parent.type !== "ForStatement";
|
|
40
|
+
if (isLetNotStartingAForStatement) {
|
|
41
|
+
context.report({
|
|
42
|
+
node,
|
|
43
|
+
messageId: "reassignable",
|
|
44
|
+
data: {
|
|
45
|
+
names: node.declarations
|
|
46
|
+
.flatMap((declarator) => bindingNamesOf(declarator.id))
|
|
47
|
+
.join(", "),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export const noMutableRef = defineRule({
|
|
57
|
+
meta: {
|
|
58
|
+
type: "suggestion",
|
|
59
|
+
docs: {
|
|
60
|
+
description:
|
|
61
|
+
"Keep shared state in an Effect Ref instead of MutableRef, the synchronous box that would stand in for a banned let.",
|
|
62
|
+
},
|
|
63
|
+
messages: {
|
|
64
|
+
boxedVariable: "{{usage}} keeps a reassignable value in a synchronous box",
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
create(context) {
|
|
68
|
+
const reportMutableRef = (node: ESTree.Node) => {
|
|
69
|
+
const usage = effectModuleReference(node, context.sourceCode, "MutableRef");
|
|
70
|
+
if (usage !== undefined) {
|
|
71
|
+
context.report({ node, messageId: "boxedVariable", data: { usage } });
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
Identifier: reportMutableRef,
|
|
76
|
+
MemberExpression: reportMutableRef,
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import { fileNameOf, isEntryFileName } from "../shared/match-file-names-and-path-prefixes.ts";
|
|
5
|
+
import { resolvedVariable } from "./resolve-what-an-expression-imports.ts";
|
|
6
|
+
|
|
7
|
+
const WORD_START = /(?<!^)(?=[A-Z][a-z])/gu;
|
|
8
|
+
const ACRONYM = /^[A-Z]+$/u;
|
|
9
|
+
const VOWEL_SOUND_START = /^(?:[aeio]|up)/u;
|
|
10
|
+
const UNDEFINED = "undefined";
|
|
11
|
+
const EXPORTABLE_DECLARATIONS: ReadonlySet<string> = new Set([
|
|
12
|
+
"TSTypeAliasDeclaration",
|
|
13
|
+
"TSInterfaceDeclaration",
|
|
14
|
+
"TSImportEqualsDeclaration",
|
|
15
|
+
]);
|
|
16
|
+
const WIRING_STATEMENTS: ReadonlySet<string> = new Set([
|
|
17
|
+
"ImportDeclaration",
|
|
18
|
+
"TSImportEqualsDeclaration",
|
|
19
|
+
"ExportAllDeclaration",
|
|
20
|
+
"TSNamespaceExportDeclaration",
|
|
21
|
+
]);
|
|
22
|
+
const CONSTANT = "const";
|
|
23
|
+
const METHOD_KINDS = { get: "a getter", set: "a setter" } as const;
|
|
24
|
+
|
|
25
|
+
const kindOf = (node: ESTree.Node) => {
|
|
26
|
+
if (node.type === "TSModuleDeclaration") {
|
|
27
|
+
return `a ${node.kind} declaration`;
|
|
28
|
+
}
|
|
29
|
+
const words = node.type
|
|
30
|
+
.replace(WORD_START, " ")
|
|
31
|
+
.split(" ")
|
|
32
|
+
.map((word) => (ACRONYM.test(word) ? word : word.toLowerCase()))
|
|
33
|
+
.join(" ");
|
|
34
|
+
return `${VOWEL_SOUND_START.test(words) ? "an" : "a"} ${words}`;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const isNegativeNumber = (node: ESTree.Node) =>
|
|
38
|
+
node.type === "UnaryExpression" &&
|
|
39
|
+
node.operator === "-" &&
|
|
40
|
+
node.argument.type === "Literal" &&
|
|
41
|
+
(typeof node.argument.value === "number" || typeof node.argument.value === "bigint");
|
|
42
|
+
|
|
43
|
+
const partsOf = (node: ESTree.Node) => {
|
|
44
|
+
const isTypeScriptWrapper =
|
|
45
|
+
node.type === "TSAsExpression" ||
|
|
46
|
+
node.type === "TSSatisfiesExpression" ||
|
|
47
|
+
node.type === "TSTypeAssertion" ||
|
|
48
|
+
node.type === "TSNonNullExpression" ||
|
|
49
|
+
node.type === "TSInstantiationExpression";
|
|
50
|
+
if (isTypeScriptWrapper) {
|
|
51
|
+
return [node.expression];
|
|
52
|
+
}
|
|
53
|
+
const isCallOrNew = node.type === "CallExpression" || node.type === "NewExpression";
|
|
54
|
+
if (isCallOrNew) {
|
|
55
|
+
return [node.callee, ...node.arguments];
|
|
56
|
+
}
|
|
57
|
+
if (node.type === "MemberExpression") {
|
|
58
|
+
return node.computed ? [node.object, node.property] : [node.object];
|
|
59
|
+
}
|
|
60
|
+
if (node.type === "Property") {
|
|
61
|
+
return node.computed ? [node.key, node.value] : [node.value];
|
|
62
|
+
}
|
|
63
|
+
if (node.type === "TemplateLiteral") {
|
|
64
|
+
return node.expressions;
|
|
65
|
+
}
|
|
66
|
+
if (node.type === "ArrayExpression") {
|
|
67
|
+
return node.elements;
|
|
68
|
+
}
|
|
69
|
+
if (node.type === "ObjectExpression") {
|
|
70
|
+
return node.properties;
|
|
71
|
+
}
|
|
72
|
+
return node.type === "SpreadElement" ? [node.argument] : undefined;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const noIndexLogic = defineRule({
|
|
76
|
+
meta: {
|
|
77
|
+
type: "suggestion",
|
|
78
|
+
docs: {
|
|
79
|
+
description: "Keep index.ts and index.tsx to wiring.",
|
|
80
|
+
},
|
|
81
|
+
messages: {
|
|
82
|
+
holds: "index file holds {{kind}}",
|
|
83
|
+
outsideAnExport: "index file holds {{kind}} outside an export statement",
|
|
84
|
+
readsGlobal: "index file reads the global {{name}}",
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
create(context) {
|
|
88
|
+
if (!isEntryFileName(fileNameOf(context.filename))) {
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
const reportHeld = (node: ESTree.Node, kind = kindOf(node)) => {
|
|
92
|
+
context.report({ node, messageId: "holds", data: { kind } });
|
|
93
|
+
};
|
|
94
|
+
const checkExpression = (node: ESTree.Node | null): void => {
|
|
95
|
+
const isLiteralValue = node === null || node.type === "Literal" || isNegativeNumber(node);
|
|
96
|
+
if (isLiteralValue) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (node.type === "Identifier") {
|
|
100
|
+
const variable = resolvedVariable(node, context.sourceCode);
|
|
101
|
+
const isOwnBinding = node.name === UNDEFINED || (variable?.defs.length ?? 0) > 0;
|
|
102
|
+
if (!isOwnBinding) {
|
|
103
|
+
context.report({ node, messageId: "readsGlobal", data: { name: node.name } });
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (node.type === "Property") {
|
|
108
|
+
const isMethodLike = node.method || node.kind !== "init";
|
|
109
|
+
if (isMethodLike) {
|
|
110
|
+
reportHeld(node, node.kind === "init" ? "a method" : METHOD_KINDS[node.kind]);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const parts = partsOf(node);
|
|
115
|
+
if (parts === undefined) {
|
|
116
|
+
reportHeld(node);
|
|
117
|
+
} else {
|
|
118
|
+
for (const part of parts) {
|
|
119
|
+
checkExpression(part);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const checkDeclaration = (node: ESTree.Node) => {
|
|
124
|
+
if (node.type !== "VariableDeclaration") {
|
|
125
|
+
if (!EXPORTABLE_DECLARATIONS.has(node.type)) {
|
|
126
|
+
reportHeld(node);
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (node.kind !== CONSTANT) {
|
|
131
|
+
reportHeld(node, `a ${node.kind} declaration`);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
for (const declarator of node.declarations) {
|
|
135
|
+
if (declarator.id.type === "Identifier") {
|
|
136
|
+
checkExpression(declarator.init);
|
|
137
|
+
} else {
|
|
138
|
+
reportHeld(declarator.id);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const checkStatement = (node: ESTree.Directive | ESTree.Statement) => {
|
|
143
|
+
if (WIRING_STATEMENTS.has(node.type)) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const isDeclaration = node.type.endsWith("Declaration") || node.type === "TSDeclareFunction";
|
|
147
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
148
|
+
if (node.declaration !== null) {
|
|
149
|
+
checkDeclaration(node.declaration);
|
|
150
|
+
}
|
|
151
|
+
} else if (node.type === "ExportDefaultDeclaration") {
|
|
152
|
+
const exported = node.declaration;
|
|
153
|
+
const isFunctionOrClass =
|
|
154
|
+
exported.type === "FunctionDeclaration" || exported.type === "ClassDeclaration";
|
|
155
|
+
if (isFunctionOrClass) {
|
|
156
|
+
reportHeld(exported);
|
|
157
|
+
} else if (exported.type !== "TSInterfaceDeclaration") {
|
|
158
|
+
checkExpression(exported);
|
|
159
|
+
}
|
|
160
|
+
} else if (node.type === "ExpressionStatement") {
|
|
161
|
+
checkExpression(node.expression);
|
|
162
|
+
} else if (node.type === "TSExportAssignment") {
|
|
163
|
+
checkExpression(node.expression);
|
|
164
|
+
} else if (isDeclaration) {
|
|
165
|
+
context.report({ node, messageId: "outsideAnExport", data: { kind: kindOf(node) } });
|
|
166
|
+
} else {
|
|
167
|
+
reportHeld(node);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
return {
|
|
171
|
+
Program(program) {
|
|
172
|
+
for (const statement of program.body) {
|
|
173
|
+
checkStatement(statement);
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
everyBanOf,
|
|
6
|
+
MODULE_PATTERN_WILDCARD,
|
|
7
|
+
moduleSpecifiersOfGlobalObject,
|
|
8
|
+
PERMISSION_BANS,
|
|
9
|
+
} from "../shared/bans-each-permission-lifts.ts";
|
|
10
|
+
import { bansByKind, modulePatternMatcher } from "./match-banned-modules-and-globals.ts";
|
|
11
|
+
import {
|
|
12
|
+
importedExpression,
|
|
13
|
+
isGlobalReference,
|
|
14
|
+
isImportBinding,
|
|
15
|
+
isReferencePosition,
|
|
16
|
+
isShorthandKey,
|
|
17
|
+
memberName,
|
|
18
|
+
unwrappedExpression,
|
|
19
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
20
|
+
|
|
21
|
+
const EFFECT_PACKAGE = /^(?:effect|@effect\/[^/]+)(?:\/|$)/u;
|
|
22
|
+
const MODULE_FILE = /\/[A-Z]\w*$/u;
|
|
23
|
+
const MODULE_NAME = /^[A-Z]/u;
|
|
24
|
+
const LINE_BREAKS = /\s*(?:\r\n|[\n\r])\s*/gu;
|
|
25
|
+
|
|
26
|
+
const isEffectModule = (imported: NonNullable<ReturnType<typeof importedExpression>>) => {
|
|
27
|
+
if (!EFFECT_PACKAGE.test(imported.module)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (MODULE_FILE.test(imported.module)) {
|
|
31
|
+
return imported.members.length === 0;
|
|
32
|
+
}
|
|
33
|
+
const [first, ...rest] = imported.members;
|
|
34
|
+
return first === undefined || (rest.length === 0 && MODULE_NAME.test(first));
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const NAMESPACES_OF_IMPORT_BANS = Object.values(PERMISSION_BANS)
|
|
38
|
+
.flatMap((entry) => bansByKind(everyBanOf(entry)).imports)
|
|
39
|
+
.filter(({ modulePattern }) => modulePattern !== MODULE_PATTERN_WILDCARD)
|
|
40
|
+
.map(({ modulePattern, importNames }) => ({
|
|
41
|
+
matchesModule: modulePatternMatcher(modulePattern),
|
|
42
|
+
importPaths: importNames.map((name) => name.split(".")),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
const isNamespaceOfABannedImport = (imported: NonNullable<ReturnType<typeof importedExpression>>) =>
|
|
46
|
+
NAMESPACES_OF_IMPORT_BANS.some(
|
|
47
|
+
({ matchesModule, importPaths }) =>
|
|
48
|
+
matchesModule(imported.module) &&
|
|
49
|
+
importPaths.some(
|
|
50
|
+
(path) =>
|
|
51
|
+
path.length > imported.members.length &&
|
|
52
|
+
imported.members.every((member, index) => path[index] === member),
|
|
53
|
+
),
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const namesAnImportDirectly = (node: ESTree.Node, sourceCode: SourceCode): boolean => {
|
|
57
|
+
const expression = unwrappedExpression(node);
|
|
58
|
+
return expression.type === "MemberExpression"
|
|
59
|
+
? namesAnImportDirectly(expression.object, sourceCode)
|
|
60
|
+
: isImportBinding(expression, sourceCode);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const typeScriptWrapperOf = (node: ESTree.Node) => {
|
|
64
|
+
const { parent } = node;
|
|
65
|
+
const isTypeScriptWrapper =
|
|
66
|
+
parent?.type === "TSAsExpression" ||
|
|
67
|
+
parent?.type === "TSSatisfiesExpression" ||
|
|
68
|
+
parent?.type === "TSTypeAssertion" ||
|
|
69
|
+
parent?.type === "TSNonNullExpression";
|
|
70
|
+
const wrapsTheNode = isTypeScriptWrapper && parent.expression === node;
|
|
71
|
+
return wrapsTheNode ? parent : undefined;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const OBJECT_KEYS = "keys";
|
|
75
|
+
|
|
76
|
+
const isAllowedNamespaceUse = (node: ESTree.Node, sourceCode: SourceCode) => {
|
|
77
|
+
const { parent } = node;
|
|
78
|
+
if (parent === null) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const isObjectOfAMember = parent.type === "MemberExpression" && parent.object === node;
|
|
82
|
+
if (isObjectOfAMember) {
|
|
83
|
+
return memberName(parent) !== undefined;
|
|
84
|
+
}
|
|
85
|
+
const isCalledOrConstructed =
|
|
86
|
+
(parent.type === "CallExpression" || parent.type === "NewExpression") && parent.callee === node;
|
|
87
|
+
const isTypeOfOperand = parent.type === "UnaryExpression" && parent.operator === "typeof";
|
|
88
|
+
const isListedByObjectKeys =
|
|
89
|
+
parent.type === "CallExpression" &&
|
|
90
|
+
parent.arguments.length === 1 &&
|
|
91
|
+
parent.arguments[0] === node &&
|
|
92
|
+
parent.callee.type === "MemberExpression" &&
|
|
93
|
+
memberName(parent.callee) === OBJECT_KEYS &&
|
|
94
|
+
isGlobalReference(parent.callee.object, sourceCode, "Object");
|
|
95
|
+
const isInstanceOfTarget =
|
|
96
|
+
parent.type === "BinaryExpression" && parent.operator === "instanceof" && parent.right === node;
|
|
97
|
+
return isCalledOrConstructed || isTypeOfOperand || isInstanceOfTarget || isListedByObjectKeys;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const noNamespaceAliases = (globalObjects: ReadonlySet<string>) => {
|
|
101
|
+
const isGlobalObject = (node: ESTree.Node, sourceCode: SourceCode) => {
|
|
102
|
+
const isGlobalObjectReference =
|
|
103
|
+
node.type === "Identifier" &&
|
|
104
|
+
globalObjects.has(node.name) &&
|
|
105
|
+
isGlobalReference(node, sourceCode, node.name);
|
|
106
|
+
if (isGlobalObjectReference) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
const imported = namesAnImportDirectly(node, sourceCode)
|
|
110
|
+
? importedExpression(node, sourceCode)
|
|
111
|
+
: undefined;
|
|
112
|
+
if (imported?.members.length !== 0) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return [...globalObjects].some((globalObject) =>
|
|
116
|
+
moduleSpecifiersOfGlobalObject(globalObject).includes(imported.module),
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
return defineRule({
|
|
120
|
+
meta: {
|
|
121
|
+
type: "problem",
|
|
122
|
+
docs: {
|
|
123
|
+
description: `Use an Effect module, a module or namespace a permission ban names members of, or one of the globals ${[...globalObjects].join(", ")}, only through a named member, so no alias, destructuring or spread hides a banned member from the rules that ban it.`,
|
|
124
|
+
},
|
|
125
|
+
messages: {
|
|
126
|
+
wholeNamespace:
|
|
127
|
+
"{{name}} is used without naming a member, so the bans on its members can't see which one runs",
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
create(context) {
|
|
131
|
+
const { sourceCode } = context;
|
|
132
|
+
const isNamespace = (node: ESTree.Node) => {
|
|
133
|
+
if (isGlobalObject(node, sourceCode)) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
const imported = namesAnImportDirectly(node, sourceCode)
|
|
137
|
+
? importedExpression(node, sourceCode)
|
|
138
|
+
: undefined;
|
|
139
|
+
return (
|
|
140
|
+
imported !== undefined &&
|
|
141
|
+
(isEffectModule(imported) || isNamespaceOfABannedImport(imported))
|
|
142
|
+
);
|
|
143
|
+
};
|
|
144
|
+
const report = (node: ESTree.Node) => {
|
|
145
|
+
const isNamespaceUsedWhole = isNamespace(node) && !isAllowedNamespaceUse(node, sourceCode);
|
|
146
|
+
if (isNamespaceUsedWhole) {
|
|
147
|
+
context.report({
|
|
148
|
+
node,
|
|
149
|
+
messageId: "wholeNamespace",
|
|
150
|
+
data: { name: sourceCode.getText(node).replace(LINE_BREAKS, " ") },
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
Identifier(node) {
|
|
156
|
+
const isObjectOfAMember =
|
|
157
|
+
node.parent.type === "MemberExpression" && node.parent.object === node;
|
|
158
|
+
const isReference =
|
|
159
|
+
isObjectOfAMember ||
|
|
160
|
+
typeScriptWrapperOf(node) !== undefined ||
|
|
161
|
+
(isReferencePosition(node) && !isShorthandKey(node));
|
|
162
|
+
if (isReference) {
|
|
163
|
+
report(node);
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
MemberExpression(node) {
|
|
167
|
+
const receiver = unwrappedExpression(node.object);
|
|
168
|
+
const isWrappedNamespace = receiver !== node.object && isNamespace(receiver);
|
|
169
|
+
if (!isWrappedNamespace) {
|
|
170
|
+
report(node);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
NODE_BUILTIN_RULES,
|
|
6
|
+
nodeBuiltinNameOf,
|
|
7
|
+
} from "../shared/lint-rule-messages-and-patterns.ts";
|
|
8
|
+
import { importsOnlyTypes, staticKeyName } from "./resolve-what-an-expression-imports.ts";
|
|
9
|
+
|
|
10
|
+
export const nodeBuiltinRule = (ruleName: keyof typeof NODE_BUILTIN_RULES) => {
|
|
11
|
+
const bannedModules: ReadonlySet<string> = new Set(NODE_BUILTIN_RULES[ruleName].modules);
|
|
12
|
+
return defineRule({
|
|
13
|
+
meta: {
|
|
14
|
+
type: "problem",
|
|
15
|
+
docs: {
|
|
16
|
+
description: `Import ${[...bannedModules].join(", ")} from Node only through its Effect replacement, a literal import() included; a re-export is left to no-re-exports and an import {} to typescript(no-import-type-side-effects).`,
|
|
17
|
+
},
|
|
18
|
+
messages: { builtin: "importing {{source}} is banned" },
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
const check = (node: ESTree.Node, source: string) => {
|
|
22
|
+
if (bannedModules.has(nodeBuiltinNameOf(source))) {
|
|
23
|
+
context.report({ node, messageId: "builtin", data: { source } });
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
ImportDeclaration(node) {
|
|
29
|
+
if (!importsOnlyTypes(node, context.sourceCode)) {
|
|
30
|
+
check(node.source, node.source.value);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
ImportExpression(node) {
|
|
34
|
+
const source = staticKeyName(node.source);
|
|
35
|
+
if (source !== undefined) {
|
|
36
|
+
check(node.source, source);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|