How to use the isImportSpecifier function from @babel/types

Find comprehensive JavaScript @babel/types.isImportSpecifier code examples handpicked from public code repositorys.

The @babel/types.isImportSpecifier function checks whether a given syntax node represents an import specifier in a JavaScript module.

76
77
78
79
80
81
82
83
84
85
  // Import specifiers are in statement order, which for mixed imports must be either "defaultImport, * as namespaceImport"
  // or "defaultImport, { namedImport [as alias]... } according to current ECMA-262.
  // Given that two equivalent import statements can only differ in the order of the items in a NamedImports block,
  // we only need to sort these items in relation to each other to normalize the statements for caching purposes.
  // Where the node is anything other than ImportSpecifier (Babel terminology for NamedImports), preserve the original statement order.
  if (t.isImportSpecifier(s1) && t.isImportSpecifier(s2)) {
    return s1.imported.name < s2.imported.name ? -1 : 1;
  }
  return 0;
})
fork icon80
star icon0
watch icon286

+ 11 other calls in file

145
146
147
148
149
150
151
152
153
154
  if (!allowedTag.includes(tagName)) {
    nodePath.skip();
  }
}

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, {
fork icon0
star icon0
watch icon0

+ 2 other calls in file

How does @babel/types.isImportSpecifier work?

The @babel/types.isImportSpecifier function is part of the Babel compiler toolkit for JavaScript and is used to check whether a given syntax node represents an import specifier in a JavaScript module. To accomplish this, the function first checks whether the given node is not null and has a type property that matches the ImportSpecifier string. If this check passes, it further examines the node to ensure that it has an imported property, which represents the name of the imported binding, and an local property, which represents the name of the binding that is used within the importing module. If both of these conditions are met, the @babel/types.isImportSpecifier function returns true. Otherwise, it returns false. This function is useful when working with the ASTs (Abstract Syntax Trees) produced by the Babel compiler, and enables developers to write code that selectively operates on import specifier nodes in their JavaScript code.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const t = require("@babel/types");

const node = {
  type: "ImportSpecifier",
  imported: { type: "Identifier", name: "foo" },
  local: { type: "Identifier", name: "bar" },
};

if (t.isImportSpecifier(node)) {
  console.log("Found import specifier for:", node.imported.name);
}

In this example, we're using the @babel/types.isImportSpecifier function to check whether a given syntax node represents an import specifier in a JavaScript module. We define a node object that represents an import specifier, with an imported property representing the name of the imported binding (foo) and a local property representing the name of the binding that is used within the importing module (bar). We use the isImportSpecifier function to check whether node represents an import specifier, and if so, we log the name of the imported binding to the console. When we run this code, it will output: arduino Copy code

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)