How to use the stringLiteral function from @babel/types

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

@babel/types.stringLiteral is a tool used in JavaScript for creating a string literal node in an Abstract Syntax Tree (AST) representation of JavaScript code.

131
132
133
134
135
136
137
138
139
140
}
if (qx.lang.Type.isRegExp(value)) {
  return types.regExpLiteral(value.toString());
}
if (qx.lang.Type.isDate(value)) {
  return types.stringLiteral(value.toString());
}

if (qx.lang.Type.isArray(value)) {
  var arr = [];
fork icon257
star icon757
watch icon77

+ 67 other calls in file

61
62
63
64
65
66
67
68
69
70
    t.identifier(USE_TRANSLATION_FUN),
    t.identifier(USE_TRANSLATION_FUN),
  );
  return t.importDeclaration(
    [importSpecifier],
    t.stringLiteral(IMPORT_SOURCE),
  );
};

const createUseTranslationExpression = function () {
fork icon79
star icon190
watch icon15

+ 63 other calls in file

How does @babel/types.stringLiteral work?

@babel/types.stringLiteral is a function provided by the @babel/types library that is used in JavaScript for creating a string literal node in an Abstract Syntax Tree (AST) representation of JavaScript code. To use @babel/types.stringLiteral, developers first import the function from the @babel/types library. They can then call the function with one argument: a string representing the value of the string literal. The @babel/types.stringLiteral function then creates an AST node representing the string literal with the specified value. This node can be included in an AST representation of JavaScript code. The @babel/types.stringLiteral function also supports additional options that can be passed as a second argument, such as specifying the location of the string literal in the source code. @babel/types.stringLiteral is a useful tool for manipulating and generating AST nodes for JavaScript code. It is often used as part of a larger process for transforming or analyzing JavaScript code programmatically.

97
98
99
100
101
102
103
104
105
106

path.node.attributes = [
  t.jSXAttribute(t.jSXIdentifier('is'), t.stringLiteral(templateName)),
  t.jSXAttribute(
    t.jSXIdentifier('data'),
    t.stringLiteral(`{{${templateData}}}`)
  ),
]
path.node.selfClosing = false
path.parent.closingElement = t.jSXClosingElement(
fork icon12
star icon84
watch icon1

+ 11 other calls in file

262
263
264
265
266
267
268
269
270
271
traverse(ast, {
  ClassDeclaration(path) {
    const local = t.Identifier('SENTRY_PROVIDERS');
    const imported = t.Identifier('SENTRY_PROVIDERS');
    const specifiers = [t.ImportSpecifier(local, imported)];
    const source = t.stringLiteral('@core/sentry');
    const importDeclaration = t.importDeclaration(specifiers, source);

    console.log(importDeclaration);
  },
fork icon4
star icon56
watch icon0

+ 35 other calls in file

Ai Example

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

const stringLiteral = t.stringLiteral("Hello, world!");

console.log(stringLiteral);

In this example, we first import the t object from the @babel/types library. We then create a string literal node using the t.stringLiteral function and pass in the string "Hello, world!" as the argument. We store the resulting string literal node in a variable called stringLiteral, and log it to the console using console.log(stringLiteral). When this code runs, it will create a string literal node representing the string "Hello, world!" in an AST representation of JavaScript code, and log the resulting node to the console. This demonstrates how @babel/types.stringLiteral can be used to create a string literal node in an AST representation of JavaScript code.

42
43
44
45
46
47
48
49
50
51
  path.node.value = types.stringLiteral(
    path.node.value.quasis[0].value.raw
  );
}
if (types.isIdentifier(path.node.key)) {
  path.node.key = types.stringLiteral(path.node.key.name);
}
if (types.isNumericLiteral(path.node.key)) {
  path.node.key = types.stringLiteral(path.node.key.extra.raw);
}
fork icon3
star icon45
watch icon5

+ 1179 other calls in file

66
67
68
69
70
71
72
73
74
75
  t.jsxAttribute(t.jsxIdentifier('type'), t.stringLiteral(action.type)),
  t.jsxAttribute(t.jsxIdentifier('record'), t.jSXExpressionContainer(t.identifier('record'))),
]
if (action.type === 'delete') {
  attrArray.push(t.jsxAttribute(t.jsxIdentifier('visible'), t.jSXExpressionContainer(t.booleanLiteral(!!showDelete))))
  attrArray.push(t.jsxAttribute(t.jsxIdentifier('name'), t.stringLiteral(action.name)))
  attrArray.push(t.jsxAttribute(t.jsxIdentifier('primaryKey'), t.stringLiteral(primaryKey)))
  attrArray.push(t.jsxAttribute(t.jsxIdentifier('onClick'), t.jSXExpressionContainer(t.identifier('handleRemove'))))
}
if (action.type === 'detail') {
fork icon1
star icon25
watch icon0

+ 6 other calls in file

29
30
31
32
33
34
35
36
37
38
const attrArray = [
  t.jsxAttribute(t.jsxIdentifier('value'), t.jSXExpressionContainer(t.memberExpression(t.identifier('current'), t.identifier(item.key)))),
  t.jsxAttribute(t.jsxIdentifier('mode'), t.stringLiteral('detail'))
]
if (item.showType) {
  attrArray.push(t.jsxAttribute(t.jsxIdentifier('showType'), t.stringLiteral(item.showType)))
}
if (item.format) {
  attrArray.push(t.jsxAttribute(t.jsxIdentifier('format'), t.stringLiteral(item.format)))
}
fork icon1
star icon25
watch icon0

409
410
411
412
413
414
415
416
417
418
  [],
  true
),
t.callExpression(
  t.callExpression(t.identifier('getFieldDecorator'), [
    t.stringLiteral(key),
    t.objectExpression(configArray)
  ]),
  itemExpression
)
fork icon1
star icon25
watch icon0

+ 19 other calls in file

240
241
242
243
244
245
246
247
248
249
var _Object$prototype$toS;

if (value instanceof TanFuObjectExpression) return value.expression;
if (value instanceof TanFuArrayExpression) return value.expression;
if (t.isExpression(value)) return value;
if (typeof value === 'string') return t.stringLiteral(value);
if (typeof value === 'number') return t.numericLiteral(value);
if (typeof value === 'boolean') return t.booleanLiteral(value);
if ((_Object$prototype$toS = Object.prototype.toString.call(value)) === null || _Object$prototype$toS === void 0 ? void 0 : _Object$prototype$toS.includes('Object')) return ObjectExpression(value);
if (Object.prototype.toString.call(value).includes('Array')) return ArrayExpression(value);
fork icon5
star icon8
watch icon2

+ 8 other calls in file

68
69
70
71
72
73
74
75
76
77
t.jSXAttribute(
  t.jsxIdentifier("aria-labelledby"),
  t.jsxExpressionContainer(
    t.conditionalExpression(
      t.memberExpression(t.identifier("props"), t.identifier("title")),
      t.stringLiteral(nameSpacedId),
      t.identifier("undefined"),
    ),
  ),
),
fork icon6
star icon20
watch icon2

+ 2 other calls in file

261
262
263
264
265
266
267
268
269
270
  }
  addDepModule(REQUIRE_FROM_STRING_VAR, REQUIRE_FROM_STRING_MODULE, scope);
  return t.objectProperty(
    t.identifier('handler'),
    t.callExpression(t.identifier(REQUIRE_FROM_STRING_VAR), [
      t.stringLiteral(step.handler)
    ])
  );
}

