@proteinjs/user-server 1.24.2 → 1.24.3
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.
|
@@ -21,6 +21,16 @@ type DeclarationImport = {
|
|
|
21
21
|
file: string;
|
|
22
22
|
/** How it is named: an import / export clause, an `import('x')` type, or a `/// <reference types>`. */
|
|
23
23
|
via: 'import' | 'import()' | 'reference types';
|
|
24
|
+
/** False for a bindingless `import 'x'` — a side effect a consumer's compiler needs no declarations for. */
|
|
25
|
+
bindings: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** The package whose declaration file answers a specifier in this tree, by TypeScript's own resolver. */
|
|
29
|
+
type DeclarationProvider = {
|
|
30
|
+
/** `@types/name`, or the package itself when it ships its own declarations. */
|
|
31
|
+
packageName: string;
|
|
32
|
+
/** The declaration file resolved, relative to the package. */
|
|
33
|
+
file: string;
|
|
24
34
|
};
|
|
25
35
|
|
|
26
36
|
/**
|
|
@@ -48,6 +58,28 @@ class PublicDeclarationSurface {
|
|
|
48
58
|
return Array.from(new Set(this.imports.map((entry) => entry.packageName))).sort();
|
|
49
59
|
}
|
|
50
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Where a specifier's declarations come from in this package's own tree — the resolution a
|
|
63
|
+
* consumer's compiler repeats from its own node_modules — or undefined when no declaration file
|
|
64
|
+
* answers it (a JavaScript-only package with no `@types` twin installed resolves to its `.js`).
|
|
65
|
+
*/
|
|
66
|
+
providerOf(entry: DeclarationImport): DeclarationProvider | undefined {
|
|
67
|
+
const from = path.resolve(this.packageDir, entry.file);
|
|
68
|
+
const specifier = entry.via === 'reference types' ? `@types/${entry.specifier}` : entry.specifier;
|
|
69
|
+
const { resolvedModule } = ts.resolveModuleName(
|
|
70
|
+
specifier,
|
|
71
|
+
from,
|
|
72
|
+
{ moduleResolution: ts.ModuleResolutionKind.Node10 },
|
|
73
|
+
ts.sys
|
|
74
|
+
);
|
|
75
|
+
if (!resolvedModule || !PublicDeclarationSurface.DECLARATION_EXTENSIONS.has(resolvedModule.extension)) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const file = path.relative(this.packageDir, resolvedModule.resolvedFileName);
|
|
79
|
+
const packageName = resolvedModule.packageId?.name ?? PublicDeclarationSurface.packageUnderNodeModules(file);
|
|
80
|
+
return packageName ? { packageName, file } : undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
51
83
|
/** `@scope/name/sub` -> `@scope/name`; `name/sub` -> `name`; `node:fs` -> `fs`. */
|
|
52
84
|
static packageNameOf(specifier: string): string {
|
|
53
85
|
const bare = specifier.startsWith('node:') ? specifier.slice('node:'.length) : specifier;
|
|
@@ -55,6 +87,26 @@ class PublicDeclarationSurface {
|
|
|
55
87
|
return bare.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
56
88
|
}
|
|
57
89
|
|
|
90
|
+
private static readonly DECLARATION_EXTENSIONS = new Set<string>([
|
|
91
|
+
'.d.ts',
|
|
92
|
+
'.ts',
|
|
93
|
+
'.tsx',
|
|
94
|
+
'.d.mts',
|
|
95
|
+
'.mts',
|
|
96
|
+
'.d.cts',
|
|
97
|
+
'.cts',
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
/** The package a resolved file belongs to, read from its path's last `node_modules/` segment. */
|
|
101
|
+
private static packageUnderNodeModules(file: string): string | undefined {
|
|
102
|
+
const segments = file.split(path.sep);
|
|
103
|
+
const at = segments.lastIndexOf('node_modules');
|
|
104
|
+
if (at < 0 || at + 1 >= segments.length) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
return segments[at + 1].startsWith('@') ? segments.slice(at + 1, at + 3).join('/') : segments[at + 1];
|
|
108
|
+
}
|
|
109
|
+
|
|
58
110
|
private walk(file: string) {
|
|
59
111
|
if (this.seen.has(file)) {
|
|
60
112
|
return;
|
|
@@ -69,7 +121,7 @@ class PublicDeclarationSurface {
|
|
|
69
121
|
this.walk(this.declarationFileFor(file, reference.fileName));
|
|
70
122
|
}
|
|
71
123
|
for (const reference of source.typeReferenceDirectives) {
|
|
72
|
-
this.record(file, reference.fileName, 'reference types');
|
|
124
|
+
this.record(file, reference.fileName, 'reference types', true);
|
|
73
125
|
}
|
|
74
126
|
const visit = (node: ts.Node) => {
|
|
75
127
|
const specifier = this.specifierOf(node);
|
|
@@ -77,7 +129,7 @@ class PublicDeclarationSurface {
|
|
|
77
129
|
if (specifier.text.startsWith('.')) {
|
|
78
130
|
this.walk(this.declarationFileFor(file, specifier.text));
|
|
79
131
|
} else {
|
|
80
|
-
this.record(file, specifier.text, specifier.via);
|
|
132
|
+
this.record(file, specifier.text, specifier.via, specifier.bindings);
|
|
81
133
|
}
|
|
82
134
|
}
|
|
83
135
|
ts.forEachChild(node, visit);
|
|
@@ -85,27 +137,31 @@ class PublicDeclarationSurface {
|
|
|
85
137
|
visit(source);
|
|
86
138
|
}
|
|
87
139
|
|
|
88
|
-
private specifierOf(node: ts.Node): { text: string; via: DeclarationImport['via'] } | undefined {
|
|
89
|
-
if (
|
|
90
|
-
return
|
|
140
|
+
private specifierOf(node: ts.Node): { text: string; via: DeclarationImport['via']; bindings: boolean } | undefined {
|
|
141
|
+
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
142
|
+
return { text: node.moduleSpecifier.text, via: 'import', bindings: node.importClause !== undefined };
|
|
143
|
+
}
|
|
144
|
+
if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
145
|
+
return { text: node.moduleSpecifier.text, via: 'import', bindings: true };
|
|
91
146
|
}
|
|
92
147
|
if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
|
|
93
148
|
const { expression } = node.moduleReference;
|
|
94
|
-
return ts.isStringLiteral(expression) ? { text: expression.text, via: 'import' } : undefined;
|
|
149
|
+
return ts.isStringLiteral(expression) ? { text: expression.text, via: 'import', bindings: true } : undefined;
|
|
95
150
|
}
|
|
96
151
|
if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument)) {
|
|
97
152
|
const { literal } = node.argument;
|
|
98
|
-
return ts.isStringLiteral(literal) ? { text: literal.text, via: 'import()' } : undefined;
|
|
153
|
+
return ts.isStringLiteral(literal) ? { text: literal.text, via: 'import()', bindings: true } : undefined;
|
|
99
154
|
}
|
|
100
155
|
return undefined;
|
|
101
156
|
}
|
|
102
157
|
|
|
103
|
-
private record(file: string, specifier: string, via: DeclarationImport['via']) {
|
|
158
|
+
private record(file: string, specifier: string, via: DeclarationImport['via'], bindings: boolean) {
|
|
104
159
|
this.imports.push({
|
|
105
160
|
specifier,
|
|
106
161
|
packageName: PublicDeclarationSurface.packageNameOf(specifier),
|
|
107
162
|
file: path.relative(this.packageDir, file),
|
|
108
163
|
via,
|
|
164
|
+
bindings,
|
|
109
165
|
});
|
|
110
166
|
}
|
|
111
167
|
|
|
@@ -132,6 +188,13 @@ class PublicDeclarationSurface {
|
|
|
132
188
|
* same types whether its copy is installed from the registry (devDependencies absent) or linked
|
|
133
189
|
* from a checkout (devDependencies present), and the same whether or not a transitive package
|
|
134
190
|
* happens to hoist beside it.
|
|
191
|
+
*
|
|
192
|
+
* Two clauses, because a specifier and its declarations can come from different packages: the
|
|
193
|
+
* specifier's own package must be declared (the runtime import resolves for the consumer), AND the
|
|
194
|
+
* package whose declaration file answers it must be declared — a dependency that ships no
|
|
195
|
+
* declarations of its own (nodemailer) is typed by its `@types` twin, which is then part of the
|
|
196
|
+
* public surface and belongs beside it in `dependencies`, never in `devDependencies`. Node's
|
|
197
|
+
* builtins and the `node` types that declare them are the consumer's own environment.
|
|
135
198
|
*/
|
|
136
199
|
describe('the public declaration surface resolves against the declared dependencies alone', () => {
|
|
137
200
|
const manifest = JSON.parse(fs.readFileSync(path.join(PACKAGE_DIR, 'package.json'), 'utf8')) as PackageManifest;
|
|
@@ -161,6 +224,9 @@ describe('the public declaration surface resolves against the declared dependenc
|
|
|
161
224
|
expect(surface.packageNames()).toEqual(
|
|
162
225
|
expect.arrayContaining(['@proteinjs/reflection', '@proteinjs/user', 'moment'])
|
|
163
226
|
);
|
|
227
|
+
// The resolver half of the control: a package that ships its own declarations answers for itself.
|
|
228
|
+
const reflection = surface.imports.find((entry) => entry.packageName === '@proteinjs/reflection');
|
|
229
|
+
expect(reflection && surface.providerOf(reflection)?.packageName).toBe('@proteinjs/reflection');
|
|
164
230
|
});
|
|
165
231
|
|
|
166
232
|
it('names no package outside dependencies / peerDependencies / node builtins', () => {
|
|
@@ -172,4 +238,23 @@ describe('the public declaration surface resolves against the declared dependenc
|
|
|
172
238
|
);
|
|
173
239
|
expect(undeclared).toEqual([]);
|
|
174
240
|
});
|
|
241
|
+
|
|
242
|
+
it('resolves every declaration it names from a declared package (a dependency typed by its @types twin declares the twin too)', () => {
|
|
243
|
+
const untyped = surface.imports
|
|
244
|
+
.filter((entry) => !isBuiltin(entry) && entry.bindings)
|
|
245
|
+
.map((entry) => {
|
|
246
|
+
const provider = surface.providerOf(entry);
|
|
247
|
+
if (!provider) {
|
|
248
|
+
return [`${entry.file} names '${entry.specifier}' (${entry.via}) — no declaration file resolves for it`];
|
|
249
|
+
}
|
|
250
|
+
if (declared.has(provider.packageName)) {
|
|
251
|
+
return [];
|
|
252
|
+
}
|
|
253
|
+
return [
|
|
254
|
+
`${entry.file} names '${entry.specifier}' (${entry.via}) — its declarations come from ${provider.packageName} (${provider.file}), which is not in dependencies or peerDependencies`,
|
|
255
|
+
];
|
|
256
|
+
})
|
|
257
|
+
.reduce((all, lines) => all.concat(lines), [] as string[]);
|
|
258
|
+
expect(untyped).toEqual([]);
|
|
259
|
+
});
|
|
175
260
|
});
|