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

Function convertSingleImport

test/fixtures/snapshot/typescript.js:152419–152448  ·  view source on GitHub ↗

* Converts `const < > = require("x");`. * Returns nodes that will replace the variable declaration for the commonjs import. * May also make use `changes` to remove qualifiers at the use sites of imports, to change `mod.x` to `x`.

(name, moduleSpecifier, checker, identifiers, target, quotePreference)

Source from the content-addressed store, hash-verified

152417 * May also make use `changes` to remove qualifiers at the use sites of imports, to change `mod.x` to `x`.
152418 */
152419 function convertSingleImport(name, moduleSpecifier, checker, identifiers, target, quotePreference) {
152420 switch (name.kind) {
152421 case 201 /* SyntaxKind.ObjectBindingPattern */: {
152422 var importSpecifiers = ts.mapAllOrFail(name.elements, function (e) {
152423 return e.dotDotDotToken || e.initializer || e.propertyName && !ts.isIdentifier(e.propertyName) || !ts.isIdentifier(e.name)
152424 ? undefined
152425 : makeImportSpecifier(e.propertyName && e.propertyName.text, e.name.text);
152426 });
152427 if (importSpecifiers) {
152428 return convertedImports([ts.makeImport(/*name*/ undefined, importSpecifiers, moduleSpecifier, quotePreference)]);
152429 }
152430 }
152431 // falls through -- object destructuring has an interesting pattern and must be a variable declaration
152432 case 202 /* SyntaxKind.ArrayBindingPattern */: {
152433 /*
152434 import x from "x";
152435 const [a, b, c] = x;
152436 */
152437 var tmp = makeUniqueName(codefix.moduleSpecifierToValidIdentifier(moduleSpecifier.text, target), identifiers);
152438 return convertedImports([
152439 ts.makeImport(ts.factory.createIdentifier(tmp), /*namedImports*/ undefined, moduleSpecifier, quotePreference),
152440 makeConst(/*modifiers*/ undefined, ts.getSynthesizedDeepClone(name), ts.factory.createIdentifier(tmp)),
152441 ]);
152442 }
152443 case 79 /* SyntaxKind.Identifier */:
152444 return convertSingleIdentifierImport(name, moduleSpecifier, checker, identifiers, quotePreference);
152445 default:
152446 return ts.Debug.assertNever(name, "Convert to ES module got invalid name kind ".concat(name.kind));
152447 }
152448 }
152449 /**
152450 * Convert `import x = require("x").`
152451 * Also:

Callers 1

convertVariableStatementFunction · 0.85

Calls 6

makeImportSpecifierFunction · 0.85
convertedImportsFunction · 0.85
makeUniqueNameFunction · 0.85
makeConstFunction · 0.85
concatMethod · 0.80

Tested by

no test coverage detected