How to use the importSpecifier function from @babel/types

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

55
56
57
58
59
60
61
62
63
64
  let newString = t.stringLiteral(value);
  return t.callExpression(callee, [newString]);
};

const createImportIntl = function () {
  let importSpecifier = t.importSpecifier(
    t.identifier(USE_TRANSLATION_FUN),
    t.identifier(USE_TRANSLATION_FUN),
  );
  return t.importDeclaration(
fork icon79
star icon190
watch icon15

+ 5 other calls in file

160
161
162
163
164
165
166
167
168
169
    );
} else {
    localName = name in BINDINGS ? `_${name}` : name;

    IMPORT.specifiers.push(
        t.importSpecifier(t.identifier(localName), t.identifier(name)),
    );
}

imports[name] = localName;
fork icon15
star icon363
watch icon18

+ 29 other calls in file

85
86
87
88
89
90
91
92
93
94
} = this.source.get(component);
ast.body.push(
  genBuildImportDeclaration(
    types,
    [
      types.importSpecifier(
        types.identifier('AppRegistry'),
        types.identifier('AppRegistry')
      ),
    ],
fork icon2
star icon7
watch icon2

+ 3 other calls in file

61
62
63
64
65
66
67
68
69
70
function createImportNamespaceSpecifier(id) {
  return types.importNamespaceSpecifier(types.identifier(id));
}

function createImportSpecifier(local, imported) {
  return types.importSpecifier(
    types.identifier(local),
    types.identifier(imported)
  );
}
fork icon3
star icon6
watch icon2

1
2
3
4
5
6
7
8
const t = require('@babel/types')

module.exports = function(name) {
  if (hasImportNamed(name)) return
  
  const specifier = t.importSpecifier(t.identifier(name), t.identifier(name))
  global.ImportCompositionApiAST.specifiers.push(specifier)
}
fork icon0
star icon5
watch icon2

8
9
10
11
12
13
14
15
16
17
const { body } = node;
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,
fork icon3
star icon3
watch icon11

99
100
101
102
103
104
105
106
107
108
109
}


function generateImport(varName, moduleName) {
  return t.importDeclaration(
    Array.isArray(varName)
      ? varName.map(n => t.importSpecifier(t.identifier(n), t.identifier(n)))
      : [t.importDefaultSpecifier(t.identifier(varName))],
    t.stringLiteral(moduleName)
  );
}
fork icon1
star icon2
watch icon1

+ 57 other calls in file

342
343
344
345
346
347
348
349
350
351
352


function makeImportTestUtils (declaration) {
  if (btypes.isObjectPattern(declaration.id)) {
    return btypes.importDeclaration(
      declaration.id.properties.map(prop =>
        btypes.importSpecifier(
          btypes.identifier(prop.key.name),
          btypes.identifier(prop.key.name)
        )
      ),
fork icon0
star icon15
watch icon1

12
13
14
15
16
17
18
19
20
21
    },
})

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

21
22
23
24
25
26
27
28
29
30
function statement(items) {
  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

7
8
9
10
11
12
13
14
15
16
function generateDeclaration (url, names) {
  const temArr = []
  names.forEach(cur => {
    if (cur.key === 'normal') {
      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))
fork icon0
star icon2
watch icon2

+ 3 other calls in file

27
28
29
30
31
32
33
34
35
36
  return `export const ${constantName} = '${constantName}'`;
};

exports.makeImportSpecifier = actionName => {
  const identifier = t.identifier(changeCase.constantCase(actionName));
  return t.importSpecifier(identifier, identifier);
};

exports.renderActionCreator = actionName => {
  const variableName = changeCase.camelCase(actionName);
fork icon0
star icon2
watch icon0

206
207
208
209
210
211
212
213
214
215
let terminalchildren;
terminalchildren = this.getTerminalNodes(importItem.children[1]);
if (terminalchildren.length > 1) {
  return t.importDeclaration(
    [
      t.importSpecifier(
        t.identifier(terminalchildren.pop()),
        t.identifier("default")
      )
    ],
fork icon0
star icon1
watch icon0

97
98
99
100
101
102
103
104
105
106
  names: $ReadOnlyArray<string>,
  module: string,
): $FlowFixMe {
  const importDeclaration = t.importDeclaration(
    names.map(name =>
      t.importSpecifier(t.identifier(name), t.identifier(name)),
    ),
    t.stringLiteral(module),
  );
  importDeclaration.importKind = 'type';
fork icon0
star icon1
watch icon3

+ 3 other calls in file

382
383
384
385
386
387
388
389
390
391
if (specifiers.length > 0) {
  let literal =  t.stringLiteral("@watheia/react-spectrum");
  literal.raw = "'@watheia/react-spectrum'";

  let decl = t.importDeclaration(
    specifiers.map(s => t.importSpecifier(t.identifier(s), t.identifier(s))),
    literal
  );

  decl.loc = last.loc;
fork icon0
star icon1
watch icon0

57
58
59
60
61
62
63
64
65
66
 */
const getExportNamedNamespaceCode = (nsName, exportNames, source) => {
  return generate(
    t.program([
      t.importDeclaration(
        exportNames.map((n) => t.importSpecifier(t.identifier(n), t.identifier(n))),
        t.stringLiteral(source)
      ),
      t.exportNamedDeclaration(
        t.variableDeclaration('const', [
fork icon0
star icon0
watch icon1

+ 74 other calls in file

244
245
246
247
248
249
250
251
252
253
let insertTarget = state.insertTarget;
//没有导入Vue
if (!insertTarget) {
    insertTarget = t.importDeclaration([
        t.importSpecifier(t.identifier("defineComponent"), t.identifier("defineComponent")),
        t.importSpecifier(t.identifier("PropType"), t.identifier("PropType")),
    ], t.stringLiteral(state.opts.vue));
    path.node.body.unshift(insertTarget);
    state.propLocalName = "PropType";
    state.defineComponentLocalName = "defineComponent";
fork icon0
star icon0
watch icon1

+ 51 other calls in file

142
143
144
145
146
147
148
149
150
151
debug('Found type import:', { type, alias, mod, key });
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

18
19
20
21
22
23
24
25
26
27
if (t.isObjectPattern(path.parent.id)) {
  const { properties = [] } = path.parent.id;
  if (properties.length !== 0) {
    const needReplacePath = path.findParent(parent => parent.isVariableDeclaration());
    const specifiers = properties.map(
      prop => t.importSpecifier(
        t.identifier(prop.key.name),
        t.identifier(prop.key.name),
      ),
    );
fork icon0
star icon0
watch icon2

Other functions in @babel/types

Sorted by popularity

function icon

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