@codefast/cli 0.14.0 → 0.15.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 +51 -0
- package/README.md +70 -5
- package/dist/arrange/simplify/variant-classname-probe.js +2 -11
- package/dist/audit/command.js +40 -0
- package/dist/audit/comments/domain/comment-dividers.js +8 -1
- package/dist/audit/display-names/domain/display-names.js +4 -3
- package/dist/audit/domain/types.d.ts +94 -0
- package/dist/audit/engines/cli-result.d.ts +13 -0
- package/dist/audit/engines/cli-result.js +22 -0
- package/dist/audit/engines/cli-schema.d.ts +18 -0
- package/dist/audit/engines/cli-schema.js +12 -0
- package/dist/audit/engines/domain/node-floor.d.ts +34 -0
- package/dist/audit/engines/domain/node-floor.js +40 -0
- package/dist/audit/engines/domain/range-gaps.d.ts +21 -0
- package/dist/audit/engines/domain/range-gaps.js +136 -0
- package/dist/audit/engines/output.d.ts +7 -0
- package/dist/audit/engines/output.js +40 -0
- package/dist/audit/engines/prepare.d.ts +16 -0
- package/dist/audit/engines/prepare.js +12 -0
- package/dist/audit/engines/run.d.ts +18 -0
- package/dist/audit/engines/run.js +225 -0
- package/dist/audit/props/cli-result.d.ts +13 -0
- package/dist/audit/props/cli-result.js +22 -0
- package/dist/audit/props/cli-schema.d.ts +18 -0
- package/dist/audit/props/cli-schema.js +12 -0
- package/dist/audit/props/domain/props-intersection.d.ts +56 -0
- package/dist/audit/props/domain/props-intersection.js +189 -0
- package/dist/audit/props/output.d.ts +7 -0
- package/dist/audit/props/output.js +21 -0
- package/dist/audit/props/prepare.d.ts +13 -0
- package/dist/audit/props/prepare.js +9 -0
- package/dist/audit/props/run.d.ts +17 -0
- package/dist/audit/props/run.js +57 -0
- package/dist/audit/props/type-probe.d.ts +23 -0
- package/dist/audit/props/type-probe.js +85 -0
- package/dist/core/config/schema.d.ts +1 -0
- package/dist/core/config/schema.js +1 -0
- package/dist/core/type-server.d.ts +21 -0
- package/dist/core/type-server.js +32 -0
- package/package.json +6 -5
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/** Finds component props types joined with `&`, and judges each from what its parts resolved to. */
|
|
2
|
+
import { parseSync } from "oxc-parser";
|
|
3
|
+
import { isOxcNode, programStatements } from "#core/oxc-node";
|
|
4
|
+
import { firstLineOf, lineOfOffset } from "#core/source-position";
|
|
5
|
+
function unwrapParentheses(node) {
|
|
6
|
+
let current = node;
|
|
7
|
+
while (current.type === "TSParenthesizedType" && isOxcNode(current.typeAnnotation)) {
|
|
8
|
+
current = current.typeAnnotation;
|
|
9
|
+
}
|
|
10
|
+
return current;
|
|
11
|
+
}
|
|
12
|
+
function intersectionParts(sourceText, intersection) {
|
|
13
|
+
const types = Array.isArray(intersection.types) ? intersection.types.filter(isOxcNode) : [];
|
|
14
|
+
return types.map(({ start, end }) => ({ start, end, text: sourceText.slice(start, end) }));
|
|
15
|
+
}
|
|
16
|
+
function typeArgumentsOf(reference) {
|
|
17
|
+
const typeArguments = reference.typeArguments;
|
|
18
|
+
if (!isOxcNode(typeArguments) || !Array.isArray(typeArguments.params)) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
return typeArguments.params.filter(isOxcNode).map(unwrapParentheses);
|
|
22
|
+
}
|
|
23
|
+
function intersectionsOfAlias(sourceText, alias) {
|
|
24
|
+
if (!isOxcNode(alias.id) || typeof alias.id.name !== "string" || !alias.id.name.endsWith("Props")) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
if (!isOxcNode(alias.typeAnnotation)) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
const base = {
|
|
31
|
+
site: "alias",
|
|
32
|
+
line: lineOfOffset(sourceText, alias.start),
|
|
33
|
+
raw: firstLineOf(sourceText.slice(alias.start, alias.end)),
|
|
34
|
+
};
|
|
35
|
+
const body = unwrapParentheses(alias.typeAnnotation);
|
|
36
|
+
if (body.type === "TSIntersectionType") {
|
|
37
|
+
return [{ ...base, wrapper: undefined, parts: intersectionParts(sourceText, body) }];
|
|
38
|
+
}
|
|
39
|
+
if (body.type !== "TSTypeReference" || !isOxcNode(body.typeName)) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
const wrapper = sourceText.slice(body.typeName.start, body.typeName.end);
|
|
43
|
+
return typeArgumentsOf(body)
|
|
44
|
+
.filter((argument) => argument.type === "TSIntersectionType")
|
|
45
|
+
.map((argument) => ({ ...base, wrapper, parts: intersectionParts(sourceText, argument) }));
|
|
46
|
+
}
|
|
47
|
+
const FUNCTION_DECLARATION_TYPES = new Set(["FunctionDeclaration", "TSDeclareFunction"]);
|
|
48
|
+
/** JSX renders a capitalised name as a component and a lowercase one as a host element. */
|
|
49
|
+
const COMPONENT_NAME = /^[A-Z]/;
|
|
50
|
+
function isComponentName(node) {
|
|
51
|
+
return isOxcNode(node) && typeof node.name === "string" && COMPONENT_NAME.test(node.name);
|
|
52
|
+
}
|
|
53
|
+
/** Returns the functions an initialiser is: itself, or each one passed to the call it is, such as `memo(…)`. */
|
|
54
|
+
function functionsOfExpression(expression) {
|
|
55
|
+
if (expression.type === "ParenthesizedExpression" && isOxcNode(expression.expression)) {
|
|
56
|
+
return functionsOfExpression(expression.expression);
|
|
57
|
+
}
|
|
58
|
+
if (expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression") {
|
|
59
|
+
return [expression];
|
|
60
|
+
}
|
|
61
|
+
if (expression.type === "CallExpression" && Array.isArray(expression.arguments)) {
|
|
62
|
+
return expression.arguments.filter(isOxcNode).flatMap(functionsOfExpression);
|
|
63
|
+
}
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Returns the components a top-level statement declares: a capitalised function, or the functions a
|
|
68
|
+
* capitalised binding holds; a function nested in another is never one.
|
|
69
|
+
*/
|
|
70
|
+
function componentsOfStatement(statement) {
|
|
71
|
+
const declaration = (statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration") &&
|
|
72
|
+
isOxcNode(statement.declaration)
|
|
73
|
+
? statement.declaration
|
|
74
|
+
: statement;
|
|
75
|
+
if (FUNCTION_DECLARATION_TYPES.has(declaration.type)) {
|
|
76
|
+
return isComponentName(declaration.id) ? [declaration] : [];
|
|
77
|
+
}
|
|
78
|
+
if (declaration.type === "VariableDeclaration" && Array.isArray(declaration.declarations)) {
|
|
79
|
+
return declaration.declarations
|
|
80
|
+
.filter(isOxcNode)
|
|
81
|
+
.flatMap(({ id, init }) => (isComponentName(id) && isOxcNode(init) ? functionsOfExpression(init) : []));
|
|
82
|
+
}
|
|
83
|
+
// `export default memo(function Row(…))` names the component on the function itself.
|
|
84
|
+
return statement.type === "ExportDefaultDeclaration"
|
|
85
|
+
? functionsOfExpression(declaration).filter((fn) => isComponentName(fn.id))
|
|
86
|
+
: [];
|
|
87
|
+
}
|
|
88
|
+
/** Returns the type annotating a function's first parameter, past a `this` parameter and a default value. */
|
|
89
|
+
function firstParameterAnnotation(fn) {
|
|
90
|
+
const params = Array.isArray(fn.params) ? fn.params.filter(isOxcNode) : [];
|
|
91
|
+
const first = params.find((param) => !(param.type === "Identifier" && param.name === "this"));
|
|
92
|
+
const binding = first?.type === "AssignmentPattern" && isOxcNode(first.left) ? first.left : first;
|
|
93
|
+
const annotation = binding?.typeAnnotation;
|
|
94
|
+
return isOxcNode(annotation) && isOxcNode(annotation.typeAnnotation) ? annotation.typeAnnotation : undefined;
|
|
95
|
+
}
|
|
96
|
+
function intersectionsOfParameter(sourceText, fn) {
|
|
97
|
+
const annotation = firstParameterAnnotation(fn);
|
|
98
|
+
if (annotation === undefined) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
const body = unwrapParentheses(annotation);
|
|
102
|
+
if (body.type !== "TSIntersectionType") {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
return [
|
|
106
|
+
{
|
|
107
|
+
site: "parameter",
|
|
108
|
+
line: lineOfOffset(sourceText, annotation.start),
|
|
109
|
+
raw: firstLineOf(sourceText.slice(annotation.start, annotation.end)),
|
|
110
|
+
wrapper: undefined,
|
|
111
|
+
parts: intersectionParts(sourceText, body),
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
function collectNodes(node, matches, found) {
|
|
116
|
+
if (matches(node)) {
|
|
117
|
+
found.push(node);
|
|
118
|
+
}
|
|
119
|
+
for (const value of Object.values(node)) {
|
|
120
|
+
if (Array.isArray(value)) {
|
|
121
|
+
for (const item of value) {
|
|
122
|
+
if (isOxcNode(item)) {
|
|
123
|
+
collectNodes(item, matches, found);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (isOxcNode(value)) {
|
|
128
|
+
collectNodes(value, matches, found);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Finds every props type in one TypeScript source that joins types with `&`.
|
|
134
|
+
*
|
|
135
|
+
* @remarks A candidate is a `*Props` alias, or in a `.tsx` file a module-level component's first
|
|
136
|
+
* parameter whose annotation is an intersection at its top level. Syntax alone decides the candidates;
|
|
137
|
+
* whether an interface could express one is for the type checker, through {@link judgePropsIntersection}.
|
|
138
|
+
*
|
|
139
|
+
* @since 0.15.0
|
|
140
|
+
*/
|
|
141
|
+
export function findPropsIntersections(filePath, sourceText) {
|
|
142
|
+
const readsParameters = filePath.endsWith(".tsx");
|
|
143
|
+
// Most files hold no props type with an `&`, so a text probe spares them the parse.
|
|
144
|
+
if (!sourceText.includes("&") || (!readsParameters && !sourceText.includes("Props"))) {
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
const { program } = parseSync(filePath, sourceText);
|
|
148
|
+
const candidates = readsParameters ? programStatements(program).flatMap(componentsOfStatement) : [];
|
|
149
|
+
if (isOxcNode(program)) {
|
|
150
|
+
collectNodes(program, (node) => node.type === "TSTypeAliasDeclaration", candidates);
|
|
151
|
+
}
|
|
152
|
+
return candidates
|
|
153
|
+
.toSorted((a, b) => a.start - b.start)
|
|
154
|
+
.flatMap((node) => node.type === "TSTypeAliasDeclaration"
|
|
155
|
+
? intersectionsOfAlias(sourceText, node)
|
|
156
|
+
: intersectionsOfParameter(sourceText, node));
|
|
157
|
+
}
|
|
158
|
+
function listParts(parts) {
|
|
159
|
+
return parts.map(({ text }) => `\`${firstLineOf(text)}\``).join(", ");
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Judges a props intersection from the kinds its parts resolved to, or returns `undefined` when it must stay.
|
|
163
|
+
*
|
|
164
|
+
* @remarks An intersection stays a `type` when any part is a union, a type parameter or not an
|
|
165
|
+
* object; it is reported when an interface could extend every part, or when a part did not resolve.
|
|
166
|
+
*
|
|
167
|
+
* @since 0.15.0
|
|
168
|
+
*/
|
|
169
|
+
export function judgePropsIntersection(intersection, kinds) {
|
|
170
|
+
const { site, line, raw, wrapper, parts } = intersection;
|
|
171
|
+
const unresolved = parts.filter((_, index) => kinds[index] === "unresolved" || kinds[index] === undefined);
|
|
172
|
+
if (unresolved.length > 0) {
|
|
173
|
+
return {
|
|
174
|
+
line,
|
|
175
|
+
raw,
|
|
176
|
+
reason: `${listParts(unresolved)} did not resolve to a type, so whether an interface can extend it is unknown — fix the type or build what it imports`,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (!kinds.every((kind) => kind === "object")) {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
const where = site === "parameter" ? " inline" : wrapper === undefined ? "" : ` passed to \`${wrapper}\``;
|
|
183
|
+
const remedy = site === "parameter" ? "name it as an interface" : "declare an interface";
|
|
184
|
+
return {
|
|
185
|
+
line,
|
|
186
|
+
raw,
|
|
187
|
+
reason: `joins object types with \`&\`${where} — ${remedy} that extends them, so a conflicting member fails at the declaration instead of narrowing silently`,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { logger } from "#core/logger";
|
|
2
|
+
/**
|
|
3
|
+
* Human-readable props-declaration report.
|
|
4
|
+
*
|
|
5
|
+
* @since 0.15.0
|
|
6
|
+
*/
|
|
7
|
+
export function presentPropsAuditResult(result) {
|
|
8
|
+
for (const file of result.files) {
|
|
9
|
+
logger.out(`\n${file.relativePath}`);
|
|
10
|
+
for (const { line, raw, reason } of file.violations) {
|
|
11
|
+
logger.out(` ${line}: ${raw} → ${reason}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const allowlistSuffix = result.allowlistedCount > 0 ? ` (${result.allowlistedCount} allowlisted)` : "";
|
|
15
|
+
if (result.violationCount > 0) {
|
|
16
|
+
logger.out(`\n✖ ${result.violationCount} props-declaration violation(s)${allowlistSuffix}`);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
logger.out(`✓ No props intersection an interface could express across ${result.scannedFileCount} file(s)${allowlistSuffix}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AuditCommandPrelude } from "#audit/prepare";
|
|
2
|
+
import type { AppError } from "#core/errors";
|
|
3
|
+
import type { Filesystem } from "#core/filesystem/filesystem";
|
|
4
|
+
import type { Result } from "#core/result";
|
|
5
|
+
/**
|
|
6
|
+
* Loads config and resolves the scan target for `audit props`, defaulting to the repo root.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.15.0
|
|
9
|
+
*/
|
|
10
|
+
export declare function preparePropsAudit(fs: Filesystem, args: {
|
|
11
|
+
readonly currentWorkingDirectory: string;
|
|
12
|
+
readonly rawTarget: string | undefined;
|
|
13
|
+
}): Promise<Result<AuditCommandPrelude, AppError>>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { prepareRepoRootAudit } from "#audit/prepare";
|
|
2
|
+
/**
|
|
3
|
+
* Loads config and resolves the scan target for `audit props`, defaulting to the repo root.
|
|
4
|
+
*
|
|
5
|
+
* @since 0.15.0
|
|
6
|
+
*/
|
|
7
|
+
export async function preparePropsAudit(fs, args) {
|
|
8
|
+
return prepareRepoRootAudit(fs, args, (config) => config.audit?.props?.allowlist ?? []);
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PropsAuditResult } from "#audit/domain/types";
|
|
2
|
+
import type { PropsTypeProbe } from "#audit/props/type-probe";
|
|
3
|
+
import { AppError } from "#core/errors";
|
|
4
|
+
import type { Filesystem } from "#core/filesystem/filesystem";
|
|
5
|
+
import type { Result } from "#core/result";
|
|
6
|
+
/**
|
|
7
|
+
* Scans a target path for component props aliases joined with `&` that an interface could express.
|
|
8
|
+
*
|
|
9
|
+
* @remarks The type server starts only once a file holds a candidate, so a tree without one never spawns it.
|
|
10
|
+
*
|
|
11
|
+
* @since 0.15.0
|
|
12
|
+
*/
|
|
13
|
+
export declare function runPropsAudit(fs: Filesystem, args: {
|
|
14
|
+
readonly rootDir: string;
|
|
15
|
+
readonly targetPath: string;
|
|
16
|
+
readonly allowlist: ReadonlyArray<string>;
|
|
17
|
+
}, createProbe?: () => Promise<PropsTypeProbe>): Promise<Result<PropsAuditResult, AppError>>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { findPropsIntersections, judgePropsIntersection } from "#audit/props/domain/props-intersection";
|
|
3
|
+
import { createPropsTypeProbe } from "#audit/props/type-probe";
|
|
4
|
+
import { AppError, messageFrom } from "#core/errors";
|
|
5
|
+
import { err, ok } from "#core/result";
|
|
6
|
+
import { walkTsxFiles } from "#core/workspace/typescript-walk";
|
|
7
|
+
/**
|
|
8
|
+
* Scans a target path for component props aliases joined with `&` that an interface could express.
|
|
9
|
+
*
|
|
10
|
+
* @remarks The type server starts only once a file holds a candidate, so a tree without one never spawns it.
|
|
11
|
+
*
|
|
12
|
+
* @since 0.15.0
|
|
13
|
+
*/
|
|
14
|
+
export async function runPropsAudit(fs, args, createProbe = createPropsTypeProbe) {
|
|
15
|
+
let probe;
|
|
16
|
+
try {
|
|
17
|
+
const allowlist = new Set(args.allowlist);
|
|
18
|
+
const { rootDir, targetPath } = args;
|
|
19
|
+
const filesToScan = fs.statSync(targetPath).isFile() ? [targetPath] : walkTsxFiles(targetPath, fs);
|
|
20
|
+
const files = [];
|
|
21
|
+
let violationCount = 0;
|
|
22
|
+
let allowlistedCount = 0;
|
|
23
|
+
for (const absolutePath of filesToScan) {
|
|
24
|
+
const intersections = findPropsIntersections(absolutePath, fs.readFileSync(absolutePath, "utf8"));
|
|
25
|
+
if (intersections.length === 0) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
probe ??= await createProbe();
|
|
29
|
+
const classify = probe.forFile(absolutePath);
|
|
30
|
+
const relativePath = path.relative(rootDir, absolutePath).split(path.sep).join("/");
|
|
31
|
+
const remaining = intersections
|
|
32
|
+
.map((intersection) => judgePropsIntersection(intersection, classify(intersection.parts)))
|
|
33
|
+
.filter((violation) => violation !== undefined)
|
|
34
|
+
.filter(({ raw }) => {
|
|
35
|
+
const isAllowed = allowlist.has(raw) || allowlist.has(`${relativePath}:${raw}`);
|
|
36
|
+
if (isAllowed) {
|
|
37
|
+
allowlistedCount++;
|
|
38
|
+
}
|
|
39
|
+
return !isAllowed;
|
|
40
|
+
});
|
|
41
|
+
if (remaining.length === 0) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
violationCount += remaining.length;
|
|
45
|
+
files.push({ relativePath, violations: remaining });
|
|
46
|
+
}
|
|
47
|
+
return ok({ files, violationCount, allowlistedCount, scannedFileCount: filesToScan.length });
|
|
48
|
+
}
|
|
49
|
+
catch (caughtError) {
|
|
50
|
+
return err(caughtError instanceof AppError
|
|
51
|
+
? caughtError
|
|
52
|
+
: new AppError("INFRA_FAILURE", messageFrom(caughtError), caughtError));
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
probe?.dispose();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Type probe that resolves what each part of a props intersection is. */
|
|
2
|
+
import type { ConstituentKind, IntersectionPart } from "#audit/props/domain/props-intersection";
|
|
3
|
+
/** Resolves the kind of each intersection part in one file, in the order given. */
|
|
4
|
+
type ConstituentClassifier = (parts: ReadonlyArray<IntersectionPart>) => Array<ConstituentKind>;
|
|
5
|
+
/**
|
|
6
|
+
* A whole-run type probe backing `audit props`, bound to one type server.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.15.0
|
|
9
|
+
*/
|
|
10
|
+
export interface PropsTypeProbe {
|
|
11
|
+
/** Returns the file's classifier, which reports every part unresolved when no project types the file. */
|
|
12
|
+
forFile(filePath: string): ConstituentClassifier;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a type probe over the native TypeScript type server for `audit props`.
|
|
17
|
+
*
|
|
18
|
+
* @throws AppError when the `typescript` package providing the type server cannot be loaded.
|
|
19
|
+
*
|
|
20
|
+
* @since 0.15.0
|
|
21
|
+
*/
|
|
22
|
+
export declare function createPropsTypeProbe(): Promise<PropsTypeProbe>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Type probe that resolves what each part of a props intersection is. */
|
|
2
|
+
import { loadTypeServer, loadTypeServerSyntax } from "#core/type-server";
|
|
3
|
+
// ── Type inspection ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
4
|
+
/** True when a type argument leaves a mapped type's keys unknown until instantiation. */
|
|
5
|
+
function isGenericArgument(type, mappedFlag) {
|
|
6
|
+
if (type.isTypeParameter() || type.isIndexType()) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (type.isUnionType() || type.isIntersectionType()) {
|
|
10
|
+
return (type.getTypes() ?? []).some((part) => isGenericArgument(part, mappedFlag));
|
|
11
|
+
}
|
|
12
|
+
return isGenericMappedType(type, mappedFlag);
|
|
13
|
+
}
|
|
14
|
+
/** True for a mapped type over a type parameter (`Partial<T>`, `Omit<T, K>`), whose members are not statically known. */
|
|
15
|
+
function isGenericMappedType(type, mappedFlag) {
|
|
16
|
+
return (type.isObjectType() &&
|
|
17
|
+
(type.objectFlags & mappedFlag) !== 0 &&
|
|
18
|
+
type.getAliasTypeArguments().some((argument) => isGenericArgument(argument, mappedFlag)));
|
|
19
|
+
}
|
|
20
|
+
/** Classifies a resolved part by whether an interface may extend it — TypeScript's own valid-base-type rule. */
|
|
21
|
+
function kindOfType(type, mappedFlag) {
|
|
22
|
+
if (type.isErrorType()) {
|
|
23
|
+
return "unresolved";
|
|
24
|
+
}
|
|
25
|
+
if (type.isUnionType()) {
|
|
26
|
+
return "union";
|
|
27
|
+
}
|
|
28
|
+
if (type.isTypeParameter() || isGenericMappedType(type, mappedFlag)) {
|
|
29
|
+
return "type-parameter";
|
|
30
|
+
}
|
|
31
|
+
if (type.isIntersectionType()) {
|
|
32
|
+
const kinds = (type.getTypes() ?? []).map((part) => kindOfType(part, mappedFlag));
|
|
33
|
+
return kinds.includes("unresolved") ? "unresolved" : (kinds.find((kind) => kind !== "object") ?? "object");
|
|
34
|
+
}
|
|
35
|
+
return type.isObjectType() ? "object" : "other";
|
|
36
|
+
}
|
|
37
|
+
// ── Probe lifecycle ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
38
|
+
/**
|
|
39
|
+
* Creates a type probe over the native TypeScript type server for `audit props`.
|
|
40
|
+
*
|
|
41
|
+
* @throws AppError when the `typescript` package providing the type server cannot be loaded.
|
|
42
|
+
*
|
|
43
|
+
* @since 0.15.0
|
|
44
|
+
*/
|
|
45
|
+
export async function createPropsTypeProbe() {
|
|
46
|
+
const sync = await loadTypeServer("audit props");
|
|
47
|
+
const syntax = await loadTypeServerSyntax("audit props");
|
|
48
|
+
const api = new sync.API();
|
|
49
|
+
const mappedFlag = sync.ObjectFlags.Mapped;
|
|
50
|
+
/** Indexes every intersection part in a file by its span, the key oxc reports it under. */
|
|
51
|
+
function indexParts(sourceFile) {
|
|
52
|
+
const parts = new Map();
|
|
53
|
+
const visit = (node) => {
|
|
54
|
+
if (syntax.isIntersectionTypeNode(node)) {
|
|
55
|
+
for (const part of node.types) {
|
|
56
|
+
parts.set(`${syntax.getTokenPosOfNode(part, sourceFile)}:${part.end}`, part);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
node.forEachChild(visit);
|
|
60
|
+
};
|
|
61
|
+
sourceFile.forEachChild(visit);
|
|
62
|
+
return parts;
|
|
63
|
+
}
|
|
64
|
+
function classifierFor(checker, sourceFile) {
|
|
65
|
+
const partsBySpan = indexParts(sourceFile);
|
|
66
|
+
return (parts) => parts.map(({ start, end }) => {
|
|
67
|
+
const node = partsBySpan.get(`${start}:${end}`);
|
|
68
|
+
const type = node === undefined ? undefined : checker.getTypeFromTypeNode(node);
|
|
69
|
+
return type === undefined ? "unresolved" : kindOfType(type, mappedFlag);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
forFile(filePath) {
|
|
74
|
+
const project = api.updateSnapshot({ openFiles: [filePath] }).getDefaultProjectForFile(filePath);
|
|
75
|
+
const sourceFile = project?.program.getSourceFile(filePath);
|
|
76
|
+
if (project === undefined || sourceFile === undefined) {
|
|
77
|
+
return (parts) => parts.map(() => "unresolved");
|
|
78
|
+
}
|
|
79
|
+
return classifierFor(project.checker, sourceFile);
|
|
80
|
+
},
|
|
81
|
+
dispose() {
|
|
82
|
+
api.close();
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -84,6 +84,7 @@ interface CodefastAuditConfig {
|
|
|
84
84
|
imports?: CodefastAuditAllowlistConfig | undefined;
|
|
85
85
|
assertions?: CodefastAuditAllowlistConfig | undefined;
|
|
86
86
|
displayNames?: CodefastAuditAllowlistConfig | undefined;
|
|
87
|
+
props?: CodefastAuditAllowlistConfig | undefined;
|
|
87
88
|
layers?: CodefastAuditLayersConfig | undefined;
|
|
88
89
|
}
|
|
89
90
|
/**
|
|
@@ -72,6 +72,7 @@ const codefastAuditConfigSchema = z
|
|
|
72
72
|
imports: codefastAuditAllowlistConfigSchema.optional(),
|
|
73
73
|
assertions: codefastAuditAllowlistConfigSchema.optional(),
|
|
74
74
|
displayNames: codefastAuditAllowlistConfigSchema.optional(),
|
|
75
|
+
props: codefastAuditAllowlistConfigSchema.optional(),
|
|
75
76
|
layers: codefastAuditLayersConfigSchema.optional(),
|
|
76
77
|
})
|
|
77
78
|
.strict();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Loads the native TypeScript type server, and the syntax tree it serves, for the type-aware commands. */
|
|
2
|
+
import type * as TypeServerSyntax from "typescript/unstable/ast";
|
|
3
|
+
import type * as TypeServer from "typescript/unstable/sync";
|
|
4
|
+
/**
|
|
5
|
+
* Loads the native TypeScript type-server API on demand, since `typescript` is an optional peer.
|
|
6
|
+
*
|
|
7
|
+
* @param purpose - The command or flag that needs the type server, named in the error when it is missing.
|
|
8
|
+
* @throws AppError when the `typescript` package providing the type server cannot be loaded.
|
|
9
|
+
*
|
|
10
|
+
* @since 0.15.0
|
|
11
|
+
*/
|
|
12
|
+
export declare function loadTypeServer(purpose: string): Promise<typeof TypeServer>;
|
|
13
|
+
/**
|
|
14
|
+
* Loads the syntax-tree helpers for the nodes the type server returns, on the same optional peer.
|
|
15
|
+
*
|
|
16
|
+
* @param purpose - The command or flag that needs the helpers, named in the error when they are missing.
|
|
17
|
+
* @throws AppError when the `typescript` package providing them cannot be loaded.
|
|
18
|
+
*
|
|
19
|
+
* @since 0.15.0
|
|
20
|
+
*/
|
|
21
|
+
export declare function loadTypeServerSyntax(purpose: string): Promise<typeof TypeServerSyntax>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Loads the native TypeScript type server, and the syntax tree it serves, for the type-aware commands. */
|
|
2
|
+
import { AppError, messageFrom } from "#core/errors";
|
|
3
|
+
async function loadOptionalPeer(purpose, load) {
|
|
4
|
+
try {
|
|
5
|
+
return await load();
|
|
6
|
+
}
|
|
7
|
+
catch (cause) {
|
|
8
|
+
throw new AppError("INFRA_FAILURE", `${purpose} needs the "typescript" package (v7) resolvable from the target: ${messageFrom(cause)}`, cause);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Loads the native TypeScript type-server API on demand, since `typescript` is an optional peer.
|
|
13
|
+
*
|
|
14
|
+
* @param purpose - The command or flag that needs the type server, named in the error when it is missing.
|
|
15
|
+
* @throws AppError when the `typescript` package providing the type server cannot be loaded.
|
|
16
|
+
*
|
|
17
|
+
* @since 0.15.0
|
|
18
|
+
*/
|
|
19
|
+
export async function loadTypeServer(purpose) {
|
|
20
|
+
return loadOptionalPeer(purpose, () => import("typescript/unstable/sync"));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Loads the syntax-tree helpers for the nodes the type server returns, on the same optional peer.
|
|
24
|
+
*
|
|
25
|
+
* @param purpose - The command or flag that needs the helpers, named in the error when they are missing.
|
|
26
|
+
* @throws AppError when the `typescript` package providing them cannot be loaded.
|
|
27
|
+
*
|
|
28
|
+
* @since 0.15.0
|
|
29
|
+
*/
|
|
30
|
+
export async function loadTypeServerSyntax(purpose) {
|
|
31
|
+
return loadOptionalPeer(purpose, () => import("typescript/unstable/ast"));
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Developer CLI for the Codefast monorepo (arrange, audit, mirror, pack-slim, tag)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -51,13 +51,14 @@
|
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@microsoft/tsdoc": "^0.17.
|
|
54
|
+
"@microsoft/tsdoc": "^0.17.1",
|
|
55
55
|
"commander": "^15.0.0",
|
|
56
56
|
"jiti": "^2.7.0",
|
|
57
|
-
"oxc-parser": "^0.
|
|
57
|
+
"oxc-parser": "^0.153.0",
|
|
58
58
|
"picomatch": "^4.0.7",
|
|
59
|
+
"semver": "^7.8.5",
|
|
59
60
|
"yaml": "^2.9.1",
|
|
60
|
-
"zod": "^4.6.
|
|
61
|
+
"zod": "^4.6.7"
|
|
61
62
|
},
|
|
62
63
|
"peerDependencies": {
|
|
63
64
|
"typescript": ">=7.0.0"
|
|
@@ -68,6 +69,6 @@
|
|
|
68
69
|
}
|
|
69
70
|
},
|
|
70
71
|
"engines": {
|
|
71
|
-
"node": ">=
|
|
72
|
+
"node": ">=22.12.0"
|
|
72
73
|
}
|
|
73
74
|
}
|