MCPcopy Index your code
hub / github.com/nodejs/node / convertSingleIdentifierImport

Function convertSingleIdentifierImport

test/fixtures/snapshot/typescript.js:152455–152499  ·  view source on GitHub ↗

* Convert `import x = require("x").` * Also: * - Convert `x.default()` to `x()` to handle ES6 default export * - Converts uses like `x.y()` to `y()` and uses a named import.

(name, moduleSpecifier, checker, identifiers, quotePreference)

Source from the content-addressed store, hash-verified

152453 * - Converts uses like `x.y()` to `y()` and uses a named import.
152454 */
152455 function convertSingleIdentifierImport(name, moduleSpecifier, checker, identifiers, quotePreference) {
152456 var nameSymbol = checker.getSymbolAtLocation(name);
152457 // Maps from module property name to name actually used. (The same if there isn't shadowing.)
152458 var namedBindingsNames = new ts.Map();
152459 // True if there is some non-property use like `x()` or `f(x)`.
152460 var needDefaultImport = false;
152461 var useSitesToUnqualify;
152462 for (var _i = 0, _a = identifiers.original.get(name.text); _i < _a.length; _i++) {
152463 var use = _a[_i];
152464 if (checker.getSymbolAtLocation(use) !== nameSymbol || use === name) {
152465 // This was a use of a different symbol with the same name, due to shadowing. Ignore.
152466 continue;
152467 }
152468 var parent = use.parent;
152469 if (ts.isPropertyAccessExpression(parent)) {
152470 var propertyName = parent.name.text;
152471 if (propertyName === "default") {
152472 needDefaultImport = true;
152473 var importDefaultName = use.getText();
152474 (useSitesToUnqualify !== null && useSitesToUnqualify !== void 0 ? useSitesToUnqualify : (useSitesToUnqualify = new ts.Map())).set(parent, ts.factory.createIdentifier(importDefaultName));
152475 }
152476 else {
152477 ts.Debug.assert(parent.expression === use, "Didn't expect expression === use"); // Else shouldn't have been in `collectIdentifiers`
152478 var idName = namedBindingsNames.get(propertyName);
152479 if (idName === undefined) {
152480 idName = makeUniqueName(propertyName, identifiers);
152481 namedBindingsNames.set(propertyName, idName);
152482 }
152483 (useSitesToUnqualify !== null && useSitesToUnqualify !== void 0 ? useSitesToUnqualify : (useSitesToUnqualify = new ts.Map())).set(parent, ts.factory.createIdentifier(idName));
152484 }
152485 }
152486 else {
152487 needDefaultImport = true;
152488 }
152489 }
152490 var namedBindings = namedBindingsNames.size === 0 ? undefined : ts.arrayFrom(ts.mapIterator(namedBindingsNames.entries(), function (_a) {
152491 var propertyName = _a[0], idName = _a[1];
152492 return ts.factory.createImportSpecifier(/*isTypeOnly*/ false, propertyName === idName ? undefined : ts.factory.createIdentifier(propertyName), ts.factory.createIdentifier(idName));
152493 }));
152494 if (!namedBindings) {
152495 // If it was unused, ensure that we at least import *something*.
152496 needDefaultImport = true;
152497 }
152498 return convertedImports([ts.makeImport(needDefaultImport ? ts.getSynthesizedDeepClone(name) : undefined, namedBindings, moduleSpecifier, quotePreference)], useSitesToUnqualify);
152499 }
152500 // Identifiers helpers
152501 function makeUniqueName(name, identifiers) {
152502 while (identifiers.original.has(name) || identifiers.additional.has(name)) {

Callers 1

convertSingleImportFunction · 0.85

Calls 6

makeUniqueNameFunction · 0.85
convertedImportsFunction · 0.85
assertMethod · 0.80
getMethod · 0.65
setMethod · 0.45
entriesMethod · 0.45

Tested by

no test coverage detected