@openrewrite/rewrite 8.101.1 → 8.101.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.
Files changed (34) hide show
  1. package/dist/java/rpc.d.ts.map +1 -1
  2. package/dist/java/rpc.js +3 -1
  3. package/dist/java/rpc.js.map +1 -1
  4. package/dist/javascript/markers.d.ts +26 -0
  5. package/dist/javascript/markers.d.ts.map +1 -1
  6. package/dist/javascript/markers.js +21 -0
  7. package/dist/javascript/markers.js.map +1 -1
  8. package/dist/javascript/name-resolver.d.ts +68 -0
  9. package/dist/javascript/name-resolver.d.ts.map +1 -0
  10. package/dist/javascript/name-resolver.js +820 -0
  11. package/dist/javascript/name-resolver.js.map +1 -0
  12. package/dist/javascript/package-exported-types.d.ts +11 -0
  13. package/dist/javascript/package-exported-types.d.ts.map +1 -1
  14. package/dist/javascript/package-exported-types.js +63 -2
  15. package/dist/javascript/package-exported-types.js.map +1 -1
  16. package/dist/javascript/parser.d.ts +4 -0
  17. package/dist/javascript/parser.d.ts.map +1 -1
  18. package/dist/javascript/parser.js +27 -4
  19. package/dist/javascript/parser.js.map +1 -1
  20. package/dist/javascript/type-mapping.d.ts +17 -1
  21. package/dist/javascript/type-mapping.d.ts.map +1 -1
  22. package/dist/javascript/type-mapping.js +53 -5
  23. package/dist/javascript/type-mapping.js.map +1 -1
  24. package/dist/rewrite-javascript-version.txt +1 -1
  25. package/dist/rpc/request/dependency-types.js +17 -6
  26. package/dist/rpc/request/dependency-types.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/java/rpc.ts +3 -1
  29. package/src/javascript/markers.ts +50 -0
  30. package/src/javascript/name-resolver.ts +853 -0
  31. package/src/javascript/package-exported-types.ts +66 -3
  32. package/src/javascript/parser.ts +28 -4
  33. package/src/javascript/type-mapping.ts +58 -6
  34. package/src/rpc/request/dependency-types.ts +19 -7
