How to use the isImportDefaultSpecifier function from @babel/types
Find comprehensive JavaScript @babel/types.isImportDefaultSpecifier code examples handpicked from public code repositorys.
In the Babel library for JavaScript, @babel/types.isImportDefaultSpecifier is a function that checks whether a given AST node represents an import default specifier.
GitHub: t880216t/IAT
![t880216t profile picture](/t880216t.png?size=40)
18 19 20 21 22 23 24 25 26 27
const { body } = node; body.forEach(item => { if (t.isImportDeclaration(item)) { const { specifiers } = item; const defaultEpecifier = specifiers.find(s => { return t.isImportDefaultSpecifier(s) && t.isIdentifier(s.local); }); if (defaultEpecifier && t.isStringLiteral(item.source)) { importModules.push({ identifierName: defaultEpecifier.local.name,
+ 7 other calls in file
18 19 20 21 22 23 24 25 26 27
const { body } = node; body.forEach((item) => { if (t.isImportDeclaration(item)) { const { specifiers } = item; const defaultEpecifier = specifiers.find( (s) => t.isImportDefaultSpecifier(s) && t.isIdentifier(s.local), ); if (defaultEpecifier && t.isStringLiteral(item.source)) { importModules.push({ identifierName: defaultEpecifier.local.name,
+ 7 other calls in file
How does @babel/types.isImportDefaultSpecifier work?
@babel/types.isImportDefaultSpecifier
is a function in the Babel library for JavaScript that checks whether a given AST node represents an import default specifier.
When @babel/types.isImportDefaultSpecifier
is called with an AST node as input, it performs the following operations:
- It returns
true
if the input node represents an import default specifier,false
otherwise. - To check if the node is an import default specifier, it checks whether the node is an object with a
type
property set to"ImportDefaultSpecifier"
.
By using @babel/types.isImportDefaultSpecifier
, developers can easily determine whether a given AST node represents an import default specifier, which can be useful for traversing and manipulating JavaScript code using Babel. Note that @babel/types.isImportDefaultSpecifier
is part of the Babel types API, which provides a set of functions for working with the AST nodes that represent JavaScript code.
GitHub: fecym/ast-share
![fecym profile picture](/fecym.png?size=40)
17 18 19 20 21 22 23 24 25 26
// console.log("ImportDeclaration -> node", node) // 得到节点的详细说明,然后转换成多个的 import 声明 const specifiers = node.specifiers // 要处理这个我们做一些判断,首先判断不是默认导出我们才处理,要考虑 import vant, { Button, Icon } from 'vant' 写法 // 还要考虑 specifiers 的长度,如果长度不是 1 并且不是默认导出我们才需要转换 if (!(specifiers.length === 1 && t.isImportDefaultSpecifier(specifiers[0]))) { const result = specifiers.map(specifier => { let local = specifier.local, source if (t.isImportDefaultSpecifier(specifier)) { source = t.stringLiteral(node.source.value)
+ 3 other calls in file
GitHub: facebook/fbt
![facebook profile picture](/facebook.png?size=40)
94 95 96 97 98 99 100 101 102 103
return; } const specifier = node.specifiers[0]; if ( t.isImportDefaultSpecifier(specifier) || t.isImportNamespaceSpecifier(specifier) ) { const alias = specifier.local.name; const modulePath = node.source.value;
+ 5 other calls in file
Ai Example
1 2 3 4 5 6 7
const t = require("@babel/types"); const importNode = t.importDefaultSpecifier(t.identifier("myModule")); const isDefault = t.isImportDefaultSpecifier(importNode); console.log(isDefault); // Outputs: true
In this example, we're using @babel/types.isImportDefaultSpecifier to check whether the importNode AST node represents an import default specifier. We create the importNode using the t.importDefaultSpecifier function, which creates an AST node representing an import default specifier for the myModule module. We then pass importNode to t.isImportDefaultSpecifier, which returns true since importNode is indeed an import default specifier.
GitHub: SaraVieira/rax
![SaraVieira profile picture](/SaraVieira.png?size=40)
34 35 36 37 38 39 40 41 42
const source = path.node.source.value; imported[source] = []; path.node.specifiers.forEach((specifier) => { const local = specifier.local.name; const ret = { local, default: t.isImportDefaultSpecifier(specifier), namespace: t.isImportNamespaceSpecifier(specifier) }; if (ret.default === false && ret.namespace === false) { ret.importFrom = specifier.imported.name; }
14 15 16 17 18 19 20 21 22 23
let isDefaultExport = false; let importedName = ''; let isImportedIdentifier = path.node.specifiers.some(specifier => { const matchesName = specifier.local.name === name; if (matchesName) { isDefaultExport = types.isImportDefaultSpecifier(specifier); importedName = specifier.imported && specifier.imported.name; return true; } });
+ 231 other calls in file
392 393 394 395 396 397 398 399 400 401
const specifiers = node.specifiers // 要处理这个我们做一些判断,首先判断不是默认导出我们才处理,要考虑 import vant, { Button, Icon } from 'vant' 写法 // 还要考虑 specifiers 的长度,如果长度不是 1 并且不是默认导出我们才需要转换 if ( !( specifiers.length === 1 && t.isImportDefaultSpecifier(specifiers[0]) ) ) { const result = specifiers.map((specifier) => { let local = specifier.local,
+ 103 other calls in file
150 151 152 153 154 155 156 157 158 159
if (t.isImportDefaultSpecifier(nodePath) || t.isImportSpecifier(nodePath)) { const importPath = nodePath.parent.source.value; const name = nodePath?.node?.local?.name || ''; if (name && importPath) { importMap.set(name, { type: t.isImportDefaultSpecifier(nodePath) ? 'default' : 'module', path: importPath, cwd: path.parse(mdPath)?.dir, }); }
+ 2 other calls in file
34 35 36 37 38 39 40 41 42 43
if(typeof prepare !== "object") return ; if(nodePath.node.specifiers && nodePath.node.specifiers.map && nodePath.node.specifiers.length > 0 ){ var iterator = {}; for (iterator of nodePath.node.specifiers) { isDefault = babelTypes.isImportDefaultSpecifier(iterator); isImported = iterator.imported ? iterator.imported.name !== iterator.local.name : false; importName = iterator.local.name; importNameList.push({ name : iterator.local.name ,
@babel/types.identifier is the most popular function in @babel/types (20936 examples)