@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,299 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
effectModuleOperation,
|
|
6
|
+
effectModuleReference,
|
|
7
|
+
effectOperation,
|
|
8
|
+
importedExpression,
|
|
9
|
+
isGlobalReference,
|
|
10
|
+
isMemberExpressionObject,
|
|
11
|
+
isReferencePosition,
|
|
12
|
+
isShorthandKey,
|
|
13
|
+
memberName,
|
|
14
|
+
propertyKeyName,
|
|
15
|
+
resolvedVariable,
|
|
16
|
+
staticKeyName,
|
|
17
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
18
|
+
|
|
19
|
+
export const NUMBER_PARSERS = new Set(["parseInt", "parseFloat"]);
|
|
20
|
+
const RADIX_PARSER = "parseInt";
|
|
21
|
+
const SCHEMA_DECODED_RADIX = 10;
|
|
22
|
+
const SMALLEST_RADIX = 2;
|
|
23
|
+
const LARGEST_RADIX = 36;
|
|
24
|
+
const UNCHECKED_SCHEMAS = new Set(["Schema.Any"]);
|
|
25
|
+
const JSON_BODY_READ = "json";
|
|
26
|
+
const PROMISE_OPERATIONS = new Set(["promise", "tryPromise"]);
|
|
27
|
+
const PROMISE_THUNK_KEY = "try";
|
|
28
|
+
const PIPE = "pipe";
|
|
29
|
+
const FLAT_MAP_OPERATION = "flatMap";
|
|
30
|
+
const SCHEMA_DECODERS = new Set([
|
|
31
|
+
"decodeEffect",
|
|
32
|
+
"decodeExit",
|
|
33
|
+
"decodeOption",
|
|
34
|
+
"decodePromise",
|
|
35
|
+
"decodeResult",
|
|
36
|
+
"decodeSync",
|
|
37
|
+
"decodeUnknownEffect",
|
|
38
|
+
"decodeUnknownExit",
|
|
39
|
+
"decodeUnknownOption",
|
|
40
|
+
"decodeUnknownPromise",
|
|
41
|
+
"decodeUnknownResult",
|
|
42
|
+
"decodeUnknownSync",
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
const localDefinition = (node: ESTree.Node, sourceCode: SourceCode) => {
|
|
46
|
+
const variable = resolvedVariable(node, sourceCode);
|
|
47
|
+
if (variable?.references.some((reference) => reference.isWrite() && !reference.init) === true) {
|
|
48
|
+
return node;
|
|
49
|
+
}
|
|
50
|
+
const definition = variable?.defs[0]?.node;
|
|
51
|
+
return definition?.type === "VariableDeclarator"
|
|
52
|
+
? (definition.init ?? node)
|
|
53
|
+
: (definition ?? node);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const hasLocalJsonMethod = (receiver: ESTree.Node, sourceCode: SourceCode) => {
|
|
57
|
+
const value = localDefinition(receiver, sourceCode);
|
|
58
|
+
if (value.type === "ObjectExpression") {
|
|
59
|
+
const property = value.properties.findLast(
|
|
60
|
+
(entry) => entry.type === "SpreadElement" || propertyKeyName(entry) === JSON_BODY_READ,
|
|
61
|
+
);
|
|
62
|
+
return (
|
|
63
|
+
property?.type === "Property" &&
|
|
64
|
+
property.kind === "init" &&
|
|
65
|
+
(property.value.type === "FunctionExpression" ||
|
|
66
|
+
property.value.type === "ArrowFunctionExpression")
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (value.type !== "NewExpression") {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const constructor = localDefinition(value.callee, sourceCode);
|
|
73
|
+
const isLocalClass =
|
|
74
|
+
constructor.type === "ClassDeclaration" || constructor.type === "ClassExpression";
|
|
75
|
+
return (
|
|
76
|
+
isLocalClass &&
|
|
77
|
+
constructor.body.body.some((member) => {
|
|
78
|
+
if (member.type !== "MethodDefinition") {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const { computed, key } = member;
|
|
82
|
+
const isNamedMethod = !computed && key.type === "Identifier";
|
|
83
|
+
const name = isNamedMethod ? key.name : staticKeyName(key);
|
|
84
|
+
return !member.static && member.kind === "method" && name === JSON_BODY_READ;
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const numberLiteral = (node: ESTree.Node) => {
|
|
90
|
+
const isTypeOnlyWrapper = node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression";
|
|
91
|
+
const literal = isTypeOnlyWrapper ? node.expression : node;
|
|
92
|
+
const isNumberLiteral = literal.type === "Literal" && typeof literal.value === "number";
|
|
93
|
+
return isNumberLiteral ? literal.value : undefined;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const constantNumber = (node: ESTree.Node, sourceCode: SourceCode) => {
|
|
97
|
+
const literal = numberLiteral(node);
|
|
98
|
+
if (literal !== undefined) {
|
|
99
|
+
return literal;
|
|
100
|
+
}
|
|
101
|
+
const definition = resolvedVariable(node, sourceCode)?.defs[0]?.node;
|
|
102
|
+
const isConstant =
|
|
103
|
+
definition?.type === "VariableDeclarator" &&
|
|
104
|
+
definition.parent.type === "VariableDeclaration" &&
|
|
105
|
+
definition.parent.kind === "const";
|
|
106
|
+
const value = isConstant ? definition.init : null;
|
|
107
|
+
return value === null ? undefined : numberLiteral(value);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const parsesARadixNoSchemaDecodes = (parser: ESTree.Node, name: string, sourceCode: SourceCode) => {
|
|
111
|
+
const call = parser.parent;
|
|
112
|
+
const isCalledParseInt =
|
|
113
|
+
name === RADIX_PARSER && call?.type === "CallExpression" && call.callee === parser;
|
|
114
|
+
if (!isCalledParseInt) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const radixArgument = call.arguments[1];
|
|
118
|
+
const radix = radixArgument === undefined ? undefined : constantNumber(radixArgument, sourceCode);
|
|
119
|
+
const isRadixOtherThanDecimal =
|
|
120
|
+
radix !== undefined &&
|
|
121
|
+
Number.isInteger(radix) &&
|
|
122
|
+
radix >= SMALLEST_RADIX &&
|
|
123
|
+
radix <= LARGEST_RADIX &&
|
|
124
|
+
radix !== SCHEMA_DECODED_RADIX;
|
|
125
|
+
return isRadixOtherThanDecimal;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const isSchemaDecoder = (node: ESTree.Node, sourceCode: SourceCode) =>
|
|
129
|
+
node.type === "CallExpression" &&
|
|
130
|
+
SCHEMA_DECODERS.has(effectModuleOperation(node.callee, sourceCode, "Schema") ?? "");
|
|
131
|
+
|
|
132
|
+
const promiseCallRunning = (thunk: ESTree.ArrowFunctionExpression, sourceCode: SourceCode) => {
|
|
133
|
+
const holder = thunk.parent;
|
|
134
|
+
const isTryProperty =
|
|
135
|
+
holder.type === "Property" &&
|
|
136
|
+
holder.value === thunk &&
|
|
137
|
+
propertyKeyName(holder) === PROMISE_THUNK_KEY;
|
|
138
|
+
const argument = isTryProperty ? holder.parent : thunk;
|
|
139
|
+
const call = argument.parent;
|
|
140
|
+
const isPromiseCall =
|
|
141
|
+
call?.type === "CallExpression" &&
|
|
142
|
+
call.arguments[0] === argument &&
|
|
143
|
+
PROMISE_OPERATIONS.has(effectOperation(call.callee, sourceCode) ?? "");
|
|
144
|
+
return isPromiseCall ? call : undefined;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const awaitedValue = (promise: ESTree.Node) =>
|
|
148
|
+
promise.parent?.type === "AwaitExpression" ? promise.parent : promise;
|
|
149
|
+
|
|
150
|
+
const yieldedValue = (effect: ESTree.Node) => {
|
|
151
|
+
const yielding = effect.parent;
|
|
152
|
+
const isYielded = yielding?.type === "YieldExpression" && yielding.delegate;
|
|
153
|
+
return isYielded ? yielding : undefined;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const isFlatMapInto = (step: ESTree.Node | undefined, sourceCode: SourceCode) =>
|
|
157
|
+
step?.type === "CallExpression" &&
|
|
158
|
+
effectOperation(step.callee, sourceCode) === FLAT_MAP_OPERATION &&
|
|
159
|
+
step.arguments.length === 1 &&
|
|
160
|
+
step.arguments[0] !== undefined &&
|
|
161
|
+
isSchemaDecoder(step.arguments[0], sourceCode);
|
|
162
|
+
|
|
163
|
+
const isPipeFunction = (callee: ESTree.Node, sourceCode: SourceCode) => {
|
|
164
|
+
const imported = importedExpression(callee, sourceCode);
|
|
165
|
+
const isPipeOfEffect = imported?.module === "effect" && imported.members.join(".") === PIPE;
|
|
166
|
+
return isPipeOfEffect || effectModuleOperation(callee, sourceCode, "Function") === PIPE;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const isPipedIntoDecoder = (effect: ESTree.Node, sourceCode: SourceCode) => {
|
|
170
|
+
const parent = effect.parent;
|
|
171
|
+
const isPipeMethod =
|
|
172
|
+
parent?.type === "MemberExpression" && parent.object === effect && memberName(parent) === PIPE;
|
|
173
|
+
if (isPipeMethod) {
|
|
174
|
+
const pipeCall = parent.parent;
|
|
175
|
+
return (
|
|
176
|
+
pipeCall.type === "CallExpression" &&
|
|
177
|
+
pipeCall.callee === parent &&
|
|
178
|
+
isFlatMapInto(pipeCall.arguments[0], sourceCode)
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
if (parent?.type !== "CallExpression") {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
const [first, second, ...rest] = parent.arguments;
|
|
185
|
+
const isFirstOfTwoArguments = first === effect && second !== undefined && rest.length === 0;
|
|
186
|
+
if (!isFirstOfTwoArguments) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
const isDataFirstFlatMap =
|
|
190
|
+
effectOperation(parent.callee, sourceCode) === FLAT_MAP_OPERATION &&
|
|
191
|
+
isSchemaDecoder(second, sourceCode);
|
|
192
|
+
const isPipeFunctionCall =
|
|
193
|
+
isPipeFunction(parent.callee, sourceCode) && isFlatMapInto(second, sourceCode);
|
|
194
|
+
return isDataFirstFlatMap || isPipeFunctionCall;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const isDecodedOrBoundAsUnknown = (value: ESTree.Node, sourceCode: SourceCode) => {
|
|
198
|
+
const parent = value.parent;
|
|
199
|
+
const isDecoded =
|
|
200
|
+
parent?.type === "CallExpression" &&
|
|
201
|
+
parent.arguments[0] === value &&
|
|
202
|
+
isSchemaDecoder(parent.callee, sourceCode);
|
|
203
|
+
const isBoundAsUnknown =
|
|
204
|
+
parent?.type === "VariableDeclarator" &&
|
|
205
|
+
parent.init === value &&
|
|
206
|
+
parent.id.type === "Identifier" &&
|
|
207
|
+
parent.id.typeAnnotation?.typeAnnotation.type === "TSUnknownKeyword";
|
|
208
|
+
return isDecoded || isBoundAsUnknown;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const isDecodedOrBoundAsUnknownRightAway = (
|
|
212
|
+
body: ESTree.CallExpression,
|
|
213
|
+
sourceCode: SourceCode,
|
|
214
|
+
) => {
|
|
215
|
+
const parent = body.parent;
|
|
216
|
+
const isThenMethod =
|
|
217
|
+
parent.type === "MemberExpression" && parent.object === body && memberName(parent) === "then";
|
|
218
|
+
if (isThenMethod) {
|
|
219
|
+
const call = parent.parent;
|
|
220
|
+
const isThenCall = call.type === "CallExpression" && call.callee === parent;
|
|
221
|
+
const decoder = isThenCall ? call.arguments[0] : undefined;
|
|
222
|
+
return decoder !== undefined && isSchemaDecoder(decoder, sourceCode);
|
|
223
|
+
}
|
|
224
|
+
const value = awaitedValue(body);
|
|
225
|
+
const thunk = value.parent;
|
|
226
|
+
const isThunkBody = thunk?.type === "ArrowFunctionExpression" && thunk.body === value;
|
|
227
|
+
const effect = isThunkBody ? promiseCallRunning(thunk, sourceCode) : undefined;
|
|
228
|
+
if (effect === undefined) {
|
|
229
|
+
return value !== body && isDecodedOrBoundAsUnknown(value, sourceCode);
|
|
230
|
+
}
|
|
231
|
+
const yielded = yieldedValue(effect);
|
|
232
|
+
return yielded === undefined
|
|
233
|
+
? isPipedIntoDecoder(effect, sourceCode)
|
|
234
|
+
: isDecodedOrBoundAsUnknown(yielded, sourceCode);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
export const noUncheckedParsing = defineRule({
|
|
238
|
+
meta: {
|
|
239
|
+
type: "problem",
|
|
240
|
+
docs: {
|
|
241
|
+
description:
|
|
242
|
+
"Parse outside data with a Schema instead of json(), decimal parseInt, parseFloat or Schema.Any, which hand back values nothing checked. A json() body that goes straight into a Schema decoder or an unknown binding, and a parseInt with a constant radix other than 10, which no Schema decodes, pass.",
|
|
243
|
+
},
|
|
244
|
+
messages: {
|
|
245
|
+
jsonBody: "json() parses JSON without a Schema",
|
|
246
|
+
numberParse: "{{usage}} parses a number without a Schema",
|
|
247
|
+
uncheckedSchema: "{{usage}} accepts any value without a check",
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
create(context) {
|
|
251
|
+
const { sourceCode } = context;
|
|
252
|
+
const reportUncheckedSchema = (node: ESTree.Node) => {
|
|
253
|
+
const usage = effectModuleReference(node, sourceCode, "Schema");
|
|
254
|
+
const schema = [...UNCHECKED_SCHEMAS].find(
|
|
255
|
+
(unchecked) => usage === unchecked || usage?.startsWith(`${unchecked}.`) === true,
|
|
256
|
+
);
|
|
257
|
+
if (schema !== undefined) {
|
|
258
|
+
context.report({ node, messageId: "uncheckedSchema", data: { usage: schema } });
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
return {
|
|
262
|
+
CallExpression(node) {
|
|
263
|
+
const readsTheBodyAsJson =
|
|
264
|
+
node.callee.type === "MemberExpression" &&
|
|
265
|
+
memberName(node.callee) === JSON_BODY_READ &&
|
|
266
|
+
node.arguments.length === 0 &&
|
|
267
|
+
!hasLocalJsonMethod(node.callee.object, sourceCode) &&
|
|
268
|
+
!isDecodedOrBoundAsUnknownRightAway(node, sourceCode);
|
|
269
|
+
if (readsTheBodyAsJson) {
|
|
270
|
+
context.report({ node, messageId: "jsonBody" });
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
Identifier(node) {
|
|
274
|
+
reportUncheckedSchema(node);
|
|
275
|
+
const isGlobalNumberParser =
|
|
276
|
+
NUMBER_PARSERS.has(node.name) &&
|
|
277
|
+
!isShorthandKey(node) &&
|
|
278
|
+
(isMemberExpressionObject(node) || isReferencePosition(node)) &&
|
|
279
|
+
isGlobalReference(node, sourceCode, node.name) &&
|
|
280
|
+
!parsesARadixNoSchemaDecodes(node, node.name, sourceCode);
|
|
281
|
+
if (isGlobalNumberParser) {
|
|
282
|
+
context.report({ node, messageId: "numberParse", data: { usage: node.name } });
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
MemberExpression(node) {
|
|
286
|
+
reportUncheckedSchema(node);
|
|
287
|
+
const parser = memberName(node);
|
|
288
|
+
const isNumberParser =
|
|
289
|
+
parser !== undefined &&
|
|
290
|
+
NUMBER_PARSERS.has(parser) &&
|
|
291
|
+
isGlobalReference(node.object, sourceCode, "Number") &&
|
|
292
|
+
!parsesARadixNoSchemaDecodes(node, parser, sourceCode);
|
|
293
|
+
if (isNumberParser) {
|
|
294
|
+
context.report({ node, messageId: "numberParse", data: { usage: `Number.${parser}` } });
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
},
|
|
299
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import { FORMAT_CONFIG_BINDING, FORMAT_CONFIG_FILE } from "../shared/config-file-names.ts";
|
|
5
|
+
import {
|
|
6
|
+
fileNameOf,
|
|
7
|
+
INTERNAL_DIRECTORY_NAME,
|
|
8
|
+
isEntryFileName,
|
|
9
|
+
LIBRARY_DIRECTORY_NAME,
|
|
10
|
+
SHARED_DIRECTORY_NAME,
|
|
11
|
+
} from "../shared/match-file-names-and-path-prefixes.ts";
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_EXPORT,
|
|
14
|
+
importedBindingOf,
|
|
15
|
+
importedExpression,
|
|
16
|
+
resolvedVariable,
|
|
17
|
+
unwrappedExpression,
|
|
18
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
19
|
+
|
|
20
|
+
const OWN_LAYOUT_DIRECTORIES = `${INTERNAL_DIRECTORY_NAME}/ and ${SHARED_DIRECTORY_NAME}/`;
|
|
21
|
+
const RE_EXPORTABLE_EXTENSIONS = ".ts or .tsx";
|
|
22
|
+
|
|
23
|
+
export const noReExports = defineRule({
|
|
24
|
+
meta: {
|
|
25
|
+
type: "suggestion",
|
|
26
|
+
docs: {
|
|
27
|
+
description: `Define each export where it is written; an exception is an index.ts or index.tsx that re-exports names, unrenamed, with export { … } from or export type { … } from a path that resolves to a ${RE_EXPORTABLE_EXTENSIONS} file, not an index or a .d.ts, directly in its own ${OWN_LAYOUT_DIRECTORIES}. An index.ts may also export * as Name from the index.ts of a module directly in its own ${LIBRARY_DIRECTORY_NAME}/ that only re-exports unrenamed names from its own internal files. export { … } or export default naming an imported binding, or export default of a direct static member of an imported binding, is a re-export too, and so is an exported declaration initialized to one of them. ${FORMAT_CONFIG_FILE} may export default the imported ${FORMAT_CONFIG_BINDING}, which the format-config check requires. The re-exports check judges where an index's export from and export * as resolve.`,
|
|
28
|
+
},
|
|
29
|
+
messages: {
|
|
30
|
+
fromModule: "export from {{source}} re-exports another module",
|
|
31
|
+
everything: "export * from {{source}} re-exports another module",
|
|
32
|
+
importedValue: "export {{exported}} re-exports {{binding}}, imported from {{source}}",
|
|
33
|
+
importedMember:
|
|
34
|
+
"export {{exported}} re-exports {{member}}, a member of {{binding}} imported from {{source}}",
|
|
35
|
+
initializedToImport:
|
|
36
|
+
"export {{exported}} is initialized to {{binding}}, imported from {{source}}",
|
|
37
|
+
initializedToMember:
|
|
38
|
+
"export {{exported}} is initialized to {{member}}, a member of {{binding}} imported from {{source}}",
|
|
39
|
+
importEquals: "export import {{name}} re-exports another module",
|
|
40
|
+
exportAssignment: "export = replaces the module's exports",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
create(context) {
|
|
44
|
+
const isIndexFile = isEntryFileName(fileNameOf(context.filename));
|
|
45
|
+
const isFormatConfigFile = fileNameOf(context.filename) === FORMAT_CONFIG_FILE;
|
|
46
|
+
const reportImport = (
|
|
47
|
+
node: ESTree.Node,
|
|
48
|
+
exported: string,
|
|
49
|
+
imported: NonNullable<ReturnType<typeof importedBindingOf>>,
|
|
50
|
+
bindingMessageId: "importedValue" | "initializedToImport",
|
|
51
|
+
memberMessageId: "importedMember" | "initializedToMember",
|
|
52
|
+
) => {
|
|
53
|
+
const { name: binding, module: source } = imported;
|
|
54
|
+
context.report(
|
|
55
|
+
imported.member === undefined
|
|
56
|
+
? { node, messageId: bindingMessageId, data: { exported, binding, source } }
|
|
57
|
+
: {
|
|
58
|
+
node,
|
|
59
|
+
messageId: memberMessageId,
|
|
60
|
+
data: { exported, binding, member: imported.member, source },
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
const localInitializerOf = (value: ESTree.Node) => {
|
|
65
|
+
const definitions = resolvedVariable(unwrappedExpression(value), context.sourceCode)?.defs;
|
|
66
|
+
const declarator = definitions?.length === 1 ? definitions[0]?.node : undefined;
|
|
67
|
+
const isNamedDeclarator =
|
|
68
|
+
declarator?.type === "VariableDeclarator" && declarator.id.type === "Identifier";
|
|
69
|
+
return isNamedDeclarator ? (declarator.init ?? undefined) : undefined;
|
|
70
|
+
};
|
|
71
|
+
const reportInitializedToImport = (
|
|
72
|
+
node: ESTree.Node,
|
|
73
|
+
exported: string,
|
|
74
|
+
initializer: ESTree.Node,
|
|
75
|
+
followedInitializers: ReadonlySet<ESTree.Node> = new Set([initializer]),
|
|
76
|
+
) => {
|
|
77
|
+
const imported = importedBindingOf(initializer, context.sourceCode);
|
|
78
|
+
if (imported !== undefined) {
|
|
79
|
+
reportImport(node, exported, imported, "initializedToImport", "initializedToMember");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const aliased = localInitializerOf(initializer);
|
|
83
|
+
const isUnfollowedAlias = aliased !== undefined && !followedInitializers.has(aliased);
|
|
84
|
+
if (isUnfollowedAlias) {
|
|
85
|
+
reportInitializedToImport(
|
|
86
|
+
node,
|
|
87
|
+
exported,
|
|
88
|
+
aliased,
|
|
89
|
+
new Set([...followedInitializers, aliased]),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const reportImported = (node: ESTree.Node, exported: string, value: ESTree.Node) => {
|
|
94
|
+
const imported = importedBindingOf(value, context.sourceCode);
|
|
95
|
+
if (imported !== undefined) {
|
|
96
|
+
reportImport(node, exported, imported, "importedValue", "importedMember");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const initializer = localInitializerOf(value);
|
|
100
|
+
if (initializer !== undefined) {
|
|
101
|
+
reportInitializedToImport(node, exported, initializer);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
Program(program) {
|
|
106
|
+
for (const statement of program.body) {
|
|
107
|
+
if (statement.type === "ExportAllDeclaration") {
|
|
108
|
+
const isLeftToTheReExportsCheck = isIndexFile && statement.exported !== null;
|
|
109
|
+
if (isLeftToTheReExportsCheck) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
context.report({
|
|
113
|
+
node: statement,
|
|
114
|
+
messageId: "everything",
|
|
115
|
+
data: { source: statement.source.value },
|
|
116
|
+
});
|
|
117
|
+
} else if (statement.type === "ExportNamedDeclaration") {
|
|
118
|
+
if (statement.source !== null) {
|
|
119
|
+
if (!isIndexFile) {
|
|
120
|
+
context.report({
|
|
121
|
+
node: statement,
|
|
122
|
+
messageId: "fromModule",
|
|
123
|
+
data: { source: statement.source.value },
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
for (const specifier of statement.specifiers) {
|
|
129
|
+
reportImported(
|
|
130
|
+
specifier,
|
|
131
|
+
context.sourceCode.getText(specifier.exported),
|
|
132
|
+
specifier.local,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
const declaration = statement.declaration;
|
|
136
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
137
|
+
for (const declarator of declaration.declarations) {
|
|
138
|
+
const { id, init: initializer } = declarator;
|
|
139
|
+
const isInitializedName = id.type === "Identifier" && initializer !== null;
|
|
140
|
+
if (isInitializedName) {
|
|
141
|
+
reportInitializedToImport(declarator, id.name, initializer);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const importsAModule =
|
|
146
|
+
declaration?.type === "TSImportEqualsDeclaration" &&
|
|
147
|
+
declaration.moduleReference.type === "TSExternalModuleReference";
|
|
148
|
+
if (importsAModule) {
|
|
149
|
+
context.report({
|
|
150
|
+
node: declaration,
|
|
151
|
+
messageId: "importEquals",
|
|
152
|
+
data: { name: declaration.id.name },
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
} else if (statement.type === "ExportDefaultDeclaration") {
|
|
156
|
+
const exportsTheFormatConfig =
|
|
157
|
+
isFormatConfigFile &&
|
|
158
|
+
statement.declaration.type === "Identifier" &&
|
|
159
|
+
statement.declaration.name === FORMAT_CONFIG_BINDING &&
|
|
160
|
+
importedExpression(statement.declaration, context.sourceCode)?.members.join() ===
|
|
161
|
+
FORMAT_CONFIG_BINDING;
|
|
162
|
+
if (!exportsTheFormatConfig) {
|
|
163
|
+
reportImported(statement, DEFAULT_EXPORT, statement.declaration);
|
|
164
|
+
}
|
|
165
|
+
} else if (statement.type === "TSExportAssignment") {
|
|
166
|
+
context.report({ node: statement, messageId: "exportAssignment" });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
});
|