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

Function tryAddToExistingImport

test/fixtures/snapshot/typescript.js:153272–153312  ·  view source on GitHub ↗
(existingImports, isValidTypeOnlyUseSite, checker, compilerOptions)

Source from the content-addressed store, hash-verified

153270 return 1 /* AddAsTypeOnly.Allowed */;
153271 }
153272 function tryAddToExistingImport(existingImports, isValidTypeOnlyUseSite, checker, compilerOptions) {
153273 return ts.firstDefined(existingImports, function (_a) {
153274 var declaration = _a.declaration, importKind = _a.importKind, symbol = _a.symbol, targetFlags = _a.targetFlags;
153275 if (importKind === 3 /* ImportKind.CommonJS */ || importKind === 2 /* ImportKind.Namespace */ || declaration.kind === 265 /* SyntaxKind.ImportEqualsDeclaration */) {
153276 // These kinds of imports are not combinable with anything
153277 return undefined;
153278 }
153279 if (declaration.kind === 254 /* SyntaxKind.VariableDeclaration */) {
153280 return (importKind === 0 /* ImportKind.Named */ || importKind === 1 /* ImportKind.Default */) && declaration.name.kind === 201 /* SyntaxKind.ObjectBindingPattern */
153281 ? { kind: 2 /* ImportFixKind.AddToExisting */, importClauseOrBindingPattern: declaration.name, importKind: importKind, moduleSpecifier: declaration.initializer.arguments[0].text, addAsTypeOnly: 4 /* AddAsTypeOnly.NotAllowed */ }
153282 : undefined;
153283 }
153284 var importClause = declaration.importClause;
153285 if (!importClause || !ts.isStringLiteralLike(declaration.moduleSpecifier))
153286 return undefined;
153287 var name = importClause.name, namedBindings = importClause.namedBindings;
153288 // A type-only import may not have both a default and named imports, so the only way a name can
153289 // be added to an existing type-only import is adding a named import to existing named bindings.
153290 if (importClause.isTypeOnly && !(importKind === 0 /* ImportKind.Named */ && namedBindings))
153291 return undefined;
153292 // N.B. we don't have to figure out whether to use the main program checker
153293 // or the AutoImportProvider checker because we're adding to an existing import; the existence of
153294 // the import guarantees the symbol came from the main program.
153295 var addAsTypeOnly = getAddAsTypeOnly(isValidTypeOnlyUseSite, /*isForNewImportDeclaration*/ false, symbol, targetFlags, checker, compilerOptions);
153296 if (importKind === 1 /* ImportKind.Default */ && (name || // Cannot add a default import to a declaration that already has one
153297 addAsTypeOnly === 2 /* AddAsTypeOnly.Required */ && namedBindings // Cannot add a default import as type-only if the import already has named bindings
153298 ))
153299 return undefined;
153300 if (importKind === 0 /* ImportKind.Named */ &&
153301 (namedBindings === null || namedBindings === void 0 ? void 0 : namedBindings.kind) === 268 /* SyntaxKind.NamespaceImport */ // Cannot add a named import to a declaration that has a namespace import
153302 )
153303 return undefined;
153304 return {
153305 kind: 2 /* ImportFixKind.AddToExisting */,
153306 importClauseOrBindingPattern: importClause,
153307 importKind: importKind,
153308 moduleSpecifier: declaration.moduleSpecifier.text,
153309 addAsTypeOnly: addAsTypeOnly,
153310 };
153311 });
153312 }
153313 function createExistingImportMap(checker, importingFile, compilerOptions) {
153314 var importMap;
153315 for (var _i = 0, _a = importingFile.imports; _i < _a.length; _i++) {

Callers 1

getImportFixesFunction · 0.85

Calls 1

getAddAsTypeOnlyFunction · 0.85

Tested by

no test coverage detected