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

Function addImport

test/fixtures/snapshot/typescript.js:152929–153031  ·  view source on GitHub ↗
(info)

Source from the content-addressed store, hash-verified

152927 }
152928 }
152929 function addImport(info) {
152930 var _a, _b;
152931 var fixes = info.fixes, symbolName = info.symbolName;
152932 var fix = ts.first(fixes);
152933 switch (fix.kind) {
152934 case 0 /* ImportFixKind.UseNamespace */:
152935 addToNamespace.push(fix);
152936 break;
152937 case 1 /* ImportFixKind.JsdocTypeImport */:
152938 importType.push(fix);
152939 break;
152940 case 2 /* ImportFixKind.AddToExisting */: {
152941 var importClauseOrBindingPattern = fix.importClauseOrBindingPattern, importKind = fix.importKind, addAsTypeOnly = fix.addAsTypeOnly;
152942 var key = String(ts.getNodeId(importClauseOrBindingPattern));
152943 var entry = addToExisting.get(key);
152944 if (!entry) {
152945 addToExisting.set(key, entry = { importClauseOrBindingPattern: importClauseOrBindingPattern, defaultImport: undefined, namedImports: new ts.Map() });
152946 }
152947 if (importKind === 0 /* ImportKind.Named */) {
152948 var prevValue = entry === null || entry === void 0 ? void 0 : entry.namedImports.get(symbolName);
152949 entry.namedImports.set(symbolName, reduceAddAsTypeOnlyValues(prevValue, addAsTypeOnly));
152950 }
152951 else {
152952 ts.Debug.assert(entry.defaultImport === undefined || entry.defaultImport.name === symbolName, "(Add to Existing) Default import should be missing or match symbolName");
152953 entry.defaultImport = {
152954 name: symbolName,
152955 addAsTypeOnly: reduceAddAsTypeOnlyValues((_a = entry.defaultImport) === null || _a === void 0 ? void 0 : _a.addAsTypeOnly, addAsTypeOnly),
152956 };
152957 }
152958 break;
152959 }
152960 case 3 /* ImportFixKind.AddNew */: {
152961 var moduleSpecifier = fix.moduleSpecifier, importKind = fix.importKind, useRequire = fix.useRequire, addAsTypeOnly = fix.addAsTypeOnly;
152962 var entry = getNewImportEntry(moduleSpecifier, importKind, useRequire, addAsTypeOnly);
152963 ts.Debug.assert(entry.useRequire === useRequire, "(Add new) Tried to add an `import` and a `require` for the same module");
152964 switch (importKind) {
152965 case 1 /* ImportKind.Default */:
152966 ts.Debug.assert(entry.defaultImport === undefined || entry.defaultImport.name === symbolName, "(Add new) Default import should be missing or match symbolName");
152967 entry.defaultImport = { name: symbolName, addAsTypeOnly: reduceAddAsTypeOnlyValues((_b = entry.defaultImport) === null || _b === void 0 ? void 0 : _b.addAsTypeOnly, addAsTypeOnly) };
152968 break;
152969 case 0 /* ImportKind.Named */:
152970 var prevValue = (entry.namedImports || (entry.namedImports = new ts.Map())).get(symbolName);
152971 entry.namedImports.set(symbolName, reduceAddAsTypeOnlyValues(prevValue, addAsTypeOnly));
152972 break;
152973 case 3 /* ImportKind.CommonJS */:
152974 case 2 /* ImportKind.Namespace */:
152975 ts.Debug.assert(entry.namespaceLikeImport === undefined || entry.namespaceLikeImport.name === symbolName, "Namespacelike import shoudl be missing or match symbolName");
152976 entry.namespaceLikeImport = { importKind: importKind, name: symbolName, addAsTypeOnly: addAsTypeOnly };
152977 break;
152978 }
152979 break;
152980 }
152981 case 4 /* ImportFixKind.PromoteTypeOnly */:
152982 // Excluding from fix-all
152983 break;
152984 default:
152985 ts.Debug.assertNever(fix, "fix wasn't never - got kind ".concat(fix.kind));
152986 }

Callers 2

addImportFromDiagnosticFunction · 0.85

Calls 9

StringClass · 0.85
getNewImportEntryFunction · 0.85
assertMethod · 0.80
concatMethod · 0.80
getMethod · 0.65
firstMethod · 0.45
pushMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected