(sourceFile, program, changes, toConvert, shouldUseDefault)
| 159437 | return ts.isPropertyAccessExpression(propertyAccessOrQualifiedName) ? propertyAccessOrQualifiedName.expression : propertyAccessOrQualifiedName.left; |
| 159438 | } |
| 159439 | function doChangeNamedToNamespaceOrDefault(sourceFile, program, changes, toConvert, shouldUseDefault) { |
| 159440 | if (shouldUseDefault === void 0) { shouldUseDefault = getShouldUseDefault(program, toConvert.parent); } |
| 159441 | var checker = program.getTypeChecker(); |
| 159442 | var importDecl = toConvert.parent.parent; |
| 159443 | var moduleSpecifier = importDecl.moduleSpecifier; |
| 159444 | var toConvertSymbols = new ts.Set(); |
| 159445 | toConvert.elements.forEach(function (namedImport) { |
| 159446 | var symbol = checker.getSymbolAtLocation(namedImport.name); |
| 159447 | if (symbol) { |
| 159448 | toConvertSymbols.add(symbol); |
| 159449 | } |
| 159450 | }); |
| 159451 | var preferredName = moduleSpecifier && ts.isStringLiteral(moduleSpecifier) ? ts.codefix.moduleSpecifierToValidIdentifier(moduleSpecifier.text, 99 /* ScriptTarget.ESNext */) : "module"; |
| 159452 | function hasNamespaceNameConflict(namedImport) { |
| 159453 | // We need to check if the preferred namespace name (`preferredName`) we'd like to use in the refactored code will present a name conflict. |
| 159454 | // A name conflict means that, in a scope where we would like to use the preferred namespace name, there already exists a symbol with that name in that scope. |
| 159455 | // We are going to use the namespace name in the scopes the named imports being refactored are referenced, |
| 159456 | // so we look for conflicts by looking at every reference to those named imports. |
| 159457 | return !!ts.FindAllReferences.Core.eachSymbolReferenceInFile(namedImport.name, checker, sourceFile, function (id) { |
| 159458 | var symbol = checker.resolveName(preferredName, id, 67108863 /* SymbolFlags.All */, /*excludeGlobals*/ true); |
| 159459 | if (symbol) { // There already is a symbol with the same name as the preferred namespace name. |
| 159460 | if (toConvertSymbols.has(symbol)) { // `preferredName` resolves to a symbol for one of the named import references we are going to transform into namespace import references... |
| 159461 | return ts.isExportSpecifier(id.parent); // ...but if this reference is an export specifier, it will not be transformed, so it is a conflict; otherwise, it will be renamed and is not a conflict. |
| 159462 | } |
| 159463 | return true; // `preferredName` resolves to any other symbol, which will be present in the refactored code and so poses a name conflict. |
| 159464 | } |
| 159465 | return false; // There is no symbol with the same name as the preferred namespace name, so no conflict. |
| 159466 | }); |
| 159467 | } |
| 159468 | var namespaceNameConflicts = toConvert.elements.some(hasNamespaceNameConflict); |
| 159469 | var namespaceImportName = namespaceNameConflicts ? ts.getUniqueName(preferredName, sourceFile) : preferredName; |
| 159470 | // Imports that need to be kept as named imports in the refactored code, to avoid changing the semantics. |
| 159471 | // More specifically, those are named imports that appear in named exports in the original code, e.g. `a` in `import { a } from "m"; export { a }`. |
| 159472 | var neededNamedImports = new ts.Set(); |
| 159473 | var _loop_17 = function (element) { |
| 159474 | var propertyName = (element.propertyName || element.name).text; |
| 159475 | ts.FindAllReferences.Core.eachSymbolReferenceInFile(element.name, checker, sourceFile, function (id) { |
| 159476 | var access = ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(namespaceImportName), propertyName); |
| 159477 | if (ts.isShorthandPropertyAssignment(id.parent)) { |
| 159478 | changes.replaceNode(sourceFile, id.parent, ts.factory.createPropertyAssignment(id.text, access)); |
| 159479 | } |
| 159480 | else if (ts.isExportSpecifier(id.parent)) { |
| 159481 | neededNamedImports.add(element); |
| 159482 | } |
| 159483 | else { |
| 159484 | changes.replaceNode(sourceFile, id, access); |
| 159485 | } |
| 159486 | }); |
| 159487 | }; |
| 159488 | for (var _i = 0, _a = toConvert.elements; _i < _a.length; _i++) { |
| 159489 | var element = _a[_i]; |
| 159490 | _loop_17(element); |
| 159491 | } |
| 159492 | changes.replaceNode(sourceFile, toConvert, shouldUseDefault |
| 159493 | ? ts.factory.createIdentifier(namespaceImportName) |
| 159494 | : ts.factory.createNamespaceImport(ts.factory.createIdentifier(namespaceImportName))); |
| 159495 | if (neededNamedImports.size) { |
| 159496 | var newNamedImports = ts.arrayFrom(neededNamedImports.values()).map(function (element) { |
no test coverage detected