@sshkeda/check 0.0.0-stage → 1.0.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 +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 +848 -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,848 @@
|
|
|
1
|
+
import { Task } from "@sshkeda/task";
|
|
2
|
+
import { Effect, FileSystem, Option, Path, Predicate, References, Result, Schema } from "effect";
|
|
3
|
+
import { parseSync, Visitor } from "oxc-parser";
|
|
4
|
+
import type { CallExpression, Expression, ParseResult, Statement } from "oxc-parser";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
TASK_PACKAGE,
|
|
8
|
+
TASK_NAMESPACE_EXPORT,
|
|
9
|
+
TASK_MAIN_MEMBER,
|
|
10
|
+
} from "../../../../../shared/bans-each-permission-lifts.ts";
|
|
11
|
+
import { retiredWordIn } from "../../../../../shared/find-retired-words.ts";
|
|
12
|
+
import {
|
|
13
|
+
isEntryFileName,
|
|
14
|
+
fileNameOf,
|
|
15
|
+
isWithin,
|
|
16
|
+
LAYOUT_DIRECTORY_NAMES,
|
|
17
|
+
LIBRARY_DIRECTORY_NAME,
|
|
18
|
+
} from "../../../../../shared/match-file-names-and-path-prefixes.ts";
|
|
19
|
+
import {
|
|
20
|
+
cannotImportPermission,
|
|
21
|
+
RedundantGrant,
|
|
22
|
+
} from "../../../../../shared/permission-grant-errors.ts";
|
|
23
|
+
import {
|
|
24
|
+
generatedPathsOf,
|
|
25
|
+
DIRECTORY,
|
|
26
|
+
FILE_TYPE,
|
|
27
|
+
isMarkdown,
|
|
28
|
+
normalizedPath,
|
|
29
|
+
PATCH,
|
|
30
|
+
} from "../../../../../shared/record-and-grant-permissions-by-path.ts";
|
|
31
|
+
import type { PERMISSION_NAMES } from "../../../../../shared/record-and-grant-permissions-by-path.ts";
|
|
32
|
+
import {
|
|
33
|
+
SCRIPTS_DIRECTORY,
|
|
34
|
+
SOURCE_DIRECTORY,
|
|
35
|
+
TEST_DIRECTORY,
|
|
36
|
+
} from "../../../../../shared/test-home-directories.ts";
|
|
37
|
+
import {
|
|
38
|
+
enclosingDirectoriesOf,
|
|
39
|
+
isRootSupportFile,
|
|
40
|
+
repeatsSourceNeverImportsScripts,
|
|
41
|
+
moduleDirectoriesAmong,
|
|
42
|
+
isTopLevelScript,
|
|
43
|
+
isScript,
|
|
44
|
+
isDeclarationFile,
|
|
45
|
+
isUnderTestDirectory,
|
|
46
|
+
isPackageManifest,
|
|
47
|
+
directoryOf,
|
|
48
|
+
directoriesNestedInLayoutDirectoriesOf,
|
|
49
|
+
} from "../../../shared/classify-files-by-role.ts";
|
|
50
|
+
import { lineNumberAt } from "../../../shared/count-the-line-of-an-offset.ts";
|
|
51
|
+
import { PACKAGE_MANIFEST_FILE } from "../../../shared/find-named-package-manifests.ts";
|
|
52
|
+
import { isInsideAnyDirectory, listedPackageFiles } from "../../../shared/list-package-files.ts";
|
|
53
|
+
import { loadedLintConfigIn } from "../../../shared/load-the-lint-config.ts";
|
|
54
|
+
import {
|
|
55
|
+
bunPatchFileNameOf,
|
|
56
|
+
isGrantedPatchFile,
|
|
57
|
+
listedPatchesOf,
|
|
58
|
+
patchedDependenciesOf,
|
|
59
|
+
rootManifestIn,
|
|
60
|
+
} from "../../../shared/match-granted-patch-files.ts";
|
|
61
|
+
import { CHECK_PACKAGE } from "../../../shared/project-config-and-tool-errors.ts";
|
|
62
|
+
import {
|
|
63
|
+
JSON_OBJECT,
|
|
64
|
+
packageManifestObject,
|
|
65
|
+
} from "../../../shared/read-package-manifest-object.ts";
|
|
66
|
+
import { absolutePathOf } from "../../../shared/resolve-absolute-paths.ts";
|
|
67
|
+
import { toolFileOf, toolFileIsPlaced } from "../../../shared/tool-file-locations.ts";
|
|
68
|
+
import {
|
|
69
|
+
BunTestFileName,
|
|
70
|
+
CannotImportTargetMissing,
|
|
71
|
+
CheckOrTaskPatched,
|
|
72
|
+
DOCS_DIRECTORY,
|
|
73
|
+
FileLayoutViolations,
|
|
74
|
+
DirectoryGrantedPermissions,
|
|
75
|
+
DirectoryGrantOnFile,
|
|
76
|
+
DependencyOutsideRepository,
|
|
77
|
+
DirectoryHoldsOnlyADirectory,
|
|
78
|
+
RepositoryRootNotFound,
|
|
79
|
+
DuplicateFileName,
|
|
80
|
+
GrantedPathMissing,
|
|
81
|
+
LibraryDirectory,
|
|
82
|
+
DirectoryInInternalOrShared,
|
|
83
|
+
IndexInInternalOrShared,
|
|
84
|
+
SourceOutsideInternalOrShared,
|
|
85
|
+
MisplacedAtPackageRoot,
|
|
86
|
+
RetiredWordInPath,
|
|
87
|
+
NotKebabCase,
|
|
88
|
+
PackageScriptsField,
|
|
89
|
+
PatchedDependenciesMalformed,
|
|
90
|
+
PatchNotGranted,
|
|
91
|
+
MisplacedToolFile,
|
|
92
|
+
TopLevelNonScript,
|
|
93
|
+
TopLevelScriptHoldsOtherStatement,
|
|
94
|
+
TopLevelScriptWithoutMain,
|
|
95
|
+
UngrantedDocument,
|
|
96
|
+
UngrantedDirectory,
|
|
97
|
+
UnlistedPatchGranted,
|
|
98
|
+
} from "./file-layout-errors.ts";
|
|
99
|
+
import { exportPathMismatches } from "./find-export-path-mismatches.ts";
|
|
100
|
+
import {
|
|
101
|
+
directoriesOutsideModuleLayoutAmong,
|
|
102
|
+
isLibraryDirectoryOfAModule,
|
|
103
|
+
isModuleDirectory,
|
|
104
|
+
moduleLayoutViolations,
|
|
105
|
+
modulesWithLayoutDirectoryNamesAmong,
|
|
106
|
+
moduleWithLayoutDirectoryNameViolations,
|
|
107
|
+
} from "./find-module-layout-violations.ts";
|
|
108
|
+
import { taskImportsIn, isTaskLayoutFile } from "./resolve-task-imports.ts";
|
|
109
|
+
|
|
110
|
+
const KEBAB_CASE_PART = /^[a-z0-9]+(?:-[a-z0-9]+)*$/u;
|
|
111
|
+
const BUN_TEST_FILE_EXTENSION = /\.(?:test|spec)(?<extension>\.[^.]+)$/u;
|
|
112
|
+
const DIRECTORY_AND_FILE = 2;
|
|
113
|
+
|
|
114
|
+
const GRANTS_OF_A_REFUSED_CONFIG_ARE_REPORTED_BY_THE_LINT_CONFIG_TASK = Effect.succeedNone;
|
|
115
|
+
|
|
116
|
+
const isKebabCaseSegment = (segment: string) =>
|
|
117
|
+
toolFileOf(segment) !== undefined ||
|
|
118
|
+
segment
|
|
119
|
+
.replace(/^\./u, "")
|
|
120
|
+
.split(".")
|
|
121
|
+
.every((part) => KEBAB_CASE_PART.test(part));
|
|
122
|
+
|
|
123
|
+
const isNamedLikeABunTest = (fileName: string) => {
|
|
124
|
+
const extension = BUN_TEST_FILE_EXTENSION.exec(fileName.toLowerCase())?.groups?.["extension"];
|
|
125
|
+
return extension !== undefined && isScript(extension);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export const pathViolations = (path: string, isNamedByBunPatch = false) => {
|
|
129
|
+
const violations: (BunTestFileName | MisplacedToolFile | NotKebabCase | TopLevelNonScript)[] = [];
|
|
130
|
+
const segments = path.split("/");
|
|
131
|
+
const fileName = fileNameOf(path);
|
|
132
|
+
const tool = toolFileOf(path);
|
|
133
|
+
const misnamed = segments.find(
|
|
134
|
+
(segment, index) =>
|
|
135
|
+
!isKebabCaseSegment(segment) && !(isNamedByBunPatch && index === segments.length - 1),
|
|
136
|
+
);
|
|
137
|
+
if (misnamed !== undefined) {
|
|
138
|
+
violations.push(new NotKebabCase({ location: path, segment: misnamed }));
|
|
139
|
+
}
|
|
140
|
+
const isMisplacedTool = tool?.directories !== undefined && !toolFileIsPlaced(path);
|
|
141
|
+
if (isMisplacedTool) {
|
|
142
|
+
violations.push(new MisplacedToolFile({ location: path, directories: tool.directories }));
|
|
143
|
+
}
|
|
144
|
+
if (isNamedLikeABunTest(fileName)) {
|
|
145
|
+
violations.push(new BunTestFileName({ location: path }));
|
|
146
|
+
}
|
|
147
|
+
const isPlacedNonDocumentTool = tool !== undefined && toolFileIsPlaced(path) && !isMarkdown(path);
|
|
148
|
+
const isNonScriptDirectlyInScriptsDirectory =
|
|
149
|
+
segments.length === DIRECTORY_AND_FILE &&
|
|
150
|
+
segments[0] === SCRIPTS_DIRECTORY &&
|
|
151
|
+
!isScript(fileName.toLowerCase()) &&
|
|
152
|
+
!isMisplacedTool &&
|
|
153
|
+
!isPlacedNonDocumentTool;
|
|
154
|
+
if (isNonScriptDirectlyInScriptsDirectory) {
|
|
155
|
+
violations.push(new TopLevelNonScript({ location: path }));
|
|
156
|
+
}
|
|
157
|
+
return violations;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const directTaskImports = (parsed: ParseResult) =>
|
|
161
|
+
new Map(
|
|
162
|
+
parsed.module.staticImports
|
|
163
|
+
.filter((declaration) => declaration.moduleRequest.value === TASK_PACKAGE)
|
|
164
|
+
.flatMap((declaration) =>
|
|
165
|
+
declaration.entries
|
|
166
|
+
.filter((entry) => !entry.isType)
|
|
167
|
+
.map(
|
|
168
|
+
(entry) =>
|
|
169
|
+
[
|
|
170
|
+
entry.localName.value,
|
|
171
|
+
entry.importName.kind === "NamespaceObject" ? [] : [entry.importName.name ?? ""],
|
|
172
|
+
] as const,
|
|
173
|
+
),
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const callsTaskMember = (
|
|
178
|
+
parsed: ParseResult,
|
|
179
|
+
member: string,
|
|
180
|
+
imports: ReadonlyMap<string, readonly string[]>,
|
|
181
|
+
) => {
|
|
182
|
+
const calls: CallExpression[] = [];
|
|
183
|
+
const aliases = new Map<string, Expression>();
|
|
184
|
+
new Visitor({
|
|
185
|
+
CallExpression(node) {
|
|
186
|
+
calls.push(node);
|
|
187
|
+
},
|
|
188
|
+
VariableDeclarator({ id, init: initializer }) {
|
|
189
|
+
const isNamedInitializer = id.type === "Identifier" && initializer !== null;
|
|
190
|
+
if (isNamedInitializer) {
|
|
191
|
+
aliases.set(id.name, initializer);
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
}).visit(parsed.program);
|
|
195
|
+
const taskReference = (
|
|
196
|
+
expression: Expression,
|
|
197
|
+
seen: ReadonlySet<string> = new Set(),
|
|
198
|
+
): readonly string[] | undefined => {
|
|
199
|
+
if (expression.type === "Identifier") {
|
|
200
|
+
const imported = imports.get(expression.name);
|
|
201
|
+
if (imported !== undefined) {
|
|
202
|
+
return imported;
|
|
203
|
+
}
|
|
204
|
+
const alias = aliases.get(expression.name);
|
|
205
|
+
const isUnfollowedAlias = alias !== undefined && !seen.has(expression.name);
|
|
206
|
+
return isUnfollowedAlias
|
|
207
|
+
? taskReference(alias, new Set([...seen, expression.name]))
|
|
208
|
+
: undefined;
|
|
209
|
+
}
|
|
210
|
+
const isStaticMember =
|
|
211
|
+
expression.type === "MemberExpression" &&
|
|
212
|
+
!expression.computed &&
|
|
213
|
+
expression.property.type === "Identifier";
|
|
214
|
+
if (!isStaticMember) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const object = taskReference(expression.object, seen);
|
|
218
|
+
return object === undefined ? undefined : [...object, expression.property.name];
|
|
219
|
+
};
|
|
220
|
+
return calls.some(
|
|
221
|
+
({ callee }) => taskReference(callee)?.join(".") === `${TASK_NAMESPACE_EXPORT}.${member}`,
|
|
222
|
+
);
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const importedMemberPath = (
|
|
226
|
+
expression: Expression,
|
|
227
|
+
imports: ReadonlyMap<string, readonly string[]>,
|
|
228
|
+
): readonly string[] | undefined => {
|
|
229
|
+
if (expression.type === "Identifier") {
|
|
230
|
+
return imports.get(expression.name);
|
|
231
|
+
}
|
|
232
|
+
const isStaticMember =
|
|
233
|
+
expression.type === "MemberExpression" &&
|
|
234
|
+
!expression.computed &&
|
|
235
|
+
expression.property.type === "Identifier";
|
|
236
|
+
if (!isStaticMember) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const object = importedMemberPath(expression.object, imports);
|
|
240
|
+
return object === undefined ? undefined : [...object, expression.property.name];
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const isMainGuard = (statement: Statement, imports: ReadonlyMap<string, readonly string[]>) => {
|
|
244
|
+
if (statement.type !== "IfStatement") {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
const { test, consequent, alternate } = statement;
|
|
248
|
+
const testsImportMetaMain =
|
|
249
|
+
test.type === "MemberExpression" &&
|
|
250
|
+
!test.computed &&
|
|
251
|
+
test.object.type === "MetaProperty" &&
|
|
252
|
+
test.object.meta.name === "import" &&
|
|
253
|
+
test.object.property.name === "meta" &&
|
|
254
|
+
test.property.type === "Identifier" &&
|
|
255
|
+
test.property.name === "main";
|
|
256
|
+
const [only, ...others] = consequent.type === "BlockStatement" ? consequent.body : [];
|
|
257
|
+
const expression = only?.type === "ExpressionStatement" ? only.expression : undefined;
|
|
258
|
+
const call = expression?.type === "CallExpression" ? expression : undefined;
|
|
259
|
+
const [task, ...otherArguments] = call?.arguments ?? [];
|
|
260
|
+
const runsOneNamedTask =
|
|
261
|
+
call !== undefined &&
|
|
262
|
+
others.length === 0 &&
|
|
263
|
+
otherArguments.length === 0 &&
|
|
264
|
+
task?.type === "Identifier" &&
|
|
265
|
+
importedMemberPath(call.callee, imports)?.join(".") ===
|
|
266
|
+
`${TASK_NAMESPACE_EXPORT}.${TASK_MAIN_MEMBER}`;
|
|
267
|
+
return testsImportMetaMain && alternate === null && runsOneNamedTask;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const statementsOtherThanImportsAndMain = (
|
|
271
|
+
parsed: ParseResult,
|
|
272
|
+
imports: ReadonlyMap<string, readonly string[]>,
|
|
273
|
+
) => {
|
|
274
|
+
const nonImports = parsed.program.body.filter(
|
|
275
|
+
(statement) => statement.type !== "ImportDeclaration",
|
|
276
|
+
);
|
|
277
|
+
const firstGuard = nonImports.findIndex((statement) => isMainGuard(statement, imports));
|
|
278
|
+
return nonImports.filter((_statement, index) => index !== firstGuard);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
export const sourceViolations = (
|
|
282
|
+
path: string,
|
|
283
|
+
text: string,
|
|
284
|
+
resolvedImports?: ReadonlyMap<string, readonly string[]>,
|
|
285
|
+
) => {
|
|
286
|
+
const violations: (
|
|
287
|
+
| MisplacedAtPackageRoot
|
|
288
|
+
| SourceOutsideInternalOrShared
|
|
289
|
+
| TopLevelScriptHoldsOtherStatement
|
|
290
|
+
| TopLevelScriptWithoutMain
|
|
291
|
+
)[] = [];
|
|
292
|
+
const isMisplacedTool = toolFileOf(path) !== undefined && !toolFileIsPlaced(path);
|
|
293
|
+
if (isMisplacedTool) {
|
|
294
|
+
return violations;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const isSourceOutsideModuleImplementation =
|
|
298
|
+
(isWithin(path, SOURCE_DIRECTORY) || isWithin(path, TEST_DIRECTORY)) &&
|
|
299
|
+
!isLibraryDirectoryOfAModule(directoryOf(path)) &&
|
|
300
|
+
!isEntryFileName(fileNameOf(path)) &&
|
|
301
|
+
!isDeclarationFile(path) &&
|
|
302
|
+
!enclosingDirectoriesOf(path).some((directory) =>
|
|
303
|
+
LAYOUT_DIRECTORY_NAMES.has(fileNameOf(directory)),
|
|
304
|
+
);
|
|
305
|
+
if (isSourceOutsideModuleImplementation) {
|
|
306
|
+
violations.push(new SourceOutsideInternalOrShared({ location: path }));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const parsed = parseSync(path, text);
|
|
310
|
+
const imports = resolvedImports ?? directTaskImports(parsed);
|
|
311
|
+
const isRootCodeFile = !path.includes("/") && !isRootSupportFile(path);
|
|
312
|
+
if (isRootCodeFile) {
|
|
313
|
+
violations.push(
|
|
314
|
+
new MisplacedAtPackageRoot({
|
|
315
|
+
location: path,
|
|
316
|
+
place: callsTaskMember(parsed, TASK_MAIN_MEMBER, imports)
|
|
317
|
+
? "root-task-script"
|
|
318
|
+
: "root-code-file",
|
|
319
|
+
}),
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
const isTopLevelScriptWithoutMain =
|
|
323
|
+
isTopLevelScript(path) && !callsTaskMember(parsed, TASK_MAIN_MEMBER, imports);
|
|
324
|
+
if (isTopLevelScriptWithoutMain) {
|
|
325
|
+
violations.push(new TopLevelScriptWithoutMain({ location: path }));
|
|
326
|
+
}
|
|
327
|
+
const isTopLevelScriptWithMain = isTopLevelScript(path) && !isTopLevelScriptWithoutMain;
|
|
328
|
+
if (isTopLevelScriptWithMain) {
|
|
329
|
+
violations.push(
|
|
330
|
+
...statementsOtherThanImportsAndMain(parsed, imports).map(
|
|
331
|
+
(statement) =>
|
|
332
|
+
new TopLevelScriptHoldsOtherStatement({
|
|
333
|
+
location: `${path}:${lineNumberAt(text, statement.start)}`,
|
|
334
|
+
}),
|
|
335
|
+
),
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
return violations;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const directoryNameWords = (directoryName: string) =>
|
|
342
|
+
directoryName.replace(/^\./u, "").split(/[-_.]/u);
|
|
343
|
+
|
|
344
|
+
const nameViolationsOf = (path: string, words: readonly string[]) => {
|
|
345
|
+
const retired = retiredWordIn(words);
|
|
346
|
+
return retired === undefined ? [] : [new RetiredWordInPath({ location: path, ...retired })];
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const distinctDirectoriesOf = (paths: readonly string[]) => [
|
|
350
|
+
...new Set(paths.flatMap((path) => enclosingDirectoriesOf(path))),
|
|
351
|
+
];
|
|
352
|
+
|
|
353
|
+
const outermostDirectoriesOf = (directories: readonly string[]) =>
|
|
354
|
+
directories.filter((directory) => !isInsideAnyDirectory(directory, directories));
|
|
355
|
+
|
|
356
|
+
const shapeViolationsOf = (paths: readonly string[]) => {
|
|
357
|
+
const directories = distinctDirectoriesOf(paths);
|
|
358
|
+
const directoriesOutsideModuleLayout = directoriesOutsideModuleLayoutAmong(paths);
|
|
359
|
+
return [
|
|
360
|
+
...moduleLayoutViolations(paths),
|
|
361
|
+
...directories
|
|
362
|
+
.filter((directory) => {
|
|
363
|
+
const isLibraryDirectoryOutsideAModule =
|
|
364
|
+
directory.endsWith(`/${LIBRARY_DIRECTORY_NAME}`) &&
|
|
365
|
+
!isModuleDirectory(directoryOf(directory));
|
|
366
|
+
return (
|
|
367
|
+
isLibraryDirectoryOutsideAModule &&
|
|
368
|
+
!isInsideAnyDirectory(directory, directoriesOutsideModuleLayout)
|
|
369
|
+
);
|
|
370
|
+
})
|
|
371
|
+
.map((directory) => new LibraryDirectory({ location: directory })),
|
|
372
|
+
...[
|
|
373
|
+
...new Set(
|
|
374
|
+
paths
|
|
375
|
+
.filter((path) => isScript(path) && !isUnderTestDirectory(path))
|
|
376
|
+
.flatMap((path) => directoriesNestedInLayoutDirectoriesOf(path)),
|
|
377
|
+
),
|
|
378
|
+
].map(
|
|
379
|
+
(directory) =>
|
|
380
|
+
new DirectoryInInternalOrShared({ location: directoryOf(directory), directory }),
|
|
381
|
+
),
|
|
382
|
+
...paths
|
|
383
|
+
.filter(
|
|
384
|
+
(path) =>
|
|
385
|
+
isEntryFileName(fileNameOf(path)) &&
|
|
386
|
+
LAYOUT_DIRECTORY_NAMES.has(fileNameOf(directoryOf(path))),
|
|
387
|
+
)
|
|
388
|
+
.map((path) => new IndexInInternalOrShared({ location: path })),
|
|
389
|
+
];
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
export const directoryShapeViolations = (paths: readonly string[]) => {
|
|
393
|
+
const modulesWithLayoutDirectoryNames = modulesWithLayoutDirectoryNamesAmong(paths);
|
|
394
|
+
return [
|
|
395
|
+
...moduleWithLayoutDirectoryNameViolations(paths),
|
|
396
|
+
...shapeViolationsOf(paths).filter(
|
|
397
|
+
({ location }) =>
|
|
398
|
+
!modulesWithLayoutDirectoryNames.some((module) => isWithin(location, module)),
|
|
399
|
+
),
|
|
400
|
+
];
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const PACKAGE_ROOT_SCOPE = "";
|
|
404
|
+
const PACKAGE_ROOT_DIRECTORIES: ReadonlySet<string> = new Set([
|
|
405
|
+
SOURCE_DIRECTORY,
|
|
406
|
+
SCRIPTS_DIRECTORY,
|
|
407
|
+
TEST_DIRECTORY,
|
|
408
|
+
DOCS_DIRECTORY,
|
|
409
|
+
]);
|
|
410
|
+
|
|
411
|
+
const isRootDirectoryOutsideTheLayout = (directory: string) =>
|
|
412
|
+
!directory.includes("/") && !PACKAGE_ROOT_DIRECTORIES.has(directory);
|
|
413
|
+
|
|
414
|
+
const directoriesExemptFromShapeRules = (
|
|
415
|
+
directories: Iterable<string>,
|
|
416
|
+
grants: Effect.Success<ReturnType<typeof loadedLintConfigIn>>,
|
|
417
|
+
) =>
|
|
418
|
+
[...directories].filter(
|
|
419
|
+
(directory) =>
|
|
420
|
+
isRootDirectoryOutsideTheLayout(directory) &&
|
|
421
|
+
grants.some((grant) => grant.path === directory && grant.allow.includes(DIRECTORY)),
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
export const directoriesHoldingOnlyADirectory = (
|
|
425
|
+
paths: readonly string[],
|
|
426
|
+
nestedPackageDirectories: readonly string[],
|
|
427
|
+
) => {
|
|
428
|
+
const outermostPackageDirectories = outermostDirectoriesOf(nestedPackageDirectories);
|
|
429
|
+
const directories = new Set([
|
|
430
|
+
...distinctDirectoriesOf([...paths, ...outermostPackageDirectories]),
|
|
431
|
+
...outermostPackageDirectories,
|
|
432
|
+
]);
|
|
433
|
+
const entriesByDirectory = Map.groupBy([...paths, ...directories], directoryOf);
|
|
434
|
+
return [...directories].flatMap((directory) => {
|
|
435
|
+
const [only, ...others] = entriesByDirectory.get(directory) ?? [];
|
|
436
|
+
const holdsOnlyADirectory =
|
|
437
|
+
only !== undefined &&
|
|
438
|
+
others.length === 0 &&
|
|
439
|
+
directories.has(only) &&
|
|
440
|
+
!isLibraryDirectoryOfAModule(directory);
|
|
441
|
+
return holdsOnlyADirectory
|
|
442
|
+
? [new DirectoryHoldsOnlyADirectory({ location: directory, child: fileNameOf(only) })]
|
|
443
|
+
: [];
|
|
444
|
+
});
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
const SCRIPT_EXTENSION = /\.[cm]?[jt]sx?$/u;
|
|
448
|
+
|
|
449
|
+
const moduleNameOf = (path: string) => fileNameOf(path).replace(SCRIPT_EXTENSION, "");
|
|
450
|
+
|
|
451
|
+
export const duplicateFileNameViolations = (paths: readonly string[]) => {
|
|
452
|
+
const moduleDirectories = moduleDirectoriesAmong(paths);
|
|
453
|
+
const nameScopeOf = (path: string) =>
|
|
454
|
+
enclosingDirectoriesOf(path)
|
|
455
|
+
.toReversed()
|
|
456
|
+
.find(
|
|
457
|
+
(directory) =>
|
|
458
|
+
moduleDirectories.has(directory) ||
|
|
459
|
+
directory === TEST_DIRECTORY ||
|
|
460
|
+
directory === SCRIPTS_DIRECTORY,
|
|
461
|
+
) ?? PACKAGE_ROOT_SCOPE;
|
|
462
|
+
const sources = paths.filter((path) => isScript(path) && !isEntryFileName(fileNameOf(path)));
|
|
463
|
+
const sourcesByScopedName = Map.groupBy(
|
|
464
|
+
sources,
|
|
465
|
+
(path) => `${nameScopeOf(path)}/${moduleNameOf(path)}`,
|
|
466
|
+
);
|
|
467
|
+
return [...sourcesByScopedName.values()].flatMap((sameNamed) =>
|
|
468
|
+
sameNamed.length === 1
|
|
469
|
+
? []
|
|
470
|
+
: sameNamed.map(
|
|
471
|
+
(path) =>
|
|
472
|
+
new DuplicateFileName({
|
|
473
|
+
location: path,
|
|
474
|
+
otherPaths: sameNamed.filter((other) => other !== path),
|
|
475
|
+
scope: nameScopeOf(path),
|
|
476
|
+
}),
|
|
477
|
+
),
|
|
478
|
+
);
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
export const packageRootViolations = (paths: readonly string[]) =>
|
|
482
|
+
paths
|
|
483
|
+
.filter((path) => {
|
|
484
|
+
const isMisplacedTool = toolFileOf(path) !== undefined && !toolFileIsPlaced(path);
|
|
485
|
+
return isWithin(path, DOCS_DIRECTORY) && !isMarkdown(path) && !isMisplacedTool;
|
|
486
|
+
})
|
|
487
|
+
.map((path) => new MisplacedAtPackageRoot({ location: path, place: "docs-non-markdown" }));
|
|
488
|
+
|
|
489
|
+
export const directoryNameViolations = (paths: readonly string[]) =>
|
|
490
|
+
distinctDirectoriesOf(paths).flatMap((directory) =>
|
|
491
|
+
nameViolationsOf(directory, directoryNameWords(fileNameOf(directory))),
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
const DEPENDENCY_FIELDS = [
|
|
495
|
+
"dependencies",
|
|
496
|
+
"devDependencies",
|
|
497
|
+
"peerDependencies",
|
|
498
|
+
"optionalDependencies",
|
|
499
|
+
"overrides",
|
|
500
|
+
] as const;
|
|
501
|
+
const LOCAL_PATH_PROTOCOL = /^(?:file|link):/u;
|
|
502
|
+
const BARE_LOCAL_PATH = /^(?:\.{1,2}(?:\/|$)|\/)/u;
|
|
503
|
+
const isJsonObject = Schema.is(JSON_OBJECT);
|
|
504
|
+
|
|
505
|
+
const localPathOf = (specifier: string) => {
|
|
506
|
+
if (LOCAL_PATH_PROTOCOL.test(specifier)) {
|
|
507
|
+
return specifier.replace(LOCAL_PATH_PROTOCOL, "");
|
|
508
|
+
}
|
|
509
|
+
return BARE_LOCAL_PATH.test(specifier) ? specifier : undefined;
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
const specifiersIn = (
|
|
513
|
+
field: string,
|
|
514
|
+
value: Schema.Json | undefined,
|
|
515
|
+
): { field: string; specifier: string }[] => {
|
|
516
|
+
if (typeof value === "string") {
|
|
517
|
+
return [{ field, specifier: value }];
|
|
518
|
+
}
|
|
519
|
+
if (!isJsonObject(value)) {
|
|
520
|
+
return [];
|
|
521
|
+
}
|
|
522
|
+
return Object.entries(value).flatMap(([name, nested]) =>
|
|
523
|
+
specifiersIn(`${field}.${name}`, nested),
|
|
524
|
+
);
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
const isOutsideDirectory = (pathModule: Path.Path, directory: string, target: string) => {
|
|
528
|
+
const fromDirectory = pathModule.relative(directory, target);
|
|
529
|
+
return (
|
|
530
|
+
fromDirectory === ".." ||
|
|
531
|
+
fromDirectory.startsWith("../") ||
|
|
532
|
+
pathModule.isAbsolute(fromDirectory)
|
|
533
|
+
);
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
export const dependenciesOutsideRepository = (
|
|
537
|
+
path: string,
|
|
538
|
+
manifest: typeof JSON_OBJECT.Type,
|
|
539
|
+
repository: { readonly root: string; readonly packageDirectory: string },
|
|
540
|
+
) =>
|
|
541
|
+
Effect.gen(function* () {
|
|
542
|
+
const pathModule = yield* Path.Path;
|
|
543
|
+
const manifestDirectory = pathModule.join(
|
|
544
|
+
repository.packageDirectory,
|
|
545
|
+
pathModule.dirname(path),
|
|
546
|
+
);
|
|
547
|
+
return DEPENDENCY_FIELDS.flatMap((field) => specifiersIn(field, manifest[field])).flatMap(
|
|
548
|
+
({ field, specifier }) => {
|
|
549
|
+
const localPath = localPathOf(specifier);
|
|
550
|
+
if (localPath === undefined) {
|
|
551
|
+
return [];
|
|
552
|
+
}
|
|
553
|
+
return isOutsideDirectory(
|
|
554
|
+
pathModule,
|
|
555
|
+
repository.root,
|
|
556
|
+
pathModule.resolve(manifestDirectory, localPath),
|
|
557
|
+
)
|
|
558
|
+
? [new DependencyOutsideRepository({ location: path, field, specifier })]
|
|
559
|
+
: [];
|
|
560
|
+
},
|
|
561
|
+
);
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
export const manifestViolations = (
|
|
565
|
+
path: string,
|
|
566
|
+
text: string,
|
|
567
|
+
repository: Parameters<typeof dependenciesOutsideRepository>[2],
|
|
568
|
+
) =>
|
|
569
|
+
Effect.gen(function* () {
|
|
570
|
+
const manifest = packageManifestObject(path, text);
|
|
571
|
+
if (Result.isFailure(manifest)) {
|
|
572
|
+
return [manifest.failure];
|
|
573
|
+
}
|
|
574
|
+
const scriptsViolations = Predicate.hasProperty(manifest.success, "scripts")
|
|
575
|
+
? [new PackageScriptsField({ location: path })]
|
|
576
|
+
: [];
|
|
577
|
+
return [
|
|
578
|
+
...scriptsViolations,
|
|
579
|
+
...(yield* dependenciesOutsideRepository(path, manifest.success, repository)),
|
|
580
|
+
...exportPathMismatches(path, manifest.success),
|
|
581
|
+
];
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
const ROOT_README = "README.md";
|
|
585
|
+
|
|
586
|
+
export const permissionGrantViolations = (
|
|
587
|
+
paths: readonly string[],
|
|
588
|
+
grants: Effect.Success<ReturnType<typeof loadedLintConfigIn>>,
|
|
589
|
+
nestedPackageDirectories: readonly string[] = [],
|
|
590
|
+
) => {
|
|
591
|
+
const repositoryFiles = new Set(paths);
|
|
592
|
+
const generated = generatedPathsOf(grants);
|
|
593
|
+
const outermostPackageDirectories = outermostDirectoriesOf(nestedPackageDirectories);
|
|
594
|
+
const repositoryDirectories = new Set([
|
|
595
|
+
...distinctDirectoriesOf([...paths, ...outermostPackageDirectories]),
|
|
596
|
+
...outermostPackageDirectories,
|
|
597
|
+
]);
|
|
598
|
+
const ownedDirectories = new Set([
|
|
599
|
+
...distinctDirectoriesOf([
|
|
600
|
+
...paths.filter((file) => !generated.has(file)),
|
|
601
|
+
...outermostPackageDirectories,
|
|
602
|
+
]),
|
|
603
|
+
...outermostPackageDirectories,
|
|
604
|
+
]);
|
|
605
|
+
const directoriesNeedingGrants = [...ownedDirectories].filter((directory) =>
|
|
606
|
+
isRootDirectoryOutsideTheLayout(directory),
|
|
607
|
+
);
|
|
608
|
+
const violations: (
|
|
609
|
+
| CannotImportTargetMissing
|
|
610
|
+
| DirectoryGrantedPermissions
|
|
611
|
+
| DirectoryGrantOnFile
|
|
612
|
+
| GrantedPathMissing
|
|
613
|
+
| RedundantGrant
|
|
614
|
+
| UngrantedDirectory
|
|
615
|
+
| UngrantedDocument
|
|
616
|
+
)[] = [];
|
|
617
|
+
const isGranted = (path: string, permission: (typeof PERMISSION_NAMES)[number]) =>
|
|
618
|
+
grants.some((grant) => grant.path === path && grant.allow.includes(permission));
|
|
619
|
+
for (const path of paths) {
|
|
620
|
+
const isUngrantedDocument =
|
|
621
|
+
isMarkdown(path) &&
|
|
622
|
+
path !== ROOT_README &&
|
|
623
|
+
!generated.has(path) &&
|
|
624
|
+
!isGranted(path, FILE_TYPE);
|
|
625
|
+
if (isUngrantedDocument) {
|
|
626
|
+
violations.push(new UngrantedDocument({ location: path }));
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const holdsOnlyGrantedPatches = (directory: string) => {
|
|
630
|
+
const held = paths.filter((path) => isWithin(path, directory));
|
|
631
|
+
return held.length > 0 && held.every((path) => isGrantedPatchFile(path, grants));
|
|
632
|
+
};
|
|
633
|
+
for (const directory of directoriesNeedingGrants) {
|
|
634
|
+
const isGrantedDirectory =
|
|
635
|
+
isGranted(directory, DIRECTORY) ||
|
|
636
|
+
isGranted(directory, PATCH) ||
|
|
637
|
+
holdsOnlyGrantedPatches(directory);
|
|
638
|
+
if (!isGrantedDirectory) {
|
|
639
|
+
violations.push(new UngrantedDirectory({ location: directory }));
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
const isRepositoryDirectory = (path: string) => repositoryDirectories.has(path);
|
|
643
|
+
const exists = (path: string) => repositoryFiles.has(path) || isRepositoryDirectory(path);
|
|
644
|
+
for (const grant of grants) {
|
|
645
|
+
if (!exists(grant.path)) {
|
|
646
|
+
violations.push(new GrantedPathMissing({ path: grant.path }));
|
|
647
|
+
}
|
|
648
|
+
const grantsDirectoryToFile =
|
|
649
|
+
repositoryFiles.has(grant.path) && grant.allow.includes(DIRECTORY);
|
|
650
|
+
if (grantsDirectoryToFile) {
|
|
651
|
+
violations.push(new DirectoryGrantOnFile({ path: grant.path }));
|
|
652
|
+
}
|
|
653
|
+
const isDirectoryGrantedPermissions =
|
|
654
|
+
isRepositoryDirectory(grant.path) &&
|
|
655
|
+
(grant.allow.some((permission) => permission !== DIRECTORY && permission !== PATCH) ||
|
|
656
|
+
grant.services.length > 0);
|
|
657
|
+
if (isDirectoryGrantedPermissions) {
|
|
658
|
+
violations.push(new DirectoryGrantedPermissions({ path: grant.path }));
|
|
659
|
+
}
|
|
660
|
+
const grantsFileTypeToRootReadme =
|
|
661
|
+
grant.path === ROOT_README && grant.allow.includes(FILE_TYPE);
|
|
662
|
+
if (grantsFileTypeToRootReadme) {
|
|
663
|
+
violations.push(
|
|
664
|
+
new RedundantGrant({
|
|
665
|
+
path: grant.path,
|
|
666
|
+
permission: FILE_TYPE,
|
|
667
|
+
reason: "the root README.md needs no grant",
|
|
668
|
+
}),
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
for (const target of grant.cannotImport.filter((candidate) => !exists(candidate))) {
|
|
672
|
+
violations.push(new CannotImportTargetMissing({ path: grant.path, target }));
|
|
673
|
+
}
|
|
674
|
+
for (const target of grant.cannotImport.filter((candidate) =>
|
|
675
|
+
repeatsSourceNeverImportsScripts(grant.path, candidate),
|
|
676
|
+
)) {
|
|
677
|
+
violations.push(
|
|
678
|
+
new RedundantGrant({
|
|
679
|
+
path: grant.path,
|
|
680
|
+
permission: cannotImportPermission(target),
|
|
681
|
+
reason: `files in ${SOURCE_DIRECTORY}/ cannot import ${SCRIPTS_DIRECTORY}/`,
|
|
682
|
+
}),
|
|
683
|
+
);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return violations;
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
export const patchGrantViolations = (
|
|
690
|
+
manifest: typeof JSON_OBJECT.Type,
|
|
691
|
+
grants: Effect.Success<ReturnType<typeof loadedLintConfigIn>>,
|
|
692
|
+
) => {
|
|
693
|
+
const patchedDependencies = patchedDependenciesOf(manifest);
|
|
694
|
+
if (Result.isFailure(patchedDependencies)) {
|
|
695
|
+
return [
|
|
696
|
+
new PatchedDependenciesMalformed({
|
|
697
|
+
location: PACKAGE_MANIFEST_FILE,
|
|
698
|
+
reason: patchedDependencies.failure.message,
|
|
699
|
+
}),
|
|
700
|
+
];
|
|
701
|
+
}
|
|
702
|
+
const patches = Object.entries(patchedDependencies.success).map(([dependency, patchPath]) => ({
|
|
703
|
+
dependency,
|
|
704
|
+
patchPath: normalizedPath(patchPath),
|
|
705
|
+
}));
|
|
706
|
+
const patchPaths = new Set(patches.map(({ patchPath }) => patchPath));
|
|
707
|
+
const patchGrants = grants.filter((grant) => grant.allow.includes(PATCH));
|
|
708
|
+
const grantedPatchPaths = new Set(patchGrants.map((grant) => grant.path));
|
|
709
|
+
return [
|
|
710
|
+
...patches.flatMap(({ dependency, patchPath }) => {
|
|
711
|
+
const patchesCheckOrTask = [CHECK_PACKAGE, TASK_PACKAGE].some((name) => {
|
|
712
|
+
const namesPackage = dependency === name;
|
|
713
|
+
const namesVersion = dependency.startsWith(`${name}@`);
|
|
714
|
+
return namesPackage || namesVersion;
|
|
715
|
+
});
|
|
716
|
+
const isGrantedPatch =
|
|
717
|
+
grantedPatchPaths.has(patchPath) || grantedPatchPaths.has(directoryOf(patchPath));
|
|
718
|
+
const patchIsAllowed = !patchesCheckOrTask && isGrantedPatch;
|
|
719
|
+
if (patchIsAllowed) {
|
|
720
|
+
return [];
|
|
721
|
+
}
|
|
722
|
+
const fields = { location: PACKAGE_MANIFEST_FILE, dependency, patchPath };
|
|
723
|
+
return [patchesCheckOrTask ? new CheckOrTaskPatched(fields) : new PatchNotGranted(fields)];
|
|
724
|
+
}),
|
|
725
|
+
...patchGrants
|
|
726
|
+
.filter(
|
|
727
|
+
(grant) =>
|
|
728
|
+
!patchPaths.has(grant.path) &&
|
|
729
|
+
![...patchPaths].some((patchPath) => directoryOf(patchPath) === grant.path),
|
|
730
|
+
)
|
|
731
|
+
.map((grant) => new UnlistedPatchGranted({ path: grant.path })),
|
|
732
|
+
];
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
const repositoryOf = (directory: string) =>
|
|
736
|
+
Effect.gen(function* () {
|
|
737
|
+
const repositoryRoot = yield* Task.exec("git", ["rev-parse", "--show-toplevel"], {
|
|
738
|
+
cwd: directory,
|
|
739
|
+
}).pipe(
|
|
740
|
+
Effect.provideService(References.MinimumLogLevel, "Warn"),
|
|
741
|
+
Effect.catchTag("CommandFailed", (failure) =>
|
|
742
|
+
Effect.fail(new RepositoryRootNotFound({ directory, reason: failure.message })),
|
|
743
|
+
),
|
|
744
|
+
);
|
|
745
|
+
const packageDirectory = yield* FileSystem.FileSystem.use((fileSystem) =>
|
|
746
|
+
fileSystem.realPath(directory),
|
|
747
|
+
);
|
|
748
|
+
return { root: repositoryRoot.trim(), packageDirectory };
|
|
749
|
+
});
|
|
750
|
+
|
|
751
|
+
export const findFileLayoutViolations = (directory: string) =>
|
|
752
|
+
Effect.gen(function* () {
|
|
753
|
+
const fileSystem = yield* FileSystem.FileSystem;
|
|
754
|
+
const pathModule = yield* Path.Path;
|
|
755
|
+
const repository = yield* repositoryOf(directory);
|
|
756
|
+
const { packageFiles: paths, nestedPackageDirectories } = yield* listedPackageFiles(
|
|
757
|
+
directory,
|
|
758
|
+
[],
|
|
759
|
+
);
|
|
760
|
+
const grants = yield* loadedLintConfigIn(directory).pipe(
|
|
761
|
+
Effect.asSome,
|
|
762
|
+
Effect.catchTag(
|
|
763
|
+
"LintConfigRefused",
|
|
764
|
+
() => GRANTS_OF_A_REFUSED_CONFIG_ARE_REPORTED_BY_THE_LINT_CONFIG_TASK,
|
|
765
|
+
),
|
|
766
|
+
);
|
|
767
|
+
const rootManifest = yield* rootManifestIn(directory, paths);
|
|
768
|
+
const generated = Option.match(grants, {
|
|
769
|
+
onNone: () => new Set<string>(),
|
|
770
|
+
onSome: generatedPathsOf,
|
|
771
|
+
});
|
|
772
|
+
const ownedPaths = paths.filter((file) => !generated.has(file));
|
|
773
|
+
const listedPatches = listedPatchesOf(rootManifest);
|
|
774
|
+
const isNamedByBunPatch = (path: string) =>
|
|
775
|
+
listedPatches.some(
|
|
776
|
+
({ dependency, patchPath }) =>
|
|
777
|
+
patchPath === path && fileNameOf(path) === bunPatchFileNameOf(dependency),
|
|
778
|
+
) &&
|
|
779
|
+
Option.match(grants, {
|
|
780
|
+
onNone: () => false,
|
|
781
|
+
onSome: (found) => isGrantedPatchFile(path, found),
|
|
782
|
+
});
|
|
783
|
+
const ownedDirectories = new Set(
|
|
784
|
+
distinctDirectoriesOf([...ownedPaths, ...nestedPackageDirectories]),
|
|
785
|
+
);
|
|
786
|
+
const shapeExemptDirectories = Option.match(grants, {
|
|
787
|
+
onNone: () => [],
|
|
788
|
+
onSome: (found) => directoriesExemptFromShapeRules(ownedDirectories, found),
|
|
789
|
+
});
|
|
790
|
+
const isShapeChecked = ({ location }: { readonly location: string }) =>
|
|
791
|
+
!shapeExemptDirectories.some((exempt) => isWithin(location, exempt));
|
|
792
|
+
const grantViolations = Option.match(grants, {
|
|
793
|
+
onNone: () => [],
|
|
794
|
+
onSome: (found) => [
|
|
795
|
+
...permissionGrantViolations(
|
|
796
|
+
[...new Set([...paths, ...generated])],
|
|
797
|
+
found,
|
|
798
|
+
nestedPackageDirectories,
|
|
799
|
+
),
|
|
800
|
+
...patchGrantViolations(rootManifest, found),
|
|
801
|
+
],
|
|
802
|
+
});
|
|
803
|
+
const fileViolations = yield* Effect.forEach(
|
|
804
|
+
paths.filter((file) => !generated.has(file) || isPackageManifest(file)),
|
|
805
|
+
(path) =>
|
|
806
|
+
Effect.gen(function* () {
|
|
807
|
+
const found = pathViolations(path, isNamedByBunPatch(path));
|
|
808
|
+
const isScriptFile = isScript(path);
|
|
809
|
+
const isNeitherScriptNorManifest = !isScriptFile && !isPackageManifest(path);
|
|
810
|
+
if (isNeitherScriptNorManifest) {
|
|
811
|
+
return found;
|
|
812
|
+
}
|
|
813
|
+
const text = yield* fileSystem.readFileString(pathModule.join(directory, path));
|
|
814
|
+
const absoluteDirectory = yield* absolutePathOf(directory);
|
|
815
|
+
const imports = isTaskLayoutFile(path)
|
|
816
|
+
? yield* taskImportsIn(
|
|
817
|
+
pathModule.join(absoluteDirectory, path),
|
|
818
|
+
text,
|
|
819
|
+
absoluteDirectory,
|
|
820
|
+
)
|
|
821
|
+
: undefined;
|
|
822
|
+
return [
|
|
823
|
+
...found,
|
|
824
|
+
...(isScriptFile
|
|
825
|
+
? sourceViolations(path, text, imports)
|
|
826
|
+
: yield* manifestViolations(path, text, repository)),
|
|
827
|
+
];
|
|
828
|
+
}),
|
|
829
|
+
{ concurrency: "unbounded" },
|
|
830
|
+
);
|
|
831
|
+
const violations = [
|
|
832
|
+
...fileViolations.flat(),
|
|
833
|
+
...directoryNameViolations(ownedPaths),
|
|
834
|
+
...directoryShapeViolations(ownedPaths).filter((violation) => isShapeChecked(violation)),
|
|
835
|
+
...directoriesHoldingOnlyADirectory(
|
|
836
|
+
[...new Set([...paths, ...generated])],
|
|
837
|
+
nestedPackageDirectories,
|
|
838
|
+
).filter(
|
|
839
|
+
(violation) => ownedDirectories.has(violation.location) && isShapeChecked(violation),
|
|
840
|
+
),
|
|
841
|
+
...packageRootViolations(ownedPaths),
|
|
842
|
+
...duplicateFileNameViolations(ownedPaths),
|
|
843
|
+
...grantViolations,
|
|
844
|
+
];
|
|
845
|
+
if (violations.length > 0) {
|
|
846
|
+
return yield* new FileLayoutViolations({ violations });
|
|
847
|
+
}
|
|
848
|
+
});
|