fork icon1
star icon2
watch icon1

+ 347 other calls in file

463
464
465
466
467
468
469
470
471
472
  plugins: ['jsx']
});

if (t.isJSXOpeningElement(ast.openingElement) && ['Preview', 'Canvas'].includes(ast.openingElement.name.name) && !hasStoryChild(ast)) {
  const canvasAst = ast.openingElement;
  canvasAst.attributes.push(t.jsxAttribute(t.jsxIdentifier('mdxSource'), t.stringLiteral(getMdxSource(ast.children))));
}

const {
  code
fork icon1
star icon2
watch icon1

43
44
45
46
47
48
49
50
51
52
}

function gen_Node(funcName, body) {

    let _arguments = [
        types.stringLiteral("当前 Hook 函数名称: " + funcName + "   " + "点击展开详细"),
        types.identifier("arguments")]
    body.unshift(types.callExpression(types.identifier("WjsHook_log"), _arguments))
    if (Debug) body.unshift(types.debuggerStatement());  //debugger 插桩
}
fork icon1
star icon0
watch icon1

+ 2 other calls in file

249
250
251
252
253
254
255
256
257
258
for (const attr of openingElement.node.attributes) {

    // 4.1 JSXAttribute => more sophisticated scheme
    if (attr.type === 'JSXAttribute') {
        const name = attrPropDict[attr.name.name] || Object.keys(propDict[tagName] || {}).find(e => e.toLowerCase() === attr.name.name) || attr.name.name;
        const value = attr.value ? attr.value.expression ? attr.value.expression : attr.value : t.stringLiteral('');

        // 4.1.1 transpile `on${event}` handlers
        if (name.startsWith('on')) {
            const event = name.slice(2).toLowerCase();
fork icon1
star icon0
watch icon1

+ 13 other calls in file

244
245
246
247
248
249
250
251
252
253

const currentScope = this._findScopeUid(path, scopeData);

if (scopeUidToData.has(currentScope.uid)) {
  const scopeData = scopeUidToData.get(currentScope.uid);
  node.property = t.stringLiteral(scopeData.getString(parseInt(callNode.arguments[0].value)));
}

this._shuffleInnerArray(
  currentScope,
fork icon0
star icon6
watch icon1

+ 110 other calls in file

2
3
4
5
6
7
8
9
10
11
12
const generateCode = require('./generateCode')
const moduleExports = require('./moduleExports')


function getKey(encoded) {
  if (/\d/.test(encoded[0])) {
    return t.stringLiteral(encoded)
  }
  return t.identifier(encoded)
}

fork icon0
star icon3
watch icon8

+ 119 other calls in file

108
109
110
111
112
113
114
115
116
117
  depModulePath = require.resolve(depModuleName)
}
this.fileDependencies.add(depModulePath)
//获取依赖的模块的ID,修改语法树,把依赖的模块名换成模块ID
let depModuleId = "./" + path.posix.relative(baseDir, depModulePath)
node.arguments[0] = types.stringLiteral(depModuleId)
//把依赖的块ID和依赖的模块路径放置到当前模块的依赖数组中
module.dependencies.push({
  depModuleId,
  depModulePath,
fork icon1
star icon0
watch icon1

+ 52 other calls in file

347
348
349
350
351
352
353
354
355
356
      btypes.importSpecifier(
        btypes.identifier(prop.key.name),
        btypes.identifier(prop.key.name)
      )
    ),
    btypes.stringLiteral('./helpers/utils')
  )
}
return btypes.importDeclaration(
  [btypes.importNamespaceSpecifier(btypes.identifier('utils'))],
fork icon0
star icon15
watch icon1

14
15
16
17
18
19
20
21
22
23
let typeName = typeof obj;
switch (typeName) {
    case 'number':
        return types.numericLiteral(obj);
    case 'string':
        return types.stringLiteral(obj);
    case 'boolean':
        return types.booleanLiteral(obj);
    default:
        return null;
fork icon0
star icon2
watch icon1

+ 11 other calls in file

132
133
134
135
136
137
138
139
140
141
  state.globals.add(prop.value.name);
});

const decl = t.importDeclaration(
  specifiers,
  t.stringLiteral(str.value)
);

// @ts-ignore
decl.__replaced = true;
fork icon0
star icon1
watch icon1

+ 21 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)