@@ -0,0 +1,820 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.JavaScriptNameResolver = void 0;
40
+ /*
41
+ * Copyright 2026 the original author or authors.
42
+ * <p>
43
+ * Licensed under the Moderne Source Available License (the "License");
44
+ * you may not use this file except in compliance with the License.
45
+ * You may obtain a copy of the License at
46
+ * <p>
47
+ * https://docs.moderne.io/licensing/moderne-source-available-license
48
+ * <p>
49
+ * Unless required by applicable law or agreed to in writing, software
50
+ * distributed under the License is distributed on an "AS IS" BASIS,
51
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
52
+ * See the License for the specific language governing permissions and
53
+ * limitations under the License.
54
+ */
55
+ const typescript_1 = __importDefault(require("typescript"));
56
+ const path = __importStar(require("path"));
57
+ const uuid_1 = require("../uuid");
58
+ const tree_1 = require("./tree");
59
+ const type_mapping_1 = require("./type-mapping");
60
+ const package_name_1 = require("./package-name");
61
+ /** A lone surrogate prints as a six character `\uXXXX` escape in place of its one code unit. */
62
+ const ESCAPED_SURROGATE_GROWTH = 5;
63
+ /** Each row of a file, with the names and the kinds they share. */
64
+ class Rows {
65
+ constructor(sourceFile) {
66
+ this.sourceFile = sourceFile;
67
+ this.names = [];
68
+ this.packages = [];
69
+ this.kinds = [];
70
+ this.spans = [];
71
+ this.nameIndices = new Map();
72
+ this.kindIndices = new Map();
73
+ this.printed = printedOffsets(sourceFile.text);
74
+ }
75
+ add(node, name, pkg, kind) {
76
+ const key = `${pkg}\0${name}`;
77
+ let nameIndex = this.nameIndices.get(key);
78
+ if (nameIndex === undefined) {
79
+ this.nameIndices.set(key, nameIndex = this.names.length);
80
+ this.names.push(name);
81
+ this.packages.push(pkg);
82
+ }
83
+ let kindIndex = this.kindIndices.get(kind);
84
+ if (kindIndex === undefined) {
85
+ this.kindIndices.set(kind, kindIndex = this.kinds.length);
86
+ this.kinds.push(kind);
87
+ }
88
+ this.spans.push(this.printed(node.getStart(this.sourceFile)), this.printed(node.getEnd()), nameIndex, kindIndex);
89
+ }
90
+ }
91
+ /** Gives a reference and its declaration one canonical name, from what the checker resolves each name to. */
92
+ class JavaScriptNameResolver {
93
+ /**
94
+ * @param resolveModule resolves a specifier as the program did, for one it did not load
95
+ * @param repositoryRoot what a path in the repository is relative to
96
+ */
97
+ constructor(program, resolveModule, repositoryRoot) {
98
+ var _a, _b, _c;
99
+ this.program = program;
100
+ this.resolveModule = resolveModule;
101
+ this.targets = new Map();
102
+ this.places = new Map();
103
+ this.mappings = new Map();
104
+ this.packageNames = new Map();
105
+ this.checker = program.getTypeChecker();
106
+ // Module resolution reports a symlinked path by what it links to.
107
+ this.roots = [...new Set([repositoryRoot, (_c = (_b = (_a = typescript_1.default.sys).realpath) === null || _b === void 0 ? void 0 : _b.call(_a, repositoryRoot)) !== null && _c !== void 0 ? _c : repositoryRoot])];
108
+ }
109
+ resolvedNames(sourceFile) {
110
+ const rows = new Rows(sourceFile);
111
+ const visit = (node) => {
112
+ this.rowsAt(node, rows);
113
+ typescript_1.default.forEachChild(node, visit);
114
+ };
115
+ visit(sourceFile);
116
+ const { names, packages, kinds, spans } = rows;
117
+ return { kind: tree_1.JS.Markers.ResolvedNames, id: (0, uuid_1.randomId)(), names, packages, kinds, spans };
118
+ }
119
+ rowsAt(node, rows) {
120
+ try {
121
+ if (typescript_1.default.isIdentifier(node) || typescript_1.default.isPrivateIdentifier(node)) {
122
+ this.nameRows(node, rows);
123
+ }
124
+ else if (typescript_1.default.isStringLiteralLike(node) && isModuleSpecifier(node)) {
125
+ this.add(rows, node, this.specifiedModule(node), "import-ref");
126
+ }
127
+ else if (typescript_1.default.isStringLiteralLike(node) || typescript_1.default.isNumericLiteral(node)) {
128
+ this.literalNameRow(node, rows);
129
+ }
130
+ else if (typescript_1.default.isConstructorDeclaration(node)) {
131
+ this.keywordRow(node, typescript_1.default.SyntaxKind.ConstructorKeyword, rows);
132
+ }
133
+ else if (isNamelessDefaultExport(node)) {
134
+ this.keywordRow(node, typescript_1.default.SyntaxKind.DefaultKeyword, rows);
135
+ }
136
+ }
137
+ catch (_a) {
138
+ // The checker asserts on some code it cannot place, and one name must not cost the file its LST.
139
+ }
140
+ }
141
+ nameRows(name, rows) {
142
+ var _a;
143
+ const parent = name.parent;
144
+ if (this.isOwnModule(name)) {
145
+ const place = this.placeOf(name.getSourceFile().fileName);
146
+ this.add(rows, name, place && moduleTarget(place), undefined);
147
+ return;
148
+ }
149
+ if (typescript_1.default.isShorthandPropertyAssignment(parent) && parent.name === name) {
150
+ this.shorthandRows(name, parent, rows);
151
+ return;
152
+ }
153
+ if (typescript_1.default.isParameter(parent) && parent.name === name && typescript_1.default.isParameterPropertyDeclaration(parent, parent.parent)) {
154
+ const [, property] = this.checker.getSymbolsOfParameterPropertyDeclaration(parent, name.text);
155
+ this.declarationRow(name, property, rows);
156
+ return;
157
+ }
158
+ const symbol = this.checker.getSymbolAtLocation(name);
159
+ if (isBoundProperty(name)) {
160
+ const element = name.parent;
161
+ if (!(symbol && symbol.flags & typescript_1.default.SymbolFlags.Alias)) {
162
+ const read = propertyOf(this.checker.getTypeAtLocation(element.parent), name.text);
163
+ if (read) {
164
+ this.referenceRow(name, read, rows, moduleSpecifierOf(element) ? "import-ref" : undefined);
165
+ }
166
+ }
167
+ if (element.propertyName === name) {
168
+ return;
169
+ }
170
+ }
171
+ if (!symbol) {
172
+ return;
173
+ }
174
+ if (isBinding(name, symbol)) {
175
+ const target = this.targetOf(symbol);
176
+ if (target && this.isUnbound(symbol, target)) {
177
+ rows.add(name, target.name, target.pkg, target.kind);
178
+ }
179
+ else {
180
+ this.add(rows, name, target, "import-ref");
181
+ }
182
+ return;
183
+ }
184
+ // The binding it is assigned to declares it. A reference here would be a call on the declaration's own line.
185
+ const assigned = (typescript_1.default.isFunctionExpression(parent) || typescript_1.default.isClassExpression(parent)) && parent.name === name &&
186
+ !isExportedValue(parent) ? this.assignedTarget(parent) : undefined;
187
+ if (assigned) {
188
+ return;
189
+ }
190
+ const expected = this.contextualProperty(name);
191
+ const fills = expected !== undefined && !((_a = expected.declarations) === null || _a === void 0 ? void 0 : _a.includes(parent));
192
+ if (fills) {
193
+ this.referenceRow(name, expected, rows);
194
+ }
195
+ if (typescript_1.default.isJsxAttribute(parent)) {
196
+ return;
197
+ }
198
+ if (declares(symbol, name)) {
199
+ this.declarationRow(name, symbol, rows);
200
+ }
201
+ else if (!fills) {
202
+ this.referenceRow(name, symbol, rows);
203
+ }
204
+ }
205
+ /** How a CommonJS file names itself, which the checker answers with whatever is being assigned. */
206
+ isOwnModule(name) {
207
+ if (name.text !== "module" && name.text !== "exports") {
208
+ return false;
209
+ }
210
+ const file = name.getSourceFile();
211
+ if (!file.commonJsModuleIndicator || typescript_1.default.getNameOfDeclaration(name.parent) === name) {
212
+ return false;
213
+ }
214
+ const bound = this.checker.resolveName(name.text, name, typescript_1.default.SymbolFlags.Value, true);
215
+ return bound === file.symbol || bound !== undefined && (bound.flags & typescript_1.default.SymbolFlags.ModuleExports) !== 0;
216
+ }
217
+ /** `{a}` reads the value `a` and declares a property of the same name, in that order, as a click on it does. */
218
+ shorthandRows(name, element, rows) {
219
+ const value = this.checker.getShorthandAssignmentValueSymbol(element);
220
+ if (value) {
221
+ this.referenceRow(name, value, rows);
222
+ }
223
+ const property = this.checker.getSymbolAtLocation(name);
224
+ if (property && !(property.flags & typescript_1.default.SymbolFlags.Alias) && this.declared(property) !== (value && this.targetOf(value))) {
225
+ this.declarationRow(name, property, rows);
226
+ }
227
+ }
228
+ /** A declaration named by a string or a number, which a reference by name still reaches. */
229
+ literalNameRow(literal, rows) {
230
+ const declaration = literal.parent;
231
+ if (typescript_1.default.getNameOfDeclaration(declaration) === literal) {
232
+ const symbol = this.symbolOfDeclaration(declaration);
233
+ if (symbol) {
234
+ this.declarationRow(literal, symbol, rows);
235
+ }
236
+ }
237
+ }
238
+ /** A declaration with no name of its own stands on its keyword, as `constructor` and `export default` do. */
239
+ keywordRow(declaration, keyword, rows) {
240
+ const token = declaration.getChildren().flatMap(child => child.kind === typescript_1.default.SyntaxKind.SyntaxList ? child.getChildren() : [child])
241
+ .find(child => child.kind === keyword);
242
+ const symbol = this.symbolOfDeclaration(declaration);
243
+ if (token && symbol) {
244
+ this.declarationRow(token, symbol, rows);
245
+ }
246
+ }
247
+ declarationRow(node, symbol, rows) {
248
+ var _a, _b, _c;
249
+ const target = this.declared(symbol);
250
+ const declaration = (_b = (_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a.find(d => nameOf(d) === node)) !== null && _b !== void 0 ? _b : (_c = symbol.declarations) === null || _c === void 0 ? void 0 : _c[0];
251
+ if (target && declaration && target === this.valueTarget(declaration)) {
252
+ this.add(rows, node, target, undefined);
253
+ }
254
+ else if (target && declaration) {
255
+ rows.add(node, target.name, target.pkg, this.kindOf(symbol, declaration));
256
+ }
257
+ }
258
+ referenceRow(node, symbol, rows, kind) {
259
+ this.add(rows, node, this.targetOf(symbol), kind);
260
+ }
261
+ /** A row for a name that refers to `target`, in the kind given, else the one its target asks for. */
262
+ add(rows, node, target, kind) {
263
+ if (!target) {
264
+ return;
265
+ }
266
+ if (kind) {
267
+ rows.add(node, target.name, target.pkg, kind);
268
+ }
269
+ else if (isConstructed(node) && (target.kind === "class" || target.kind === "interface")) {
270
+ rows.add(node, `${target.name}.<init>`, target.pkg, "constructor-call");
271
+ }
272
+ else {
273
+ rows.add(node, target.name, target.pkg, referenceKind(target.kind));
274
+ }
275
+ }
276
+ /** The property an object literal's key fills in the type the literal is expected to have. */
277
+ contextualProperty(name) {
278
+ const element = name.parent;
279
+ const inLiteral = typescript_1.default.isObjectLiteralElementLike(element) && typescript_1.default.isObjectLiteralExpression(element.parent) ||
280
+ typescript_1.default.isJsxAttribute(element);
281
+ if (!inLiteral || element.name !== name) {
282
+ return undefined;
283
+ }
284
+ const expected = this.checker.getContextualType(element.parent);
285
+ return expected && propertyOf(expected, name.text);
286
+ }
287
+ targetOf(symbol) {
288
+ var _a, _b;
289
+ if (!(symbol.flags & typescript_1.default.SymbolFlags.Alias)) {
290
+ return this.declared(symbol);
291
+ }
292
+ const aliased = this.checker.getAliasedSymbol(symbol);
293
+ if ((_a = aliased.declarations) === null || _a === void 0 ? void 0 : _a.length) {
294
+ return this.declared(aliased);
295
+ }
296
+ const binding = this.moduleBinding(symbol, new Set());
297
+ return binding && ((_b = this.resolvedModule(binding.specifier)) !== null && _b !== void 0 ? _b : this.unbound(binding.declaration));
298
+ }
299
+ /** The declaration that binds a symbol to a module, through whatever assignments and exports stand between them. */
300
+ moduleBinding(symbol, seen) {
301
+ var _a;
302
+ if (seen.has(symbol)) {
303
+ return undefined;
304
+ }
305
+ seen.add(symbol);
306
+ for (const declaration of (_a = symbol.declarations) !== null && _a !== void 0 ? _a : []) {
307
+ const specifier = moduleSpecifierOf(declaration);
308
+ if (specifier) {
309
+ return typescript_1.default.isStringLiteralLike(specifier) ? { declaration, specifier } : undefined;
310
+ }
311
+ const source = this.assignedSymbol(declaration);
312
+ const bound = source && this.moduleBinding(source, seen);
313
+ if (bound) {
314
+ return bound;
315
+ }
316
+ }
317
+ return undefined;
318
+ }
319
+ /** A binding to a module that does not resolve, which is all there is of what it binds. A re-export binds nothing here. */
320
+ unbound(binding) {
321
+ const symbol = this.symbolOfDeclaration(binding);
322
+ return symbol && !typescript_1.default.isExportSpecifier(binding) && !typescript_1.default.isNamespaceExport(binding) ? this.declared(symbol) : undefined;
323
+ }
324
+ /** Whether a binding is the target of its own uses, which only {@link unbound} makes an alias. */
325
+ isUnbound(binding, target) {
326
+ return (binding.flags & typescript_1.default.SymbolFlags.Alias) !== 0 && this.targets.get(binding) === target;
327
+ }
328
+ assignedSymbol(declaration) {
329
+ if (typescript_1.default.isExportSpecifier(declaration)) {
330
+ return this.checker.getExportSpecifierLocalTargetSymbol(declaration);
331
+ }
332
+ if (typescript_1.default.isShorthandPropertyAssignment(declaration)) {
333
+ return this.checker.getShorthandAssignmentValueSymbol(declaration);
334
+ }
335
+ const parent = declaration.parent;
336
+ const assigned = typescript_1.default.isExportAssignment(declaration) ? declaration.expression :
337
+ typescript_1.default.isBinaryExpression(declaration) ? declaration.right :
338
+ parent && typescript_1.default.isBinaryExpression(parent) && parent.left === declaration ? parent.right :
339
+ undefined;
340
+ return assigned && this.checker.getSymbolAtLocation(leftmost(assigned));
341
+ }
342
+ specifiedModule(specifier) {
343
+ var _a;
344
+ const symbol = this.checker.getSymbolAtLocation(specifier);
345
+ const declaration = (_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a[0];
346
+ if (!declaration || isAugmentation(declaration)) {
347
+ return this.resolvedModule(specifier);
348
+ }
349
+ if (typescript_1.default.isModuleDeclaration(declaration)) {
350
+ return this.declared(symbol);
351
+ }
352
+ const place = this.placeOf(declaration.getSourceFile().fileName);
353
+ return place && moduleTarget(place);
354
+ }
355
+ resolvedModule(specifier) {
356
+ const file = this.resolveModule(specifier);
357
+ const place = file === undefined ? undefined : this.placeOf(file);
358
+ return place && moduleTarget(place);
359
+ }
360
+ symbolOfDeclaration(declaration) {
361
+ return declaration.symbol;
362
+ }
363
+ declared(symbol) {
364
+ var _a;
365
+ const declaration = primaryDeclaration(symbol);
366
+ if (!declaration || typescript_1.default.isIndexSignatureDeclaration(declaration)) {
367
+ return undefined;
368
+ }
369
+ // A transient symbol, as a union's property is, has no owner, where the symbol it stands for does.
370
+ const own = symbol.flags & typescript_1.default.SymbolFlags.Transient ? (_a = this.symbolOfDeclaration(declaration)) !== null && _a !== void 0 ? _a : symbol : symbol;
371
+ if (!this.targets.has(own)) {
372
+ this.targets.set(own, undefined);
373
+ this.targets.set(own, this.isLocal(own, declaration) ? undefined : this.target(own, declaration));
374
+ }
375
+ return this.targets.get(own);
376
+ }
377
+ target(symbol, declaration) {
378
+ const place = this.placeOf(declaration.getSourceFile().fileName);
379
+ if (!place) {
380
+ return undefined;
381
+ }
382
+ const name = this.nameIn(place, symbol, declaration);
383
+ if (isNamedByFile(declaration, place)) {
384
+ return Object.assign(Object.assign({}, moduleTarget(place)), { owner: name });
385
+ }
386
+ const value = this.valueTarget(declaration);
387
+ return (value === null || value === void 0 ? void 0 : value.name) === name ? value : { name, owner: name, pkg: place.pkg, kind: this.kindOf(symbol, declaration) };
388
+ }
389
+ /** What a property holds by name, which is the one declaration when it restates it, as `module.exports = {f}` does `f`. */
390
+ valueTarget(declaration) {
391
+ const value = typescript_1.default.isShorthandPropertyAssignment(declaration) ? this.checker.getShorthandAssignmentValueSymbol(declaration) :
392
+ typescript_1.default.isPropertyAssignment(declaration) && typescript_1.default.isIdentifier(declaration.initializer) ?
393
+ this.checker.getSymbolAtLocation(declaration.initializer) : undefined;
394
+ return value && this.targetOf(value);
395
+ }
396
+ isLocal(symbol, declaration) {
397
+ var _a, _b, _c;
398
+ if (isMember(symbol, declaration)) {
399
+ const owner = symbol.parent;
400
+ const ownerDeclaration = (_a = owner === null || owner === void 0 ? void 0 : owner.valueDeclaration) !== null && _a !== void 0 ? _a : (_b = owner === null || owner === void 0 ? void 0 : owner.declarations) === null || _b === void 0 ? void 0 : _b[0];
401
+ if (owner && ownerDeclaration) {
402
+ return this.isLocal(owner, ownerDeclaration);
403
+ }
404
+ }
405
+ let child = (_c = typescript_1.default.getNameOfDeclaration(declaration)) !== null && _c !== void 0 ? _c : declaration;
406
+ for (let node = child.parent; node && !typescript_1.default.isSourceFile(node); child = node, node = node.parent) {
407
+ if (typescript_1.default.isFunctionLike(node) && node.name !== child || typescript_1.default.isMappedTypeNode(node) || typescript_1.default.isInferTypeNode(node)) {
408
+ return true;
409
+ }
410
+ }
411
+ return false;
412
+ }
413
+ nameIn(place, symbol, declaration) {
414
+ var _a;
415
+ if (typescript_1.default.isSourceFile(declaration) || isModuleExportsObject(declaration)) {
416
+ return place.module;
417
+ }
418
+ if (typescript_1.default.isModuleDeclaration(declaration) && typescript_1.default.isStringLiteral(declaration.name)) {
419
+ const augmented = isAugmentation(declaration) ? this.specifiedModule(declaration.name) : undefined;
420
+ return (_a = augmented === null || augmented === void 0 ? void 0 : augmented.owner) !== null && _a !== void 0 ? _a : (0, package_name_1.moduleName)(declaration.name.text);
421
+ }
422
+ if (place.mapping && isNamedByType(declaration)) {
423
+ return place.mapping.fullyQualifiedName(symbol);
424
+ }
425
+ if (typescript_1.default.isObjectLiteralExpression(declaration)) {
426
+ return this.objectName(declaration, place);
427
+ }
428
+ if (typescript_1.default.isTypeLiteralNode(declaration)) {
429
+ return this.literalName(declaration, place);
430
+ }
431
+ if (isExpression(declaration)) {
432
+ return this.expressionName(declaration, place);
433
+ }
434
+ if (typescript_1.default.isBinaryExpression(declaration) && isModuleExports(declaration.left)) {
435
+ return this.exportedValueName(declaration.right, place);
436
+ }
437
+ return `${this.ownerName(place, symbol, declaration)}.${lastSegment(declaration)}`;
438
+ }
439
+ ownerName(place, symbol, declaration) {
440
+ var _a, _b;
441
+ if (place.mapping && typescript_1.default.isFunctionDeclaration(declaration)) {
442
+ return place.mapping.functionOwnerName(declaration);
443
+ }
444
+ const owner = this.ownerOf(symbol, declaration);
445
+ return owner && !isGlobalScope(owner) ? (_b = (_a = this.declared(owner)) === null || _a === void 0 ? void 0 : _a.owner) !== null && _b !== void 0 ? _b : place.module : place.module;
446
+ }
447
+ ownerOf(symbol, declaration) {
448
+ var _a;
449
+ if (typescript_1.default.isTypeParameterDeclaration(declaration)) {
450
+ return this.symbolOfDeclaration(declaration.parent);
451
+ }
452
+ const namespace = typescript_1.default.findAncestor(declaration.parent, typescript_1.default.isModuleDeclaration);
453
+ return (_a = symbol.parent) !== null && _a !== void 0 ? _a : (namespace && this.symbolOfDeclaration(namespace));
454
+ }
455
+ /** The name an object literal's members hang from: what it is assigned to. */
456
+ objectName(object, place) {
457
+ return isExportedValue(object) ? place.module : this.literalName(object, place);
458
+ }
459
+ /** A function or a class is named by what it is assigned to, as its members are. */
460
+ expressionName(expression, place) {
461
+ var _a;
462
+ if (isExportedValue(expression)) {
463
+ return this.exportedValueName(expression, place);
464
+ }
465
+ const own = (_a = expression.name) === null || _a === void 0 ? void 0 : _a.text;
466
+ if (!own || this.assignedTarget(expression)) {
467
+ return this.literalName(expression, place);
468
+ }
469
+ return assignedName(outermost(expression)) ? `${place.module}.${own}` : `${this.literalName(expression, place)}.${own}`;
470
+ }
471
+ /** What `module.exports =` gives the value it is assigned, which `export default` gives `default`. */
472
+ exportedValueName(value, place) {
473
+ var _a;
474
+ const named = typescript_1.default.isFunctionExpression(value) || typescript_1.default.isClassExpression(value) ? value.name : undefined;
475
+ return `${place.module}.${(_a = named === null || named === void 0 ? void 0 : named.text) !== null && _a !== void 0 ? _a : "default"}`;
476
+ }
477
+ literalName(literal, place) {
478
+ if (typescript_1.default.isExportAssignment(outermost(literal).parent)) {
479
+ return `${place.module}.default`;
480
+ }
481
+ const assigned = this.assignedTarget(literal);
482
+ if (assigned) {
483
+ return assigned.owner;
484
+ }
485
+ const { line, column } = lineAndColumn(literal.getSourceFile(), literal.getStart());
486
+ return `${place.module}.<object:${line}:${column}>`;
487
+ }
488
+ assignedTarget(literal) {
489
+ const assigned = assignedName(outermost(literal));
490
+ const symbol = assigned && this.checker.getSymbolAtLocation(assigned);
491
+ return symbol && this.targetOf(symbol);
492
+ }
493
+ kindOf(symbol, declaration) {
494
+ var _a;
495
+ if (typescript_1.default.isModuleDeclaration(declaration)) {
496
+ return "module";
497
+ }
498
+ if (typescript_1.default.isClassLike(declaration)) {
499
+ return "class";
500
+ }
501
+ if (typescript_1.default.isInterfaceDeclaration(declaration) || typescript_1.default.isTypeAliasDeclaration(declaration) || typescript_1.default.isTypeParameterDeclaration(declaration)) {
502
+ return "interface";
503
+ }
504
+ if (typescript_1.default.isEnumDeclaration(declaration)) {
505
+ return "enum";
506
+ }
507
+ if (typescript_1.default.isEnumMember(declaration)) {
508
+ return "enum-member";
509
+ }
510
+ if (typescript_1.default.isConstructorDeclaration(declaration)) {
511
+ return "constructor";
512
+ }
513
+ if (typescript_1.default.isAccessor(declaration)) {
514
+ return "field";
515
+ }
516
+ if (typescript_1.default.isFunctionLike(declaration)) {
517
+ return "method";
518
+ }
519
+ const value = assignedValue(declaration);
520
+ if (value && typescript_1.default.isClassExpression(unwrapped(value))) {
521
+ return "class";
522
+ }
523
+ // A package's property is the field the walk indexed it as, whatever it holds.
524
+ const external = (_a = this.placeOf(declaration.getSourceFile().fileName)) === null || _a === void 0 ? void 0 : _a.external;
525
+ return !external && this.checker.getTypeOfSymbolAtLocation(symbol, declaration).getCallSignatures().length > 0 ? "method" : "field";
526
+ }
527
+ placeOf(fileName) {
528
+ if (!this.places.has(fileName)) {
529
+ this.places.set(fileName, this.place(fileName));
530
+ }
531
+ return this.places.get(fileName);
532
+ }
533
+ place(fileName) {
534
+ var _a;
535
+ const file = fileName.replace(/\\/g, "/");
536
+ const sourceFile = this.program.getSourceFile(fileName);
537
+ if (sourceFile && this.program.isSourceFileDefaultLibrary(sourceFile)) {
538
+ return this.mappedPlace(file, path.dirname(path.dirname(file)), null, true);
539
+ }
540
+ const nodeModules = "/node_modules/";
541
+ const at = file.lastIndexOf(nodeModules);
542
+ if (at >= 0) {
543
+ const segments = file.substring(at + nodeModules.length).split("/");
544
+ const directory = segments.slice(0, segments[0].startsWith("@") ? 2 : 1).join("/");
545
+ const root = file.substring(0, at + nodeModules.length) + directory;
546
+ return this.mappedPlace(file, root, (_a = this.packageNameAt(root)) !== null && _a !== void 0 ? _a : directory, true);
547
+ }
548
+ for (const root of this.roots) {
549
+ const relative = path.relative(root, path.resolve(root, file)).replace(/\\/g, "/");
550
+ if (!relative.startsWith("../") && !path.isAbsolute(relative)) {
551
+ return (sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.isDeclarationFile) ? this.mappedPlace(file, root, null, false) :
552
+ { module: withoutExtension(relative), file: relative, pkg: null, external: false };
553
+ }
554
+ }
555
+ return undefined;
556
+ }
557
+ mappedPlace(file, root, pkg, external) {
558
+ let mapping = this.mappings.get(root);
559
+ if (!mapping) {
560
+ this.mappings.set(root, mapping = new type_mapping_1.JavaScriptTypeMapping(this.checker, root, true));
561
+ }
562
+ const relative = path.relative(root, file).replace(/\\/g, "/");
563
+ return { module: withoutExtension(relative), file: relative, pkg, external, mapping };
564
+ }
565
+ packageNameAt(directory) {
566
+ if (!this.packageNames.has(directory)) {
567
+ this.packageNames.set(directory, readPackageName(directory));
568
+ }
569
+ return this.packageNames.get(directory);
570
+ }
571
+ }
572
+ exports.JavaScriptNameResolver = JavaScriptNameResolver;
573
+ function withoutExtension(file) {
574
+ return file.replace(/\.[^/.]+$/, "");
575
+ }
576
+ function moduleTarget(place) {
577
+ return { name: place.file, owner: place.module, pkg: place.pkg, kind: "module" };
578
+ }
579
+ /** A module is its file. A namespace is too, except where a declaration row of ours stands on it. */
580
+ function isNamedByFile(declaration, place) {
581
+ return typescript_1.default.isSourceFile(declaration) || isModuleExportsObject(declaration) ||
582
+ typescript_1.default.isModuleDeclaration(declaration) && (typescript_1.default.isStringLiteral(declaration.name) || place.external);
583
+ }
584
+ function referenceKind(kind) {
585
+ switch (kind) {
586
+ case "method":
587
+ return "method-call";
588
+ case "field":
589
+ case "enum-member":
590
+ return "field-access";
591
+ default:
592
+ return "type-ref";
593
+ }
594
+ }
595
+ /** The implementation over an overload signature, and a class over the interface merged with it. */
596
+ function primaryDeclaration(symbol) {
597
+ const declarations = symbol.declarations;
598
+ if (!(declarations === null || declarations === void 0 ? void 0 : declarations.length)) {
599
+ return undefined;
600
+ }
601
+ return declarations.find(typescript_1.default.isClassLike) ||
602
+ declarations.find(d => typescript_1.default.isFunctionLike(d) && d.body !== undefined) ||
603
+ namingDeclarations(symbol)[0];
604
+ }
605
+ /** What declares a symbol, which the container of an expando does only when nothing else does. */
606
+ function namingDeclarations(symbol) {
607
+ var _a;
608
+ const declarations = (_a = symbol.declarations) !== null && _a !== void 0 ? _a : [];
609
+ const written = declarations.filter(d => !typescript_1.default.isIdentifier(d));
610
+ return written.length > 0 ? written : declarations;
611
+ }
612
+ /** Where a declaration is named. `module.exports =` is at `exports`, unless what it assigns is named or the module's own. */
613
+ function nameOf(declaration) {
614
+ if (typescript_1.default.isBinaryExpression(declaration) && isModuleExports(declaration.left)) {
615
+ const value = unwrapped(declaration.right);
616
+ const named = (typescript_1.default.isFunctionExpression(value) || typescript_1.default.isClassExpression(value)) && value.name;
617
+ return !named && !typescript_1.default.isObjectLiteralExpression(value) && typescript_1.default.isPropertyAccessExpression(declaration.left) ?
618
+ declaration.left.name : undefined;
619
+ }
620
+ return typescript_1.default.getNameOfDeclaration(declaration);
621
+ }
622
+ /** Whether a value is what `module.exports =` assigns. */
623
+ function isExportedValue(value) {
624
+ const parent = outermost(value).parent;
625
+ return typescript_1.default.isBinaryExpression(parent) && isModuleExports(parent.left);
626
+ }
627
+ function declares(symbol, name) {
628
+ return namingDeclarations(symbol).some(declaration => nameOf(declaration) === name);
629
+ }
630
+ function isBinding(name, symbol) {
631
+ const parent = name.parent;
632
+ return typescript_1.default.isImportSpecifier(parent) || typescript_1.default.isImportClause(parent) || typescript_1.default.isNamespaceImport(parent) ||
633
+ typescript_1.default.isImportEqualsDeclaration(parent) || typescript_1.default.isExportSpecifier(parent) || typescript_1.default.isNamespaceExport(parent) ||
634
+ (symbol.flags & typescript_1.default.SymbolFlags.Alias) !== 0 && declares(symbol, name) ||
635
+ isModuleExports(parent) && typescript_1.default.isBinaryExpression(parent.parent) && parent.parent.left === parent && !declares(symbol, name);
636
+ }
637
+ /** Whether `name` reads a property in an object pattern, which `const {a} = b` does as it declares `a`. */
638
+ function isBoundProperty(name) {
639
+ var _a;
640
+ const element = name.parent;
641
+ return typescript_1.default.isBindingElement(element) && typescript_1.default.isObjectBindingPattern(element.parent) &&
642
+ name === ((_a = element.propertyName) !== null && _a !== void 0 ? _a : element.name);
643
+ }
644
+ function isConstructed(name) {
645
+ return typescript_1.default.isNewExpression(name.parent) && name.parent.expression === name ||
646
+ typescript_1.default.isPropertyAccessExpression(name.parent) && name.parent.name === name &&
647
+ typescript_1.default.isNewExpression(name.parent.parent) && name.parent.parent.expression === name.parent;
648
+ }
649
+ function isModuleExports(node) {
650
+ return typescript_1.default.isPropertyAccessExpression(node) && node.name.text === "exports" &&
651
+ typescript_1.default.isIdentifier(node.expression) && node.expression.text === "module" ||
652
+ typescript_1.default.isIdentifier(node) && node.text === "exports";
653
+ }
654
+ /** `module.exports = {...}`, which is the module's own value, and its members the module's. */
655
+ function isModuleExportsObject(declaration) {
656
+ return typescript_1.default.isBinaryExpression(declaration) && isModuleExports(declaration.left) &&
657
+ typescript_1.default.isObjectLiteralExpression(unwrapped(declaration.right));
658
+ }
659
+ /** The value inside any parentheses or assertion, where {@link outermost} is the expression around it. */
660
+ function unwrapped(expression) {
661
+ let value = expression;
662
+ while (typescript_1.default.isParenthesizedExpression(value) || typescript_1.default.isAssertionExpression(value) || typescript_1.default.isSatisfiesExpression(value) ||
663
+ typescript_1.default.isNonNullExpression(value)) {
664
+ value = value.expression;
665
+ }
666
+ return value;
667
+ }
668
+ function isExpression(declaration) {
669
+ return typescript_1.default.isFunctionExpression(declaration) || typescript_1.default.isArrowFunction(declaration) || typescript_1.default.isClassExpression(declaration);
670
+ }
671
+ /** What the parser names by its type, which a package's own naming gives whole. */
672
+ function isNamedByType(declaration) {
673
+ return typescript_1.default.isClassLike(declaration) || typescript_1.default.isInterfaceDeclaration(declaration) || typescript_1.default.isEnumDeclaration(declaration) ||
674
+ typescript_1.default.isTypeAliasDeclaration(declaration) || typescript_1.default.isModuleDeclaration(declaration);
675
+ }
676
+ function isGlobalScope(symbol) {
677
+ var _a;
678
+ return ((_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => typescript_1.default.isModuleDeclaration(d) && (d.flags & typescript_1.default.NodeFlags.GlobalAugmentation) !== 0)) === true;
679
+ }
680
+ function lastSegment(declaration) {
681
+ const name = typescript_1.default.getNameOfDeclaration(declaration);
682
+ if (!name) {
683
+ return typescript_1.default.isConstructorDeclaration(declaration) ? "<constructor>" : "default";
684
+ }
685
+ return typescript_1.default.isComputedPropertyName(name) ? name.getText() : name.text;
686
+ }
687
+ /** The expression in the position a literal is assigned from, past any parentheses or assertion around it. */
688
+ function outermost(literal) {
689
+ let value = literal;
690
+ while (typescript_1.default.isParenthesizedExpression(value.parent) || typescript_1.default.isAssertionExpression(value.parent) ||
691
+ typescript_1.default.isSatisfiesExpression(value.parent) || typescript_1.default.isNonNullExpression(value.parent)) {
692
+ value = value.parent;
693
+ }
694
+ return value;
695
+ }
696
+ /** The name a value or a type literal is assigned to, as `o` in `const o = {}`, or none. */
697
+ function assignedName(value) {
698
+ const parent = value.parent;
699
+ if ((typescript_1.default.isVariableDeclaration(parent) || typescript_1.default.isPropertyAssignment(parent) || typescript_1.default.isPropertyDeclaration(parent) ||
700
+ typescript_1.default.isPropertySignature(parent) || typescript_1.default.isTypeAliasDeclaration(parent)) &&
701
+ parent.name !== value) {
702
+ return parent.name;
703
+ }
704
+ if (!typescript_1.default.isBinaryExpression(parent) || parent.right !== value || parent.operatorToken.kind !== typescript_1.default.SyntaxKind.EqualsToken) {
705
+ return undefined;
706
+ }
707
+ const left = parent.left;
708
+ if (typescript_1.default.isPropertyAccessExpression(left) && left.name.text === "prototype") {
709
+ return leftmost(left.expression);
710
+ }
711
+ return typescript_1.default.isPropertyAccessExpression(left) ? left.name : left;
712
+ }
713
+ function assignedValue(declaration) {
714
+ if (typescript_1.default.isVariableDeclaration(declaration) || typescript_1.default.isPropertyAssignment(declaration) || typescript_1.default.isPropertyDeclaration(declaration)) {
715
+ return declaration.initializer;
716
+ }
717
+ if (typescript_1.default.isBinaryExpression(declaration)) {
718
+ return declaration.right;
719
+ }
720
+ const parent = declaration.parent;
721
+ return parent && typescript_1.default.isBinaryExpression(parent) && parent.left === declaration ? parent.right : undefined;
722
+ }
723
+ function propertyOf(type, name) {
724
+ return type.isUnion() ? type.types.map(t => t.getProperty(name)).find(p => p !== undefined) : type.getProperty(name);
725
+ }
726
+ function leftmost(expression) {
727
+ while (typescript_1.default.isPropertyAccessExpression(expression) || typescript_1.default.isElementAccessExpression(expression) ||
728
+ typescript_1.default.isParenthesizedExpression(expression)) {
729
+ expression = expression.expression;
730
+ }
731
+ return expression;
732
+ }
733
+ /** Counted as the search index counts: lines end at a line feed and nothing else, as TypeScript's do not. */
734
+ function lineAndColumn(file, offset) {
735
+ const before = file.text.substring(0, offset);
736
+ const lineStart = before.lastIndexOf("\n") + 1;
737
+ return { line: before.split("\n").length, column: offset - lineStart + 1 };
738
+ }
739
+ function printedOffsets(text) {
740
+ const surrogates = [...text.matchAll(/\p{Cs}/gu)].map(match => match.index);
741
+ return surrogates.length === 0 ? offset => offset :
742
+ offset => offset + ESCAPED_SURROGATE_GROWTH * surrogates.filter(at => at < offset).length;
743
+ }
744
+ function isModuleSpecifier(node) {
745
+ const parent = node.parent;
746
+ if (typescript_1.default.isImportDeclaration(parent) || typescript_1.default.isExportDeclaration(parent)) {
747
+ return parent.moduleSpecifier === node;
748
+ }
749
+ if (typescript_1.default.isExternalModuleReference(parent)) {
750
+ return true;
751
+ }
752
+ if (typescript_1.default.isLiteralTypeNode(parent)) {
753
+ return typescript_1.default.isImportTypeNode(parent.parent);
754
+ }
755
+ return typescript_1.default.isModuleDeclaration(parent) || typescript_1.default.isCallExpression(parent) && isModuleLoad(parent) && parent.arguments[0] === node;
756
+ }
757
+ /** In a module, `declare module` adds to the module it names, and declares none. */
758
+ function isAugmentation(node) {
759
+ return typescript_1.default.isModuleDeclaration(node) && typescript_1.default.isExternalModule(node.getSourceFile());
760
+ }
761
+ function isModuleLoad(call) {
762
+ return call.expression.kind === typescript_1.default.SyntaxKind.ImportKeyword ||
763
+ typescript_1.default.isIdentifier(call.expression) && call.expression.text === "require";
764
+ }
765
+ /** `export default` of a value with no name of its own, as `export default a.b` has in `b`. */
766
+ function isNamelessDefaultExport(node) {
767
+ if (typescript_1.default.isExportAssignment(node)) {
768
+ return !node.isExportEquals && !typescript_1.default.isIdentifier(node.expression) && !typescript_1.default.isPropertyAccessExpression(node.expression);
769
+ }
770
+ return (typescript_1.default.isFunctionDeclaration(node) || typescript_1.default.isClassDeclaration(node)) && !node.name;
771
+ }
772
+ /** Declared where a function runs, as `this.x =` is, but on the class or module that holds it. */
773
+ function isMember(symbol, declaration) {
774
+ return typescript_1.default.isBinaryExpression(declaration) || typescript_1.default.isPropertyAccessExpression(declaration) ||
775
+ typescript_1.default.isElementAccessExpression(declaration) || typescript_1.default.isCallExpression(declaration) ||
776
+ typescript_1.default.isParameter(declaration) && (symbol.flags & typescript_1.default.SymbolFlags.Property) !== 0;
777
+ }
778
+ function moduleSpecifierOf(declaration) {
779
+ if (typescript_1.default.isImportClause(declaration)) {
780
+ return declaration.parent.moduleSpecifier;
781
+ }
782
+ if (typescript_1.default.isNamespaceImport(declaration)) {
783
+ return declaration.parent.parent.moduleSpecifier;
784
+ }
785
+ if (typescript_1.default.isImportSpecifier(declaration)) {
786
+ return declaration.parent.parent.parent.moduleSpecifier;
787
+ }
788
+ if (typescript_1.default.isExportSpecifier(declaration)) {
789
+ return declaration.parent.parent.moduleSpecifier;
790
+ }
791
+ if (typescript_1.default.isNamespaceExport(declaration)) {
792
+ return declaration.parent.moduleSpecifier;
793
+ }
794
+ if (typescript_1.default.isImportEqualsDeclaration(declaration)) {
795
+ return typescript_1.default.isExternalModuleReference(declaration.moduleReference) ?
796
+ declaration.moduleReference.expression : undefined;
797
+ }
798
+ if (typescript_1.default.isBindingElement(declaration)) {
799
+ return moduleSpecifierOf(declaration.parent.parent);
800
+ }
801
+ if (typescript_1.default.isVariableDeclaration(declaration) && declaration.initializer) {
802
+ return requiredModule(declaration.initializer);
803
+ }
804
+ return undefined;
805
+ }
806
+ function requiredModule(expression) {
807
+ const root = leftmost(expression);
808
+ return typescript_1.default.isCallExpression(root) && isModuleLoad(root) ? root.arguments[0] : undefined;
809
+ }
810
+ function readPackageName(directory) {
811
+ var _a;
812
+ try {
813
+ const { name } = JSON.parse((_a = typescript_1.default.sys.readFile(`${directory}/package.json`)) !== null && _a !== void 0 ? _a : "{}");
814
+ return typeof name === "string" ? name : undefined;
815
+ }
816
+ catch (_b) {
817
+ return undefined;
818
+ }
819
+ }
820
+ //# sourceMappingURL=name-resolver.js.map