How to use the isStringLiteral function from @babel/types
Find comprehensive JavaScript @babel/types.isStringLiteral code examples handpicked from public code repositorys.
@babel/types.isStringLiteral is a function provided by the Babel compiler API that checks whether a given node is a string literal in an Abstract Syntax Tree (AST).
363 364 365 366 367 368 369 370 371 372
// if (alternate !== null && !t.isBlockStatement(alternate)) { // path.node.alternate = t.BlockStatement([alternate]) // } let { node } = path; if (t.isStringLiteral(node.test.left) && t.isStringLiteral(node.test.right)) { // 获取节点在容器中的索引 let index = path.key // 获取容器名字 let container = path.listKey
+ 2 other calls in file
GitHub: wwhtrbbtt/GPJSD
45 46 47 48 49 50 51 52 53 54
BinaryExpression(path) { const left = path.get("left"); const right = path.get("right"); const operator = path.get("operator").node; if (t.isStringLiteral(left.node) && t.isStringLiteral(right.node)) { // In this case, we can use the old algorithm // Evaluate the binary expression let { confident, value } = path.evaluate(); // Skip if not confident
+ 356 other calls in file
How does @babel/types.isStringLiteral work?
@babel/types.isStringLiteral
works by examining a given node in an Abstract Syntax Tree (AST) and determining whether it represents a string literal.
In JavaScript, a string literal is a sequence of characters surrounded by either single or double quotes. To identify string literals in an AST, the @babel/types.isStringLiteral
function checks whether the given node is a StringLiteral
node, which represents a string literal in the AST.
If the node is determined to be a string literal, then @babel/types.isStringLiteral
returns true
. Otherwise, it returns false
.
By using @babel/types.isStringLiteral
, Babel plugin developers can programmatically determine whether a given node represents a string literal, allowing for more powerful and flexible syntax analysis and manipulation.
305 306 307 308 309 310 311 312 313 314
} else { console.warn(`Unknown title attr: ${titleAttr.type}`); } } const id = t.isStringLiteral(idAttr) ? `'${idAttr.value}'` : null; const parameters = genAttribute('parameters', ast.openingElement); const decorators = genAttribute('decorators', ast.openingElement); const loaders = genAttribute('loaders', ast.openingElement); const component = genAttribute('component', ast.openingElement);
119 120 121 122 123 124 125 126 127 128
const node_ = path_.node; if ( node_.callee && t.isMemberExpression(node_.callee) && // !node_.callee.computed && t.isStringLiteral(node_.callee.object) && t.isIdentifier(node_.callee.property) && node_.callee.property.name === 'split' ) { const delimiter = node_.arguments[0].value;
+ 36 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9
const t = require("@babel/types"); // create a new string literal node const stringLiteral = t.stringLiteral("Hello, world!"); // check whether the node is a string literal using @babel/types.isStringLiteral console.log( `Is the node a string literal? ${t.isStringLiteral(stringLiteral)}` );
In this example, we're using @babel/types.isStringLiteral to check whether a given node (stringLiteral) represents a string literal in a Babel Abstract Syntax Tree (AST). We first create a new string literal node using the t.stringLiteral function provided by the Babel compiler API. This function takes a single argument, which is the string value of the literal. We then use @babel/types.isStringLiteral to check whether the newly created node is a string literal, logging the result to the console. When we run this code, it will output the following message to the console: vbnet Copy code
250 251 252 253 254 255 256 257 258 259
let tmpast = parse.parse(generator(BinNode).code); let addstr = ''; traverse(tmpast, { BinaryExpression: { exit: function (_p) { if (t.isStringLiteral(_p.node.right) && t.isStringLiteral(_p.node.left)) {//二进制表达式左右有一个类型为字符型 _p.replaceWith(t.StringLiteral(eval(generator(_p.node).code))) // 值替换节点 } addstr = _p.toString(); }
+ 14 other calls in file
89 90 91 92 93 94 95 96 97 98
// Require must be global for us to consider this a CommonJS // module. state.isCJS = true; // Check for nested string and template literals. const isString = t.isStringLiteral(node.arguments[0]); const isLiteral = t.isTemplateLiteral(node.arguments[0]); // Normalize the string value, default to the standard string // literal format of `{ value: "" }`.
+ 10 other calls in file
152 153 154 155 156 157 158 159 160 161
typeParameters: path.node.typeParameters ? path.get('typeParameters.params').map(p => processExport(p)) : [] }, docs)); } if (path.isClassProperty()) { let name = t.isStringLiteral(path.node.key) ? path.node.key.value : path.node.key.name; let docs = getJSDocs(path); return Object.assign(node, addDocs({ type: 'property', name,
+ 143 other calls in file
26 27 28 29 30 31 32 33 34 35
traverse(ast, { ExportNamedDeclaration(path) { const init = get(path, 'node.declaration.declarations.0.init'); const isArrowFn = t.isArrowFunctionExpression(init); const isStringLiteral = t.isStringLiteral(init); if (isStringLiteral) { lastConstantExport = path; }
+ 5 other calls in file
GitHub: jiangjing1994/fishhook
36 37 38 39 40 41 42 43 44 45
function getDefaultVal(node) { if ( types.isRegExpLiteral(node) || types.isBooleanLiteral(node) || types.isNumericLiteral(node) || types.isStringLiteral(node) ) { return node.value } else if ( types.isFunctionExpression(node) ||
GitHub: sven36/MayReactV16
62 63 64 65 66 67 68 69 70 71
} break; case 'BinaryExpression': { // a + b if (attrName === 'class' || attrName === 'className') { let { left, right } = expr; if (t.isStringLiteral(left) || t.isStringLiteral(right)) { let className = `${toString( expr.left )}${toString(expr.right)}`; astPath.replaceWith(t.stringLiteral(className));
48 49 50 51 52 53 54 55 56
} let isDynamicImport = callee.type === 'Import' && args.length === 1 && types.isStringLiteral(args[0]); if (isDynamicImport) { if (isURL(args[0].value)) return;
+ 3 other calls in file
GitHub: codefrau/parcel-bundler
278 279 280 281 282 283 284 285 286 287
let {object, property, computed} = path.node; if ( !( t.isIdentifier(object) && ((t.isIdentifier(property) && !computed) || t.isStringLiteral(property)) ) ) { return; }
GitHub: codefrau/parcel-bundler
240 241 242 243 244 245 246 247 248 249
// This allows us to remove the CommonJS export object completely in many cases. if ( t.isMemberExpression(left) && t.isIdentifier(left.object, {name: 'exports'}) && ((t.isIdentifier(left.property) && !left.computed) || t.isStringLiteral(left.property)) ) { let name = t.isIdentifier(left.property) ? left.property.name : left.property.value;
67 68 69 70 71 72 73 74 75 76
// TODO order identifierArray.forEach(identifier => { if (Array.isArray(identifier)) { let opts = false identifier.forEach(stringLiteral => { const key = t.isStringLiteral(stringLiteral) ? 'value' : 'name' if (opts === false) { opts = identifierOpts[stringLiteral[key]] stringLiteral[key] = `${opts.prefix + opts.id++}` } else {
+ 11 other calls in file
219 220 221 222 223 224 225 226 227 228
if (t.isObjectExpression(objectExpression)) { const nameProperty = objectExpression.properties[0] const isShowDir = nameProperty && nameProperty.key.name === 'name' && t.isStringLiteral(nameProperty.value) && nameProperty.value.value === 'show' if (isShowDir) { objectExpression.properties.find(valueProperty => { const isValue = valueProperty.key.name === 'value'
+ 5 other calls in file
GitHub: yandex/reselector
209 210 211 212 213 214 215 216 217 218
hashmap[key] = { id, loc: componentNode.loc, name: key, [NAME]: id } }) const [elementName, props] = p.node.arguments const isTag = t.isStringLiteral(elementName) let helper const spread = (objs) => {
+ 9 other calls in file
GitHub: t880216t/IAT
20 21 22 23 24 25 26 27 28 29
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, modulePath: item.source.value, });
+ 7 other calls in file
GitHub: msojocs/wcc.js
163 164 165 166 167 168 169 170 171 172
if (path.isIdentifier()) { if (path.node.name !== 'arguments' && path.node.name !== 'Infinity' && path.node.name !== 'undefined' && path.node.name !== 'NaN' && path.node.name !== 'Math' && path.node.name !== 'Number') { path.node.name = `nv_${path.node.name}`; } } if (path.isObjectProperty() && babelTypes.isStringLiteral(path.node.key)) { path.node.key.value = `nv_${path.node.key.value}`; } if (path.isMemberExpression() && babelTypes.isStringLiteral(path.node.property)) { path.node.property.value = `nv_${path.node.property.value}`;
+ 11 other calls in file
176 177 178 179 180 181 182 183 184 185
if ( t.isIdentifier(callee) && callee.name === 'require' && args.length === 1 && t.isStringLiteral(first) && !rule.test(first.value) ) { callee.name = 'requireJSX'; args.push(t.identifier('__dirname'));
+ 33 other calls in file
GitHub: raxjs/miniapp
150 151 152 153 154 155 156 157 158 159
JSXAttribute: { exit(innerPath) { const { node } = innerPath; // Handle renderItem if (node.name.name === 'data' && t.isStringLiteral(node.value) ) { const renderItemFnIdx = findIndex(renderItemFunctions || [], (fn) => node.value.value === `{{...${fn.name}}}`); if (renderItemFnIdx > -1) { const renderItem = renderItemFunctions[renderItemFnIdx];
+ 13 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)