@skathi-native/cli 0.0.0 → 0.1.0-beta.2
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/aot-inputs.ts +294 -0
- package/ascii.ts +397 -0
- package/build.ts +1236 -0
- package/check/builtins.json +1219 -0
- package/check/builtins.ts +112 -0
- package/check/codes.ts +178 -0
- package/check/command.ts +105 -0
- package/check/config.ts +76 -0
- package/check/finding.ts +30 -0
- package/check/graph.ts +69 -0
- package/check/highlight.ts +40 -0
- package/check/index.ts +308 -0
- package/check/packages.ts +54 -0
- package/check/program.ts +209 -0
- package/check/report.ts +219 -0
- package/check/rules.ts +307 -0
- package/check/schema.ts +69 -0
- package/check/typing.ts +703 -0
- package/cli/assets.ts +114 -0
- package/cli/build-app.ts +587 -0
- package/cli/config.ts +646 -0
- package/cli/create.ts +327 -0
- package/cli/dev.ts +92 -0
- package/cli/elf.ts +142 -0
- package/cli/help.ts +160 -0
- package/cli/host-fetch.ts +178 -0
- package/cli/host.ts +378 -0
- package/cli/icon.ts +303 -0
- package/cli/macos-bundle.ts +451 -0
- package/cli/pe.ts +429 -0
- package/cli/platform.ts +74 -0
- package/cli/sk.ts +290 -0
- package/cli/skpk.ts +268 -0
- package/cli/stamp.ts +254 -0
- package/cli/term.ts +176 -0
- package/cli/versioninfo.ts +123 -0
- package/css-prefixes.ts +118 -0
- package/devtools/apps.ts +68 -0
- package/devtools/commands.ts +860 -0
- package/devtools/ctl.ts +166 -0
- package/package.json +52 -3
- package/prod.ts +701 -0
- package/runtime-path.ts +24 -0
- package/tailwind.ts +76 -0
- package/templates/minimal/assets/hello.txt +1 -0
- package/templates/minimal/src/App.vue +142 -0
- package/templates/minimal/src/main.ts +4 -0
- package/templates/showcase/assets/hello.txt +1 -0
- package/templates/showcase/assets/media/aurora.png +0 -0
- package/templates/showcase/assets/media/clip.mp4 +0 -0
- package/templates/showcase/assets/media/sticker.tgs +0 -0
- package/templates/showcase/src/App.vue +128 -0
- package/templates/showcase/src/components/Card.vue +41 -0
- package/templates/showcase/src/components/Icon.vue +29 -0
- package/templates/showcase/src/components/Missing.vue +26 -0
- package/templates/showcase/src/components/Page.vue +41 -0
- package/templates/showcase/src/components/Titlebar.vue +128 -0
- package/templates/showcase/src/engine.ts +33 -0
- package/templates/showcase/src/icons.ts +22 -0
- package/templates/showcase/src/main.ts +7 -0
- package/templates/showcase/src/pages/AudioPage.vue +124 -0
- package/templates/showcase/src/pages/FilesPage.vue +91 -0
- package/templates/showcase/src/pages/FormsPage.vue +139 -0
- package/templates/showcase/src/pages/GraphicsPage.vue +156 -0
- package/templates/showcase/src/pages/HomePage.vue +141 -0
- package/templates/showcase/src/pages/LocalePage.vue +71 -0
- package/templates/showcase/src/pages/MediaPage.vue +85 -0
- package/templates/showcase/src/pages/MotionPage.vue +199 -0
- package/templates/showcase/src/pages/NetworkPage.vue +103 -0
- package/templates/showcase/src/pages/PermissionsPage.vue +80 -0
- package/templates/showcase/src/pages/StoragePage.vue +117 -0
- package/templates/showcase/src/pages/StylesPage.vue +199 -0
- package/templates/showcase/src/pages/SystemPage.vue +89 -0
- package/templates/showcase/src/pages/TrayPage.vue +66 -0
- package/templates/showcase/src/pages/WindowPage.vue +128 -0
- package/templates/showcase/src/product.ts +2 -0
- package/templates/showcase/src/router.ts +149 -0
- package/templates/showcase/src/stores/counter.ts +9 -0
- package/templates/showcase/src/stores/desktop.ts +115 -0
- package/templates/showcase/src/styles.css +140 -0
- package/vitecompat.ts +1151 -0
package/check/typing.ts
ADDED
|
@@ -0,0 +1,703 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import type ts from "typescript";
|
|
4
|
+
import { nodeRange, ZERO_RANGE, type Finding, type FindingRelated } from "./finding";
|
|
5
|
+
import type { PackageInfo, Packages } from "./packages";
|
|
6
|
+
import type { TypeScript, Unit } from "./program";
|
|
7
|
+
|
|
8
|
+
export interface Score {
|
|
9
|
+
owner: PackageInfo | null;
|
|
10
|
+
declarations: number;
|
|
11
|
+
any: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Hit {
|
|
15
|
+
node: ts.Node;
|
|
16
|
+
what: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Weak {
|
|
20
|
+
declaration: ts.Declaration;
|
|
21
|
+
hits: Hit[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const MAX_DEPTH = 3;
|
|
25
|
+
/** SK3002 fires from this share of `any` declarations: a few deliberate catch-alls in a well-typed package are not worth a warning. */
|
|
26
|
+
export const WEAK_SHARE = 0.15;
|
|
27
|
+
const MAX_RELATED = 5;
|
|
28
|
+
const JS_FILE = /\.[cm]?jsx?$/i;
|
|
29
|
+
const JSDOC_TYPES = /\/\*\*[\s\S]*?@(?:type|typedef|param|returns?|callback|template)\b/;
|
|
30
|
+
const DIRECTIVE = /^(?:\/\/\/?|\/\*+)\s*@ts-(nocheck|ignore|expect-error)\b/;
|
|
31
|
+
|
|
32
|
+
function hasTypesCondition(exports: unknown): boolean {
|
|
33
|
+
if (!exports || typeof exports !== "object") return false;
|
|
34
|
+
return Object.entries(exports as Record<string, unknown>).some(
|
|
35
|
+
([key, value]) => key === "types" || key === "typings" || hasTypesCondition(value),
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const typesPackageName = (name: string) => (name.startsWith("@") ? name.slice(1).replace("/", "__") : name);
|
|
40
|
+
|
|
41
|
+
export class Typing {
|
|
42
|
+
private readonly scores = new Map<string, Score>();
|
|
43
|
+
private readonly weak = new Map<string, Weak[]>();
|
|
44
|
+
private readonly untyped = new Set<string>();
|
|
45
|
+
private readonly byProgramPath = new Map<string, Unit>();
|
|
46
|
+
private readonly caseless: boolean;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly typescript: TypeScript,
|
|
50
|
+
private readonly program: ts.Program,
|
|
51
|
+
private readonly checker: ts.TypeChecker,
|
|
52
|
+
private readonly packages: Packages,
|
|
53
|
+
private readonly units: Unit[],
|
|
54
|
+
) {
|
|
55
|
+
this.caseless = !typescript.sys.useCaseSensitiveFileNames;
|
|
56
|
+
for (const unit of units) this.byProgramPath.set(this.key(unit.programPath), unit);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private key(path: string): string {
|
|
60
|
+
return this.caseless ? resolve(path).toLowerCase() : resolve(path);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private realPath(file: ts.SourceFile): string {
|
|
64
|
+
return this.byProgramPath.get(this.key(file.fileName))?.path ?? resolve(file.fileName);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private ownerOf(file: ts.SourceFile): PackageInfo | null {
|
|
68
|
+
return this.packages.ownerOf(this.realPath(file));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private score(owner: PackageInfo | null): Score {
|
|
72
|
+
const id = owner ? `${owner.name}@${owner.version}` : "";
|
|
73
|
+
let score = this.scores.get(id);
|
|
74
|
+
if (!score) this.scores.set(id, (score = { owner, declarations: 0, any: 0 }));
|
|
75
|
+
return score;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** SK3001: packages with code in the bundle and no declarations anywhere. */
|
|
79
|
+
untypedPackages(out: Finding[]): void {
|
|
80
|
+
const byPackage = new Map<string, { owner: PackageInfo; units: Unit[] }>();
|
|
81
|
+
for (const unit of this.units) {
|
|
82
|
+
if (!unit.owner) continue;
|
|
83
|
+
const id = `${unit.owner.name}@${unit.owner.version}`;
|
|
84
|
+
const entry = byPackage.get(id) ?? { owner: unit.owner, units: [] };
|
|
85
|
+
entry.units.push(unit);
|
|
86
|
+
byPackage.set(id, entry);
|
|
87
|
+
}
|
|
88
|
+
for (const [id, { owner, units }] of byPackage) {
|
|
89
|
+
if (this.typed(owner, units)) continue;
|
|
90
|
+
this.untyped.add(id);
|
|
91
|
+
out.push({
|
|
92
|
+
code: "SK3001",
|
|
93
|
+
message: `\`${owner.name}@${owner.version}\` ships no type declarations; its ${units.length} module${units.length === 1 ? "" : "s"} in the bundle ${units.length === 1 ? "is" : "are"} compiled untyped`,
|
|
94
|
+
file: join(owner.dir, "package.json"),
|
|
95
|
+
range: ZERO_RANGE,
|
|
96
|
+
help: `Install @types/${typesPackageName(owner.name)} if it exists, or prefer a typed alternative.`,
|
|
97
|
+
related: [],
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private typed(owner: PackageInfo, units: Unit[]): boolean {
|
|
103
|
+
const json = owner.json;
|
|
104
|
+
if (typeof json.types === "string" || typeof json.typings === "string" || hasTypesCondition(json.exports))
|
|
105
|
+
return true;
|
|
106
|
+
if (units.some((u) => u.typed)) return true;
|
|
107
|
+
if (existsSync(join(owner.dir, "index.d.ts"))) return true;
|
|
108
|
+
for (const unit of units) {
|
|
109
|
+
const base = unit.path.replace(JS_FILE, "");
|
|
110
|
+
const ext = /\.([cm]?)jsx?$/i.exec(unit.path)?.[1] ?? "";
|
|
111
|
+
if (existsSync(`${base}.d.${ext}ts`) || existsSync(`${base}.d.ts`)) return true;
|
|
112
|
+
}
|
|
113
|
+
const types = join("node_modules", "@types", typesPackageName(owner.name), "package.json");
|
|
114
|
+
for (const start of [owner.dir, this.packages.appRoot]) {
|
|
115
|
+
for (let dir = start; ; dir = dirname(dir)) {
|
|
116
|
+
if (existsSync(join(dir, types))) return true;
|
|
117
|
+
if (dirname(dir) === dir) break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** SK3002 and SK3004: what the app imports, and how well it is typed. */
|
|
124
|
+
imports(out: Finding[]): void {
|
|
125
|
+
const t = this.typescript;
|
|
126
|
+
const shorthand = new Map<ts.ModuleDeclaration, FindingRelated[]>();
|
|
127
|
+
const plainJs = new Map<ts.SourceFile, FindingRelated[]>();
|
|
128
|
+
const queue: { declaration: ts.Declaration; depth: number }[] = [];
|
|
129
|
+
const untypedImports = new Map<string, Set<string>>();
|
|
130
|
+
const importSites = new Map<string, FindingRelated[]>();
|
|
131
|
+
|
|
132
|
+
const enqueueSymbol = (symbol: ts.Symbol | undefined, depth: number) => {
|
|
133
|
+
if (!symbol) return;
|
|
134
|
+
const target = symbol.flags & t.SymbolFlags.Alias ? this.safeAliased(symbol) : symbol;
|
|
135
|
+
for (const declaration of target?.declarations ?? []) queue.push({ declaration, depth });
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
for (const unit of this.units) {
|
|
139
|
+
if (unit.owner || !unit.sourceFile) continue;
|
|
140
|
+
const file = unit.sourceFile;
|
|
141
|
+
const site = (node: ts.Node): FindingRelated => ({
|
|
142
|
+
file: unit.path,
|
|
143
|
+
range: nodeRange(file, node),
|
|
144
|
+
message: "imported here",
|
|
145
|
+
});
|
|
146
|
+
const visit = (node: ts.Node): void => {
|
|
147
|
+
let specifier: ts.Expression | undefined;
|
|
148
|
+
if (t.isImportDeclaration(node) || t.isExportDeclaration(node)) specifier = node.moduleSpecifier;
|
|
149
|
+
else if (t.isImportEqualsDeclaration(node) && t.isExternalModuleReference(node.moduleReference))
|
|
150
|
+
specifier = node.moduleReference.expression;
|
|
151
|
+
else if (
|
|
152
|
+
t.isCallExpression(node) &&
|
|
153
|
+
(node.expression.kind === t.SyntaxKind.ImportKeyword ||
|
|
154
|
+
(t.isIdentifier(node.expression) && node.expression.text === "require"))
|
|
155
|
+
)
|
|
156
|
+
specifier = node.arguments[0];
|
|
157
|
+
if (specifier && t.isStringLiteralLike(specifier)) {
|
|
158
|
+
const moduleSymbol = this.checker.getSymbolAtLocation(specifier);
|
|
159
|
+
const declaration = moduleSymbol?.declarations?.[0];
|
|
160
|
+
if (moduleSymbol && declaration) {
|
|
161
|
+
if (t.isModuleDeclaration(declaration) && t.isStringLiteral(declaration.name)) {
|
|
162
|
+
if (!declaration.body || declaration.name.text === "*") {
|
|
163
|
+
const list = shorthand.get(declaration) ?? [];
|
|
164
|
+
list.push(site(specifier));
|
|
165
|
+
shorthand.set(declaration, list);
|
|
166
|
+
}
|
|
167
|
+
} else if (t.isSourceFile(declaration)) {
|
|
168
|
+
const owner = this.ownerOf(declaration);
|
|
169
|
+
const isJs = !declaration.isDeclarationFile && JS_FILE.test(declaration.fileName);
|
|
170
|
+
if (!owner) {
|
|
171
|
+
if (
|
|
172
|
+
isJs &&
|
|
173
|
+
!/[\\/]node_modules[\\/]/.test(declaration.fileName) &&
|
|
174
|
+
!JSDOC_TYPES.test(declaration.text)
|
|
175
|
+
) {
|
|
176
|
+
const list = plainJs.get(declaration) ?? [];
|
|
177
|
+
list.push(site(specifier));
|
|
178
|
+
plainJs.set(declaration, list);
|
|
179
|
+
}
|
|
180
|
+
} else if (isJs) {
|
|
181
|
+
const id = `${owner.name}@${owner.version}`;
|
|
182
|
+
const names = untypedImports.get(id) ?? new Set<string>();
|
|
183
|
+
for (const name of this.importedNames(node)) names.add(name);
|
|
184
|
+
untypedImports.set(id, names);
|
|
185
|
+
importSites.set(id, [...(importSites.get(id) ?? []), site(specifier)]);
|
|
186
|
+
this.score(owner);
|
|
187
|
+
} else {
|
|
188
|
+
this.score(owner);
|
|
189
|
+
for (const symbol of this.importedSymbols(node, moduleSymbol, file)) enqueueSymbol(symbol, 0);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
t.forEachChild(node, visit);
|
|
195
|
+
};
|
|
196
|
+
visit(file);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
for (const [id, names] of untypedImports) {
|
|
200
|
+
const score = this.scores.get(id);
|
|
201
|
+
if (!score?.owner) continue;
|
|
202
|
+
score.declarations += Math.max(names.size, 1);
|
|
203
|
+
score.any += Math.max(names.size, 1);
|
|
204
|
+
const owner = score.owner;
|
|
205
|
+
if (
|
|
206
|
+
!this.typed(
|
|
207
|
+
owner,
|
|
208
|
+
this.units.filter((u) => u.owner?.dir === owner.dir),
|
|
209
|
+
)
|
|
210
|
+
)
|
|
211
|
+
continue;
|
|
212
|
+
// It ships declarations, but its "exports" (or main) lead TypeScript to the JavaScript.
|
|
213
|
+
out.push({
|
|
214
|
+
code: "SK3004",
|
|
215
|
+
message: `\`${id}\` ships type declarations, but its imports resolve to the JavaScript (for example through an "exports" map without a "types" condition), so they are \`any\``,
|
|
216
|
+
file: join(owner.dir, "package.json"),
|
|
217
|
+
range: ZERO_RANGE,
|
|
218
|
+
help: 'Ask the package to add a "types" condition to its "exports"; meanwhile a local declare module with real types restores them.',
|
|
219
|
+
related: importSites.get(id) ?? [],
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const seen = new Set<ts.Node>();
|
|
224
|
+
while (queue.length) {
|
|
225
|
+
const { declaration, depth } = queue.pop()!;
|
|
226
|
+
if (seen.has(declaration)) continue;
|
|
227
|
+
seen.add(declaration);
|
|
228
|
+
const file = declaration.getSourceFile();
|
|
229
|
+
if (this.program.isSourceFileDefaultLibrary(file)) continue;
|
|
230
|
+
if (!file.isDeclarationFile && JS_FILE.test(file.fileName)) continue;
|
|
231
|
+
const owner = this.ownerOf(file);
|
|
232
|
+
if (!owner) continue;
|
|
233
|
+
const score = this.score(owner);
|
|
234
|
+
const hits: Hit[] = [];
|
|
235
|
+
const follow = (symbol: ts.Symbol | undefined) => {
|
|
236
|
+
if (depth < MAX_DEPTH) enqueueSymbol(symbol, depth + 1);
|
|
237
|
+
};
|
|
238
|
+
if (this.declarationHits(declaration, hits, follow)) {
|
|
239
|
+
score.declarations++;
|
|
240
|
+
if (hits.length) {
|
|
241
|
+
score.any++;
|
|
242
|
+
const id = `${owner.name}@${owner.version}`;
|
|
243
|
+
const list = this.weak.get(id) ?? [];
|
|
244
|
+
list.push({ declaration, hits });
|
|
245
|
+
this.weak.set(id, list);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (depth < MAX_DEPTH)
|
|
249
|
+
for (const member of this.members(declaration)) queue.push({ declaration: member, depth: depth + 1 });
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
for (const [id, score] of this.scores) {
|
|
253
|
+
if (!score.owner || this.untyped.has(id) || score.any < score.declarations * WEAK_SHARE) continue;
|
|
254
|
+
const list = (this.weak.get(id) ?? []).sort((a, b) => b.hits.length - a.hits.length);
|
|
255
|
+
const first = list[0];
|
|
256
|
+
if (!first) continue;
|
|
257
|
+
const related = list.slice(0, MAX_RELATED).map((w) => this.weakRelated(w));
|
|
258
|
+
out.push({
|
|
259
|
+
code: "SK3002",
|
|
260
|
+
message: `\`${score.owner.name}@${score.owner.version}\`: ${score.any} of ${score.declarations} exported declarations the app reaches use \`any\``,
|
|
261
|
+
file: related[0]!.file,
|
|
262
|
+
range: related[0]!.range,
|
|
263
|
+
related,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
for (const [declaration, sites] of shorthand) {
|
|
268
|
+
const file = declaration.getSourceFile();
|
|
269
|
+
const name = (declaration.name as ts.StringLiteral).text;
|
|
270
|
+
out.push({
|
|
271
|
+
code: "SK3004",
|
|
272
|
+
message:
|
|
273
|
+
name === "*"
|
|
274
|
+
? '`declare module "*"` makes every unresolved import `any`'
|
|
275
|
+
: `\`declare module "${name}"\` has no body, so everything imported from it is \`any\``,
|
|
276
|
+
file: this.realPath(file),
|
|
277
|
+
range: nodeRange(file, declaration),
|
|
278
|
+
help: "Remove the shorthand declaration so the package's own types apply, or give it a body with real types.",
|
|
279
|
+
related: sites,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
for (const [file, sites] of plainJs) {
|
|
283
|
+
out.push({
|
|
284
|
+
code: "SK3004",
|
|
285
|
+
message: `this JavaScript file has no JSDoc types, so what ${sites.length === 1 ? "an import takes" : `${sites.length} imports take`} from it is \`any\``,
|
|
286
|
+
file: this.realPath(file),
|
|
287
|
+
range: ZERO_RANGE,
|
|
288
|
+
help: "Convert the file to TypeScript, or annotate its exports with JSDoc types.",
|
|
289
|
+
related: sites,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private safeAliased(symbol: ts.Symbol): ts.Symbol | undefined {
|
|
295
|
+
try {
|
|
296
|
+
return this.checker.getAliasedSymbol(symbol);
|
|
297
|
+
} catch {
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private importedNames(node: ts.Node): string[] {
|
|
303
|
+
const t = this.typescript;
|
|
304
|
+
if (t.isImportDeclaration(node) && node.importClause) {
|
|
305
|
+
const clause = node.importClause;
|
|
306
|
+
const names = clause.name ? ["default"] : [];
|
|
307
|
+
if (clause.namedBindings && t.isNamedImports(clause.namedBindings))
|
|
308
|
+
names.push(...clause.namedBindings.elements.map((e) => (e.propertyName ?? e.name).text));
|
|
309
|
+
else if (clause.namedBindings) names.push("*");
|
|
310
|
+
return names;
|
|
311
|
+
}
|
|
312
|
+
if (t.isExportDeclaration(node) && node.exportClause && t.isNamedExports(node.exportClause))
|
|
313
|
+
return node.exportClause.elements.map((e) => (e.propertyName ?? e.name).text);
|
|
314
|
+
return ["*"];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private importedSymbols(node: ts.Node, moduleSymbol: ts.Symbol, file: ts.SourceFile): ts.Symbol[] {
|
|
318
|
+
const t = this.typescript;
|
|
319
|
+
const symbols: (ts.Symbol | undefined)[] = [];
|
|
320
|
+
const exportsOf = () => {
|
|
321
|
+
try {
|
|
322
|
+
return this.checker.getExportsOfModule(moduleSymbol);
|
|
323
|
+
} catch {
|
|
324
|
+
return [];
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
if (t.isImportDeclaration(node)) {
|
|
328
|
+
const clause = node.importClause;
|
|
329
|
+
if (!clause) return [];
|
|
330
|
+
if (clause.name) symbols.push(this.checker.getSymbolAtLocation(clause.name));
|
|
331
|
+
const bindings = clause.namedBindings;
|
|
332
|
+
if (bindings && t.isNamedImports(bindings))
|
|
333
|
+
for (const element of bindings.elements) symbols.push(this.checker.getSymbolAtLocation(element.name));
|
|
334
|
+
else if (bindings && t.isNamespaceImport(bindings)) {
|
|
335
|
+
const namespace = this.checker.getSymbolAtLocation(bindings.name);
|
|
336
|
+
const used: ts.Symbol[] = [];
|
|
337
|
+
const look = (n: ts.Node): void => {
|
|
338
|
+
if (
|
|
339
|
+
t.isPropertyAccessExpression(n) &&
|
|
340
|
+
t.isIdentifier(n.expression) &&
|
|
341
|
+
this.checker.getSymbolAtLocation(n.expression) === namespace
|
|
342
|
+
) {
|
|
343
|
+
const member = this.checker.getSymbolAtLocation(n.name);
|
|
344
|
+
if (member) used.push(member);
|
|
345
|
+
}
|
|
346
|
+
t.forEachChild(n, look);
|
|
347
|
+
};
|
|
348
|
+
look(file);
|
|
349
|
+
symbols.push(...(used.length ? used : exportsOf()));
|
|
350
|
+
}
|
|
351
|
+
} else if (t.isExportDeclaration(node)) {
|
|
352
|
+
if (node.exportClause && t.isNamedExports(node.exportClause))
|
|
353
|
+
for (const element of node.exportClause.elements)
|
|
354
|
+
symbols.push(this.checker.getExportSpecifierLocalTargetSymbol(element));
|
|
355
|
+
} else symbols.push(...exportsOf());
|
|
356
|
+
return symbols.filter((s): s is ts.Symbol => !!s);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
private members(declaration: ts.Declaration): ts.Declaration[] {
|
|
360
|
+
const t = this.typescript;
|
|
361
|
+
if (t.isInterfaceDeclaration(declaration) || t.isClassDeclaration(declaration) || t.isTypeLiteralNode(declaration))
|
|
362
|
+
return [...declaration.members].filter(
|
|
363
|
+
(m) => !(t.isPropertyDeclaration(m) && m.modifiers?.some((x) => x.kind === t.SyntaxKind.PrivateKeyword)),
|
|
364
|
+
);
|
|
365
|
+
if (t.isModuleDeclaration(declaration) && declaration.body && t.isModuleBlock(declaration.body)) {
|
|
366
|
+
const out: ts.Declaration[] = [];
|
|
367
|
+
for (const statement of declaration.body.statements) {
|
|
368
|
+
if (t.isVariableStatement(statement)) out.push(...statement.declarationList.declarations);
|
|
369
|
+
else if (
|
|
370
|
+
t.isFunctionDeclaration(statement) ||
|
|
371
|
+
t.isClassDeclaration(statement) ||
|
|
372
|
+
t.isInterfaceDeclaration(statement) ||
|
|
373
|
+
t.isTypeAliasDeclaration(statement)
|
|
374
|
+
)
|
|
375
|
+
out.push(statement);
|
|
376
|
+
}
|
|
377
|
+
return out;
|
|
378
|
+
}
|
|
379
|
+
return [];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Collects the `any`s of one declaration's own signature; false when it is not a declaration that counts. */
|
|
383
|
+
private declarationHits(
|
|
384
|
+
declaration: ts.Declaration,
|
|
385
|
+
hits: Hit[],
|
|
386
|
+
follow: (symbol: ts.Symbol | undefined) => void,
|
|
387
|
+
): boolean {
|
|
388
|
+
const t = this.typescript;
|
|
389
|
+
if (t.getNameOfDeclaration(declaration)?.getText().startsWith("__VLS_")) return false;
|
|
390
|
+
const file = declaration.getSourceFile();
|
|
391
|
+
const ambient = file.isDeclarationFile;
|
|
392
|
+
const scan = (type: ts.TypeNode | undefined) => {
|
|
393
|
+
if (type) this.scanType(type, hits, follow);
|
|
394
|
+
};
|
|
395
|
+
const untypedValue = (node: ts.Node, what: string) => {
|
|
396
|
+
if (ambient) hits.push({ node, what });
|
|
397
|
+
else if (this.isAny(node)) hits.push({ node, what });
|
|
398
|
+
};
|
|
399
|
+
if (
|
|
400
|
+
t.isFunctionDeclaration(declaration) ||
|
|
401
|
+
t.isMethodDeclaration(declaration) ||
|
|
402
|
+
t.isMethodSignature(declaration) ||
|
|
403
|
+
t.isCallSignatureDeclaration(declaration) ||
|
|
404
|
+
t.isConstructSignatureDeclaration(declaration) ||
|
|
405
|
+
t.isConstructorDeclaration(declaration) ||
|
|
406
|
+
t.isIndexSignatureDeclaration(declaration) ||
|
|
407
|
+
t.isGetAccessorDeclaration(declaration) ||
|
|
408
|
+
t.isSetAccessorDeclaration(declaration)
|
|
409
|
+
) {
|
|
410
|
+
for (const parameter of declaration.parameters) {
|
|
411
|
+
if (t.isIdentifier(parameter.name) && parameter.name.text === "this") {
|
|
412
|
+
scan(parameter.type);
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (parameter.type) scan(parameter.type);
|
|
416
|
+
else untypedValue(parameter, "an untyped parameter");
|
|
417
|
+
}
|
|
418
|
+
if (declaration.type) scan(declaration.type);
|
|
419
|
+
else if (!t.isConstructorDeclaration(declaration) && !t.isSetAccessorDeclaration(declaration)) {
|
|
420
|
+
if (ambient) hits.push({ node: declaration, what: "no return type" });
|
|
421
|
+
else {
|
|
422
|
+
const signature = this.checker.getSignatureFromDeclaration(declaration as ts.SignatureDeclaration);
|
|
423
|
+
if (signature && this.checker.getReturnTypeOfSignature(signature).flags & t.TypeFlags.Any)
|
|
424
|
+
hits.push({ node: declaration, what: "an `any` result" });
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
if (
|
|
430
|
+
t.isVariableDeclaration(declaration) ||
|
|
431
|
+
t.isPropertySignature(declaration) ||
|
|
432
|
+
t.isPropertyDeclaration(declaration)
|
|
433
|
+
) {
|
|
434
|
+
if (declaration.type) scan(declaration.type);
|
|
435
|
+
else if (!(t.isVariableDeclaration(declaration) && declaration.initializer && ambient))
|
|
436
|
+
untypedValue(declaration.name, "no type");
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
if (t.isTypeAliasDeclaration(declaration)) {
|
|
440
|
+
scan(declaration.type);
|
|
441
|
+
return true;
|
|
442
|
+
}
|
|
443
|
+
if (t.isInterfaceDeclaration(declaration) || t.isClassDeclaration(declaration)) {
|
|
444
|
+
for (const clause of declaration.heritageClauses ?? []) for (const type of clause.types) scan(type);
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private isAny(node: ts.Node): boolean {
|
|
451
|
+
try {
|
|
452
|
+
return (this.checker.getTypeAtLocation(node).flags & this.typescript.TypeFlags.Any) !== 0;
|
|
453
|
+
} catch {
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** `any`, and the catch-alls `Function`, `Object` and `{}`, outside type-level machinery (constraints, conditions). */
|
|
459
|
+
private scanType(root: ts.Node, hits: Hit[], follow: (symbol: ts.Symbol | undefined) => void): void {
|
|
460
|
+
const t = this.typescript;
|
|
461
|
+
/** `=> any` of a callback the caller passes in: its result is not used as a value here. */
|
|
462
|
+
const callbackResult = (node: ts.Node) => {
|
|
463
|
+
if (!(t.isFunctionTypeNode(node.parent) || t.isConstructorTypeNode(node.parent)) || node.parent.type !== node)
|
|
464
|
+
return false;
|
|
465
|
+
for (let n: ts.Node = node.parent; n !== root && n.parent; n = n.parent) if (t.isParameter(n)) return true;
|
|
466
|
+
return t.isParameter(root.parent);
|
|
467
|
+
};
|
|
468
|
+
/** The type of a value as a whole (or a union member of it), not a piece of type-level machinery. */
|
|
469
|
+
const direct = (node: ts.Node) => {
|
|
470
|
+
let n = node;
|
|
471
|
+
while (
|
|
472
|
+
t.isUnionTypeNode(n.parent) ||
|
|
473
|
+
t.isParenthesizedTypeNode(n.parent) ||
|
|
474
|
+
t.isArrayTypeNode(n.parent) ||
|
|
475
|
+
(t.isTypeOperatorNode(n.parent) && n.parent.operator === t.SyntaxKind.ReadonlyKeyword)
|
|
476
|
+
)
|
|
477
|
+
n = n.parent;
|
|
478
|
+
const p = n.parent;
|
|
479
|
+
return (
|
|
480
|
+
(t.isParameter(p) ||
|
|
481
|
+
t.isPropertySignature(p) ||
|
|
482
|
+
t.isPropertyDeclaration(p) ||
|
|
483
|
+
t.isVariableDeclaration(p) ||
|
|
484
|
+
t.isIndexSignatureDeclaration(p) ||
|
|
485
|
+
t.isFunctionTypeNode(p) ||
|
|
486
|
+
t.isMethodSignature(p) ||
|
|
487
|
+
t.isMethodDeclaration(p) ||
|
|
488
|
+
t.isFunctionDeclaration(p) ||
|
|
489
|
+
t.isCallSignatureDeclaration(p)) &&
|
|
490
|
+
"type" in p &&
|
|
491
|
+
p.type === n
|
|
492
|
+
);
|
|
493
|
+
};
|
|
494
|
+
const visit = (node: ts.Node): void => {
|
|
495
|
+
if (node.kind === t.SyntaxKind.AnyKeyword) {
|
|
496
|
+
if (!callbackResult(node)) hits.push({ node, what: "`any`" });
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (t.isTypeParameterDeclaration(node) || t.isInferTypeNode(node)) return;
|
|
500
|
+
if (t.isTypeOperatorNode(node) && node.operator === t.SyntaxKind.KeyOfKeyword) return;
|
|
501
|
+
if (t.isConditionalTypeNode(node)) {
|
|
502
|
+
visit(node.trueType);
|
|
503
|
+
visit(node.falseType);
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (t.isTypeLiteralNode(node) && node.members.length === 0) {
|
|
507
|
+
if (direct(node)) hits.push({ node, what: "`{}`" });
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (t.isTypeReferenceNode(node) || t.isExpressionWithTypeArguments(node) || t.isImportTypeNode(node)) {
|
|
511
|
+
const name = t.isTypeReferenceNode(node)
|
|
512
|
+
? node.typeName
|
|
513
|
+
: t.isImportTypeNode(node)
|
|
514
|
+
? node.qualifier
|
|
515
|
+
: node.expression;
|
|
516
|
+
if (!name) return;
|
|
517
|
+
const symbol = this.symbolOf(t.isQualifiedName(name) ? name.right : name);
|
|
518
|
+
const text = t.isIdentifier(name) ? name.text : t.isQualifiedName(name) ? name.right.text : "";
|
|
519
|
+
// vue-tsc's component declarations: the arguments of DefineComponent and its __VLS_ helpers
|
|
520
|
+
// are the template's plumbing (slot signatures, emits maps), not values the app's code reads.
|
|
521
|
+
if (text === "DefineComponent" || text.startsWith("__VLS_")) return;
|
|
522
|
+
const fromLib = symbol?.declarations?.some((d) => this.program.isSourceFileDefaultLibrary(d.getSourceFile()));
|
|
523
|
+
if ((text === "Function" || text === "Object") && fromLib && direct(node)) {
|
|
524
|
+
hits.push({ node, what: `\`${text}\`` });
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (!fromLib) follow(symbol);
|
|
528
|
+
const target = symbol && symbol.flags & t.SymbolFlags.Alias ? this.safeAliased(symbol) : symbol;
|
|
529
|
+
const parameters = target?.declarations
|
|
530
|
+
?.map((d) =>
|
|
531
|
+
"typeParameters" in d ? (d.typeParameters as ts.NodeArray<ts.TypeParameterDeclaration>) : undefined,
|
|
532
|
+
)
|
|
533
|
+
.find((list) => list?.length);
|
|
534
|
+
node.typeArguments?.forEach((argument, i) => {
|
|
535
|
+
// `any` spelled where the parameter defaults to `any` anyway (`FunctionalComponent<P, {}, any>`).
|
|
536
|
+
if (argument.kind === t.SyntaxKind.AnyKeyword && parameters?.[i]?.default?.kind === t.SyntaxKind.AnyKeyword)
|
|
537
|
+
return;
|
|
538
|
+
visit(argument);
|
|
539
|
+
});
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (t.isTypeQueryNode(node)) {
|
|
543
|
+
follow(this.symbolOf(node.exprName));
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (t.isParameter(node) && !node.type && node.getSourceFile().isDeclarationFile) {
|
|
547
|
+
hits.push({ node, what: "an untyped parameter" });
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
t.forEachChild(node, visit);
|
|
551
|
+
};
|
|
552
|
+
visit(root);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
private symbolOf(node: ts.Node): ts.Symbol | undefined {
|
|
556
|
+
try {
|
|
557
|
+
return this.checker.getSymbolAtLocation(node);
|
|
558
|
+
} catch {
|
|
559
|
+
return undefined;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
private weakRelated(weak: Weak): FindingRelated {
|
|
564
|
+
const t = this.typescript;
|
|
565
|
+
const file = weak.declaration.getSourceFile();
|
|
566
|
+
const name = t.getNameOfDeclaration(weak.declaration);
|
|
567
|
+
const parent = weak.declaration.parent;
|
|
568
|
+
const parentName =
|
|
569
|
+
parent && (t.isInterfaceDeclaration(parent) || t.isClassDeclaration(parent) || t.isTypeAliasDeclaration(parent))
|
|
570
|
+
? parent.name?.text
|
|
571
|
+
: undefined;
|
|
572
|
+
const own = name
|
|
573
|
+
? name.getText(file)
|
|
574
|
+
: t.isIndexSignatureDeclaration(weak.declaration)
|
|
575
|
+
? "[index]"
|
|
576
|
+
: t.isCallSignatureDeclaration(weak.declaration)
|
|
577
|
+
? "(call)"
|
|
578
|
+
: t.isConstructSignatureDeclaration(weak.declaration) || t.isConstructorDeclaration(weak.declaration)
|
|
579
|
+
? "constructor"
|
|
580
|
+
: "a declaration";
|
|
581
|
+
const label = parentName ? `${parentName}${own.startsWith("[") || own.startsWith("(") ? "" : "."}${own}` : own;
|
|
582
|
+
const kinds = [...new Set(weak.hits.map((h) => h.what))].join(", ");
|
|
583
|
+
return {
|
|
584
|
+
file: this.realPath(file),
|
|
585
|
+
range: nodeRange(file, weak.hits[0]!.node),
|
|
586
|
+
message: `${label}: ${weak.hits.length} × ${kinds}`,
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/** SK3003 and the app's own score. */
|
|
591
|
+
appCode(out: Finding[]): void {
|
|
592
|
+
const t = this.typescript;
|
|
593
|
+
const score = this.score(null);
|
|
594
|
+
for (const unit of this.units) {
|
|
595
|
+
if (unit.owner || !unit.sourceFile) continue;
|
|
596
|
+
const file = unit.sourceFile;
|
|
597
|
+
const hits: Hit[] = [];
|
|
598
|
+
const comments = new Set<number>();
|
|
599
|
+
const directives = (pos: number) => {
|
|
600
|
+
for (const range of [
|
|
601
|
+
...(t.getLeadingCommentRanges(file.text, pos) ?? []),
|
|
602
|
+
...(t.getTrailingCommentRanges(file.text, pos) ?? []),
|
|
603
|
+
]) {
|
|
604
|
+
if (comments.has(range.pos)) continue;
|
|
605
|
+
comments.add(range.pos);
|
|
606
|
+
const match = DIRECTIVE.exec(file.text.slice(range.pos, range.end));
|
|
607
|
+
if (match) hits.push({ node: { pos: range.pos, end: range.end } as ts.Node, what: `\`@ts-${match[1]}\`` });
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
const countValue = (node: ts.Node) => {
|
|
611
|
+
score.declarations++;
|
|
612
|
+
if (this.isAny(node)) score.any++;
|
|
613
|
+
};
|
|
614
|
+
const visit = (node: ts.Node, typeLevel: boolean): void => {
|
|
615
|
+
directives(node.pos);
|
|
616
|
+
if (unit.typed && node.kind === t.SyntaxKind.AnyKeyword && !typeLevel) {
|
|
617
|
+
const assertion = t.isAsExpression(node.parent) || t.isTypeAssertionExpression(node.parent);
|
|
618
|
+
hits.push({ node, what: assertion ? "`as any`" : "`any`" });
|
|
619
|
+
}
|
|
620
|
+
if (t.isParameter(node) && !(t.isIdentifier(node.name) && node.name.text === "this")) {
|
|
621
|
+
score.declarations++;
|
|
622
|
+
const any = this.isAny(node.name);
|
|
623
|
+
if (any) score.any++;
|
|
624
|
+
if (unit.typed && any && !node.type && !node.initializer && !this.inTypeOnly(node) && !this.contextual(node))
|
|
625
|
+
hits.push({ node: node.name, what: "an implicitly `any` parameter" });
|
|
626
|
+
} else if (t.isVariableDeclaration(node) && t.isIdentifier(node.name)) countValue(node.name);
|
|
627
|
+
else if ((t.isPropertyDeclaration(node) || t.isPropertySignature(node)) && node.name) countValue(node.name);
|
|
628
|
+
else if ((t.isFunctionDeclaration(node) || t.isMethodDeclaration(node)) && node.body) {
|
|
629
|
+
score.declarations++;
|
|
630
|
+
const signature = this.checker.getSignatureFromDeclaration(node);
|
|
631
|
+
if (signature && this.checker.getReturnTypeOfSignature(signature).flags & t.TypeFlags.Any) score.any++;
|
|
632
|
+
}
|
|
633
|
+
const level =
|
|
634
|
+
typeLevel ||
|
|
635
|
+
t.isTypeParameterDeclaration(node) ||
|
|
636
|
+
t.isInferTypeNode(node) ||
|
|
637
|
+
(t.isConditionalTypeNode(node) && false);
|
|
638
|
+
if (t.isConditionalTypeNode(node)) {
|
|
639
|
+
visit(node.checkType, true);
|
|
640
|
+
visit(node.extendsType, true);
|
|
641
|
+
visit(node.trueType, level);
|
|
642
|
+
visit(node.falseType, level);
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
t.forEachChild(node, (child) => visit(child, level));
|
|
646
|
+
};
|
|
647
|
+
visit(file, false);
|
|
648
|
+
directives(file.endOfFileToken.pos);
|
|
649
|
+
if (!hits.length) continue;
|
|
650
|
+
hits.sort((a, b) => a.node.pos - b.node.pos);
|
|
651
|
+
const range = (hit: Hit) =>
|
|
652
|
+
hit.node.parent === undefined && !("kind" in hit.node && hit.node.kind)
|
|
653
|
+
? {
|
|
654
|
+
start: file.getLineAndCharacterOfPosition(hit.node.pos),
|
|
655
|
+
end: file.getLineAndCharacterOfPosition(hit.node.end),
|
|
656
|
+
}
|
|
657
|
+
: nodeRange(file, hit.node);
|
|
658
|
+
const counts = new Map<string, number>();
|
|
659
|
+
for (const hit of hits) counts.set(hit.what, (counts.get(hit.what) ?? 0) + 1);
|
|
660
|
+
const summary = [...counts].map(([what, n]) => `${n} × ${what}`).join(", ");
|
|
661
|
+
out.push({
|
|
662
|
+
code: "SK3003",
|
|
663
|
+
message: `${hits.length} untyped place${hits.length === 1 ? "" : "s"} in this file: ${summary}`,
|
|
664
|
+
file: unit.path,
|
|
665
|
+
range: range(hits[0]!),
|
|
666
|
+
related: hits.map((hit) => ({ file: unit.path, range: range(hit), message: hit.what })),
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/** The parameter's function gets its type from where it is passed (`.catch((e) => …)`): any `any` comes from there. */
|
|
672
|
+
private contextual(parameter: ts.ParameterDeclaration): boolean {
|
|
673
|
+
const t = this.typescript;
|
|
674
|
+
const fn = parameter.parent;
|
|
675
|
+
try {
|
|
676
|
+
if (t.isArrowFunction(fn) || t.isFunctionExpression(fn)) return this.checker.getContextualType(fn) !== undefined;
|
|
677
|
+
if (t.isMethodDeclaration(fn) && t.isObjectLiteralExpression(fn.parent))
|
|
678
|
+
return this.checker.getContextualType(fn.parent) !== undefined;
|
|
679
|
+
} catch {}
|
|
680
|
+
return false;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** A parameter of a signature that has no body (an overload, a function type): no value flows through it here. */
|
|
684
|
+
private inTypeOnly(parameter: ts.ParameterDeclaration): boolean {
|
|
685
|
+
const t = this.typescript;
|
|
686
|
+
const fn = parameter.parent;
|
|
687
|
+
return (
|
|
688
|
+
!(
|
|
689
|
+
t.isFunctionDeclaration(fn) ||
|
|
690
|
+
t.isMethodDeclaration(fn) ||
|
|
691
|
+
t.isArrowFunction(fn) ||
|
|
692
|
+
t.isFunctionExpression(fn) ||
|
|
693
|
+
t.isConstructorDeclaration(fn) ||
|
|
694
|
+
t.isGetAccessorDeclaration(fn) ||
|
|
695
|
+
t.isSetAccessorDeclaration(fn)
|
|
696
|
+
) || !("body" in fn && fn.body)
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
results(): Score[] {
|
|
701
|
+
return [...this.scores.values()];
|
|
702
|
+
}
|
|
703
|
+
}
|