MCPcopy Create free account
hub / github.com/nodejs/node / doAddExistingFix

Function doAddExistingFix

test/fixtures/snapshot/typescript.js:153779–153855  ·  view source on GitHub ↗
(changes, sourceFile, clause, defaultImport, namedImports, compilerOptions)

Source from the content-addressed store, hash-verified

153777 }
153778 }
153779 function doAddExistingFix(changes, sourceFile, clause, defaultImport, namedImports, compilerOptions) {
153780 var _a;
153781 if (clause.kind === 201 /* SyntaxKind.ObjectBindingPattern */) {
153782 if (defaultImport) {
153783 addElementToBindingPattern(clause, defaultImport.name, "default");
153784 }
153785 for (var _i = 0, namedImports_1 = namedImports; _i < namedImports_1.length; _i++) {
153786 var specifier = namedImports_1[_i];
153787 addElementToBindingPattern(clause, specifier.name, /*propertyName*/ undefined);
153788 }
153789 return;
153790 }
153791 var promoteFromTypeOnly = clause.isTypeOnly && ts.some(__spreadArray([defaultImport], namedImports, true), function (i) { return (i === null || i === void 0 ? void 0 : i.addAsTypeOnly) === 4 /* AddAsTypeOnly.NotAllowed */; });
153792 var existingSpecifiers = clause.namedBindings && ((_a = ts.tryCast(clause.namedBindings, ts.isNamedImports)) === null || _a === void 0 ? void 0 : _a.elements);
153793 // If we are promoting from a type-only import and `--isolatedModules` and `--preserveValueImports`
153794 // are enabled, we need to make every existing import specifier type-only. It may be possible that
153795 // some of them don't strictly need to be marked type-only (if they have a value meaning and are
153796 // never used in an emitting position). These are allowed to be imported without being type-only,
153797 // but the user has clearly already signified that they don't need them to be present at runtime
153798 // by placing them in a type-only import. So, just mark each specifier as type-only.
153799 var convertExistingToTypeOnly = promoteFromTypeOnly && compilerOptions.preserveValueImports && compilerOptions.isolatedModules;
153800 if (defaultImport) {
153801 ts.Debug.assert(!clause.name, "Cannot add a default import to an import clause that already has one");
153802 changes.insertNodeAt(sourceFile, clause.getStart(sourceFile), ts.factory.createIdentifier(defaultImport.name), { suffix: ", " });
153803 }
153804 if (namedImports.length) {
153805 var newSpecifiers = ts.stableSort(namedImports.map(function (namedImport) { return ts.factory.createImportSpecifier((!clause.isTypeOnly || promoteFromTypeOnly) && needsTypeOnly(namedImport),
153806 /*propertyName*/ undefined, ts.factory.createIdentifier(namedImport.name)); }), ts.OrganizeImports.compareImportOrExportSpecifiers);
153807 if ((existingSpecifiers === null || existingSpecifiers === void 0 ? void 0 : existingSpecifiers.length) && ts.OrganizeImports.importSpecifiersAreSorted(existingSpecifiers)) {
153808 for (var _b = 0, newSpecifiers_1 = newSpecifiers; _b < newSpecifiers_1.length; _b++) {
153809 var spec = newSpecifiers_1[_b];
153810 // Organize imports puts type-only import specifiers last, so if we're
153811 // adding a non-type-only specifier and converting all the other ones to
153812 // type-only, there's no need to ask for the insertion index - it's 0.
153813 var insertionIndex = convertExistingToTypeOnly && !spec.isTypeOnly
153814 ? 0
153815 : ts.OrganizeImports.getImportSpecifierInsertionIndex(existingSpecifiers, spec);
153816 changes.insertImportSpecifierAtIndex(sourceFile, spec, clause.namedBindings, insertionIndex);
153817 }
153818 }
153819 else if (existingSpecifiers === null || existingSpecifiers === void 0 ? void 0 : existingSpecifiers.length) {
153820 for (var _c = 0, newSpecifiers_2 = newSpecifiers; _c < newSpecifiers_2.length; _c++) {
153821 var spec = newSpecifiers_2[_c];
153822 changes.insertNodeInListAfter(sourceFile, ts.last(existingSpecifiers), spec, existingSpecifiers);
153823 }
153824 }
153825 else {
153826 if (newSpecifiers.length) {
153827 var namedImports_2 = ts.factory.createNamedImports(newSpecifiers);
153828 if (clause.namedBindings) {
153829 changes.replaceNode(sourceFile, clause.namedBindings, namedImports_2);
153830 }
153831 else {
153832 changes.insertNodeAfter(sourceFile, ts.Debug.checkDefined(clause.name, "Import clause must have either named imports or a default import"), namedImports_2);
153833 }
153834 }
153835 }
153836 }

Callers 2

writeFixesFunction · 0.85
codeActionForFixWorkerFunction · 0.85

Calls 7

needsTypeOnlyFunction · 0.85
someMethod · 0.80
assertMethod · 0.80
mapMethod · 0.65
deleteMethod · 0.65
lastMethod · 0.45

Tested by

no test coverage detected