@starci/eslint-canon-be 3.5.0 → 3.6.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 +15 -0
- package/README.md +11 -2
- package/dist/ambient-id.d.ts +9 -0
- package/{ambient-id.mjs → dist/ambient-id.js} +43 -39
- package/dist/app-composition.d.ts +21 -0
- package/{app-composition.mjs → dist/app-composition.js} +396 -300
- package/dist/async-discipline.d.ts +9 -0
- package/dist/async-discipline.js +77 -0
- package/dist/authorization.d.ts +12 -0
- package/{authorization.mjs → dist/authorization.js} +53 -59
- package/dist/boundary-schema.d.ts +15 -0
- package/{boundary-schema.mjs → dist/boundary-schema.js} +120 -105
- package/dist/builders.d.ts +21 -0
- package/{builders.mjs → dist/builders.js} +162 -146
- package/dist/cli.d.ts +15 -0
- package/{cli.mjs → dist/cli.js} +97 -82
- package/dist/comments.d.ts +52 -0
- package/dist/comments.js +412 -0
- package/dist/config-owner.d.ts +24 -0
- package/{config-owner.mjs → dist/config-owner.js} +256 -219
- package/dist/connections.d.ts +12 -0
- package/{connections.mjs → dist/connections.js} +138 -140
- package/dist/cookies.d.ts +9 -0
- package/{cookies.mjs → dist/cookies.js} +58 -58
- package/dist/cqrs.d.ts +27 -0
- package/{cqrs.mjs → dist/cqrs.js} +341 -282
- package/dist/data-access.d.ts +50 -0
- package/{data-access.mjs → dist/data-access.js} +238 -243
- package/dist/default-deny.d.ts +15 -0
- package/{default-deny.mjs → dist/default-deny.js} +103 -88
- package/dist/e2e-flow.d.ts +25 -0
- package/{e2e-flow.mjs → dist/e2e-flow.js} +113 -112
- package/dist/edge-hardening.d.ts +10 -0
- package/dist/edge-hardening.js +173 -0
- package/dist/error-handling.d.ts +21 -0
- package/{error-handling.mjs → dist/error-handling.js} +289 -239
- package/dist/event-bus.d.ts +14 -0
- package/{event-bus.mjs → dist/event-bus.js} +146 -129
- package/dist/feature-thin.d.ts +12 -0
- package/{feature-thin.mjs → dist/feature-thin.js} +208 -191
- package/dist/function-budget.d.ts +7 -0
- package/{function-budget.mjs → dist/function-budget.js} +24 -27
- package/dist/health-ready.d.ts +10 -0
- package/dist/health-ready.js +703 -0
- package/dist/idempotency.d.ts +10 -0
- package/{idempotency.mjs → dist/idempotency.js} +296 -226
- package/dist/index.d.ts +54 -0
- package/{index.mjs → dist/index.js} +91 -109
- package/dist/infra-owner.d.ts +18 -0
- package/{infra-owner.mjs → dist/infra-owner.js} +58 -51
- package/dist/injection.d.ts +30 -0
- package/{injection.mjs → dist/injection.js} +397 -344
- package/dist/input-bounds.d.ts +15 -0
- package/{input-bounds.mjs → dist/input-bounds.js} +143 -132
- package/dist/jobs.d.ts +17 -0
- package/{jobs.mjs → dist/jobs.js} +116 -99
- package/dist/jwt-alg.d.ts +6 -0
- package/dist/jwt-alg.js +49 -0
- package/dist/lib/app-roles.d.ts +25 -0
- package/{lib/app-roles.mjs → dist/lib/app-roles.js} +15 -17
- package/dist/lib/ast.d.ts +40 -0
- package/dist/lib/ast.js +100 -0
- package/dist/lib/client-idempotency.d.ts +3 -0
- package/{lib/client-idempotency.mjs → dist/lib/client-idempotency.js} +316 -268
- package/dist/lib/config.d.ts +36 -0
- package/{lib/config.mjs → dist/lib/config.js} +24 -27
- package/dist/lib/db-writes.d.ts +29 -0
- package/{lib/db-writes.mjs → dist/lib/db-writes.js} +27 -27
- package/dist/lib/declared.d.ts +77 -0
- package/{lib/declared.mjs → dist/lib/declared.js} +83 -78
- package/dist/lib/doors.d.ts +50 -0
- package/{lib/doors.mjs → dist/lib/doors.js} +45 -50
- package/dist/lib/edition.d.ts +25 -0
- package/{lib/edition.mjs → dist/lib/edition.js} +15 -20
- package/dist/lib/entity-writes.d.ts +7 -0
- package/{lib/entity-writes.mjs → dist/lib/entity-writes.js} +31 -22
- package/dist/lib/hfs.d.ts +34 -0
- package/{lib/hfs.mjs → dist/lib/hfs.js} +31 -29
- package/dist/lib/import-source.d.ts +63 -0
- package/dist/lib/import-source.js +134 -0
- package/dist/lib/path.d.ts +8 -0
- package/{lib/path.mjs → dist/lib/path.js} +4 -7
- package/dist/lib/persistence.d.ts +115 -0
- package/{lib/persistence.mjs → dist/lib/persistence.js} +55 -66
- package/dist/lib/ports.d.ts +70 -0
- package/{lib/ports.mjs → dist/lib/ports.js} +62 -66
- package/dist/lib/rule.d.ts +31 -0
- package/dist/lib/rule.js +2 -0
- package/dist/lib/runtime-root.d.ts +4 -0
- package/dist/lib/runtime-root.js +10 -0
- package/dist/lib/schema-values.d.ts +13 -0
- package/{lib/schema-values.mjs → dist/lib/schema-values.js} +67 -59
- package/dist/lib/specifier.d.ts +12 -0
- package/dist/lib/specifier.js +29 -0
- package/dist/lib/subject-values.d.ts +4 -0
- package/dist/lib/subject-values.js +61 -0
- package/dist/lib/transactions.d.ts +18 -0
- package/{lib/transactions.mjs → dist/lib/transactions.js} +17 -15
- package/dist/lib/transport-slots.d.ts +17 -0
- package/{lib/transport-slots.mjs → dist/lib/transport-slots.js} +2 -4
- package/dist/lib/types.d.ts +96 -0
- package/dist/lib/types.js +150 -0
- package/dist/lib/unit-spec.d.ts +48 -0
- package/{lib/unit-spec.mjs → dist/lib/unit-spec.js} +35 -44
- package/dist/lib/webhook-ack.d.ts +4 -0
- package/{lib/webhook-ack.mjs → dist/lib/webhook-ack.js} +78 -62
- package/dist/lib/webhook-handoff-inbox.d.ts +3 -0
- package/{lib/webhook-handoff-inbox.mjs → dist/lib/webhook-handoff-inbox.js} +195 -167
- package/dist/lib/webhook-handoff-intake.d.ts +4 -0
- package/{lib/webhook-handoff-intake.mjs → dist/lib/webhook-handoff-intake.js} +125 -102
- package/dist/lib/webhook-handoff.d.ts +15 -0
- package/{lib/webhook-handoff.mjs → dist/lib/webhook-handoff.js} +107 -100
- package/dist/lib/webhook-verifiers.d.ts +20 -0
- package/{lib/webhook-verifiers.mjs → dist/lib/webhook-verifiers.js} +108 -104
- package/dist/lib/why.d.ts +9 -0
- package/{lib/why.mjs → dist/lib/why.js} +6 -6
- package/dist/log-safety.d.ts +20 -0
- package/dist/log-safety.js +308 -0
- package/dist/module-layering.d.ts +42 -0
- package/dist/module-layering.js +257 -0
- package/dist/module-shape.d.ts +47 -0
- package/{module-shape.mjs → dist/module-shape.js} +278 -236
- package/dist/naming.d.ts +49 -0
- package/dist/naming.js +246 -0
- package/dist/observability.d.ts +25 -0
- package/dist/observability.js +245 -0
- package/dist/operations.d.ts +23 -0
- package/{operations.mjs → dist/operations.js} +410 -332
- package/dist/project-graph.d.ts +9 -0
- package/{project-graph.mjs → dist/project-graph.js} +8 -13
- package/dist/projections.d.ts +11 -0
- package/{projections.mjs → dist/projections.js} +60 -57
- package/dist/query-safety.d.ts +21 -0
- package/{query-safety.mjs → dist/query-safety.js} +92 -108
- package/dist/realtime.d.ts +15 -0
- package/{realtime.mjs → dist/realtime.js} +108 -106
- package/dist/resilience.d.ts +15 -0
- package/dist/resilience.js +319 -0
- package/dist/schema-authority.d.ts +15 -0
- package/{schema-authority.mjs → dist/schema-authority.js} +256 -220
- package/dist/service-deps.d.ts +17 -0
- package/{service-deps.mjs → dist/service-deps.js} +133 -130
- package/dist/service-isolation.d.ts +8 -0
- package/dist/service-isolation.js +76 -0
- package/dist/size-budget.d.ts +9 -0
- package/{size-budget.mjs → dist/size-budget.js} +27 -27
- package/dist/sonar-parity.d.ts +5 -0
- package/{sonar-parity.mjs → dist/sonar-parity.js} +7 -7
- package/dist/spec-no-skip.d.ts +9 -0
- package/dist/spec-no-skip.js +81 -0
- package/dist/spec-quality.d.ts +10 -0
- package/{spec-quality.mjs → dist/spec-quality.js} +103 -87
- package/dist/suppression.d.ts +26 -0
- package/{suppression.mjs → dist/suppression.js} +30 -45
- package/dist/temporal.d.ts +9 -0
- package/{temporal.mjs → dist/temporal.js} +75 -65
- package/dist/test-world.d.ts +28 -0
- package/{test-world.mjs → dist/test-world.js} +102 -83
- package/dist/testing.d.ts +51 -0
- package/dist/testing.js +368 -0
- package/dist/third-party-owner.d.ts +8 -0
- package/{third-party-owner.mjs → dist/third-party-owner.js} +105 -91
- package/dist/transport.d.ts +24 -0
- package/{transport.mjs → dist/transport.js} +279 -240
- package/dist/type-safety.d.ts +25 -0
- package/dist/type-safety.js +297 -0
- package/dist/unit-spec.d.ts +30 -0
- package/{unit-spec.mjs → dist/unit-spec.js} +473 -399
- package/dist/uploads.d.ts +9 -0
- package/{uploads.mjs → dist/uploads.js} +62 -54
- package/dist/user-copy.d.ts +9 -0
- package/{user-copy.mjs → dist/user-copy.js} +76 -67
- package/dist/webhooks.d.ts +16 -0
- package/{webhooks.mjs → dist/webhooks.js} +243 -231
- package/package.json +17 -8
- package/runtime/knowledge/hfs/canon-pins.yaml +17 -10
- package/async-discipline.mjs +0 -83
- package/comments.mjs +0 -416
- package/edge-hardening.mjs +0 -139
- package/health-ready.mjs +0 -539
- package/jwt-alg.mjs +0 -45
- package/lib/ast.mjs +0 -88
- package/lib/import-source.mjs +0 -104
- package/lib/specifier.mjs +0 -27
- package/lib/subject-values.mjs +0 -55
- package/lib/types.mjs +0 -133
- package/log-safety.mjs +0 -303
- package/module-layering.mjs +0 -270
- package/naming.mjs +0 -251
- package/observability.mjs +0 -241
- package/resilience.mjs +0 -304
- package/service-isolation.mjs +0 -75
- package/spec-no-skip.mjs +0 -85
- package/testing.mjs +0 -384
- package/type-safety.mjs +0 -323
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HfsView } from "./hfs.js";
|
|
2
|
+
/** Tiers a schema file may live in: the owners of boundary data (R304). */
|
|
3
|
+
export declare const SCHEMA_TIERS: ReadonlySet<string | null>;
|
|
4
|
+
/**
|
|
5
|
+
* The reasons a source text is not a pure boundary schema file: it imports zod and holds nothing but imports, types and
|
|
6
|
+
* `const X = <zod chain>` values, with no class, function, provider or other logic. An empty list means a value-only schema file.
|
|
7
|
+
*/
|
|
8
|
+
export declare const schemaFileProblems: (text: string) => string[];
|
|
9
|
+
/**
|
|
10
|
+
* Whether the sibling `<name>.schema.ts` of a `<name>.schema.spec.ts` is a real boundary schema: it exists, lives in an owner tier of R304 and is
|
|
11
|
+
* value-only (schemaFileProblems). Read from disk once per file and modification time.
|
|
12
|
+
*/
|
|
13
|
+
export declare const siblingIsValueSchema: (hfs: HfsView, specFile: string) => boolean;
|
|
@@ -1,90 +1,98 @@
|
|
|
1
|
-
import fs from "node:fs"
|
|
2
|
-
import path from "node:path"
|
|
3
|
-
import ts from "typescript"
|
|
4
|
-
import { normalizePath } from "./path.
|
|
5
|
-
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { normalizePath } from "./path.js";
|
|
6
5
|
/** Tiers a schema file may live in: the owners of boundary data (R304). */
|
|
7
|
-
export const SCHEMA_TIERS = new Set(["domain", "platform", "integrations"])
|
|
8
|
-
|
|
9
|
-
const ZOD = /^zod(?:\/|$)/
|
|
6
|
+
export const SCHEMA_TIERS = new Set(["domain", "platform", "integrations"]);
|
|
7
|
+
const ZOD = /^zod(?:\/|$)/;
|
|
10
8
|
/** A sibling schema module: its exports are schemas too, so a chain may start from them. */
|
|
11
|
-
const SIBLING_SCHEMA = /^\.{1,2}\/(?:.*\/)?[^/]+\.schema(?:\.[cm]?[jt]s)
|
|
12
|
-
|
|
9
|
+
const SIBLING_SCHEMA = /^\.{1,2}\/(?:.*\/)?[^/]+\.schema(?:\.[cm]?[jt]s)?$/;
|
|
13
10
|
/** The local names the imports of a source file bind whose module specifier matches `specifier`. */
|
|
14
11
|
const importedNames = (sourceFile, specifier) => {
|
|
15
|
-
const names = new Set()
|
|
12
|
+
const names = new Set();
|
|
16
13
|
for (const statement of sourceFile.statements) {
|
|
17
|
-
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !specifier.test(statement.moduleSpecifier.text))
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !specifier.test(statement.moduleSpecifier.text))
|
|
15
|
+
continue;
|
|
16
|
+
const clause = statement.importClause;
|
|
17
|
+
if (clause?.name)
|
|
18
|
+
names.add(clause.name.text);
|
|
19
|
+
const bindings = clause?.namedBindings;
|
|
20
|
+
if (bindings && ts.isNamespaceImport(bindings))
|
|
21
|
+
names.add(bindings.name.text);
|
|
22
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
23
|
+
for (const element of bindings.elements)
|
|
24
|
+
names.add(element.name.text);
|
|
23
25
|
}
|
|
24
|
-
return names
|
|
25
|
-
}
|
|
26
|
-
|
|
26
|
+
return names;
|
|
27
|
+
};
|
|
27
28
|
/** The identifier a call/member chain starts from (`z.string().max(5)` gives `z`), or null when it starts from anything else. */
|
|
28
29
|
const rootOf = (expression) => {
|
|
29
|
-
let current = expression
|
|
30
|
+
let current = expression;
|
|
30
31
|
for (;;) {
|
|
31
|
-
if (ts.isIdentifier(current))
|
|
32
|
+
if (ts.isIdentifier(current))
|
|
33
|
+
return current.text;
|
|
32
34
|
if (ts.isCallExpression(current) || ts.isPropertyAccessExpression(current) || ts.isElementAccessExpression(current)
|
|
33
|
-
|| ts.isNonNullExpression(current) || ts.isParenthesizedExpression(current) || ts.isAsExpression(current))
|
|
34
|
-
|
|
35
|
+
|| ts.isNonNullExpression(current) || ts.isParenthesizedExpression(current) || ts.isAsExpression(current))
|
|
36
|
+
current = current.expression;
|
|
37
|
+
else
|
|
38
|
+
return null;
|
|
35
39
|
}
|
|
36
|
-
}
|
|
37
|
-
|
|
40
|
+
};
|
|
38
41
|
/** A literal bound (`100`, `-1`, `"x"`, `true`): data a schema reads, nothing that runs. */
|
|
39
42
|
const isLiteral = (expression) => ts.isNumericLiteral(expression) || ts.isStringLiteralLike(expression)
|
|
40
43
|
|| expression.kind === ts.SyntaxKind.TrueKeyword || expression.kind === ts.SyntaxKind.FalseKeyword
|
|
41
|
-
|| (ts.isPrefixUnaryExpression(expression) && ts.isNumericLiteral(expression.operand))
|
|
42
|
-
|
|
44
|
+
|| (ts.isPrefixUnaryExpression(expression) && ts.isNumericLiteral(expression.operand));
|
|
43
45
|
/** A statement a schema file may hold: an import, a type, or `export const X = <zod chain or a sibling schema>`. */
|
|
44
46
|
const statementProblem = (statement, zodNames, declared) => {
|
|
45
|
-
if (ts.isImportDeclaration(statement) || ts.isTypeAliasDeclaration(statement) || ts.isInterfaceDeclaration(statement))
|
|
46
|
-
|
|
47
|
-
if (
|
|
47
|
+
if (ts.isImportDeclaration(statement) || ts.isTypeAliasDeclaration(statement) || ts.isInterfaceDeclaration(statement))
|
|
48
|
+
return null;
|
|
49
|
+
if (ts.isExportDeclaration(statement))
|
|
50
|
+
return null;
|
|
51
|
+
if (!ts.isVariableStatement(statement))
|
|
52
|
+
return `a ${ts.SyntaxKind[statement.kind]} is logic, not a schema value`;
|
|
48
53
|
for (const declaration of statement.declarationList.declarations) {
|
|
49
|
-
const init = declaration.initializer
|
|
50
|
-
if (init && isLiteral(init) && ts.isIdentifier(declaration.name)) {
|
|
51
|
-
|
|
54
|
+
const init = declaration.initializer;
|
|
55
|
+
if (init && isLiteral(init) && ts.isIdentifier(declaration.name)) {
|
|
56
|
+
declared.add(declaration.name.text);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const root = init ? rootOf(init) : null;
|
|
52
60
|
if (!ts.isIdentifier(declaration.name) || root === null || !(zodNames.has(root) || declared.has(root))) {
|
|
53
|
-
return `${declaration.name.getText()} is not a zod schema (a schema file declares only zod chains and types)
|
|
61
|
+
return `${declaration.name.getText()} is not a zod schema (a schema file declares only zod chains and types)`;
|
|
54
62
|
}
|
|
55
|
-
declared.add(declaration.name.text)
|
|
63
|
+
declared.add(declaration.name.text);
|
|
56
64
|
}
|
|
57
|
-
return null
|
|
58
|
-
}
|
|
59
|
-
|
|
65
|
+
return null;
|
|
66
|
+
};
|
|
60
67
|
/**
|
|
61
68
|
* The reasons a source text is not a pure boundary schema file: it imports zod and holds nothing but imports, types and
|
|
62
69
|
* `const X = <zod chain>` values, with no class, function, provider or other logic. An empty list means a value-only schema file.
|
|
63
70
|
*/
|
|
64
71
|
export const schemaFileProblems = (text) => {
|
|
65
|
-
const sourceFile = ts.createSourceFile("subject.schema.ts", text, ts.ScriptTarget.Latest, true)
|
|
66
|
-
const zodNames = importedNames(sourceFile, ZOD)
|
|
67
|
-
if (zodNames.size === 0)
|
|
68
|
-
|
|
72
|
+
const sourceFile = ts.createSourceFile("subject.schema.ts", text, ts.ScriptTarget.Latest, true);
|
|
73
|
+
const zodNames = importedNames(sourceFile, ZOD);
|
|
74
|
+
if (zodNames.size === 0)
|
|
75
|
+
return ["it imports no zod, so it is not a boundary schema"];
|
|
76
|
+
const declared = importedNames(sourceFile, SIBLING_SCHEMA);
|
|
69
77
|
return sourceFile.statements.flatMap((statement) => {
|
|
70
|
-
const problem = statementProblem(statement, zodNames, declared)
|
|
71
|
-
return problem === null ? [] : [problem]
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const CACHE = new Map()
|
|
76
|
-
|
|
78
|
+
const problem = statementProblem(statement, zodNames, declared);
|
|
79
|
+
return problem === null ? [] : [problem];
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
const CACHE = new Map();
|
|
77
83
|
/**
|
|
78
84
|
* Whether the sibling `<name>.schema.ts` of a `<name>.schema.spec.ts` is a real boundary schema: it exists, lives in an owner tier of R304 and is
|
|
79
85
|
* value-only (schemaFileProblems). Read from disk once per file and modification time.
|
|
80
86
|
*/
|
|
81
87
|
export const siblingIsValueSchema = (hfs, specFile) => {
|
|
82
|
-
const sibling = normalizePath(path.resolve(specFile.replace(/\.spec\.ts$/u, ".ts")))
|
|
83
|
-
if (!fs.existsSync(sibling) || !SCHEMA_TIERS.has(hfs.tierOf(sibling)))
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
88
|
+
const sibling = normalizePath(path.resolve(specFile.replace(/\.spec\.ts$/u, ".ts")));
|
|
89
|
+
if (!fs.existsSync(sibling) || !SCHEMA_TIERS.has(hfs.tierOf(sibling)))
|
|
90
|
+
return false;
|
|
91
|
+
const stamp = fs.statSync(sibling).mtimeMs;
|
|
92
|
+
const cached = CACHE.get(sibling);
|
|
93
|
+
if (cached?.stamp === stamp)
|
|
94
|
+
return cached.ok;
|
|
95
|
+
const ok = schemaFileProblems(fs.readFileSync(sibling, "utf8")).length === 0;
|
|
96
|
+
CACHE.set(sibling, { stamp, ok });
|
|
97
|
+
return ok;
|
|
98
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Context } from "./rule.js";
|
|
2
|
+
/**
|
|
3
|
+
* The absolute path an alias specifier maps to under the program's `compilerOptions.paths`, or null when no pattern matches.
|
|
4
|
+
*
|
|
5
|
+
* @param context - The ESLint rule context.
|
|
6
|
+
* @param specifier - An import specifier.
|
|
7
|
+
* @returns The absolute target without extension and the part the wildcard matched.
|
|
8
|
+
*/
|
|
9
|
+
export declare const resolveAlias: (context: Context, specifier: string) => {
|
|
10
|
+
target: string;
|
|
11
|
+
rest: string;
|
|
12
|
+
} | null;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where an import specifier points. A `compilerOptions.paths` alias resolves through the program's own options, so a
|
|
3
|
+
* rule asks the same question TypeScript asks instead of maintaining a second alias table.
|
|
4
|
+
*/
|
|
5
|
+
import { posix } from "node:path";
|
|
6
|
+
import { typed } from "./types.js";
|
|
7
|
+
/**
|
|
8
|
+
* The absolute path an alias specifier maps to under the program's `compilerOptions.paths`, or null when no pattern matches.
|
|
9
|
+
*
|
|
10
|
+
* @param context - The ESLint rule context.
|
|
11
|
+
* @param specifier - An import specifier.
|
|
12
|
+
* @returns The absolute target without extension and the part the wildcard matched.
|
|
13
|
+
*/
|
|
14
|
+
export const resolveAlias = (context, specifier) => {
|
|
15
|
+
const options = typed(context).program.getCompilerOptions();
|
|
16
|
+
const base = options.baseUrl ?? options.pathsBasePath;
|
|
17
|
+
if (!options.paths || typeof base !== "string")
|
|
18
|
+
return null;
|
|
19
|
+
for (const [pattern, targets] of Object.entries(options.paths)) {
|
|
20
|
+
if (!pattern.endsWith("/*") || !targets[0])
|
|
21
|
+
continue;
|
|
22
|
+
const prefix = pattern.slice(0, -1);
|
|
23
|
+
if (!specifier.startsWith(prefix))
|
|
24
|
+
continue;
|
|
25
|
+
const rest = specifier.slice(prefix.length);
|
|
26
|
+
return { target: posix.normalize(`${base.replace(/\\/g, "/")}/${targets[0].replace("*", rest)}`), rest };
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Context, Node } from "./rule.js";
|
|
2
|
+
/** The sibling module's value-object classes (undecorated, no constructor injection token), read from disk once per file; `[]` when none. */
|
|
3
|
+
export declare const valueObjectsOfSibling: (context: Context) => string[];
|
|
4
|
+
export declare const isRealSubjectValue: (context: Context, value: Node) => boolean;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { functionSubjectExports, valueObjectSubjects } from "../../runtime/scripts/hfs/architecture/function-spec-providers.mjs";
|
|
5
|
+
import { importOf } from "./import-source.js";
|
|
6
|
+
import { typed } from "./types.js";
|
|
7
|
+
/** The exported real function of a function-only sibling subject, with aliases resolved by TypeScript. */
|
|
8
|
+
const realSubjectFunction = (context, value) => {
|
|
9
|
+
if (value.type !== "Identifier")
|
|
10
|
+
return null;
|
|
11
|
+
const imported = importOf(context, value);
|
|
12
|
+
if (!imported || !imported.source.startsWith("."))
|
|
13
|
+
return null;
|
|
14
|
+
const { checker, toTs } = typed(context);
|
|
15
|
+
const tsNode = toTs(value);
|
|
16
|
+
const symbol = ts.isShorthandPropertyAssignment(tsNode.parent)
|
|
17
|
+
? checker.getShorthandAssignmentValueSymbol(tsNode.parent) : checker.getSymbolAtLocation(tsNode);
|
|
18
|
+
const alias = symbol?.declarations?.some(declaration => ts.isImportSpecifier(declaration));
|
|
19
|
+
const target = alias && symbol ? checker.getAliasedSymbol(symbol) : symbol;
|
|
20
|
+
const declaration = target?.valueDeclaration;
|
|
21
|
+
if (!declaration)
|
|
22
|
+
return null;
|
|
23
|
+
const sourceFile = declaration.getSourceFile();
|
|
24
|
+
const sibling = (context.filename || context.getFilename()).replace(/\.spec\.ts$/u, ".ts");
|
|
25
|
+
if (path.resolve(sourceFile.fileName) !== path.resolve(sibling))
|
|
26
|
+
return null;
|
|
27
|
+
const names = functionSubjectExports(ts, sourceFile);
|
|
28
|
+
return imported.imported !== null && names?.includes(imported.imported) ? imported.imported : null;
|
|
29
|
+
};
|
|
30
|
+
const VALUE_OBJECTS = new Map();
|
|
31
|
+
/** The sibling module's value-object classes (undecorated, no constructor injection token), read from disk once per file; `[]` when none. */
|
|
32
|
+
export const valueObjectsOfSibling = (context) => {
|
|
33
|
+
const sibling = path.resolve((context.filename || context.getFilename()).replace(/\.spec\.ts$/u, ".ts"));
|
|
34
|
+
const stamp = fs.existsSync(sibling) ? fs.statSync(sibling).mtimeMs : -1;
|
|
35
|
+
const cached = VALUE_OBJECTS.get(sibling);
|
|
36
|
+
if (cached?.stamp === stamp)
|
|
37
|
+
return cached.names;
|
|
38
|
+
const names = stamp < 0 ? [] : valueObjectSubjects(ts, ts.createSourceFile(sibling, fs.readFileSync(sibling, "utf8"), ts.ScriptTarget.Latest, true)) ?? [];
|
|
39
|
+
VALUE_OBJECTS.set(sibling, { stamp, names });
|
|
40
|
+
return names;
|
|
41
|
+
};
|
|
42
|
+
/** `new Subject(...)` where Subject is a value-object class imported from the sibling module. */
|
|
43
|
+
const isValueObjectSubject = (context, value) => {
|
|
44
|
+
if (value.type !== "NewExpression" || value.callee.type !== "Identifier")
|
|
45
|
+
return false;
|
|
46
|
+
const imported = importOf(context, value.callee);
|
|
47
|
+
return Boolean(imported && imported.source.startsWith(".") && imported.imported !== null && valueObjectsOfSibling(context).includes(imported.imported));
|
|
48
|
+
};
|
|
49
|
+
/** Real functions under test are subject values; an inline fake or a collaborator remains a kit double. */
|
|
50
|
+
/** The name or value a non-computed property key spells. */
|
|
51
|
+
const keyOf = (key) => (key.type === "Identifier" || key.type === "PrivateIdentifier" ? key.name : "value" in key ? key.value : undefined);
|
|
52
|
+
export const isRealSubjectValue = (context, value) => {
|
|
53
|
+
if (isValueObjectSubject(context, value))
|
|
54
|
+
return true;
|
|
55
|
+
if (value.type === "Identifier")
|
|
56
|
+
return realSubjectFunction(context, value) !== null;
|
|
57
|
+
if (value.type !== "ObjectExpression" || value.properties.length === 0)
|
|
58
|
+
return false;
|
|
59
|
+
return value.properties.every((property) => property.type === "Property" && !property.computed
|
|
60
|
+
&& keyOf(property.key) === realSubjectFunction(context, property.value));
|
|
61
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { TSESLint, TSESTree } from "@typescript-eslint/utils";
|
|
2
|
+
import type { Context } from "./rule.js";
|
|
3
|
+
/**
|
|
4
|
+
* The callback of a `.transaction(...)` call on a manager or data source, or null.
|
|
5
|
+
*
|
|
6
|
+
* @param {object} context - The ESLint rule context.
|
|
7
|
+
* @param {object} node - A CallExpression.
|
|
8
|
+
* @returns {object | null} The arrow or function expression that receives the transactional manager.
|
|
9
|
+
*/
|
|
10
|
+
export declare const transactionCallback: (context: Context, node: TSESTree.CallExpression) => TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression | null;
|
|
11
|
+
/**
|
|
12
|
+
* The variable an identifier resolves to, looking outward from the identifier's own scope.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} sourceCode - The ESLint source code.
|
|
15
|
+
* @param {object} node - An Identifier.
|
|
16
|
+
* @returns {object | null} The scope variable.
|
|
17
|
+
*/
|
|
18
|
+
export declare const resolveVariable: (sourceCode: Readonly<TSESLint.SourceCode>, node: TSESTree.Identifier) => TSESLint.Scope.Variable | null;
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
* Questions a rule asks about database transactions: the callback of a `.transaction(...)` call and the variable an identifier
|
|
3
3
|
* resolves to. The receiver of `.transaction` is judged by its TYPE (typeorm's `EntityManager` or `DataSource`), never by its name.
|
|
4
4
|
*/
|
|
5
|
-
import { infraTypeOf } from "./persistence.
|
|
6
|
-
|
|
5
|
+
import { infraTypeOf } from "./persistence.js";
|
|
7
6
|
/**
|
|
8
7
|
* The callback of a `.transaction(...)` call on a manager or data source, or null.
|
|
9
8
|
*
|
|
@@ -12,15 +11,17 @@ import { infraTypeOf } from "./persistence.mjs"
|
|
|
12
11
|
* @returns {object | null} The arrow or function expression that receives the transactional manager.
|
|
13
12
|
*/
|
|
14
13
|
export const transactionCallback = (context, node) => {
|
|
15
|
-
const callee = node.callee
|
|
16
|
-
if (callee.type !== "MemberExpression" || callee.computed)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
const callee = node.callee;
|
|
15
|
+
if (callee.type !== "MemberExpression" || callee.computed)
|
|
16
|
+
return null;
|
|
17
|
+
if (callee.property.type !== "Identifier" || callee.property.name !== "transaction")
|
|
18
|
+
return null;
|
|
19
|
+
const receiver = infraTypeOf(context, callee.object);
|
|
20
|
+
if (receiver !== "EntityManager" && receiver !== "DataSource")
|
|
21
|
+
return null;
|
|
22
|
+
const callback = node.arguments[node.arguments.length - 1];
|
|
23
|
+
return callback && (callback.type === "ArrowFunctionExpression" || callback.type === "FunctionExpression") ? callback : null;
|
|
24
|
+
};
|
|
24
25
|
/**
|
|
25
26
|
* The variable an identifier resolves to, looking outward from the identifier's own scope.
|
|
26
27
|
*
|
|
@@ -30,8 +31,9 @@ export const transactionCallback = (context, node) => {
|
|
|
30
31
|
*/
|
|
31
32
|
export const resolveVariable = (sourceCode, node) => {
|
|
32
33
|
for (let scope = sourceCode.getScope(node); scope; scope = scope.upper) {
|
|
33
|
-
const variable = scope.set.get(node.name)
|
|
34
|
-
if (variable)
|
|
34
|
+
const variable = scope.set.get(node.name);
|
|
35
|
+
if (variable)
|
|
36
|
+
return variable;
|
|
35
37
|
}
|
|
36
|
-
return null
|
|
37
|
-
}
|
|
38
|
+
return null;
|
|
39
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transport slots of the HFS manifest, in one place.
|
|
3
|
+
*
|
|
4
|
+
* A slot id is manifest vocabulary, not a path: every rule that judges "a transport file" asks this instead of spelling
|
|
5
|
+
* the ids itself. A cli command is no transport: it is an action runner of the cli feature root (slot `be.cli`). The
|
|
6
|
+
* webhook and realtime kinds hold their doors in slots of their own (`be.feature.webhooks.http`, `be.feature.realtime.graphql`, `be.feature.realtime.websocket`, `be.feature.realtime.sse`): a provider webhook and a
|
|
7
|
+
* push channel are doors like any other and answer to the door laws (default deny, bounded input, no envelope).
|
|
8
|
+
*/
|
|
9
|
+
/** The slots of the feature kinds that are doors but are not an api's `transport/<protocol>/` folders. */
|
|
10
|
+
export declare const KIND_DOOR_SLOTS: readonly string[];
|
|
11
|
+
/**
|
|
12
|
+
* True for a slot that holds transport code (`be.transport.<protocol>` and the door slots of the webhook and realtime kinds).
|
|
13
|
+
*
|
|
14
|
+
* @param {string | null} slot - A slot id from `hfs.slotOf`.
|
|
15
|
+
* @returns {boolean} Whether files of that slot are transport files.
|
|
16
|
+
*/
|
|
17
|
+
export declare const isTransportSlot: (slot: unknown) => boolean;
|
|
@@ -6,14 +6,12 @@
|
|
|
6
6
|
* webhook and realtime kinds hold their doors in slots of their own (`be.feature.webhooks.http`, `be.feature.realtime.graphql`, `be.feature.realtime.websocket`, `be.feature.realtime.sse`): a provider webhook and a
|
|
7
7
|
* push channel are doors like any other and answer to the door laws (default deny, bounded input, no envelope).
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
9
|
/** The slots of the feature kinds that are doors but are not an api's `transport/<protocol>/` folders. */
|
|
11
|
-
export const KIND_DOOR_SLOTS = Object.freeze(["be.feature.webhooks.http", "be.feature.realtime.graphql", "be.feature.realtime.websocket", "be.feature.realtime.sse"])
|
|
12
|
-
|
|
10
|
+
export const KIND_DOOR_SLOTS = Object.freeze(["be.feature.webhooks.http", "be.feature.realtime.graphql", "be.feature.realtime.websocket", "be.feature.realtime.sse"]);
|
|
13
11
|
/**
|
|
14
12
|
* True for a slot that holds transport code (`be.transport.<protocol>` and the door slots of the webhook and realtime kinds).
|
|
15
13
|
*
|
|
16
14
|
* @param {string | null} slot - A slot id from `hfs.slotOf`.
|
|
17
15
|
* @returns {boolean} Whether files of that slot are transport files.
|
|
18
16
|
*/
|
|
19
|
-
export const isTransportSlot = (slot) => typeof slot === "string" && (slot.startsWith("be.transport.") || KIND_DOOR_SLOTS.includes(slot))
|
|
17
|
+
export const isTransportSlot = (slot) => typeof slot === "string" && (slot.startsWith("be.transport.") || KIND_DOOR_SLOTS.includes(slot));
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-aware helpers: a rule asks what a node IS (its TypeScript type and where that type is declared), never what it
|
|
3
|
+
* is called. `starciBeConfig` turns typed linting on for every file (`parserOptions.projectService`), so these
|
|
4
|
+
* helpers refuse to run without the program instead of falling back to a name match: a rule that silently degrades to
|
|
5
|
+
* a heuristic is the loophole the owner closed.
|
|
6
|
+
*/
|
|
7
|
+
import ts from "typescript";
|
|
8
|
+
import type { ParserServicesWithTypeInformation } from "@typescript-eslint/utils";
|
|
9
|
+
import type { Context, Node } from "./rule.js";
|
|
10
|
+
/** The TypeScript services of the linted file: the program, its checker and the ESTree to TS node map. */
|
|
11
|
+
export interface Typed {
|
|
12
|
+
readonly program: ts.Program;
|
|
13
|
+
readonly checker: ts.TypeChecker;
|
|
14
|
+
readonly toTs: ParserServicesWithTypeInformation["esTreeNodeToTSNodeMap"]["get"];
|
|
15
|
+
}
|
|
16
|
+
/** Where a type is declared: the symbol name, the forward-slash file and the package (null for the repository's own source). */
|
|
17
|
+
export interface TypeOrigin {
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly file: string;
|
|
20
|
+
readonly module: string | null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The TypeScript services of the file being linted.
|
|
24
|
+
*
|
|
25
|
+
* @param context - The ESLint rule context.
|
|
26
|
+
* @returns The program, its checker and the ESTree to TS node map.
|
|
27
|
+
*/
|
|
28
|
+
export declare const typed: (context: Context) => Typed;
|
|
29
|
+
/**
|
|
30
|
+
* Where the type of an ESTree node (or of a type annotation node) is declared.
|
|
31
|
+
*
|
|
32
|
+
* @param context - The ESLint rule context.
|
|
33
|
+
* @param node - An expression, identifier, parameter or type node.
|
|
34
|
+
* @returns Each declaration's symbol name, forward-slash file path and package; `[]` for a primitive or an anonymous type.
|
|
35
|
+
*/
|
|
36
|
+
export declare const typeOrigins: (context: Context, node: Node) => TypeOrigin[];
|
|
37
|
+
/**
|
|
38
|
+
* Where a TypeScript type is declared, for a rule that already holds a type from the checker (an alias argument, a call signature return).
|
|
39
|
+
*
|
|
40
|
+
* @param checker - The program's type checker.
|
|
41
|
+
* @param type - A TypeScript type.
|
|
42
|
+
* @returns The declaration origins, as `typeOrigins` returns them.
|
|
43
|
+
*/
|
|
44
|
+
export declare const originsOfType: (checker: ts.TypeChecker, type: ts.Type | undefined) => TypeOrigin[];
|
|
45
|
+
/** Whether a value is a TypeScript node (it carries a numeric `kind`). */
|
|
46
|
+
export declare const isTsNode: (value: unknown) => value is ts.Node;
|
|
47
|
+
/**
|
|
48
|
+
* The TypeScript node a node holds under `key` (`body`, `initializer`, `expression`), or undefined when it holds none: the typed
|
|
49
|
+
* form of reading `declaration?.body` off a declaration whose kind is not known.
|
|
50
|
+
*/
|
|
51
|
+
export declare const childNode: (node: ts.Node | null | undefined, key: string) => ts.Node | undefined;
|
|
52
|
+
/** A symbol after alias resolution (`import { X as Y }` gives X), or the symbol itself. */
|
|
53
|
+
export declare const resolvedSymbol: (checker: ts.TypeChecker, symbol: ts.Symbol | undefined) => ts.Symbol | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* The package a declaration belongs to: the name of an enclosing `declare module "<pkg>"`, else the package directory
|
|
56
|
+
* of a `node_modules/<pkg>/` (or `node_modules/@scope/<pkg>/`) file, else null for the repository's own source.
|
|
57
|
+
*/
|
|
58
|
+
export declare const moduleOf: (declaration: ts.Node, file: string) => string | null;
|
|
59
|
+
/**
|
|
60
|
+
* True when the node's type is the named type declared in a file whose path satisfies `fromFile`.
|
|
61
|
+
*
|
|
62
|
+
* @param context - The ESLint rule context.
|
|
63
|
+
* @param node - The node to type.
|
|
64
|
+
* @param name - The declared symbol name (`EntityManager`).
|
|
65
|
+
* @param fromFile - Filter on the declaring file (a package path, a slot).
|
|
66
|
+
* @returns Whether one of the node's type origins matches.
|
|
67
|
+
*/
|
|
68
|
+
export declare const isTypeNamed: (context: Context, node: Node, name: string, fromFile?: (file: string) => boolean) => boolean;
|
|
69
|
+
/**
|
|
70
|
+
* True when the node's type is `name` declared by the package `pkg` (`typeorm`, `@nestjs/cqrs`).
|
|
71
|
+
*
|
|
72
|
+
* @param context - The ESLint rule context.
|
|
73
|
+
* @param node - The node to type.
|
|
74
|
+
* @param name - The declared symbol name.
|
|
75
|
+
* @param pkg - The package that declares it.
|
|
76
|
+
* @returns Whether the node's type is that package's `name`.
|
|
77
|
+
*/
|
|
78
|
+
export declare const isPackageType: (context: Context, node: Node, name: string, pkg: string) => boolean;
|
|
79
|
+
/**
|
|
80
|
+
* The package that declares the value an expression names (`APP_GUARD` and `Injectable` of `@nestjs/*`), after alias resolution, or
|
|
81
|
+
* null for the repository's own declaration, an unresolved name or an expression that names no symbol.
|
|
82
|
+
*
|
|
83
|
+
* @param context - The ESLint rule context.
|
|
84
|
+
* @param node - An identifier, member expression or call (its callee is read).
|
|
85
|
+
* @returns The package name.
|
|
86
|
+
*/
|
|
87
|
+
export declare const packageOfExport: (context: Context, node: Node) => string | null;
|
|
88
|
+
/**
|
|
89
|
+
* True when the node is a reference to a value the package `pkg` exports (`APP_GUARD` of `@nestjs/core`). A local constant of the same name is not.
|
|
90
|
+
*
|
|
91
|
+
* @param context - The ESLint rule context.
|
|
92
|
+
* @param node - An identifier or member expression.
|
|
93
|
+
* @param pkg - The package that must declare the referenced value.
|
|
94
|
+
* @returns Whether the node references an export of that package.
|
|
95
|
+
*/
|
|
96
|
+
export declare const isPackageExport: (context: Context, node: Node, pkg: string) => boolean;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-aware helpers: a rule asks what a node IS (its TypeScript type and where that type is declared), never what it
|
|
3
|
+
* is called. `starciBeConfig` turns typed linting on for every file (`parserOptions.projectService`), so these
|
|
4
|
+
* helpers refuse to run without the program instead of falling back to a name match: a rule that silently degrades to
|
|
5
|
+
* a heuristic is the loophole the owner closed.
|
|
6
|
+
*/
|
|
7
|
+
import ts from "typescript";
|
|
8
|
+
/**
|
|
9
|
+
* The TypeScript services of the file being linted.
|
|
10
|
+
*
|
|
11
|
+
* @param context - The ESLint rule context.
|
|
12
|
+
* @returns The program, its checker and the ESTree to TS node map.
|
|
13
|
+
*/
|
|
14
|
+
export const typed = (context) => {
|
|
15
|
+
const services = context.sourceCode?.parserServices ?? context.parserServices;
|
|
16
|
+
const program = services?.program;
|
|
17
|
+
const map = services?.esTreeNodeToTSNodeMap;
|
|
18
|
+
if (!program || !map) {
|
|
19
|
+
throw new Error(`${context.id ?? "a starci-be rule"} needs typed linting (parserOptions.projectService); lint through starciBeConfig, which turns it on`);
|
|
20
|
+
}
|
|
21
|
+
return { program, checker: program.getTypeChecker(), toTs: (node) => map.get(node) };
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The declarations of a type: those of its alias (a written `type Repo = Repository<T>`) and those of its own symbol, each
|
|
25
|
+
* after alias resolution (an imported type alias resolves to its target). Both are origins, so a rename by alias hides nothing.
|
|
26
|
+
*/
|
|
27
|
+
const declarationsOf = (checker, type) => {
|
|
28
|
+
if (!type)
|
|
29
|
+
return [];
|
|
30
|
+
return [type.aliasSymbol, type.getSymbol?.()].flatMap((symbol) => {
|
|
31
|
+
if (!symbol)
|
|
32
|
+
return [];
|
|
33
|
+
const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
34
|
+
return target.getDeclarations?.() ?? [];
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Where the type of an ESTree node (or of a type annotation node) is declared.
|
|
39
|
+
*
|
|
40
|
+
* @param context - The ESLint rule context.
|
|
41
|
+
* @param node - An expression, identifier, parameter or type node.
|
|
42
|
+
* @returns Each declaration's symbol name, forward-slash file path and package; `[]` for a primitive or an anonymous type.
|
|
43
|
+
*/
|
|
44
|
+
export const typeOrigins = (context, node) => {
|
|
45
|
+
const { checker, toTs } = typed(context);
|
|
46
|
+
const tsNode = toTs(node);
|
|
47
|
+
if (!tsNode)
|
|
48
|
+
return [];
|
|
49
|
+
return originsOfType(checker, ts.isTypeNode(tsNode) ? checker.getTypeFromTypeNode(tsNode) : checker.getTypeAtLocation(tsNode));
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Where a TypeScript type is declared, for a rule that already holds a type from the checker (an alias argument, a call signature return).
|
|
53
|
+
*
|
|
54
|
+
* @param checker - The program's type checker.
|
|
55
|
+
* @param type - A TypeScript type.
|
|
56
|
+
* @returns The declaration origins, as `typeOrigins` returns them.
|
|
57
|
+
*/
|
|
58
|
+
export const originsOfType = (checker, type) => {
|
|
59
|
+
const parts = type?.isUnion?.() ? type.types : [type];
|
|
60
|
+
return parts.flatMap((part) => declarationsOf(checker, part).map((declaration) => {
|
|
61
|
+
const file = String(declaration.getSourceFile().fileName).replace(/\\/g, "/");
|
|
62
|
+
return { name: identifierNameOf(declaration), file, module: moduleOf(declaration, file) };
|
|
63
|
+
}));
|
|
64
|
+
};
|
|
65
|
+
/** Whether a value is a TypeScript node (it carries a numeric `kind`). */
|
|
66
|
+
export const isTsNode = (value) => typeof value === "object" && value !== null && "kind" in value && typeof value.kind === "number";
|
|
67
|
+
/**
|
|
68
|
+
* The TypeScript node a node holds under `key` (`body`, `initializer`, `expression`), or undefined when it holds none: the typed
|
|
69
|
+
* form of reading `declaration?.body` off a declaration whose kind is not known.
|
|
70
|
+
*/
|
|
71
|
+
export const childNode = (node, key) => {
|
|
72
|
+
if (!node)
|
|
73
|
+
return undefined;
|
|
74
|
+
const value = Reflect.get(node, key);
|
|
75
|
+
return isTsNode(value) ? value : undefined;
|
|
76
|
+
};
|
|
77
|
+
/** A symbol after alias resolution (`import { X as Y }` gives X), or the symbol itself. */
|
|
78
|
+
export const resolvedSymbol = (checker, symbol) => (symbol && symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol);
|
|
79
|
+
/** The text of a declaration's own `name` when that name is an identifier, else "". */
|
|
80
|
+
const identifierNameOf = (declaration) => {
|
|
81
|
+
const name = "name" in declaration ? declaration.name : undefined;
|
|
82
|
+
return isTsNode(name) && ts.isIdentifier(name) ? name.text : "";
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* The package a declaration belongs to: the name of an enclosing `declare module "<pkg>"`, else the package directory
|
|
86
|
+
* of a `node_modules/<pkg>/` (or `node_modules/@scope/<pkg>/`) file, else null for the repository's own source.
|
|
87
|
+
*/
|
|
88
|
+
export const moduleOf = (declaration, file) => {
|
|
89
|
+
for (let node = declaration.parent; node; node = node.parent) {
|
|
90
|
+
if (ts.isModuleDeclaration(node) && ts.isStringLiteral(node.name))
|
|
91
|
+
return node.name.text;
|
|
92
|
+
}
|
|
93
|
+
const match = /\/node_modules\/((?:@[^/]+\/)?[^/]+)\//.exec(file);
|
|
94
|
+
return match?.[1] ?? null;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* True when the node's type is the named type declared in a file whose path satisfies `fromFile`.
|
|
98
|
+
*
|
|
99
|
+
* @param context - The ESLint rule context.
|
|
100
|
+
* @param node - The node to type.
|
|
101
|
+
* @param name - The declared symbol name (`EntityManager`).
|
|
102
|
+
* @param fromFile - Filter on the declaring file (a package path, a slot).
|
|
103
|
+
* @returns Whether one of the node's type origins matches.
|
|
104
|
+
*/
|
|
105
|
+
export const isTypeNamed = (context, node, name, fromFile = () => true) => typeOrigins(context, node).some((origin) => origin.name === name && fromFile(origin.file));
|
|
106
|
+
/**
|
|
107
|
+
* True when the node's type is `name` declared by the package `pkg` (`typeorm`, `@nestjs/cqrs`).
|
|
108
|
+
*
|
|
109
|
+
* @param context - The ESLint rule context.
|
|
110
|
+
* @param node - The node to type.
|
|
111
|
+
* @param name - The declared symbol name.
|
|
112
|
+
* @param pkg - The package that declares it.
|
|
113
|
+
* @returns Whether the node's type is that package's `name`.
|
|
114
|
+
*/
|
|
115
|
+
export const isPackageType = (context, node, name, pkg) => typeOrigins(context, node).some((origin) => origin.name === name && origin.module === pkg);
|
|
116
|
+
/**
|
|
117
|
+
* The package that declares the value an expression names (`APP_GUARD` and `Injectable` of `@nestjs/*`), after alias resolution, or
|
|
118
|
+
* null for the repository's own declaration, an unresolved name or an expression that names no symbol.
|
|
119
|
+
*
|
|
120
|
+
* @param context - The ESLint rule context.
|
|
121
|
+
* @param node - An identifier, member expression or call (its callee is read).
|
|
122
|
+
* @returns The package name.
|
|
123
|
+
*/
|
|
124
|
+
export const packageOfExport = (context, node) => {
|
|
125
|
+
const { checker, toTs } = typed(context);
|
|
126
|
+
let tsNode = toTs(node);
|
|
127
|
+
if (!tsNode)
|
|
128
|
+
return null;
|
|
129
|
+
if (ts.isCallExpression(tsNode))
|
|
130
|
+
tsNode = tsNode.expression;
|
|
131
|
+
const symbol = checker.getSymbolAtLocation(ts.isPropertyAccessExpression(tsNode) ? tsNode.name : tsNode);
|
|
132
|
+
if (!symbol)
|
|
133
|
+
return null;
|
|
134
|
+
const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
135
|
+
for (const declaration of target.getDeclarations?.() ?? []) {
|
|
136
|
+
const module = moduleOf(declaration, String(declaration.getSourceFile().fileName).replace(/\\/g, "/"));
|
|
137
|
+
if (module !== null)
|
|
138
|
+
return module;
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* True when the node is a reference to a value the package `pkg` exports (`APP_GUARD` of `@nestjs/core`). A local constant of the same name is not.
|
|
144
|
+
*
|
|
145
|
+
* @param context - The ESLint rule context.
|
|
146
|
+
* @param node - An identifier or member expression.
|
|
147
|
+
* @param pkg - The package that must declare the referenced value.
|
|
148
|
+
* @returns Whether the node references an export of that package.
|
|
149
|
+
*/
|
|
150
|
+
export const isPackageExport = (context, node, pkg) => packageOfExport(context, node) === pkg;
|