@openrewrite/rewrite 8.101.1 → 8.101.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/dist/java/rpc.d.ts.map +1 -1
- package/dist/java/rpc.js +3 -1
- package/dist/java/rpc.js.map +1 -1
- package/dist/javascript/markers.d.ts +26 -0
- package/dist/javascript/markers.d.ts.map +1 -1
- package/dist/javascript/markers.js +21 -0
- package/dist/javascript/markers.js.map +1 -1
- package/dist/javascript/name-resolver.d.ts +68 -0
- package/dist/javascript/name-resolver.d.ts.map +1 -0
- package/dist/javascript/name-resolver.js +820 -0
- package/dist/javascript/name-resolver.js.map +1 -0
- package/dist/javascript/package-exported-types.d.ts +11 -0
- package/dist/javascript/package-exported-types.d.ts.map +1 -1
- package/dist/javascript/package-exported-types.js +63 -2
- package/dist/javascript/package-exported-types.js.map +1 -1
- package/dist/javascript/parser.d.ts +4 -0
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +27 -4
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/type-mapping.d.ts +17 -1
- package/dist/javascript/type-mapping.d.ts.map +1 -1
- package/dist/javascript/type-mapping.js +53 -5
- package/dist/javascript/type-mapping.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/rpc/request/dependency-types.js +17 -6
- package/dist/rpc/request/dependency-types.js.map +1 -1
- package/package.json +1 -1
- package/src/java/rpc.ts +3 -1
- package/src/javascript/markers.ts +50 -0
- package/src/javascript/name-resolver.ts +853 -0
- package/src/javascript/package-exported-types.ts +66 -3
- package/src/javascript/parser.ts +28 -4
- package/src/javascript/type-mapping.ts +58 -6
- package/src/rpc/request/dependency-types.ts +19 -7
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
import ts from "typescript";
|
|
17
17
|
import * as path from "path";
|
|
18
18
|
import {Type} from "../java";
|
|
19
|
-
import {JavaScriptTypeMapping} from "./type-mapping";
|
|
19
|
+
import {JavaScriptTypeMapping, moduleClass} from "./type-mapping";
|
|
20
|
+
import {packageNameOf} from "./package-name";
|
|
20
21
|
import {isTypeScript} from "./tree";
|
|
21
22
|
|
|
22
23
|
// Free functions are not exported-type entries; only nominal types (and value modules, which are
|
|
@@ -34,6 +35,9 @@ const TYPE_SYMBOL_FLAGS =
|
|
|
34
35
|
* a {@code ts.Program} is rooted at each package's declaration entry, the {@code references}
|
|
35
36
|
* closure supplies node_modules resolution, and the types each own package exports are
|
|
36
37
|
* mapped with full members and methods. Types reached transitively come back shallow.
|
|
38
|
+
* A package also defines its module, any `export as namespace` name, and its own id, as empty
|
|
39
|
+
* classes: a call on the module is declared by the first, and a symbol names its declaring package
|
|
40
|
+
* by the last. An untyped package defines nothing else.
|
|
37
41
|
*
|
|
38
42
|
* @param ownArtifacts package directories whose public types should be defined (full bodies)
|
|
39
43
|
* @param references node_modules roots / sibling packages used only for symbol resolution
|
|
@@ -80,19 +84,27 @@ export function exportedTypes(ownArtifacts: string[], references: string[]): Typ
|
|
|
80
84
|
entries.push(path.normalize(entry));
|
|
81
85
|
}
|
|
82
86
|
}
|
|
87
|
+
const moduleNames = new Set(ownArtifacts.filter(ts.sys.directoryExists).flatMap(packageNames));
|
|
83
88
|
if (entries.length === 0) {
|
|
84
|
-
return [];
|
|
89
|
+
return [...moduleNames].map(moduleClass);
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
const program = ts.createProgram(entries, compilerOptions, host);
|
|
88
93
|
const checker = program.getTypeChecker();
|
|
89
94
|
const typeMapping = new JavaScriptTypeMapping(checker);
|
|
90
95
|
|
|
96
|
+
const ownFiles = ownFilesOf(ownArtifacts);
|
|
97
|
+
const declaredHere = (symbol: ts.Symbol): boolean => {
|
|
98
|
+
const files = (symbol.declarations ?? []).map(declaration => declaration.getSourceFile());
|
|
99
|
+
// A runtime declaration file adds to globals the other runtime files declare too.
|
|
100
|
+
return files.some(ownFiles) && (moduleNames.size === 0 || !files.some(program.isSourceFileDefaultLibrary));
|
|
101
|
+
};
|
|
102
|
+
|
|
91
103
|
// Lowest arity wins for a given FQN, mirroring the C# byFqn dedup.
|
|
92
104
|
const byFqn = new Map<string, Type.Class>();
|
|
93
105
|
const collect = (cls: Type.Class): void => {
|
|
94
106
|
const fqn = cls.fullyQualifiedName;
|
|
95
|
-
if (!fqn || fqn === "unknown" || fqn === "<unknown>" || fqn === Type.FUNCTION_TYPE_NAME) {
|
|
107
|
+
if (!fqn || fqn === "unknown" || fqn === "<unknown>" || fqn === Type.FUNCTION_TYPE_NAME || fqn === Type.OBJECT_TYPE_NAME) {
|
|
96
108
|
return;
|
|
97
109
|
}
|
|
98
110
|
const existing = byFqn.get(fqn);
|
|
@@ -105,6 +117,9 @@ export function exportedTypes(ownArtifacts: string[], references: string[]): Typ
|
|
|
105
117
|
for (const symbol of symbols) {
|
|
106
118
|
// Isolate per-symbol mapping: one throwing export must not abort the coordinate.
|
|
107
119
|
try {
|
|
120
|
+
if (!declaredHere(aliasTarget(checker, symbol))) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
108
123
|
const declNodes = classLikeDeclarations(symbol);
|
|
109
124
|
if (declNodes.length > 0) {
|
|
110
125
|
const cls = typeMapping.declarationType(declNodes[0]);
|
|
@@ -151,9 +166,57 @@ export function exportedTypes(ownArtifacts: string[], references: string[]): Typ
|
|
|
151
166
|
}
|
|
152
167
|
}
|
|
153
168
|
|
|
169
|
+
if (moduleNames.size > 0) {
|
|
170
|
+
for (const name of [...moduleNames, ...namespaceExports(program, ownFiles)]) {
|
|
171
|
+
if (!byFqn.has(name)) {
|
|
172
|
+
byFqn.set(name, moduleClass(name));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
154
176
|
return [...byFqn.values()];
|
|
155
177
|
}
|
|
156
178
|
|
|
179
|
+
/**
|
|
180
|
+
* What a package's own names are: the module consumers import it as, and its id. The module is the
|
|
181
|
+
* directory it is installed in, since an npm alias installs a package under a name of the alias's
|
|
182
|
+
* choosing, and a DefinitelyTyped package's module is the package it types, where its id is its own.
|
|
183
|
+
* The id is written `npm:<package>`, so that it is never a module's name: a stale `@types/uuid` defines
|
|
184
|
+
* the module `uuid`, and `uuid`, which ships its own types, is still the only package whose id that is.
|
|
185
|
+
*/
|
|
186
|
+
export function packageNames(dir: string): string[] {
|
|
187
|
+
const installed = path.resolve(dir).split(path.sep).join("/");
|
|
188
|
+
const nodeModules = "/node_modules/";
|
|
189
|
+
const at = installed.lastIndexOf(nodeModules);
|
|
190
|
+
const manifestName = packageManifest(dir)?.name;
|
|
191
|
+
const directory = at >= 0 ? installed.substring(at + nodeModules.length) : path.basename(installed);
|
|
192
|
+
const id = typeof manifestName === "string" ? manifestName : directory;
|
|
193
|
+
return [packageNameOf(at >= 0 ? directory : id), `npm:${id}`];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Whether a file is one of the artifacts, or inside one and not in a package installed beneath it. */
|
|
197
|
+
function ownFilesOf(artifacts: string[]): (file: ts.SourceFile) => boolean {
|
|
198
|
+
const roots = artifacts.flatMap(artifact => [path.resolve(artifact), ts.sys.realpath?.(artifact) ?? artifact]);
|
|
199
|
+
return file => {
|
|
200
|
+
const fileName = path.resolve(file.fileName);
|
|
201
|
+
return roots.some(root => fileName === root ||
|
|
202
|
+
containedIn(root, fileName) && !fileName.substring(root.length).includes(`${path.sep}node_modules${path.sep}`));
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** What a type alias stands for: its own name where the alias names a structure, else the type it renames. */
|
|
207
|
+
function aliasTarget(checker: ts.TypeChecker, symbol: ts.Symbol): ts.Symbol {
|
|
208
|
+
if (!(symbol.flags & ts.SymbolFlags.TypeAlias)) {
|
|
209
|
+
return symbol;
|
|
210
|
+
}
|
|
211
|
+
const declared = checker.getDeclaredTypeOfSymbol(symbol);
|
|
212
|
+
return declared.aliasSymbol ?? declared.symbol ?? symbol;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function namespaceExports(program: ts.Program, ownFiles: (file: ts.SourceFile) => boolean): string[] {
|
|
216
|
+
return program.getSourceFiles().filter(ownFiles).flatMap(file =>
|
|
217
|
+
file.statements.filter(ts.isNamespaceExportDeclaration).map(statement => statement.name.text));
|
|
218
|
+
}
|
|
219
|
+
|
|
157
220
|
/**
|
|
158
221
|
* Every package's program loads TypeScript's default lib: 93 files, 3 MB of declarations, against a
|
|
159
222
|
* typical package's few KB. Parsed and bound once and shared by every program after, as a language
|
package/src/javascript/parser.ts
CHANGED
|
@@ -49,12 +49,18 @@ import {
|
|
|
49
49
|
TextSpan
|
|
50
50
|
} from "./parser-utils";
|
|
51
51
|
import {JavaScriptTypeMapping} from "./type-mapping";
|
|
52
|
+
import {JavaScriptNameResolver} from "./name-resolver";
|
|
52
53
|
import {TsConfigResolver} from "./tsconfig";
|
|
53
54
|
import {castDraft, create as produce} from "mutative";
|
|
54
55
|
import ComputedPropertyName = JS.ComputedPropertyName;
|
|
55
56
|
import Attribute = JSX.Attribute;
|
|
56
57
|
import SpreadAttribute = JSX.SpreadAttribute;
|
|
57
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The parse option that attaches a {@link ResolvedNames} marker to each compilation unit.
|
|
61
|
+
*/
|
|
62
|
+
export const RESOLVE_NAMES = "resolveNames";
|
|
63
|
+
|
|
58
64
|
export interface JavaScriptParserOptions extends ParserOptions {
|
|
59
65
|
styles?: NamedStyles[],
|
|
60
66
|
sourceFileCache?: Map<string, ts.SourceFile>,
|
|
@@ -95,6 +101,8 @@ type WithInvalidSyntaxSlots<T> = T & {
|
|
|
95
101
|
interface CompiledProject {
|
|
96
102
|
readonly program: ts.Program;
|
|
97
103
|
readonly typeMapping: JavaScriptTypeMapping;
|
|
104
|
+
/** Present when the parse asked for {@link RESOLVE_NAMES}. */
|
|
105
|
+
readonly nameResolver?: JavaScriptNameResolver;
|
|
98
106
|
}
|
|
99
107
|
|
|
100
108
|
/**
|
|
@@ -293,7 +301,7 @@ export class JavaScriptParser extends Parser {
|
|
|
293
301
|
inputDirectories: Set<string>): Promise<SourceFile> {
|
|
294
302
|
// Built when the first of a project's inputs comes up and let go with its last, so that the
|
|
295
303
|
// programs alive at any one time are those of the projects still being worked through.
|
|
296
|
-
const {program, typeMapping} = project.compiled ??=
|
|
304
|
+
const {program, typeMapping, nameResolver} = project.compiled ??=
|
|
297
305
|
this.compile(project, carryForward, inputFiles, inputDirectories);
|
|
298
306
|
if (--project.unmapped === 0) {
|
|
299
307
|
project.compiled = undefined;
|
|
@@ -323,6 +331,9 @@ export class JavaScriptParser extends Parser {
|
|
|
323
331
|
if (this.styles) {
|
|
324
332
|
draft.markers = this.styles.reduce((m, s) => replaceMarkerByKind(m, s), draft.markers);
|
|
325
333
|
}
|
|
334
|
+
if (nameResolver) {
|
|
335
|
+
draft.markers = replaceMarkerByKind(draft.markers, nameResolver.resolvedNames(sourceFile));
|
|
336
|
+
}
|
|
326
337
|
}), input, escapeLoneSurrogates(sourceFile.getFullText()));
|
|
327
338
|
} catch (error) {
|
|
328
339
|
return this.error(input, error instanceof Error ? error : new Error('Parser threw unknown error: ' + error));
|
|
@@ -333,8 +344,8 @@ export class JavaScriptParser extends Parser {
|
|
|
333
344
|
inputDirectories: Set<string>): CompiledProject {
|
|
334
345
|
// TypeScript carries the previous program's bound files forward when the root paths match,
|
|
335
346
|
// so a parser held across parses of one path costs a fraction of a freshly built one.
|
|
336
|
-
const
|
|
337
|
-
|
|
347
|
+
const host = this.createCompilerHost(project.options, inputFiles, inputDirectories);
|
|
348
|
+
const program = ts.createProgram(project.rootNames, project.options, host, this.oldPrograms.get(project.key));
|
|
338
349
|
if (carryForward) {
|
|
339
350
|
this.oldPrograms.set(project.key, program);
|
|
340
351
|
} else {
|
|
@@ -343,7 +354,20 @@ export class JavaScriptParser extends Parser {
|
|
|
343
354
|
|
|
344
355
|
// A JavaScriptTypeMapping deduplicates by `ts.Type.id`, which is only unique within
|
|
345
356
|
// the TypeChecker that issued it, so each program needs its own.
|
|
346
|
-
|
|
357
|
+
const typeMapping = new JavaScriptTypeMapping(program.getTypeChecker(), this.relativeTo);
|
|
358
|
+
if (!this.ctx.getBoolean(RESOLVE_NAMES, false)) {
|
|
359
|
+
return {program, typeMapping};
|
|
360
|
+
}
|
|
361
|
+
const resolveModule = (specifier: ts.StringLiteralLike) => {
|
|
362
|
+
const file = specifier.getSourceFile();
|
|
363
|
+
return host.resolveModuleNameLiterals!([specifier], file.fileName, undefined, project.options, file, undefined)[0]
|
|
364
|
+
.resolvedModule?.resolvedFileName;
|
|
365
|
+
};
|
|
366
|
+
return {
|
|
367
|
+
program,
|
|
368
|
+
typeMapping,
|
|
369
|
+
nameResolver: new JavaScriptNameResolver(program, resolveModule, host.getCurrentDirectory())
|
|
370
|
+
};
|
|
347
371
|
}
|
|
348
372
|
|
|
349
373
|
private createCompilerHost(compilerOptions: ts.CompilerOptions, inputFiles: Map<SourcePath, ParserInput>,
|
|
@@ -52,6 +52,24 @@ function isGlobalAugmentation(declaration: ts.Declaration): boolean {
|
|
|
52
52
|
return ts.isModuleDeclaration(declaration) && (declaration.flags & ts.NodeFlags.GlobalAugmentation) !== 0;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* A module as a class with no body, which is what a call on it is declared by: `lodash` in `lodash.map`.
|
|
57
|
+
*/
|
|
58
|
+
export function moduleClass(fullyQualifiedName: string): Type.Class {
|
|
59
|
+
return {
|
|
60
|
+
kind: Type.Kind.Class,
|
|
61
|
+
flags: 0,
|
|
62
|
+
classKind: Type.Class.Kind.Class,
|
|
63
|
+
fullyQualifiedName,
|
|
64
|
+
typeParameters: [],
|
|
65
|
+
annotations: [],
|
|
66
|
+
interfaces: [],
|
|
67
|
+
members: [],
|
|
68
|
+
methods: [],
|
|
69
|
+
toJSON: typeSignatureToJSON
|
|
70
|
+
} as Type.Class;
|
|
71
|
+
}
|
|
72
|
+
|
|
55
73
|
export class JavaScriptTypeMapping {
|
|
56
74
|
// Primary cache: Use type signatures (preferring type.id) as cache keys
|
|
57
75
|
// TypeScript assigns stable IDs to all types, so we don't need secondary caches
|
|
@@ -70,9 +88,15 @@ export class JavaScriptTypeMapping {
|
|
|
70
88
|
private readonly functionTypesInProgress = new Set<ts.Symbol>();
|
|
71
89
|
|
|
72
90
|
|
|
91
|
+
/**
|
|
92
|
+
* @param sourceRoot What a parsed file's module is named relative to.
|
|
93
|
+
* @param sourceIsPackage Whether the source root is itself a package, whose files are named as
|
|
94
|
+
* if it had been parsed on its own, and not after the package that installed them.
|
|
95
|
+
*/
|
|
73
96
|
constructor(
|
|
74
97
|
private readonly checker: ts.TypeChecker,
|
|
75
|
-
private readonly sourceRoot?: string
|
|
98
|
+
private readonly sourceRoot?: string,
|
|
99
|
+
private readonly sourceIsPackage: boolean = false
|
|
76
100
|
) {
|
|
77
101
|
this.regExpSymbol = checker.resolveName(
|
|
78
102
|
"RegExp",
|
|
@@ -883,7 +907,7 @@ export class JavaScriptTypeMapping {
|
|
|
883
907
|
if (ts.isFunctionDeclaration(declaration)) {
|
|
884
908
|
return ts.isSourceFile(declaration.parent) || ts.isModuleBlock(declaration.parent);
|
|
885
909
|
}
|
|
886
|
-
return ts.isMethodDeclaration(declaration) &&
|
|
910
|
+
return (ts.isMethodDeclaration(declaration) || ts.isAccessor(declaration)) &&
|
|
887
911
|
!!(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Static);
|
|
888
912
|
}
|
|
889
913
|
|
|
@@ -907,7 +931,7 @@ export class JavaScriptTypeMapping {
|
|
|
907
931
|
* @returns The module name, or undefined if not from node_modules
|
|
908
932
|
*/
|
|
909
933
|
private extractModuleNameFromPath(fileName: string): string | undefined {
|
|
910
|
-
if (!
|
|
934
|
+
if (!this.isInstalled(fileName)) {
|
|
911
935
|
return undefined;
|
|
912
936
|
}
|
|
913
937
|
|
|
@@ -932,7 +956,7 @@ export class JavaScriptTypeMapping {
|
|
|
932
956
|
*/
|
|
933
957
|
private createMethodType(
|
|
934
958
|
signature: ts.Signature,
|
|
935
|
-
node: ts.MethodSignature | ts.FunctionDeclaration | ts.MethodDeclaration | ts.ConstructorDeclaration | ts.FunctionExpression | ts.CallExpression | ts.NewExpression,
|
|
959
|
+
node: ts.MethodSignature | ts.FunctionDeclaration | ts.MethodDeclaration | ts.AccessorDeclaration | ts.ConstructorDeclaration | ts.FunctionExpression | ts.CallExpression | ts.NewExpression,
|
|
936
960
|
declaringType: Type.FullyQualified,
|
|
937
961
|
name: string,
|
|
938
962
|
declaredFormalTypeNames: string[] = []
|
|
@@ -1323,6 +1347,19 @@ export class JavaScriptTypeMapping {
|
|
|
1323
1347
|
declaredFormalTypeNames.push(tp.name.getText());
|
|
1324
1348
|
}
|
|
1325
1349
|
}
|
|
1350
|
+
} else if (ts.isAccessor(node)) {
|
|
1351
|
+
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
1352
|
+
signature = this.checker.getSignatureFromDeclaration(node);
|
|
1353
|
+
if (!symbol || !signature) {
|
|
1354
|
+
return undefined;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
methodName = escapeLoneSurrogates(symbol.getName());
|
|
1358
|
+
|
|
1359
|
+
const parent = node.parent;
|
|
1360
|
+
declaringType = ts.isClassLike(parent) || ts.isInterfaceDeclaration(parent) || ts.isObjectLiteralExpression(parent) ?
|
|
1361
|
+
this.declaringType(this.checker.getTypeAtLocation(parent)) :
|
|
1362
|
+
Type.unknownType as Type.FullyQualified;
|
|
1326
1363
|
} else if (ts.isConstructorDeclaration(node)) {
|
|
1327
1364
|
// For constructor declarations
|
|
1328
1365
|
signature = this.checker.getSignatureFromDeclaration(node);
|
|
@@ -1405,7 +1442,7 @@ export class JavaScriptTypeMapping {
|
|
|
1405
1442
|
|
|
1406
1443
|
if (sourceModule) {
|
|
1407
1444
|
cleanedName = `${sourceModule}.${cleanedName}`;
|
|
1408
|
-
} else if (
|
|
1445
|
+
} else if (this.isInstalled(cleanedName)) {
|
|
1409
1446
|
// Check if this is a file path from node_modules (happens with some packages)
|
|
1410
1447
|
// TypeScript sometimes returns full paths instead of module names
|
|
1411
1448
|
// Extract the module name from the path
|
|
@@ -1500,10 +1537,25 @@ export class JavaScriptTypeMapping {
|
|
|
1500
1537
|
}
|
|
1501
1538
|
const sourceFile = declaration.parent;
|
|
1502
1539
|
const isModule = ts.isExternalModule(sourceFile) || (sourceFile as any).commonJsModuleIndicator !== undefined;
|
|
1503
|
-
return isModule && !sourceFile.isDeclarationFile && !sourceFile.fileName
|
|
1540
|
+
return isModule && !sourceFile.isDeclarationFile && !this.isInstalled(sourceFile.fileName) ?
|
|
1504
1541
|
this.sourceModuleName(sourceFile) : undefined;
|
|
1505
1542
|
}
|
|
1506
1543
|
|
|
1544
|
+
/** Whether a path is in a package installed under the source, where a package's own files are not. */
|
|
1545
|
+
private isInstalled(fileName: string): boolean {
|
|
1546
|
+
return (this.sourceIsPackage ? fileName.substring(this.sourceRoot!.length) : fileName).includes('node_modules/');
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
/** What the parser names a symbol's type, or names it after: `lodash.Foo`, `node:fs.Dirent`, `Array`. */
|
|
1550
|
+
fullyQualifiedName(symbol: ts.Symbol): string {
|
|
1551
|
+
return this.getFullyQualifiedNameFromSymbol(symbol);
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
/** What the parser names the owner of a function: the namespace around it, or else its file's module. */
|
|
1555
|
+
functionOwnerName(declaration: ts.FunctionDeclaration): string {
|
|
1556
|
+
return Type.FullyQualified.getFullyQualifiedName(this.functionOwner(declaration));
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1507
1559
|
/** The declaration of the symbol a qualified name starts from, such as a class's namespace. */
|
|
1508
1560
|
private outermostDeclaration(symbol: ts.Symbol): ts.Declaration | undefined {
|
|
1509
1561
|
let outermost = symbol;
|
|
@@ -25,7 +25,7 @@ import {lastParsedProject} from "./last-parsed-project";
|
|
|
25
25
|
|
|
26
26
|
const JAVA_TYPE_CLASS = "org.openrewrite.java.tree.JavaType$Class";
|
|
27
27
|
|
|
28
|
-
type
|
|
28
|
+
type PackageExportedTypes = typeof import("../../javascript/package-exported-types.js");
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Handle a DependencyTypes request: resolve one dependency, named by its npm coordinate, against
|
|
@@ -58,10 +58,10 @@ export class DependencyTypes {
|
|
|
58
58
|
const key = JSON.stringify([request.name, version, projectDir]);
|
|
59
59
|
if (!pending.has(key)) {
|
|
60
60
|
// Dynamic import to break the circular dependency with the javascript module.
|
|
61
|
-
const
|
|
61
|
+
const exported = await import("../../javascript/package-exported-types.js");
|
|
62
62
|
const types = version === null
|
|
63
|
-
? exportedTypes([runtimeLibFile(request.name)], [])
|
|
64
|
-
: packageTypes(request.name,
|
|
63
|
+
? exported.exportedTypes([runtimeLibFile(request.name)], [])
|
|
64
|
+
: packageTypes(request.name, exported,
|
|
65
65
|
projectDir === null ? projectNodeModules() : path.join(projectDir, "node_modules"));
|
|
66
66
|
|
|
67
67
|
const q = new RpcSendQueue(new ReferenceMap(), JAVA_TYPE_CLASS, false);
|
|
@@ -101,19 +101,31 @@ function projectNodeModules(): string {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
/** The installed package's own types, falling back to its DefinitelyTyped package when it ships none. */
|
|
104
|
-
function packageTypes(name: string, exportedTypes
|
|
104
|
+
function packageTypes(name: string, {exportedTypes, packageNames}: PackageExportedTypes,
|
|
105
|
+
nodeModules: string): Type.FullyQualified[] {
|
|
105
106
|
const pkgDir = path.join(nodeModules, ...name.split("/"));
|
|
106
107
|
if (!fs.existsSync(pkgDir)) {
|
|
107
108
|
throw new Error(`Package '${name}' is not installed under ${nodeModules}`);
|
|
108
109
|
}
|
|
109
110
|
const types = exportedTypes([pkgDir], []);
|
|
110
|
-
|
|
111
|
+
// Every package defines its own names, so those alone are a package that ships no types.
|
|
112
|
+
const own = packageNames(pkgDir);
|
|
113
|
+
if (types.some(type => !own.includes(Type.FullyQualified.getFullyQualifiedName(type)))) {
|
|
111
114
|
return types;
|
|
112
115
|
}
|
|
113
116
|
// @scope/pkg -> @types/scope__pkg
|
|
114
117
|
const typesDir = path.join(nodeModules, "@types",
|
|
115
118
|
name.startsWith("@") ? name.slice(1).replace("/", "__") : name);
|
|
116
|
-
|
|
119
|
+
if (!fs.existsSync(typesDir)) {
|
|
120
|
+
return types;
|
|
121
|
+
}
|
|
122
|
+
// The fallback stands for this package, so it goes by this package's names. The DefinitelyTyped
|
|
123
|
+
// package's id is left to that package's own table, or two tables would claim it.
|
|
124
|
+
const theirs = packageNames(typesDir).filter(typesName => !own.includes(typesName));
|
|
125
|
+
const typed = exportedTypes([typesDir], [])
|
|
126
|
+
.filter(type => !theirs.includes(Type.FullyQualified.getFullyQualifiedName(type)));
|
|
127
|
+
const named = new Set(typed.map(type => Type.FullyQualified.getFullyQualifiedName(type)));
|
|
128
|
+
return [...typed, ...types.filter(type => !named.has(Type.FullyQualified.getFullyQualifiedName(type)))];
|
|
117
129
|
}
|
|
118
130
|
|
|
119
131
|
/** A runtime declaration file (e.g. lib.dom.d.ts): the project's installed TypeScript first, else the engine's own. */
|