@shivaedev/quality 0.66.0 → 0.67.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 +12 -0
- package/README.md +6 -4
- package/dist/adapters/asymmetricMatcher.d.ts +3 -0
- package/dist/adapters/asymmetricMatcher.d.ts.map +1 -0
- package/dist/adapters/asymmetricMatcher.js +27 -0
- package/dist/adapters/asymmetricMatcher.js.map +1 -0
- package/dist/adapters/knip/configuration.d.ts.map +1 -1
- package/dist/adapters/knip/configuration.js +2 -1
- package/dist/adapters/knip/configuration.js.map +1 -1
- package/dist/adapters/pureReader.d.ts +3 -0
- package/dist/adapters/pureReader.d.ts.map +1 -0
- package/dist/adapters/pureReader.js +52 -0
- package/dist/adapters/pureReader.js.map +1 -0
- package/dist/adapters/readConfig.js +1 -1
- package/dist/adapters/readConfig.js.map +1 -1
- package/dist/adapters/scriptPaths.d.ts +2 -0
- package/dist/adapters/scriptPaths.d.ts.map +1 -0
- package/dist/adapters/scriptPaths.js +5 -0
- package/dist/adapters/scriptPaths.js.map +1 -0
- package/dist/adapters/specReaderSources.d.ts +7 -0
- package/dist/adapters/specReaderSources.d.ts.map +1 -0
- package/dist/adapters/specReaderSources.js +73 -0
- package/dist/adapters/specReaderSources.js.map +1 -0
- package/dist/cli/args.js +2 -2
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/fix.d.ts +0 -1
- package/dist/cli/fix.d.ts.map +1 -1
- package/dist/cli/fix.js +1 -1
- package/dist/cli/fix.js.map +1 -1
- package/dist/cli/limits.d.ts +2 -0
- package/dist/cli/limits.d.ts.map +1 -0
- package/dist/cli/limits.js +2 -0
- package/dist/cli/limits.js.map +1 -0
- package/dist/cli/{options.d.ts → misplaced.d.ts} +1 -1
- package/dist/cli/misplaced.d.ts.map +1 -0
- package/dist/cli/{options.js → misplaced.js} +1 -1
- package/dist/cli/misplaced.js.map +1 -0
- package/dist/cli/program.js +1 -1
- package/dist/cli/program.js.map +1 -1
- package/dist/cli/subcommands.d.ts +2 -0
- package/dist/cli/subcommands.d.ts.map +1 -0
- package/dist/cli/subcommands.js +6 -0
- package/dist/cli/subcommands.js.map +1 -0
- package/dist/cli/{messages.d.ts → usage.d.ts} +1 -2
- package/dist/cli/usage.d.ts.map +1 -0
- package/{src/cli/messages.ts → dist/cli/usage.js} +1 -6
- package/dist/cli/usage.js.map +1 -0
- package/dist/config/defaultExtensions.d.ts +2 -0
- package/dist/config/defaultExtensions.d.ts.map +1 -0
- package/{src/config/constants.ts → dist/config/defaultExtensions.js} +1 -2
- package/dist/config/defaultExtensions.js.map +1 -0
- package/dist/config/file.d.ts +2 -0
- package/dist/config/file.d.ts.map +1 -0
- package/dist/config/file.js +2 -0
- package/dist/config/file.js.map +1 -0
- package/dist/config/load.js +1 -1
- package/dist/config/load.js.map +1 -1
- package/dist/hooks/install.js +1 -1
- package/dist/hooks/install.js.map +1 -1
- package/dist/imports/testComposition.d.ts +4 -0
- package/dist/imports/testComposition.d.ts.map +1 -0
- package/dist/imports/testComposition.js +38 -0
- package/dist/imports/testComposition.js.map +1 -0
- package/dist/naming/adapters/plainData.d.ts +5 -0
- package/dist/naming/adapters/plainData.d.ts.map +1 -0
- package/dist/naming/adapters/plainData.js +77 -0
- package/dist/naming/adapters/plainData.js.map +1 -0
- package/dist/naming/constantModule.d.ts +3 -0
- package/dist/naming/constantModule.d.ts.map +1 -0
- package/dist/naming/constantModule.js +25 -0
- package/dist/naming/constantModule.js.map +1 -0
- package/dist/naming/{migrationFile.d.ts → isMigrationFile.d.ts} +1 -1
- package/dist/naming/isMigrationFile.d.ts.map +1 -0
- package/dist/naming/{migrationFile.js → isMigrationFile.js} +1 -1
- package/dist/naming/isMigrationFile.js.map +1 -0
- package/dist/naming/ownExports.d.ts +0 -1
- package/dist/naming/ownExports.d.ts.map +1 -1
- package/dist/naming/ownExports.js +11 -25
- package/dist/naming/ownExports.js.map +1 -1
- package/dist/naming/packageTests.d.ts +7 -0
- package/dist/naming/packageTests.d.ts.map +1 -0
- package/dist/naming/packageTests.js +21 -0
- package/dist/naming/packageTests.js.map +1 -0
- package/dist/naming/testCode.d.ts +4 -0
- package/dist/naming/testCode.d.ts.map +1 -0
- package/dist/naming/testCode.js +46 -0
- package/dist/naming/testCode.js.map +1 -0
- package/dist/rules/comments/kinds.js +1 -1
- package/dist/rules/comments/kinds.js.map +1 -1
- package/dist/rules/files/namedAfterExport.d.ts.map +1 -1
- package/dist/rules/files/namedAfterExport.js +23 -11
- package/dist/rules/files/namedAfterExport.js.map +1 -1
- package/dist/rules/imports/manifest.d.ts.map +1 -1
- package/dist/rules/imports/manifest.js +9 -11
- package/dist/rules/imports/manifest.js.map +1 -1
- package/dist/rules/imports/noTestCode.d.ts.map +1 -1
- package/dist/rules/imports/noTestCode.js +13 -9
- package/dist/rules/imports/noTestCode.js.map +1 -1
- package/dist/rules/maxLines.d.ts.map +1 -1
- package/dist/rules/maxLines.js +7 -2
- package/dist/rules/maxLines.js.map +1 -1
- package/dist/rules/stories/specHelpers.d.ts.map +1 -1
- package/dist/rules/stories/specHelpers.js +8 -4
- package/dist/rules/stories/specHelpers.js.map +1 -1
- package/dist/rules/suppressions/{directives.d.ts → in.d.ts} +1 -1
- package/dist/rules/suppressions/in.d.ts.map +1 -0
- package/dist/rules/suppressions/{directives.js → in.js} +1 -1
- package/dist/rules/suppressions/in.js.map +1 -0
- package/dist/rules/suppressions/noInline.js +1 -1
- package/dist/rules/suppressions/noInline.js.map +1 -1
- package/dist/rules/test-names/colocated.d.ts.map +1 -1
- package/dist/rules/test-names/colocated.js +7 -4
- package/dist/rules/test-names/colocated.js.map +1 -1
- package/dist/rules/test-names/follow.d.ts.map +1 -1
- package/dist/rules/test-names/follow.js +13 -6
- package/dist/rules/test-names/follow.js.map +1 -1
- package/package.json +3 -3
- package/src/adapters/asymmetricMatcher.ts +33 -0
- package/src/adapters/knip/configuration.ts +2 -5
- package/src/adapters/pureReader.ts +56 -0
- package/src/adapters/readConfig.ts +1 -1
- package/src/adapters/scriptPaths.ts +5 -0
- package/src/adapters/specReaderSources.ts +77 -0
- package/src/cli/args.ts +2 -2
- package/src/cli/fix.ts +1 -2
- package/src/cli/limits.ts +1 -0
- package/src/cli/program.ts +1 -1
- package/src/cli/subcommands.ts +5 -0
- package/{dist/cli/messages.js → src/cli/usage.ts} +0 -6
- package/{dist/config/constants.js → src/config/defaultExtensions.ts} +0 -2
- package/src/config/file.ts +1 -0
- package/src/config/load.ts +1 -1
- package/src/hooks/install.ts +1 -1
- package/src/imports/testComposition.ts +47 -0
- package/src/naming/adapters/plainData.ts +88 -0
- package/src/naming/constantModule.ts +28 -0
- package/src/naming/ownExports.ts +11 -28
- package/src/naming/packageTests.ts +24 -0
- package/src/naming/testCode.ts +62 -0
- package/src/rules/comments/kinds.ts +1 -1
- package/src/rules/files/namedAfterExport.ts +28 -11
- package/src/rules/imports/manifest.ts +18 -10
- package/src/rules/imports/noTestCode.ts +13 -12
- package/src/rules/maxLines.ts +7 -3
- package/src/rules/stories/specHelpers.ts +14 -4
- package/src/rules/suppressions/noInline.ts +1 -1
- package/src/rules/test-names/colocated.ts +7 -4
- package/src/rules/test-names/follow.ts +13 -6
- package/dist/cli/messages.d.ts.map +0 -1
- package/dist/cli/messages.js.map +0 -1
- package/dist/cli/options.d.ts.map +0 -1
- package/dist/cli/options.js.map +0 -1
- package/dist/config/constants.d.ts +0 -3
- package/dist/config/constants.d.ts.map +0 -1
- package/dist/config/constants.js.map +0 -1
- package/dist/naming/migrationFile.d.ts.map +0 -1
- package/dist/naming/migrationFile.js.map +0 -1
- package/dist/rules/suppressions/directives.d.ts.map +0 -1
- package/dist/rules/suppressions/directives.js.map +0 -1
- /package/src/cli/{options.ts → misplaced.ts} +0 -0
- /package/src/naming/{migrationFile.ts → isMigrationFile.ts} +0 -0
- /package/src/rules/suppressions/{directives.ts → in.ts} +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { unwrapped } from "#rules/stories/syntax.ts";
|
|
3
|
+
|
|
4
|
+
function functionOf(name: ts.Node): ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression | undefined {
|
|
5
|
+
const declaration = name.parent;
|
|
6
|
+
if (ts.isFunctionDeclaration(declaration)) {
|
|
7
|
+
return declaration;
|
|
8
|
+
}
|
|
9
|
+
const value = ts.isVariableDeclaration(declaration) ? declaration.initializer : undefined;
|
|
10
|
+
const node = value === undefined ? undefined : unwrapped(value);
|
|
11
|
+
return node !== undefined && (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) ? node : undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function action(node: ts.Node, readCall: (call: ts.CallExpression) => boolean): boolean {
|
|
15
|
+
if (
|
|
16
|
+
((ts.isArrowFunction(node) || ts.isFunctionExpression(node) || ts.isFunctionDeclaration(node))
|
|
17
|
+
&& (node.asteriskToken !== undefined || node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)))
|
|
18
|
+
|| ts.isJsxElement(node)
|
|
19
|
+
|| ts.isJsxSelfClosingElement(node)
|
|
20
|
+
|| ts.isJsxFragment(node)
|
|
21
|
+
|| ts.isAwaitExpression(node)
|
|
22
|
+
|| ts.isYieldExpression(node)
|
|
23
|
+
|| ts.isNewExpression(node)
|
|
24
|
+
|| ts.isThrowStatement(node)
|
|
25
|
+
|| ts.isDeleteExpression(node)
|
|
26
|
+
|| ts.isPostfixUnaryExpression(node)
|
|
27
|
+
|| (ts.isPrefixUnaryExpression(node) && [ts.SyntaxKind.PlusPlusToken, ts.SyntaxKind.MinusMinusToken].includes(node.operator))
|
|
28
|
+
|| (ts.isBinaryExpression(node)
|
|
29
|
+
&& node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment
|
|
30
|
+
&& node.operatorToken.kind <= ts.SyntaxKind.LastAssignment)
|
|
31
|
+
) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return ts.isCallExpression(node) && !readCall(node);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function pureReader(name: ts.Node, readCall: (call: ts.CallExpression) => boolean): boolean {
|
|
38
|
+
const fn = functionOf(name);
|
|
39
|
+
if (fn?.body === undefined || fn.asteriskToken !== undefined || fn.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
let returns = !ts.isBlock(fn.body);
|
|
43
|
+
let acts = false;
|
|
44
|
+
let reads = false;
|
|
45
|
+
function visit(node: ts.Node): void {
|
|
46
|
+
returns ||= ts.isReturnStatement(node) && node.expression !== undefined;
|
|
47
|
+
acts ||= action(node, readCall);
|
|
48
|
+
reads ||= ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) || ts.isBinaryExpression(node);
|
|
49
|
+
ts.forEachChild(node, visit);
|
|
50
|
+
}
|
|
51
|
+
for (const parameter of fn.parameters) {
|
|
52
|
+
visit(parameter);
|
|
53
|
+
}
|
|
54
|
+
visit(fn.body);
|
|
55
|
+
return returns && reads && !acts;
|
|
56
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { basename, dirname, resolve } from "node:path";
|
|
2
2
|
import { pathToFileURL } from "node:url";
|
|
3
3
|
import { Effect, FileSystem } from "effect";
|
|
4
|
-
import { CONFIG_FILE } from "#config/
|
|
4
|
+
import { CONFIG_FILE } from "#config/file.ts";
|
|
5
5
|
import { SetupFailure } from "#failure.ts";
|
|
6
6
|
|
|
7
7
|
function messageOf(cause: unknown): string {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
|
|
3
|
+
export function scriptPaths(directory: string, scripts: Readonly<Record<string, string>>): readonly string[] {
|
|
4
|
+
return Object.values(scripts).flatMap((script) => script.split(/[\s'"`;|&()]+/u).map((word) => posix.join(directory, word)));
|
|
5
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { SourceFile } from "#rule.ts";
|
|
3
|
+
import { parse } from "#rules/parse.ts";
|
|
4
|
+
import { topLevelHelpers, unwrapped } from "#rules/stories/syntax.ts";
|
|
5
|
+
import { asymmetricMatcher } from "./asymmetricMatcher.ts";
|
|
6
|
+
|
|
7
|
+
const READ_METHODS = new Set([
|
|
8
|
+
"at",
|
|
9
|
+
"concat",
|
|
10
|
+
"endsWith",
|
|
11
|
+
"entries",
|
|
12
|
+
"every",
|
|
13
|
+
"filter",
|
|
14
|
+
"find",
|
|
15
|
+
"findIndex",
|
|
16
|
+
"flat",
|
|
17
|
+
"flatMap",
|
|
18
|
+
"get",
|
|
19
|
+
"has",
|
|
20
|
+
"includes",
|
|
21
|
+
"indexOf",
|
|
22
|
+
"join",
|
|
23
|
+
"keys",
|
|
24
|
+
"lastIndexOf",
|
|
25
|
+
"map",
|
|
26
|
+
"match",
|
|
27
|
+
"reduce",
|
|
28
|
+
"replace",
|
|
29
|
+
"slice",
|
|
30
|
+
"some",
|
|
31
|
+
"split",
|
|
32
|
+
"startsWith",
|
|
33
|
+
"substring",
|
|
34
|
+
"toLowerCase",
|
|
35
|
+
"toUpperCase",
|
|
36
|
+
"trim",
|
|
37
|
+
"values",
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
export function specReaderSources(files: readonly SourceFile[]) {
|
|
41
|
+
const sources = files.flatMap((file) => {
|
|
42
|
+
const source = parse(file);
|
|
43
|
+
return source === undefined || topLevelHelpers(source).length === 0 ? [] : [source];
|
|
44
|
+
});
|
|
45
|
+
if (sources.length === 0) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const options: ts.CompilerOptions = { allowJs: true, noResolve: true, target: ts.ScriptTarget.ESNext, types: [] };
|
|
49
|
+
const host = ts.createCompilerHost(options);
|
|
50
|
+
const getSourceFile = host.getSourceFile;
|
|
51
|
+
const parsed = new Map(sources.map((source) => [source.fileName, source]));
|
|
52
|
+
host.getSourceFile = (path, ...args) => parsed.get(path) ?? getSourceFile(path, ...args);
|
|
53
|
+
const program = ts.createProgram([...parsed.keys()], options, host);
|
|
54
|
+
const checker = program.getTypeChecker();
|
|
55
|
+
function readCall(call: ts.CallExpression): boolean {
|
|
56
|
+
if (asymmetricMatcher(call, checker)) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
const target = unwrapped(call.expression);
|
|
60
|
+
if (!(ts.isPropertyAccessExpression(target) && READ_METHODS.has(target.name.text))) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const declaration = checker.getResolvedSignature(call)?.declaration;
|
|
64
|
+
return (
|
|
65
|
+
declaration !== undefined
|
|
66
|
+
&& program.isSourceFileDefaultLibrary(declaration.getSourceFile())
|
|
67
|
+
&& call.arguments.every((argument) => {
|
|
68
|
+
if (ts.isArrowFunction(argument) || ts.isFunctionExpression(argument)) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
const type = checker.getTypeAtLocation(argument);
|
|
72
|
+
return (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) === 0 && type.getCallSignatures().length === 0;
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return sources.map((source) => ({ readCall, source }));
|
|
77
|
+
}
|
package/src/cli/args.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { parseArgs } from "node:util";
|
|
2
2
|
import { Result } from "effect";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { ACCEPTS, misplaced, OPTIONS, type Values } from "./misplaced.ts";
|
|
4
|
+
import { SUBCOMMANDS } from "./subcommands.ts";
|
|
5
5
|
|
|
6
6
|
export type Command =
|
|
7
7
|
| { readonly _tag: "Ci"; readonly coverage: boolean }
|
package/src/cli/fix.ts
CHANGED
|
@@ -10,8 +10,7 @@ import { walk } from "#inventory/walk.ts";
|
|
|
10
10
|
import { sortManifests } from "#manifests/sorted.ts";
|
|
11
11
|
import { plural } from "#report/plural.ts";
|
|
12
12
|
import type { RuleServices } from "#rule.ts";
|
|
13
|
-
|
|
14
|
-
export const MAX_FIX_ROUNDS = 5;
|
|
13
|
+
import { MAX_FIX_ROUNDS } from "./limits.ts";
|
|
15
14
|
|
|
16
15
|
function isBinary(bytes: Uint8Array): boolean {
|
|
17
16
|
return bytes.includes(0);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const MAX_FIX_ROUNDS = 5;
|
package/src/cli/program.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { preCommitHook } from "#hooks/preCommit.ts";
|
|
|
18
18
|
import { setupHook } from "#hooks/setup.ts";
|
|
19
19
|
import { hookStatus } from "#hooks/status.ts";
|
|
20
20
|
import { uninstallHook } from "#hooks/uninstall.ts";
|
|
21
|
-
import { USAGE } from "./
|
|
21
|
+
import { USAGE } from "./usage.ts";
|
|
22
22
|
|
|
23
23
|
export interface Invocation {
|
|
24
24
|
readonly cwd: string;
|
|
@@ -39,9 +39,3 @@ hooks pre-commit Check lint and staged baseline counts, then package.json scrip
|
|
|
39
39
|
--against <ref> The branch the work merges into, whose merge base prune follows moves from. Defaults to origin/HEAD, then origin/main, then origin/master.
|
|
40
40
|
--coverage Collect V8 coverage and JUnit from ci's single test run into coverage/, including failed tests.
|
|
41
41
|
Exit codes: 0 passed, 1 failed the gate, 2 could not run.`;
|
|
42
|
-
export const SUBCOMMANDS = {
|
|
43
|
-
baseline: "baseline takes write, prune, tighten, migrate, merge or install-driver.",
|
|
44
|
-
exceptions: "exceptions takes migrate.",
|
|
45
|
-
hooks: "hooks takes install, uninstall, status or pre-commit.",
|
|
46
|
-
};
|
|
47
|
-
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CONFIG_FILE = "quality.config.ts";
|
package/src/config/load.ts
CHANGED
|
@@ -7,8 +7,8 @@ import type { SetupFailure } from "#failure.ts";
|
|
|
7
7
|
import type { InventoryScope } from "#inventory/collect.ts";
|
|
8
8
|
import { ConsumerDeclaration } from "#rules/suppressions/biome/ConsumerDeclaration.ts";
|
|
9
9
|
import type { Declaration } from "#rules/suppressions/biome/declarations.ts";
|
|
10
|
-
import { DEFAULT_EXTENSIONS } from "./constants.ts";
|
|
11
10
|
import { type ConfigInput, decodeConfig } from "./decode.ts";
|
|
11
|
+
import { DEFAULT_EXTENSIONS } from "./defaultExtensions.ts";
|
|
12
12
|
import { type ResolvedRules, resolveRules } from "./resolve.ts";
|
|
13
13
|
|
|
14
14
|
export interface ResolvedConfig extends ResolvedRules, InventoryScope {
|
package/src/hooks/install.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as NodePath from "@effect/platform-node/NodePath";
|
|
2
2
|
import { Console, Effect, FileSystem, Path } from "effect";
|
|
3
|
-
import { CONFIG_FILE } from "#config/
|
|
3
|
+
import { CONFIG_FILE } from "#config/file.ts";
|
|
4
4
|
import { loadConfig } from "#config/load.ts";
|
|
5
5
|
import { SetupFailure } from "#failure.ts";
|
|
6
6
|
import { gitOrFail } from "#git/command.ts";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ImportGraph } from "./graph.ts";
|
|
2
|
+
import { packageOf, type WorkspacePackage } from "./workspace.ts";
|
|
3
|
+
|
|
4
|
+
export function testComposition(graph: ImportGraph, privatePackages: readonly WorkspacePackage[], testFile: (path: string) => boolean) {
|
|
5
|
+
const candidates = new Set(privatePackages);
|
|
6
|
+
const imports = graph.edges.flatMap((edge) => {
|
|
7
|
+
const from = packageOf(graph.packages, edge.from);
|
|
8
|
+
const to = edge.to.kind === "file" ? packageOf(graph.packages, edge.to.path) : undefined;
|
|
9
|
+
return to === undefined || from === to ? [] : [{ from, path: edge.from, to }];
|
|
10
|
+
});
|
|
11
|
+
return compositions(imports, candidates, testFile);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Consumer {
|
|
15
|
+
readonly from: WorkspacePackage | undefined;
|
|
16
|
+
readonly path: string;
|
|
17
|
+
readonly to: WorkspacePackage;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function compositions(imports: readonly Consumer[], candidates: ReadonlySet<WorkspacePackage>, testFile: (path: string) => boolean) {
|
|
21
|
+
const reached = new Set<WorkspacePackage>();
|
|
22
|
+
let changed = true;
|
|
23
|
+
while (changed) {
|
|
24
|
+
changed = false;
|
|
25
|
+
for (const edge of imports) {
|
|
26
|
+
if (candidates.has(edge.to) && !reached.has(edge.to) && (testFile(edge.path) || (edge.from !== undefined && reached.has(edge.from)))) {
|
|
27
|
+
reached.add(edge.to);
|
|
28
|
+
changed = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return withoutProductionConsumers(imports, reached, testFile);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function withoutProductionConsumers(imports: readonly Consumer[], reached: Set<WorkspacePackage>, testFile: (path: string) => boolean) {
|
|
36
|
+
let changed = true;
|
|
37
|
+
while (changed) {
|
|
38
|
+
changed = false;
|
|
39
|
+
for (const edge of imports) {
|
|
40
|
+
if (reached.has(edge.to) && !testFile(edge.path) && (edge.from === undefined || !reached.has(edge.from))) {
|
|
41
|
+
reached.delete(edge.to);
|
|
42
|
+
changed = true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return reached;
|
|
47
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
const CONSTANT_CASE = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/u;
|
|
4
|
+
|
|
5
|
+
const LITERALS: ReadonlySet<ts.SyntaxKind> = new Set([
|
|
6
|
+
ts.SyntaxKind.BigIntLiteral,
|
|
7
|
+
ts.SyntaxKind.FalseKeyword,
|
|
8
|
+
ts.SyntaxKind.NoSubstitutionTemplateLiteral,
|
|
9
|
+
ts.SyntaxKind.NullKeyword,
|
|
10
|
+
ts.SyntaxKind.NumericLiteral,
|
|
11
|
+
ts.SyntaxKind.RegularExpressionLiteral,
|
|
12
|
+
ts.SyntaxKind.StringLiteral,
|
|
13
|
+
ts.SyntaxKind.TrueKeyword,
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
export function plainData(node: ts.Expression | undefined): boolean {
|
|
17
|
+
if (node === undefined) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isTypeAssertionExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
21
|
+
return plainData(node.expression);
|
|
22
|
+
}
|
|
23
|
+
if (ts.isIdentifier(node)) {
|
|
24
|
+
return CONSTANT_CASE.test(node.text);
|
|
25
|
+
}
|
|
26
|
+
if (ts.isPrefixUnaryExpression(node)) {
|
|
27
|
+
return (
|
|
28
|
+
(node.operator === ts.SyntaxKind.PlusToken || node.operator === ts.SyntaxKind.MinusToken)
|
|
29
|
+
&& (ts.isNumericLiteral(node.operand) || ts.isBigIntLiteral(node.operand))
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
33
|
+
return node.elements.every(plainData);
|
|
34
|
+
}
|
|
35
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
36
|
+
return node.properties.every((property) =>
|
|
37
|
+
ts.isPropertyAssignment(property)
|
|
38
|
+
? !ts.isComputedPropertyName(property.name) && plainData(property.initializer)
|
|
39
|
+
: ts.isShorthandPropertyAssignment(property) && CONSTANT_CASE.test(property.name.text),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return LITERALS.has(node.kind);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function constantType(node: ts.TypeNode, constants: readonly string[]): boolean {
|
|
46
|
+
let queries = 0;
|
|
47
|
+
let valid = true;
|
|
48
|
+
function visit(child: ts.Node): void {
|
|
49
|
+
if (ts.isTypeQueryNode(child)) {
|
|
50
|
+
queries += 1;
|
|
51
|
+
let name = child.exprName;
|
|
52
|
+
while (ts.isQualifiedName(name)) {
|
|
53
|
+
name = name.left;
|
|
54
|
+
}
|
|
55
|
+
valid &&= constants.includes(name.text);
|
|
56
|
+
} else {
|
|
57
|
+
ts.forEachChild(child, visit);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
visit(node);
|
|
61
|
+
return valid && queries > 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function knownDataExports(source: ts.SourceFile, names: readonly string[]): boolean {
|
|
65
|
+
return source.statements.every((statement) => {
|
|
66
|
+
if (ts.isExportDeclaration(statement)) {
|
|
67
|
+
return (
|
|
68
|
+
statement.moduleSpecifier === undefined
|
|
69
|
+
&& statement.exportClause !== undefined
|
|
70
|
+
&& ts.isNamedExports(statement.exportClause)
|
|
71
|
+
&& statement.exportClause.elements.every((entry) =>
|
|
72
|
+
names.includes(entry.name.text === "default" ? (entry.propertyName ?? entry.name).text : entry.name.text),
|
|
73
|
+
)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
if (ts.isExportAssignment(statement)) {
|
|
77
|
+
return plainData(statement.expression);
|
|
78
|
+
}
|
|
79
|
+
const exported = ts.canHaveModifiers(statement) && ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
80
|
+
if (!exported) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if (ts.isVariableStatement(statement)) {
|
|
84
|
+
return statement.declarationList.declarations.every((entry) => ts.isIdentifier(entry.name));
|
|
85
|
+
}
|
|
86
|
+
return !(ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { OwnExport } from "#naming/ownExports.ts";
|
|
2
|
+
import { CAMEL, sameWord, wordsOf } from "#naming/words.ts";
|
|
3
|
+
|
|
4
|
+
function sharedWords(constants: readonly OwnExport[]): readonly string[] {
|
|
5
|
+
return wordsOf(constants[0]?.name ?? "").filter((word) => constants.every((entry) => wordsOf(entry.name).some((own) => sameWord(own, word))));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function fits(stem: string, constants: readonly OwnExport[]): boolean {
|
|
9
|
+
if (!CAMEL.test(stem)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const words = wordsOf(stem);
|
|
13
|
+
return constants.every((entry) => {
|
|
14
|
+
const own = wordsOf(entry.name);
|
|
15
|
+
return stem === "limits"
|
|
16
|
+
? own.some((word) => ["max", "min", "limit"].some((limit) => sameWord(word, limit)))
|
|
17
|
+
: words.every((word) => own.some((constant) => sameWord(word, constant)));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function constantModule(stem: string, constants: readonly OwnExport[]): string | undefined {
|
|
22
|
+
if (fits(stem, constants)) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const candidates = sharedWords(constants);
|
|
26
|
+
const suggestion = candidates.length === 0 ? "" : ` Shared words: ${candidates.join(", ")}.`;
|
|
27
|
+
return `"${stem}" holds only plain-data constants. Name the module in camelCase after a word each constant carries, such as timeouts.ts for INSPECT_TIMEOUT_MILLIS and REMOTE_TIMEOUT_MILLIS, or split it.${suggestion} Put MAX_/MIN_ constants in limits.ts and DEFAULT_ constants in defaults.ts; every constant must fit the name.`;
|
|
28
|
+
}
|
package/src/naming/ownExports.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
+
import { constantType, plainData } from "#naming/adapters/plainData.ts";
|
|
2
3
|
|
|
3
4
|
export interface OwnExport {
|
|
4
5
|
readonly constant: boolean;
|
|
@@ -6,27 +7,6 @@ export interface OwnExport {
|
|
|
6
7
|
readonly name: string;
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
const CONSTANT_CASE = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/u;
|
|
10
|
-
|
|
11
|
-
const PRIMITIVE_LITERALS: ReadonlySet<ts.SyntaxKind> = new Set([
|
|
12
|
-
ts.SyntaxKind.BigIntLiteral,
|
|
13
|
-
ts.SyntaxKind.FalseKeyword,
|
|
14
|
-
ts.SyntaxKind.NoSubstitutionTemplateLiteral,
|
|
15
|
-
ts.SyntaxKind.NumericLiteral,
|
|
16
|
-
ts.SyntaxKind.StringLiteral,
|
|
17
|
-
ts.SyntaxKind.TrueKeyword,
|
|
18
|
-
]);
|
|
19
|
-
|
|
20
|
-
function isPrimitiveLiteral(node: ts.Expression | undefined): boolean {
|
|
21
|
-
if (node === undefined) {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
if (ts.isPrefixUnaryExpression(node)) {
|
|
25
|
-
return isPrimitiveLiteral(node.operand);
|
|
26
|
-
}
|
|
27
|
-
return PRIMITIVE_LITERALS.has(node.kind);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
10
|
function isExported(statement: ts.Statement): boolean {
|
|
31
11
|
const modifiers = ts.canHaveModifiers(statement) ? ts.getModifiers(statement) : undefined;
|
|
32
12
|
return modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword) ?? false;
|
|
@@ -38,7 +18,7 @@ function variables(statement: ts.VariableStatement): readonly OwnExport[] {
|
|
|
38
18
|
ts.isIdentifier(declaration.name)
|
|
39
19
|
? [
|
|
40
20
|
{
|
|
41
|
-
constant: constant && (
|
|
21
|
+
constant: constant && plainData(declaration.initializer),
|
|
42
22
|
kind: "value" as const,
|
|
43
23
|
name: declaration.name.text,
|
|
44
24
|
},
|
|
@@ -47,12 +27,12 @@ function variables(statement: ts.VariableStatement): readonly OwnExport[] {
|
|
|
47
27
|
);
|
|
48
28
|
}
|
|
49
29
|
|
|
50
|
-
function declared(statement: ts.Statement): readonly OwnExport[] {
|
|
30
|
+
function declared(statement: ts.Statement, constants: readonly string[] = []): readonly OwnExport[] {
|
|
51
31
|
if (ts.isVariableStatement(statement)) {
|
|
52
32
|
return variables(statement);
|
|
53
33
|
}
|
|
54
34
|
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement)) {
|
|
55
|
-
return [{ constant:
|
|
35
|
+
return [{ constant: ts.isTypeAliasDeclaration(statement) && constantType(statement.type, constants), kind: "type", name: statement.name.text }];
|
|
56
36
|
}
|
|
57
37
|
const named =
|
|
58
38
|
ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isEnumDeclaration(statement) || ts.isModuleDeclaration(statement)
|
|
@@ -61,8 +41,10 @@ function declared(statement: ts.Statement): readonly OwnExport[] {
|
|
|
61
41
|
return named !== undefined && ts.isIdentifier(named) ? [{ constant: false, kind: "value", name: named.text }] : [];
|
|
62
42
|
}
|
|
63
43
|
|
|
64
|
-
|
|
65
|
-
|
|
44
|
+
function ownDeclarations(source: ts.SourceFile): readonly OwnExport[] {
|
|
45
|
+
const values = source.statements.flatMap((statement) => (ts.isVariableStatement(statement) ? variables(statement) : []));
|
|
46
|
+
const constants = values.filter((entry) => entry.constant).map((entry) => entry.name);
|
|
47
|
+
return source.statements.flatMap((statement) => declared(statement, constants));
|
|
66
48
|
}
|
|
67
49
|
|
|
68
50
|
function locals(source: ts.SourceFile): ReadonlyMap<string, OwnExport> {
|
|
@@ -108,7 +90,8 @@ function defaulted(statement: ts.ExportAssignment, known: ReadonlyMap<string, Ow
|
|
|
108
90
|
if (name === undefined) {
|
|
109
91
|
return [];
|
|
110
92
|
}
|
|
111
|
-
|
|
93
|
+
const local = known.get(name);
|
|
94
|
+
return [{ constant: local?.constant === true && plainData(statement.expression), kind: local?.kind ?? "value", name }];
|
|
112
95
|
}
|
|
113
96
|
|
|
114
97
|
function exportsOf(statement: ts.Statement, known: ReadonlyMap<string, OwnExport>): readonly OwnExport[] {
|
|
@@ -118,7 +101,7 @@ function exportsOf(statement: ts.Statement, known: ReadonlyMap<string, OwnExport
|
|
|
118
101
|
if (ts.isExportAssignment(statement)) {
|
|
119
102
|
return defaulted(statement, known);
|
|
120
103
|
}
|
|
121
|
-
return isExported(statement) ? declared(statement) : [];
|
|
104
|
+
return isExported(statement) ? declared(statement).map((entry) => known.get(entry.name) ?? entry) : [];
|
|
122
105
|
}
|
|
123
106
|
|
|
124
107
|
export function ownExports(source: ts.SourceFile): readonly OwnExport[] {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Effect, Path } from "effect";
|
|
2
|
+
import { readPackageManifest } from "#adapters/manifestInventory.ts";
|
|
3
|
+
import { packageOf, workspacePackages } from "#imports/workspace.ts";
|
|
4
|
+
import type { RuleInputs } from "#rule.ts";
|
|
5
|
+
|
|
6
|
+
export const packageTests = Effect.fn("Tests.packageTests")(function* (inputs: RuleInputs) {
|
|
7
|
+
const posix = yield* Path.Path.pipe(Effect.provide(Path.layer));
|
|
8
|
+
const packages = [...(yield* Effect.promise(() => workspacePackages(inputs)))];
|
|
9
|
+
if ((yield* Effect.promise(() => readPackageManifest(inputs.readText, "package.json"))) !== undefined) {
|
|
10
|
+
packages.push({ directory: ".", name: "." });
|
|
11
|
+
}
|
|
12
|
+
const withSource = new Set(
|
|
13
|
+
packages.filter((owner) => inputs.files.some((path) => path.startsWith(`${owner.directory === "." ? "" : `${owner.directory}/`}src/`))),
|
|
14
|
+
);
|
|
15
|
+
function sourcePath(path: string): string | undefined {
|
|
16
|
+
const owner = packageOf(packages, path) ?? packages.find((candidate) => candidate.directory === ".");
|
|
17
|
+
if (owner === undefined || !withSource.has(owner)) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const local = posix.relative(owner.directory, path);
|
|
21
|
+
return local.startsWith("test/") ? posix.join(owner.directory, "src", local.slice("test/".length)) : undefined;
|
|
22
|
+
}
|
|
23
|
+
return { packages, sourcePath };
|
|
24
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Effect, Path } from "effect";
|
|
2
|
+
import { readPackageManifest } from "#adapters/manifestInventory.ts";
|
|
3
|
+
import { scriptPaths } from "#adapters/scriptPaths.ts";
|
|
4
|
+
import { importGraph } from "#imports/graph.ts";
|
|
5
|
+
import { isTooling } from "#imports/isTooling.ts";
|
|
6
|
+
import { testComposition } from "#imports/testComposition.ts";
|
|
7
|
+
import { packageOf } from "#imports/workspace.ts";
|
|
8
|
+
import type { RuleInputs } from "#rule.ts";
|
|
9
|
+
import { packageTests } from "./packageTests.ts";
|
|
10
|
+
import { isTestCode } from "./testName.ts";
|
|
11
|
+
|
|
12
|
+
export const testCode = Effect.fn("Tests.testCode")(function* (inputs: RuleInputs) {
|
|
13
|
+
const posix = yield* Path.Path.pipe(Effect.provide(Path.layer));
|
|
14
|
+
const trees = yield* packageTests(inputs);
|
|
15
|
+
function testFile(path: string) {
|
|
16
|
+
return isTestCode(path) || trees.sourcePath(path) !== undefined;
|
|
17
|
+
}
|
|
18
|
+
const policies = yield* Effect.forEach(trees.packages, (owner) =>
|
|
19
|
+
Effect.map(
|
|
20
|
+
Effect.promise(() => readPackageManifest(inputs.readText, posix.join(owner.directory, "package.json"))),
|
|
21
|
+
(manifest) => ({
|
|
22
|
+
owner,
|
|
23
|
+
private: manifest?.private === true && manifest.bin === undefined && owner.directory !== ".",
|
|
24
|
+
scripts: scriptPaths(
|
|
25
|
+
owner.directory,
|
|
26
|
+
Object.fromEntries(Object.entries(manifest?.scripts ?? {}).filter(([name]) => !/^test(?::|$)/u.test(name))),
|
|
27
|
+
),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
if (!(policies.some((policy) => policy.private) && inputs.sources.some((file) => /\.[cm]?[jt]sx?$/u.test(file.path)))) {
|
|
32
|
+
return testFile;
|
|
33
|
+
}
|
|
34
|
+
const graph = yield* importGraph(inputs);
|
|
35
|
+
const directories = new Set(policies.filter((policy) => policy.private).map((policy) => policy.owner.directory));
|
|
36
|
+
const scripted = new Set(policies.flatMap((policy) => policy.scripts));
|
|
37
|
+
for (const path of graph.modules) {
|
|
38
|
+
const owner = packageOf(graph.packages, path);
|
|
39
|
+
if (owner === undefined) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const built = posix.join(
|
|
43
|
+
owner.directory,
|
|
44
|
+
posix
|
|
45
|
+
.relative(owner.directory, path)
|
|
46
|
+
.replace(/^src\//u, "dist/")
|
|
47
|
+
.replace(/\.(?<mode>[cm]?)tsx?$/u, ".$<mode>js"),
|
|
48
|
+
);
|
|
49
|
+
if (!(testFile(path) || isTooling(owner.directory, path)) && (scripted.has(path) || scripted.has(built))) {
|
|
50
|
+
directories.delete(owner.directory);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const composition = testComposition(
|
|
54
|
+
graph,
|
|
55
|
+
graph.packages.filter((owner) => directories.has(owner.directory)),
|
|
56
|
+
testFile,
|
|
57
|
+
);
|
|
58
|
+
return (path: string) => {
|
|
59
|
+
const owner = packageOf(graph.packages, path);
|
|
60
|
+
return testFile(path) || (owner !== undefined && composition.has(owner));
|
|
61
|
+
};
|
|
62
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
|
-
import { SUPPRESSIONS } from "#rules/suppressions/
|
|
2
|
+
import { SUPPRESSIONS } from "#rules/suppressions/in.ts";
|
|
3
3
|
import type { SourceComment } from "./scan.ts";
|
|
4
4
|
|
|
5
5
|
export const Pragmas = Schema.Array(Schema.String).pipe(Schema.withDecodingDefaultKey(Effect.succeed<readonly string[]>([])));
|
|
@@ -5,11 +5,13 @@ import { serviceAssemblyImports } from "#adapters/serviceAssemblyImports.ts";
|
|
|
5
5
|
import { directServiceDefinitions } from "#adapters/serviceAssemblySyntax.ts";
|
|
6
6
|
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
7
7
|
import { routeFiles } from "#inventory/routeFiles.ts";
|
|
8
|
+
import { knownDataExports } from "#naming/adapters/plainData.ts";
|
|
9
|
+
import { constantModule } from "#naming/constantModule.ts";
|
|
8
10
|
import { exportFits } from "#naming/exportFits.ts";
|
|
11
|
+
import { isMigrationFile } from "#naming/isMigrationFile.ts";
|
|
9
12
|
import { mainExport } from "#naming/mainExport.ts";
|
|
10
|
-
import { isMigrationFile } from "#naming/migrationFile.ts";
|
|
11
13
|
import { moduleRoot } from "#naming/moduleRoot.ts";
|
|
12
|
-
import {
|
|
14
|
+
import { ownExports } from "#naming/ownExports.ts";
|
|
13
15
|
import { type PackageLayout, packageLayout } from "#naming/packageLayout.ts";
|
|
14
16
|
import { CAMEL, KEBAB, sameWord, wordsOf } from "#naming/words.ts";
|
|
15
17
|
import { defineRule, type Finding, type SourceFile } from "#rule.ts";
|
|
@@ -51,9 +53,24 @@ function repeated(stem: string, folders: readonly string[]): string | undefined
|
|
|
51
53
|
function nameFinding(file: SourceFile, stem: string, folders: readonly string[]): string | undefined {
|
|
52
54
|
const source = parse(file);
|
|
53
55
|
const exports = source === undefined ? [] : ownExports(source);
|
|
54
|
-
const constants =
|
|
55
|
-
if (
|
|
56
|
-
|
|
56
|
+
const constants = exports.filter((entry) => entry.kind === "value" && entry.constant);
|
|
57
|
+
if (
|
|
58
|
+
source !== undefined
|
|
59
|
+
&& constants.length > 0
|
|
60
|
+
&& exports.every((entry) => entry.constant)
|
|
61
|
+
&& knownDataExports(
|
|
62
|
+
source,
|
|
63
|
+
exports.map((entry) => entry.name),
|
|
64
|
+
)
|
|
65
|
+
) {
|
|
66
|
+
return constantModule(stem, constants);
|
|
67
|
+
}
|
|
68
|
+
const remaining = exports.filter((entry) => entry.kind === "value" && !entry.constant);
|
|
69
|
+
const main = constants.length > 0 && remaining.length === 1 ? remaining[0] : undefined;
|
|
70
|
+
if (main !== undefined) {
|
|
71
|
+
return exportFits(main.name, stem, folders)
|
|
72
|
+
? undefined
|
|
73
|
+
: `"${stem}" does not name its export ${main.name}. ${SHAPES} Rename the file or the export.`;
|
|
57
74
|
}
|
|
58
75
|
const [only] = exports;
|
|
59
76
|
if (only === undefined) {
|
|
@@ -61,16 +78,16 @@ function nameFinding(file: SourceFile, stem: string, folders: readonly string[])
|
|
|
61
78
|
? undefined
|
|
62
79
|
: `"${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.`;
|
|
63
80
|
}
|
|
64
|
-
if (exports.some((entry) => exportFits(entry.name, stem, folders))) {
|
|
81
|
+
if (exports.some((entry) => !(entry.kind === "value" && entry.constant && remaining.length > 0) && exportFits(entry.name, stem, folders))) {
|
|
65
82
|
return undefined;
|
|
66
83
|
}
|
|
67
|
-
const
|
|
68
|
-
if (
|
|
69
|
-
return `"${stem}" does not name its export ${
|
|
84
|
+
const primary = mainExport(exports);
|
|
85
|
+
if (primary !== undefined) {
|
|
86
|
+
return `"${stem}" does not name its export ${primary.name}. ${SHAPES} Rename the file or the export.`;
|
|
70
87
|
}
|
|
71
88
|
return CAMEL.test(stem)
|
|
72
89
|
? undefined
|
|
73
|
-
: `"${stem}" exports several things, so it is a topic file named in camelCase, such as
|
|
90
|
+
: `"${stem}" exports several things, so it is a topic file named in camelCase, such as helpers.ts. Or split it so each file has one main export. ${SHAPES}`;
|
|
74
91
|
}
|
|
75
92
|
|
|
76
93
|
function findingsOf(file: SourceFile, layout: PackageLayout): readonly Finding[] {
|
|
@@ -111,7 +128,7 @@ export const namedAfterExport = defineRule({
|
|
|
111
128
|
.flatMap((file) => findingsOf(file, layout));
|
|
112
129
|
}),
|
|
113
130
|
description:
|
|
114
|
-
"A file is named after its main export
|
|
131
|
+
"A file is named after its main runtime export, ignoring private constants and plain-data constants beside it, with its folders as the prefix, including in test-support; module client/admin and server/admin files may repeat names from above the admin folder. defineService declarations use service.ts; TanStack Router configuration owns route filenames. A constants module holds only plain data and derived types, in a camelCase file named for words every constant shares (timeouts.ts), limits for max/min/limit constants, or defaults for default constants. Split unrelated constants. A file of several non-constant exports is a camelCase topic file, and a script is kebab-case. Rename the file or the export; nothing renames them for you.",
|
|
115
132
|
id: "files/named-after-export",
|
|
116
133
|
options: Schema.toStandardSchemaV1(NamedAfterExportOptions, {
|
|
117
134
|
parseOptions: { errors: "all", onExcessProperty: "error" },
|