@shivaedev/quality 0.52.0 → 0.53.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/CHANGELOG.md +23 -0
- package/README.md +18 -8
- package/biome/declarations.json +0 -5
- package/biome/preset.json +1 -1
- package/dist/cli/lint.d.ts +2 -3
- package/dist/cli/lint.d.ts.map +1 -1
- package/dist/cli/lint.js +4 -2
- package/dist/cli/lint.js.map +1 -1
- package/dist/config/globs.d.ts +17 -0
- package/dist/config/globs.d.ts.map +1 -0
- package/dist/config/globs.js +18 -0
- package/dist/config/globs.js.map +1 -0
- package/dist/engine/runRules.d.ts +2 -1
- package/dist/engine/runRules.d.ts.map +1 -1
- package/dist/engine/runRules.js +4 -1
- package/dist/engine/runRules.js.map +1 -1
- package/dist/engine/session.d.ts +1 -1
- package/dist/engine/session.d.ts.map +1 -1
- package/dist/engine/session.js +9 -5
- package/dist/engine/session.js.map +1 -1
- package/dist/hooks/preCommit.d.ts +2 -1
- package/dist/hooks/preCommit.d.ts.map +1 -1
- package/dist/rule.d.ts +1 -0
- package/dist/rule.d.ts.map +1 -1
- package/dist/rule.js +7 -3
- package/dist/rule.js.map +1 -1
- package/dist/rules/builtIn.d.ts +36 -9
- package/dist/rules/builtIn.d.ts.map +1 -1
- package/dist/rules/files/folderNames.d.ts +8 -2
- package/dist/rules/files/folderNames.d.ts.map +1 -1
- package/dist/rules/files/folderNames.js +7 -5
- package/dist/rules/files/folderNames.js.map +1 -1
- package/dist/rules/files/namedAfterExport.d.ts +4 -1
- package/dist/rules/files/namedAfterExport.d.ts.map +1 -1
- package/dist/rules/files/namedAfterExport.js +6 -7
- package/dist/rules/files/namedAfterExport.js.map +1 -1
- package/dist/rules/files/otherNames.d.ts +8 -2
- package/dist/rules/files/otherNames.d.ts.map +1 -1
- package/dist/rules/files/otherNames.js +7 -5
- package/dist/rules/files/otherNames.js.map +1 -1
- package/dist/rules/stories/authSql.d.ts +4 -1
- package/dist/rules/stories/authSql.d.ts.map +1 -1
- package/dist/rules/stories/authSql.js +6 -3
- package/dist/rules/stories/authSql.js.map +1 -1
- package/dist/rules/stories/collectionOrigins.d.ts +5 -0
- package/dist/rules/stories/collectionOrigins.d.ts.map +1 -0
- package/dist/rules/stories/collectionOrigins.js +17 -0
- package/dist/rules/stories/collectionOrigins.js.map +1 -0
- package/dist/rules/stories/httpStatus.d.ts +4 -1
- package/dist/rules/stories/httpStatus.d.ts.map +1 -1
- package/dist/rules/stories/httpStatus.js +6 -3
- package/dist/rules/stories/httpStatus.js.map +1 -1
- package/dist/rules/stories/namedValues.d.ts.map +1 -1
- package/dist/rules/stories/namedValues.js +5 -5
- package/dist/rules/stories/namedValues.js.map +1 -1
- package/dist/rules/stories/noLimitImports.d.ts.map +1 -1
- package/dist/rules/stories/noLimitImports.js +4 -2
- package/dist/rules/stories/noLimitImports.js.map +1 -1
- package/dist/rules/stories/origins.d.ts +1 -2
- package/dist/rules/stories/origins.d.ts.map +1 -1
- package/dist/rules/stories/origins.js +12 -6
- package/dist/rules/stories/origins.js.map +1 -1
- package/dist/rules/stories/prerequisites.d.ts.map +1 -1
- package/dist/rules/stories/prerequisites.js +6 -24
- package/dist/rules/stories/prerequisites.js.map +1 -1
- package/dist/rules/stories/program.d.ts.map +1 -1
- package/dist/rules/stories/program.js +8 -1
- package/dist/rules/stories/program.js.map +1 -1
- package/dist/rules/stories/sources.d.ts.map +1 -1
- package/dist/rules/stories/sources.js +19 -7
- package/dist/rules/stories/sources.js.map +1 -1
- package/dist/rules/stories/specHelpers.d.ts.map +1 -1
- package/dist/rules/stories/specHelpers.js +4 -2
- package/dist/rules/stories/specHelpers.js.map +1 -1
- package/dist/rules/stories/specs.js +1 -1
- package/dist/rules/stories/specs.js.map +1 -1
- package/dist/rules/stories/syntax.d.ts +0 -1
- package/dist/rules/stories/syntax.d.ts.map +1 -1
- package/dist/rules/stories/syntax.js +0 -4
- package/dist/rules/stories/syntax.js.map +1 -1
- package/dist/rules/test-names/colocated.d.ts +4 -1
- package/dist/rules/test-names/colocated.d.ts.map +1 -1
- package/dist/rules/test-names/colocated.js +5 -3
- package/dist/rules/test-names/colocated.js.map +1 -1
- package/dist/rules/test-names/follow.d.ts +4 -1
- package/dist/rules/test-names/follow.d.ts.map +1 -1
- package/dist/rules/test-names/follow.js +5 -3
- package/dist/rules/test-names/follow.js.map +1 -1
- package/package.json +3 -2
- package/src/cli/lint.ts +33 -27
- package/src/config/globs.ts +31 -0
- package/src/engine/runRules.ts +7 -2
- package/src/engine/session.ts +10 -5
- package/src/rule.ts +12 -2
- package/src/rules/files/folderNames.ts +7 -5
- package/src/rules/files/namedAfterExport.ts +6 -7
- package/src/rules/files/otherNames.ts +7 -5
- package/src/rules/stories/authSql.ts +6 -3
- package/src/rules/stories/collectionOrigins.ts +20 -0
- package/src/rules/stories/httpStatus.ts +6 -3
- package/src/rules/stories/namedValues.ts +7 -5
- package/src/rules/stories/noLimitImports.ts +4 -2
- package/src/rules/stories/origins.ts +12 -8
- package/src/rules/stories/prerequisites.ts +6 -25
- package/src/rules/stories/program.ts +9 -1
- package/src/rules/stories/sources.ts +21 -9
- package/src/rules/stories/specHelpers.ts +4 -2
- package/src/rules/stories/specs.ts +1 -1
- package/src/rules/stories/syntax.ts +0 -5
- package/src/rules/test-names/colocated.ts +5 -3
- package/src/rules/test-names/follow.ts +5 -3
package/src/rule.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Effect, type FileSystem } from "effect";
|
|
2
2
|
import type { ChildProcessSpawner } from "effect/process";
|
|
3
3
|
import type { Bivariant } from "@shivaedev/types/Bivariant.ts";
|
|
4
|
+
import { validOrFail } from "#decoded.ts";
|
|
4
5
|
import type { ExceptionDeclaration } from "#exceptions/declaration.ts";
|
|
5
6
|
import { describeIssue, type StandardSchemaV1 } from "./standardSchema.ts";
|
|
6
7
|
|
|
@@ -57,6 +58,7 @@ interface RuleDefinition<TId extends string, TOptions, TServices> {
|
|
|
57
58
|
readonly description: string;
|
|
58
59
|
readonly id: TId;
|
|
59
60
|
readonly registrable?: false;
|
|
61
|
+
readonly validate?: (context: RuleContext<TOptions>) => readonly string[];
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
interface RuleDefinitionWithOptions<TId extends string, TInput, TOptions, TServices> extends RuleDefinition<TId, TOptions, TServices> {
|
|
@@ -78,7 +80,9 @@ const noOptions: StandardSchemaV1<undefined, undefined> = {
|
|
|
78
80
|
function configureWith<TInput, TOptions, TServices>(
|
|
79
81
|
schema: StandardSchemaV1<TInput, TOptions>,
|
|
80
82
|
check: RuleDefinition<string, TOptions, TServices>["check"],
|
|
83
|
+
id: string,
|
|
81
84
|
absent: unknown,
|
|
85
|
+
validate?: RuleDefinition<string, TOptions, TServices>["validate"],
|
|
82
86
|
) {
|
|
83
87
|
return async (options: unknown): Promise<Configured<TServices>> => {
|
|
84
88
|
const result = await schema["~standard"].validate(options ?? absent);
|
|
@@ -91,6 +95,12 @@ function configureWith<TInput, TOptions, TServices>(
|
|
|
91
95
|
check: (inputs) =>
|
|
92
96
|
Effect.scoped(
|
|
93
97
|
Effect.gen(function* () {
|
|
98
|
+
const context = { ...inputs, options: value };
|
|
99
|
+
const issues = validate?.(context).map((issue) => `rules.${id}.options: ${issue}`) ?? [];
|
|
100
|
+
yield* validOrFail(
|
|
101
|
+
inputs.configFile ?? "quality.config.ts",
|
|
102
|
+
issues.length === 0 ? { _tag: "Valid", value: undefined } : { _tag: "Invalid", issues },
|
|
103
|
+
);
|
|
94
104
|
const signal = yield* Effect.abortSignal;
|
|
95
105
|
const checked = check({ ...inputs, options: value, signal });
|
|
96
106
|
return yield* Effect.isEffect(checked) ? checked : Effect.promise(() => Promise.resolve(checked));
|
|
@@ -111,7 +121,7 @@ export function defineRule<const TId extends string, TInput, TOptions, TServices
|
|
|
111
121
|
): Rule<TId, TInput, TServices> | Rule<TId, undefined, TServices> {
|
|
112
122
|
const configure =
|
|
113
123
|
definition.options === undefined
|
|
114
|
-
? configureWith(noOptions, definition.check, undefined)
|
|
115
|
-
: configureWith(definition.options, definition.check, {});
|
|
124
|
+
? configureWith(noOptions, definition.check, definition.id, undefined, definition.validate)
|
|
125
|
+
: configureWith(definition.options, definition.check, definition.id, {}, definition.validate);
|
|
116
126
|
return { configure, description: definition.description, id: definition.id, registrable: definition.registrable ?? true };
|
|
117
127
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import { Order, Schema } from "effect";
|
|
3
3
|
import ignore, { type Ignore } from "ignore";
|
|
4
|
+
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
4
5
|
import { nativeNames } from "#naming/nativeNames.ts";
|
|
5
6
|
import { packageLayout } from "#naming/packageLayout.ts";
|
|
6
7
|
import { KEBAB, PASCAL } from "#naming/words.ts";
|
|
@@ -11,8 +12,8 @@ const SNAKE = /^[a-z0-9]+(?:_[a-z0-9]+)*$/u;
|
|
|
11
12
|
const CODE = /\.[cm]?[jt]sx?$/u;
|
|
12
13
|
|
|
13
14
|
const FolderNamesOptions = Schema.Struct({
|
|
14
|
-
content:
|
|
15
|
-
toolOwned:
|
|
15
|
+
content: GlobDeclarations,
|
|
16
|
+
toolOwned: GlobDeclarations,
|
|
16
17
|
});
|
|
17
18
|
|
|
18
19
|
function foldersIn(files: readonly string[]): readonly string[] {
|
|
@@ -75,11 +76,11 @@ export const folderNames = defineRule({
|
|
|
75
76
|
check: async ({ files, options, readText }) => {
|
|
76
77
|
const layout = await packageLayout({ files, readText });
|
|
77
78
|
const context: Context = {
|
|
78
|
-
content: ignore().add([...options.content]),
|
|
79
|
+
content: ignore().add([...globPatterns(options.content)]),
|
|
79
80
|
names: layout.names,
|
|
80
81
|
native: nativeNames(files).folders,
|
|
81
82
|
stems: stemsIn(files),
|
|
82
|
-
toolOwned: ignore().add([...options.toolOwned]),
|
|
83
|
+
toolOwned: ignore().add([...globPatterns(options.toolOwned)]),
|
|
83
84
|
};
|
|
84
85
|
return foldersIn(files).flatMap((folder): readonly Finding[] => {
|
|
85
86
|
const message = messageFor(folder, context);
|
|
@@ -90,4 +91,5 @@ export const folderNames = defineRule({
|
|
|
90
91
|
"A package folder is named after its package, a module folder after its main file, a content folder in snake_case and every other folder in kebab-case.",
|
|
91
92
|
id: "files/folder-names",
|
|
92
93
|
options: Schema.toStandardSchemaV1(FolderNamesOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
94
|
+
validate: validateGlobs,
|
|
93
95
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import { Schema } from "effect";
|
|
3
3
|
import ignore from "ignore";
|
|
4
|
+
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
4
5
|
import { exportFits } from "#naming/exportFits.ts";
|
|
5
6
|
import { mainExport } from "#naming/mainExport.ts";
|
|
6
7
|
import { isMigrationFile } from "#naming/migrationFile.ts";
|
|
@@ -28,7 +29,7 @@ const SHAPES =
|
|
|
28
29
|
"The file name and the names of its folders spell the export, in any order, and the file's first letter follows the export's case: listItems in items/ is items/list.ts, ItemPanel is ItemPanel.tsx or item/Panel.tsx.";
|
|
29
30
|
|
|
30
31
|
const NamedAfterExportOptions = Schema.Struct({
|
|
31
|
-
toolOwned:
|
|
32
|
+
toolOwned: GlobDeclarations,
|
|
32
33
|
});
|
|
33
34
|
|
|
34
35
|
function stemOf(path: string): string {
|
|
@@ -52,9 +53,6 @@ function nameFinding(file: SourceFile, stem: string, folders: readonly string[])
|
|
|
52
53
|
? undefined
|
|
53
54
|
: `"${stem}" exports nothing, so it is named in camelCase, such as setupTests.ts. A file that a package.json script or bin runs is kebab-case.`;
|
|
54
55
|
}
|
|
55
|
-
if (exports.length === 1 && only.constant) {
|
|
56
|
-
return `"${stem}" exports only the constant ${only.name}. Constants live in a topic file with related constants, such as limits.ts holding MAX_ITEMS and MAX_DEPTH.`;
|
|
57
|
-
}
|
|
58
56
|
if (exports.some((entry) => exportFits(entry.name, stem, folders))) {
|
|
59
57
|
return undefined;
|
|
60
58
|
}
|
|
@@ -86,13 +84,14 @@ function findingsOf(file: SourceFile, layout: PackageLayout): readonly Finding[]
|
|
|
86
84
|
export const namedAfterExport = defineRule({
|
|
87
85
|
check: async ({ files, options, readText, sources }) => {
|
|
88
86
|
const layout = await packageLayout({ files, readText });
|
|
89
|
-
const toolOwned = ignore().add([...DEFAULT_TOOL_OWNED, ...options.toolOwned]);
|
|
87
|
+
const toolOwned = ignore().add([...DEFAULT_TOOL_OWNED, ...globPatterns(options.toolOwned)]);
|
|
90
88
|
return sources
|
|
91
89
|
.filter((file) => !(TEST.test(file.path) || isMigrationFile(file.path) || toolOwned.ignores(file.path)))
|
|
92
90
|
.flatMap((file) => findingsOf(file, layout));
|
|
93
91
|
},
|
|
94
92
|
description:
|
|
95
|
-
"A file is named after its main export, with its folders as the prefix; a file of several exports is a camelCase topic file, and a script is kebab-case. Rename the file or the export; nothing renames them for you.",
|
|
93
|
+
"A file is named after its main export, including a single constant, with its folders as the prefix; a file of several exports is a camelCase topic file, and a script is kebab-case. Rename the file or the export; nothing renames them for you.",
|
|
96
94
|
id: "files/named-after-export",
|
|
97
95
|
options: Schema.toStandardSchemaV1(NamedAfterExportOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
96
|
+
validate: validateGlobs,
|
|
98
97
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import { Schema } from "effect";
|
|
3
3
|
import ignore, { type Ignore } from "ignore";
|
|
4
|
+
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
4
5
|
import { nativeNames } from "#naming/nativeNames.ts";
|
|
5
6
|
import { CAMEL, KEBAB } from "#naming/words.ts";
|
|
6
7
|
import { defineRule, type Finding } from "#rule.ts";
|
|
@@ -39,8 +40,8 @@ const SNAKE = /^[a-z0-9]+(?:__?[a-z0-9]+)*$/u;
|
|
|
39
40
|
const CODE = /\.[cm]?[jt]sx?$/u;
|
|
40
41
|
|
|
41
42
|
const OtherNamesOptions = Schema.Struct({
|
|
42
|
-
content:
|
|
43
|
-
toolOwned:
|
|
43
|
+
content: GlobDeclarations,
|
|
44
|
+
toolOwned: GlobDeclarations,
|
|
44
45
|
});
|
|
45
46
|
|
|
46
47
|
function stylesheetMessage(path: string, stem: string, importedBy: readonly string[]): string | undefined {
|
|
@@ -88,10 +89,10 @@ function messageFor(path: string, context: Context): string | undefined {
|
|
|
88
89
|
export const otherNames = defineRule({
|
|
89
90
|
check: async ({ files, options, readText, sources }) => {
|
|
90
91
|
const context: Context = {
|
|
91
|
-
content: ignore().add([...options.content]),
|
|
92
|
+
content: ignore().add([...globPatterns(options.content)]),
|
|
92
93
|
native: nativeNames(files),
|
|
93
94
|
stylesheets: await stylesheetImporters({ files, readText, sources }),
|
|
94
|
-
toolOwned: ignore().add([...DEFAULT_TOOL_OWNED, ...options.toolOwned]),
|
|
95
|
+
toolOwned: ignore().add([...DEFAULT_TOOL_OWNED, ...globPatterns(options.toolOwned)]),
|
|
95
96
|
};
|
|
96
97
|
return files.flatMap((path): readonly Finding[] => {
|
|
97
98
|
const message = messageFor(path, context);
|
|
@@ -102,4 +103,5 @@ export const otherNames = defineRule({
|
|
|
102
103
|
"Documents, data, assets and GritQL files are kebab-case, a stylesheet that one component imports is named after it, a Prisma schema is camelCase and content is snake_case.",
|
|
103
104
|
id: "files/other-names",
|
|
104
105
|
options: Schema.toStandardSchemaV1(OtherNamesOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
106
|
+
validate: validateGlobs,
|
|
105
107
|
});
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { matchesGlob } from "node:path";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
2
4
|
import { defineRule } from "#rule.ts";
|
|
3
5
|
import { matchesAny } from "./matchesAny.ts";
|
|
4
6
|
import { sqlTexts } from "./sqlText.ts";
|
|
5
7
|
|
|
6
8
|
const AuthSqlOptions = Schema.Struct({
|
|
7
|
-
owners:
|
|
9
|
+
owners: GlobDeclarations,
|
|
8
10
|
});
|
|
9
11
|
|
|
10
12
|
const TOKENS = /--[^\n]*|\/\*[\s\S]*?\*\/|'(?:''|[^'])*'|"(?:""|[^"])*"|[\w$]+|[.,;()]/gu;
|
|
@@ -126,7 +128,7 @@ function authTableIn(text: string): boolean {
|
|
|
126
128
|
export const authSql = defineRule({
|
|
127
129
|
check: async ({ options, ...inputs }) =>
|
|
128
130
|
(await sqlTexts(inputs))
|
|
129
|
-
.filter((query) => !matchesAny(query.file, options.owners) && authTableIn(query.text))
|
|
131
|
+
.filter((query) => !matchesAny(query.file, globPatterns(options.owners)) && authTableIn(query.text))
|
|
130
132
|
.map((query) => ({
|
|
131
133
|
file: query.file,
|
|
132
134
|
line: query.line,
|
|
@@ -136,4 +138,5 @@ export const authSql = defineRule({
|
|
|
136
138
|
description: "Apps reference user IDs by foreign key and use identity for account data.",
|
|
137
139
|
id: "boundaries/auth-sql",
|
|
138
140
|
options: Schema.toStandardSchemaV1(AuthSqlOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
141
|
+
validate: (context) => validateGlobs(context, matchesGlob),
|
|
139
142
|
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export type Origin = "factory" | "kit" | "story" | "plain" | "suite" | "vitest" | "stories" | "function" | readonly Origin[] | undefined;
|
|
4
|
+
|
|
5
|
+
export function commonOrigin(values: readonly Origin[]): Origin {
|
|
6
|
+
const first = values[0];
|
|
7
|
+
if (values.length === 0 || values.every((value) => value === first)) {
|
|
8
|
+
return first;
|
|
9
|
+
}
|
|
10
|
+
if (typeof first !== "object" || !values.every((value) => typeof value === "object" && value.length === first.length)) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
return first.map((_, index) => commonOrigin(values.map((value) => (typeof value === "object" ? value[index] : undefined))));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function iterationOrigin(node: ts.Node, origin: (node: ts.Node) => Origin): Origin {
|
|
17
|
+
const loop = node.parent.parent;
|
|
18
|
+
const values = ts.isForOfStatement(loop) ? origin(loop.expression) : undefined;
|
|
19
|
+
return typeof values === "object" ? commonOrigin(values) : undefined;
|
|
20
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { matchesGlob } from "node:path";
|
|
2
|
+
import { Schema } from "effect";
|
|
2
3
|
import ts from "typescript";
|
|
4
|
+
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
3
5
|
import { defineRule } from "#rule.ts";
|
|
4
6
|
import { matchesAny } from "./matchesAny.ts";
|
|
5
7
|
import { memberName, nodesWhere, numericValue, syntaxFindings, unwrapped } from "./syntax.ts";
|
|
@@ -8,7 +10,7 @@ const MESSAGE =
|
|
|
8
10
|
"Branches on an HTTP error status outside the HTTP edge. Handle the typed domain error here and translate it to an HTTP status in the configured edge module.";
|
|
9
11
|
|
|
10
12
|
const HttpStatusOptions = Schema.Struct({
|
|
11
|
-
edge:
|
|
13
|
+
edge: GlobDeclarations,
|
|
12
14
|
});
|
|
13
15
|
|
|
14
16
|
const COMPARISONS = new Set([
|
|
@@ -48,9 +50,10 @@ function statusBranch(node: ts.Node): boolean {
|
|
|
48
50
|
export const httpStatus = defineRule({
|
|
49
51
|
check: ({ sources, options }) =>
|
|
50
52
|
sources
|
|
51
|
-
.filter((file) => !matchesAny(file.path, options.edge))
|
|
53
|
+
.filter((file) => !matchesAny(file.path, globPatterns(options.edge)))
|
|
52
54
|
.flatMap((file) => syntaxFindings(file, (source) => nodesWhere(source, statusBranch), MESSAGE)),
|
|
53
55
|
description: "HTTP status numbers belong at the repository's HTTP edge; domain code handles typed errors.",
|
|
54
56
|
id: "boundaries/http-status",
|
|
55
57
|
options: Schema.toStandardSchemaV1(HttpStatusOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
58
|
+
validate: (context) => validateGlobs(context, matchesGlob),
|
|
56
59
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
1
2
|
import ts from "typescript";
|
|
2
3
|
import { defineRule } from "#rule.ts";
|
|
3
|
-
import {
|
|
4
|
+
import { storySources } from "./sources.ts";
|
|
5
|
+
import { memberName, nodesWhere, numericValue, syntaxFindings, unwrapped } from "./syntax.ts";
|
|
4
6
|
|
|
5
7
|
const MESSAGE =
|
|
6
8
|
'Passes a numeric literal or a matcher-only spec-local numeric const to toBe or toHaveLength. Assert the observed items, write the literal where accepted (zero or a process exitCode), or derive the value from the story\'s inputs. For HTTP statuses, import * as HttpStatus from "effect/http/HttpStatus" and use HttpStatus.fromLiteral("NotFound"), for example.';
|
|
@@ -87,10 +89,9 @@ function numericAssertion(node: ts.Node): boolean {
|
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
export const namedValues = defineRule({
|
|
90
|
-
check: (
|
|
91
|
-
|
|
92
|
-
.
|
|
93
|
-
.flatMap((file) =>
|
|
92
|
+
check: (inputs) =>
|
|
93
|
+
Effect.map(storySources(inputs), (stories) =>
|
|
94
|
+
stories.flatMap((file) =>
|
|
94
95
|
syntaxFindings(
|
|
95
96
|
file,
|
|
96
97
|
(source) => {
|
|
@@ -100,6 +101,7 @@ export const namedValues = defineRule({
|
|
|
100
101
|
MESSAGE,
|
|
101
102
|
),
|
|
102
103
|
),
|
|
104
|
+
),
|
|
103
105
|
description: "Stories assert observed items or named values instead of bare counts and numbers.",
|
|
104
106
|
id: "tests/named-values",
|
|
105
107
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
1
2
|
import ts from "typescript";
|
|
2
3
|
import { defineRule } from "#rule.ts";
|
|
3
|
-
import {
|
|
4
|
+
import { storySources } from "./sources.ts";
|
|
5
|
+
import { memberName, nodesWhere, syntaxFindings, unwrapped } from "./syntax.ts";
|
|
4
6
|
|
|
5
7
|
const MESSAGE =
|
|
6
8
|
"Imports a MAX_* constant from a limits module. Tell a story about the observed behavior instead of asserting a copy of the implementation's cap.";
|
|
@@ -74,7 +76,7 @@ function capImports(source: ts.SourceFile): readonly ts.Node[] {
|
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
export const noLimitImports = defineRule({
|
|
77
|
-
check: (
|
|
79
|
+
check: (inputs) => Effect.map(storySources(inputs), (stories) => stories.flatMap((file) => syntaxFindings(file, capImports, MESSAGE))),
|
|
78
80
|
description: "Stories prove behavior instead of importing implementation caps from limits modules.",
|
|
79
81
|
id: "tests/no-limit-imports",
|
|
80
82
|
});
|
|
@@ -2,12 +2,11 @@ import { join } from "node:path";
|
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
import type { ImportGraph } from "#imports/graph.ts";
|
|
4
4
|
import type { RuleInputs } from "#rule.ts";
|
|
5
|
+
import { iterationOrigin, type Origin } from "./collectionOrigins.ts";
|
|
5
6
|
import { importOrigin } from "./importOrigin.ts";
|
|
6
7
|
import { storyProgram } from "./program.ts";
|
|
7
8
|
import { returnValues } from "./returnValues.ts";
|
|
8
9
|
|
|
9
|
-
type Origin = "factory" | "kit" | "story" | "plain" | "suite" | "vitest" | "stories" | "function" | undefined;
|
|
10
|
-
|
|
11
10
|
function importFrom(node: ts.Node): string | undefined {
|
|
12
11
|
for (let current: ts.Node | undefined = node; current !== undefined; current = current.parent) {
|
|
13
12
|
if (ts.isImportDeclaration(current) && ts.isStringLiteral(current.moduleSpecifier)) {
|
|
@@ -27,9 +26,12 @@ export function storyOrigins(inputs: RuleInputs, graph: ImportGraph) {
|
|
|
27
26
|
if (!ts.isVariableDeclaration(node.parent.parent)) {
|
|
28
27
|
return undefined;
|
|
29
28
|
}
|
|
30
|
-
const value = node.parent.parent
|
|
29
|
+
const value = visit(node.parent.parent);
|
|
30
|
+
if (ts.isArrayBindingPattern(node.parent)) {
|
|
31
|
+
return typeof value === "object" ? value[node.parent.elements.indexOf(node)] : undefined;
|
|
32
|
+
}
|
|
31
33
|
const name = node.propertyName ?? node.name;
|
|
32
|
-
return
|
|
34
|
+
return ts.isIdentifier(name) || ts.isStringLiteral(name) ? member(value, name.text) : undefined;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
function declaration(node: ts.Node): Origin {
|
|
@@ -40,7 +42,7 @@ export function storyOrigins(inputs: RuleInputs, graph: ImportGraph) {
|
|
|
40
42
|
return importOrigin(importFrom(node));
|
|
41
43
|
}
|
|
42
44
|
if (ts.isVariableDeclaration(node) || ts.isPropertyAssignment(node)) {
|
|
43
|
-
return node.initializer === undefined ?
|
|
45
|
+
return node.initializer === undefined ? iterationOrigin(node, visit) : visit(node.initializer);
|
|
44
46
|
}
|
|
45
47
|
return localBinding(node);
|
|
46
48
|
}
|
|
@@ -72,8 +74,7 @@ export function storyOrigins(inputs: RuleInputs, graph: ImportGraph) {
|
|
|
72
74
|
return undefined;
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
function member(
|
|
76
|
-
const receiver = visit(value);
|
|
77
|
+
function member(receiver: Origin, name: string): Origin {
|
|
77
78
|
if (receiver === "kit" && name === "it") {
|
|
78
79
|
return "story";
|
|
79
80
|
}
|
|
@@ -99,6 +100,9 @@ export function storyOrigins(inputs: RuleInputs, graph: ImportGraph) {
|
|
|
99
100
|
if (ts.isObjectLiteralExpression(node)) {
|
|
100
101
|
return object(node);
|
|
101
102
|
}
|
|
103
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
104
|
+
return node.elements.map(visit);
|
|
105
|
+
}
|
|
102
106
|
return expression(node);
|
|
103
107
|
}
|
|
104
108
|
|
|
@@ -119,7 +123,7 @@ export function storyOrigins(inputs: RuleInputs, graph: ImportGraph) {
|
|
|
119
123
|
return call(node);
|
|
120
124
|
}
|
|
121
125
|
if (ts.isPropertyAccessExpression(node)) {
|
|
122
|
-
return member(node.expression, node.name.text) ?? symbol(checker.getSymbolAtLocation(node.name));
|
|
126
|
+
return member(visit(node.expression), node.name.text) ?? symbol(checker.getSymbolAtLocation(node.name));
|
|
123
127
|
}
|
|
124
128
|
return ts.isIdentifier(node) ? identifier(node) : declaration(node);
|
|
125
129
|
}
|
|
@@ -1,33 +1,14 @@
|
|
|
1
|
-
import { posix } from "node:path";
|
|
2
1
|
import { readPackageManifest } from "#adapters/manifestInventory.ts";
|
|
3
2
|
import type { WorkspacePackage } from "#imports/workspace.ts";
|
|
4
3
|
import type { RuleInputs } from "#rule.ts";
|
|
5
4
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
function object(value: unknown): value is Record<string, unknown> {
|
|
9
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
-
}
|
|
5
|
+
const PREREQUISITES = new Set(["@shivaedev/test-story", "@shivaedev/test", "@shivaedev/types"]);
|
|
11
6
|
|
|
12
7
|
export async function storyPrerequisites(inputs: RuleInputs, packages: readonly WorkspacePackage[]): Promise<ReadonlySet<string>> {
|
|
13
|
-
const root =
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const directories = new Set<string>();
|
|
18
|
-
while (pending.length > 0) {
|
|
19
|
-
const current = byName.get(pending.pop() ?? "");
|
|
20
|
-
if (current === undefined || directories.has(current.directory)) {
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
directories.add(current.directory);
|
|
24
|
-
const value = (await readPackageManifest(inputs.readText, posix.join(current.directory, "package.json"))) ?? {};
|
|
25
|
-
for (const key of DEPENDENCIES) {
|
|
26
|
-
const dependencies = value[key];
|
|
27
|
-
if (object(dependencies)) {
|
|
28
|
-
pending.push(...Object.keys(dependencies).filter((name) => byName.has(name)));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
8
|
+
const root = await readPackageManifest(inputs.readText, "package.json");
|
|
9
|
+
const directories = packages.filter((member) => PREREQUISITES.has(member.name)).map((member) => member.directory);
|
|
10
|
+
if (typeof root?.name === "string" && PREREQUISITES.has(root.name)) {
|
|
11
|
+
directories.push("");
|
|
31
12
|
}
|
|
32
|
-
return directories;
|
|
13
|
+
return new Set(directories);
|
|
33
14
|
}
|
|
@@ -3,7 +3,13 @@ import ts from "typescript";
|
|
|
3
3
|
import type { ImportGraph } from "#imports/graph.ts";
|
|
4
4
|
import type { RuleInputs } from "#rule.ts";
|
|
5
5
|
|
|
6
|
+
const programs = new WeakMap<RuleInputs["sources"], ts.Program>();
|
|
7
|
+
|
|
6
8
|
export function storyProgram(inputs: RuleInputs, graph: ImportGraph): ts.Program {
|
|
9
|
+
const known = programs.get(inputs.sources);
|
|
10
|
+
if (known !== undefined) {
|
|
11
|
+
return known;
|
|
12
|
+
}
|
|
7
13
|
const sources = new Map(inputs.sources.map((file) => [join(inputs.root, file.path), file.text]));
|
|
8
14
|
const targets = new Map(
|
|
9
15
|
graph.edges.flatMap((edge) =>
|
|
@@ -25,5 +31,7 @@ export function storyProgram(inputs: RuleInputs, graph: ImportGraph): ts.Program
|
|
|
25
31
|
return path === undefined ? undefined : { extension: ts.Extension.Ts, resolvedFileName: path };
|
|
26
32
|
}),
|
|
27
33
|
};
|
|
28
|
-
|
|
34
|
+
const program = ts.createProgram([...sources.keys()], options, host);
|
|
35
|
+
programs.set(inputs.sources, program);
|
|
36
|
+
return program;
|
|
29
37
|
}
|
|
@@ -1,20 +1,32 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { importGraph } from "#imports/graph.ts";
|
|
4
|
+
import { packageOf } from "#imports/workspace.ts";
|
|
3
5
|
import { isTestSupport, testName } from "#naming/testName.ts";
|
|
4
6
|
import type { RuleInputs } from "#rule.ts";
|
|
7
|
+
import { storyOrigins } from "./origins.ts";
|
|
5
8
|
import { storyPrerequisites } from "./prerequisites.ts";
|
|
6
9
|
|
|
7
10
|
export const storySources = Effect.fn("Stories.storySources")(function* (inputs: RuleInputs) {
|
|
8
|
-
const
|
|
11
|
+
const graph = yield* Effect.promise(() => importGraph(inputs));
|
|
12
|
+
const packages = graph.packages;
|
|
13
|
+
const origins = storyOrigins(inputs, graph);
|
|
9
14
|
const exempt = yield* Effect.promise(() => storyPrerequisites(inputs, packages));
|
|
15
|
+
function hasStory(node: ts.Node): boolean {
|
|
16
|
+
return (ts.isCallExpression(node) && origins.origin(node.expression) === "story") || ts.forEachChild(node, hasStory) === true;
|
|
17
|
+
}
|
|
10
18
|
return inputs.sources.filter((file) => {
|
|
11
19
|
const modifiers = testName(file.path)?.modifiers;
|
|
12
|
-
|
|
13
|
-
modifiers
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
)
|
|
20
|
+
if (
|
|
21
|
+
modifiers === undefined
|
|
22
|
+
|| isTestSupport(file.path)
|
|
23
|
+
|| modifiers.includes("typecheck")
|
|
24
|
+
|| modifiers.includes("unit")
|
|
25
|
+
|| exempt.has(packageOf(packages, file.path)?.directory ?? "")
|
|
26
|
+
) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const source = origins.source(file.path);
|
|
30
|
+
return source !== undefined && hasStory(source);
|
|
19
31
|
});
|
|
20
32
|
});
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
1
2
|
import { defineRule } from "#rule.ts";
|
|
2
|
-
import {
|
|
3
|
+
import { storySources } from "./sources.ts";
|
|
4
|
+
import { syntaxFindings, topLevelHelpers } from "./syntax.ts";
|
|
3
5
|
|
|
4
6
|
const MESSAGE = "Declares a top-level helper in a spec. Move the action into the story kit's verbs or the setup into its traits in test-support/.";
|
|
5
7
|
|
|
6
8
|
export const specHelpers = defineRule({
|
|
7
|
-
check: (
|
|
9
|
+
check: (inputs) => Effect.map(storySources(inputs), (stories) => stories.flatMap((file) => syntaxFindings(file, topLevelHelpers, MESSAGE))),
|
|
8
10
|
description: "Specs state expectations; story-kit traits and verbs in test-support own helpers.",
|
|
9
11
|
id: "tests/spec-helpers",
|
|
10
12
|
});
|
|
@@ -95,6 +95,6 @@ export const storySpecs = defineRule({
|
|
|
95
95
|
});
|
|
96
96
|
},
|
|
97
97
|
description:
|
|
98
|
-
"Every package tests its real engine through its own test-story kit, and a plain test lives only in a .unit file that reaches no engine and mocks nothing. Typecheck tests and test-story
|
|
98
|
+
"Every package tests its real engine through its own test-story kit, and a plain test lives only in a .unit file that reaches no engine and mocks nothing. Typecheck tests and the test-story, test and types packages need no kit.",
|
|
99
99
|
id: "tests/story-specs",
|
|
100
100
|
});
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
-
import { isTestSupport, testName } from "#naming/testName.ts";
|
|
3
2
|
import type { Finding, SourceFile } from "#rule.ts";
|
|
4
3
|
import { parse } from "#rules/parse.ts";
|
|
5
4
|
|
|
6
|
-
export function isSpec(path: string): boolean {
|
|
7
|
-
return testName(path)?.kind === "spec" && !isTestSupport(path);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
5
|
export function unwrapped(node: ts.Expression): ts.Expression {
|
|
11
6
|
return ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node) || ts.isNonNullExpression(node)
|
|
12
7
|
? unwrapped(node.expression)
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import { Schema } from "effect";
|
|
3
3
|
import ignore from "ignore";
|
|
4
|
+
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
4
5
|
import { isTestCode, testName } from "#naming/testName.ts";
|
|
5
6
|
import { defineRule, type Finding } from "#rule.ts";
|
|
6
7
|
|
|
7
8
|
const MIRROR_FOLDERS: ReadonlySet<string> = new Set(["__tests__", "spec", "test", "tests"]);
|
|
8
9
|
|
|
9
10
|
const TestsColocatedOptions = Schema.Struct({
|
|
10
|
-
suites:
|
|
11
|
+
suites: GlobDeclarations,
|
|
11
12
|
});
|
|
12
13
|
|
|
13
14
|
function findingOf(path: string, sourceFolders: ReadonlySet<string>): readonly Finding[] {
|
|
@@ -31,7 +32,7 @@ function findingOf(path: string, sourceFolders: ReadonlySet<string>): readonly F
|
|
|
31
32
|
|
|
32
33
|
export const testsColocated = defineRule({
|
|
33
34
|
check: ({ files, options }) => {
|
|
34
|
-
const suites = ignore().add([...options.suites]);
|
|
35
|
+
const suites = ignore().add([...globPatterns(options.suites)]);
|
|
35
36
|
const sourceFolders = new Set(files.filter((path) => !isTestCode(path)).map((path) => posix.dirname(path)));
|
|
36
37
|
return files.filter((path) => !suites.ignores(path)).flatMap((path) => findingOf(path, sourceFolders));
|
|
37
38
|
},
|
|
@@ -39,4 +40,5 @@ export const testsColocated = defineRule({
|
|
|
39
40
|
"A test sits beside the code it covers, at its module root or directly beside its module folder. Outside declared suites, __tests__ is always a test folder; test, tests and spec are test folders only when their directly contained checked files are all tests or test support. Shared test support belongs in test-support/.",
|
|
40
41
|
id: "tests/colocated",
|
|
41
42
|
options: Schema.toStandardSchemaV1(TestsColocatedOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
43
|
+
validate: validateGlobs,
|
|
42
44
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import { Schema } from "effect";
|
|
3
3
|
import ignore from "ignore";
|
|
4
|
+
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
4
5
|
import { ENVIRONMENTS, type TestName, testName } from "#naming/testName.ts";
|
|
5
6
|
import { CAMEL } from "#naming/words.ts";
|
|
6
7
|
import { defineRule, type Finding, type SourceFile } from "#rule.ts";
|
|
@@ -16,7 +17,7 @@ const GRAMMAR =
|
|
|
16
17
|
"A test file is <file>[.<modifier>].test.ts beside its file or <behaviour>[.<modifier>].spec.ts in the folder it covers, with at most one modifier of dom, slow, typecheck and unit.";
|
|
17
18
|
|
|
18
19
|
const TestsFollowOptions = Schema.Struct({
|
|
19
|
-
suites:
|
|
20
|
+
suites: GlobDeclarations,
|
|
20
21
|
});
|
|
21
22
|
|
|
22
23
|
function siblingsIn(files: readonly string[]): ReadonlySet<string> {
|
|
@@ -78,7 +79,7 @@ function findingsOf(source: SourceFile, siblings: ReadonlySet<string>): readonly
|
|
|
78
79
|
|
|
79
80
|
export const testsFollow = defineRule({
|
|
80
81
|
check: ({ files, options, sources }) => {
|
|
81
|
-
const suites = ignore().add([...options.suites]);
|
|
82
|
+
const suites = ignore().add([...globPatterns(options.suites)]);
|
|
82
83
|
const siblings = siblingsIn(files);
|
|
83
84
|
return sources.filter((source) => !suites.ignores(source.path)).flatMap((source) => findingsOf(source, siblings));
|
|
84
85
|
},
|
|
@@ -86,4 +87,5 @@ export const testsFollow = defineRule({
|
|
|
86
87
|
"A .test file follows the file beside it, a .spec names a behaviour of its folder, the only suffix is one modifier of dom, slow, typecheck and unit, and a test that uses the DOM is a .dom test.",
|
|
87
88
|
id: "tests/follow",
|
|
88
89
|
options: Schema.toStandardSchemaV1(TestsFollowOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
90
|
+
validate: validateGlobs,
|
|
89
91
|
});
|