How to use the importDefaultSpecifier function from @babel/types

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

The @babel/types.importDefaultSpecifier is a function provided by the Babel library that represents a default import specifier in an abstract syntax tree (AST) node.

154
155
156
157
158
159
160
161
162

if (name === 'default') {
    localName = 'styled' in BINDINGS ? '_styled' : 'styled';

    IMPORT.specifiers.push(
        t.importDefaultSpecifier(t.identifier(localName)),
    );
} else {
    localName = name in BINDINGS ? `_${name}` : name;
fork icon15
star icon363
watch icon0

+ 29 other calls in file

19
20
21
22
23
24
25
26
27
28
// 对 ast 进行深度遍历
traverse(ast, {
  Program(path) {
    const node = t.importDeclaration(
      [
        t.importDefaultSpecifier(
          t.identifier('{ isInIcestark, setLibraryName }'),
        ),
      ],
      t.stringLiteral('@ice/stark-app'),
fork icon15
star icon36
watch icon1

How does @babel/types.importDefaultSpecifier work?

The @babel/types.importDefaultSpecifier is a function provided by the Babel library that represents a default import specifier in an abstract syntax tree (AST) node. An import default specifier is used to import the default export from a module, and can be used in the import statement in JavaScript/TypeScript code. When creating an AST node using Babel, importDefaultSpecifier can be used to create a new node representing a default import specifier. The function takes in an object with the following properties: local: The local name of the default import specifier. imported: The imported name of the default import specifier (which will be the string "default"). The returned node is an instance of the ImportDefaultSpecifier class in the Babel AST, and can be used in constructing a larger AST representing the imported module. Overall, @babel/types.importDefaultSpecifier provides a way to create an AST node representing a default import specifier in JavaScript/TypeScript code, which can be useful in cases where you need to programmatically generate code or perform transformations on an existing AST.

53
54
55
56
57
58
59
60
61
62
    ]
  );
}

function createImportDefaultSpecifier(id) {
  return types.importDefaultSpecifier(types.identifier(id));
}

function createImportNamespaceSpecifier(id) {
  return types.importNamespaceSpecifier(types.identifier(id));
fork icon3
star icon6
watch icon2

9
10
11
12
13
14
15
16
17
18
const lastImportIndex = _.findLastIndex(body, (item) => t.isImportDeclaration(item));

const importSpecifiers = (Array.isArray(specifiers) ? specifiers : [specifiers]).map((specifier) => {
  return specifier.imported
    ? t.importSpecifier(t.identifier(specifier.local), t.identifier(specifier.imported))
    : t.importDefaultSpecifier(t.identifier(specifier));
});
const newImport = t.importDeclaration(
  importSpecifiers,
  t.stringLiteral(importPath),
fork icon3
star icon3
watch icon11

Ai Example

1
2
3
4
5
const t = require("@babel/types");

const specifier = t.importDefaultSpecifier(t.identifier("myModule"));

console.log(specifier);

In this example, we use @babel/types.importDefaultSpecifier to create a new AST node representing a default import specifier. We first import the @babel/types library and assign it to the variable t. We then call the importDefaultSpecifier function provided by t, passing in an object with a local property set to an identifier node with the value 'myModule'. This creates a new instance of the ImportDefaultSpecifier class in the Babel AST, which represents a default import specifier with a local name of myModule. We log the resulting node to the console to show its structure and properties. Note that in order to use @babel/types.importDefaultSpecifier, you need to have the Babel library installed and imported in your application.

310
311
312
313
314
315
316
317
318

  ```
  /**
   * @param {Identifier} local  (required)
   */
  t.importDefaultSpecifier(local)
  ```

- 在 `importDeclaration` 中还需要生成一个 `StringLiteral`
fork icon2
star icon0
watch icon1

+ 51 other calls in file

37
38
39
40
41
42
43
44
45
46
  }
}

const identifier = types.identifier(importSpec)
const importedIdent = types.identifier(imported)
const classNameDefSpec = importDefault ? types.importDefaultSpecifier(identifier) : types.importSpecifier(identifier, importedIdent)
const classnamesImpo = types.importDeclaration([ classNameDefSpec ], types.stringLiteral(importSrc))
program.unshiftContainer('body', classnamesImpo)

return importSpec
fork icon0
star icon2
watch icon2

11
12
13
14
15
16
17
18
19
20
      collect.imports.push(path.node)
    },
})

const ImportDeclarationTaro = t.importDeclaration([
    t.importDefaultSpecifier(t.identifier('Taro')),
    t.importSpecifier(t.identifier('Component'), t.identifier('Component'))
], t.stringLiteral('@tarojs/taro'))
if (Object.keys(state.props).length) {
    const importPropTypes = t.importDeclaration(
fork icon1
star icon2
watch icon1

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
    temArr.push(
      t.importSpecifier(t.identifier(cur.name), t.identifier(cur.name))
    )
  } else if (cur.key === 'default') {
    temArr.unshift(
      t.importDefaultSpecifier(t.identifier(cur.name))
    )
  }
})
return t.importDeclaration(temArr, t.stringLiteral(url))
fork icon0
star icon2
watch icon2

+ 3 other calls in file

22
23
24
25
26
27
28
29
30
31
return items.map(({ from, source, to }) =>
  t.importDeclaration(
    [
      from
        ? t.importSpecifier(t.identifier(to), t.identifier(from))
        : t.importDefaultSpecifier(t.identifier(to)),
    ],
    t.stringLiteral(source),
  ),
);
fork icon0
star icon2
watch icon0

+ 4 other calls in file

235
236
237
238
239
240
241
242
243
244
    importValItem = t.importSpecifier(
      t.identifier(terminalchildren.pop()),
      t.identifier(terminalchildren.shift())
    );
  } else {
    importValItem = t.importDefaultSpecifier(
      t.identifier(child.getText())
    );
  }
}
fork icon0
star icon1
watch icon0

44
45
46
47
48
49
50
51
52
53
ImportDeclaration(nodePath) {
  const { specifiers } = nodePath.node;
  if (!t.isImportDefaultSpecifier(specifiers[0]) && specifiers.length > 0) {
    const importDeclarations = specifiers.map((item) => {
      if (t.isImportSpecifier(item)) {
        const importDefaultSpecifier = t.importDefaultSpecifier(item.local);
        const source = t.stringLiteral(`${nodePath.node.source.value}/${item.local.name}`);
        return t.importDeclaration([importDefaultSpecifier], source);
      }
    });
fork icon0
star icon0
watch icon1

46
47
48
49
50
51
52
53
54
55
function importGeneric(path, dir) {
  const importsList = getImportsList(path)
  const importStatements = importsList.map(function (specifier) {
    const importName = specifier.imported.name
    return importDeclaration(
      types.importDefaultSpecifier(types.identifier(importName)),
      `@occmundial/atomic/${dir}/${importName}`
    )
  })
  replaceImport(path, importStatements)
fork icon0
star icon0
watch icon4

+ 15 other calls in file

143
144
145
146
147
148
149
150
151
152
stmts.unshift(
  t.importDeclaration(
    [
      key
        ? t.importSpecifier(t.identifier(alias), t.identifier(key))
        : t.importDefaultSpecifier(t.identifier(alias)),
    ],
    t.stringLiteral(mod)
  )
);
fork icon0
star icon0
watch icon7

+ 3 other calls in file

Other functions in @babel/types

Sorted by popularity

function icon

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