MCPcopy Create free account
hub / github.com/babel/babel / transformNamedBabelTypesImportToDestructuring

Function transformNamedBabelTypesImportToDestructuring

babel.config.ts:435–483  ·  view source on GitHub ↗
({
  types: {
    cloneNode,
    importNamespaceSpecifier,
    objectPattern,
    objectProperty,
    variableDeclarator,
    variableDeclaration,
  },
}: PluginAPI)

Source from the content-addressed store, hash-verified

433
434// transform `import { x } from "@babel/types"` to `import * as _t from "@babel/types"; const { x } = _t;
435function transformNamedBabelTypesImportToDestructuring({
436 types: {
437 cloneNode,
438 importNamespaceSpecifier,
439 objectPattern,
440 objectProperty,
441 variableDeclarator,
442 variableDeclaration,
443 },
444}: PluginAPI) {
445 return {
446 name: "transform-babel-types-named-imports",
447 visitor: {
448 ImportDeclaration(path) {
449 const { node } = path;
450 if (
451 node.importKind === "value" &&
452 node.source.value === "@babel/types" &&
453 node.specifiers[0].type === "ImportSpecifier"
454 ) {
455 const hoistedDestructuringProperties = [];
456 for (const {
457 imported,
458 local,
459 } of node.specifiers as t.ImportSpecifier[]) {
460 hoistedDestructuringProperties.push(
461 objectProperty(
462 imported,
463 local,
464 false,
465 (imported as t.Identifier).name === local.name
466 )
467 );
468 }
469 const babelTypeNsImport = path.scope.generateUidIdentifier("t");
470 node.specifiers = [importNamespaceSpecifier(babelTypeNsImport)];
471 path.insertAfter([
472 variableDeclaration("const", [
473 variableDeclarator(
474 objectPattern(hoistedDestructuringProperties),
475 cloneNode(babelTypeNsImport)
476 ),
477 ]),
478 ]);
479 }
480 },
481 },
482 } satisfies PluginObject;
483}
484
485function pluginReplaceTSImportExtension() {
486 return {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected