Debian Codegraph @

scripts/babel-plugin-compat-core-import.js

raw symbols SCIP
1 // Convert
2 // import { x as y } from "@babel/core";
3 // to
4 // import * as babel from "@babel/core";
5 // const { x: y } = babel.default || babel;
6
7 module.exports = function ({ types: t, template }) {
8 return {
9 visitor: {
10 ImportDeclaration(path) {
11 if (
12 t.isStringLiteral(path.node.source, { value: "@babel/core" }) &&
13 path.node.specifiers.every(node => t.isImportSpecifier(node))
14 ) {
15 const defaultName = path.scope.generateUidIdentifier("babel");
16
17 const destr = t.objectPattern(
18 path.node.specifiers.map(spec =>
19 t.objectProperty(spec.imported, spec.local)
20 )
21 );
22
23 path.replaceWithMultiple(template.statements.ast`
24 import * as ${defaultName} from "@babel/core";
25 const ${destr} = ${defaultName}.default || ${defaultName};
26 `);
27 }
28 },
29 },
30 };
31 };