How to use the objectExpression function from @babel/types
Find comprehensive JavaScript @babel/types.objectExpression code examples handpicked from public code repositorys.
@babel/types.objectExpression is a tool that creates an object in JavaScript code.
GitHub: qooxdoo/qooxdoo
153 154 155 156 157 158 159 160 161 162 163
for (var key in value) { var expr = literalValueToExpression(value[key]); var prop = types.objectProperty(types.stringLiteral(key), expr); properties.push(prop); } return types.objectExpression(properties); } function formatValueAsCode(value) { if (value === undefined) {
+ 21 other calls in file
11 12 13 14 15 16 17 18 19 20
const key = isIdentifier ? t.identifier(name) : t.stringLiteral(name); return t.objectProperty(key, astValue); }); return t.objectExpression(properties); } function replaceCreateCall(callExpr, minifiedDefinitions) { callExpr.replaceWith(objectToAST(minifiedDefinitions));
How does @babel/types.objectExpression work?
@babel/types.objectExpression is a function provided by the Babel library that allows developers to generate an object expression node in the Abstract Syntax Tree (AST) of a JavaScript program. The object expression node is a representation of an object in the code, and it contains properties and values that can be accessed by the program at runtime. To create an object expression node using @babel/types.objectExpression, developers pass an array of property nodes to the function. Each property node represents a key-value pair in the object, and it includes a key node and a value node. The key node represents the name of the property, and the value node represents the value associated with the property. Once the object expression node is created, developers can use it to manipulate the object in various ways. They can add new properties or modify existing ones, access the values of properties, and perform other operations as needed. Ultimately, the object expression node is compiled back into executable JavaScript code by the Babel compiler, allowing the program to run as intended.
19 20 21 22 23 24 25 26 27 28
} else if (typeof data === 'number') { return t.numericLiteral(data) } else if (Array.isArray(data)) { return t.arrayExpression(data.map(stringifyRecursive)) } else if (data === Object(data)) { return t.objectExpression( Object.entries(data).map(([key, value]) => t.objectProperty(getKey(key), stringifyRecursive(value)) ) )
+ 5 other calls in file
GitHub: egoist/styled-vue
189 190 191 192 193 194 195 196 197 198
].filter(Boolean), t.blockStatement([ ...varDeclarations, t.returnStatement( isGlobal ? t.objectExpression(vars) : t.arrayExpression([ t.identifier('existing'), t.objectExpression(vars) ])
+ 31 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9
const t = require("@babel/types"); const myObject = t.objectExpression([ t.objectProperty(t.identifier("name"), t.stringLiteral("Alice")), t.objectProperty(t.identifier("age"), t.numericLiteral(30)), t.objectProperty(t.identifier("isStudent"), t.booleanLiteral(true)), ]); console.log(myObject);
In this example, we first import @babel/types and assign it to t. Then we call t.objectExpression with an array of t.objectProperty nodes, which represent the properties of our object. Each t.objectProperty node takes two arguments: a t.identifier node representing the key of the property, and a value node representing the value of the property (in this case, a t.stringLiteral, t.numericLiteral, and t.booleanLiteral, respectively). Finally, we log the resulting myObject node to the console, which will output the following object expression node in the Abstract Syntax Tree (AST): yaml Copy code
GitHub: yandex/reshadow
12 13 14 15 16 17 18 19 20 21
const buildClassName = template.expression(` NAME.${KEYS.__styles__}["ELEMENT"] `); const toObjectExpression = obj => t.objectExpression( Object.entries(obj).map(([key, value]) => t.objectProperty( t.stringLiteral(key), t.templateLiteral(
+ 29 other calls in file
GitHub: startupjs/startupjs
160 161 162 163 164 165 166 167 168 169
cssIdentifier ? t.identifier(cssIdentifier.name) : t.objectExpression([]), buildSafeVar({ variable: t.identifier(GLOBAL_NAME) }), buildSafeVar({ variable: t.identifier(LOCAL_NAME) }), t.objectExpression(inlineStyles) ] ) jsxOpeningElementPath.node.attributes.push(
+ 15 other calls in file
81 82 83 84 85 86 87 88 89 90
case 'references': if (_.isPlainObject(value) && !_.isEmpty(value)) { properties.push( t.objectProperty( t.identifier(key), t.objectExpression([ t.objectProperty( t.identifier('key'), t.stringLiteral(value.key), ),
+ 47 other calls in file
14 15 16 17 18 19 20
t.stringLiteral(name); ast.push(t.objectProperty(key, astValue)); } return t.objectExpression(ast); };
+ 3 other calls in file
GitHub: baidu/wx2
33 34 35 36 37 38 39 40 41
types.objectProperty(types.identifier('type'), types.identifier('String')), types.objectProperty(types.identifier('value'), types.stringLiteral('')) ]), false, false, null)); types.objectExpression(init.properties); } }); };
+ 3 other calls in file
61 62 63 64 65 66 67 68 69 70
return true; } return false; }, getResult(mergeItems) { return t.objectExpression([ mergeItems.length > 0 && t.spreadElement( builder.buildCallRuntimeExpression( 'merge-props.js', [
+ 11 other calls in file
24 25 26 27 28 29 30 31 32 33
switch (property) { case "mixins": obj = t.arrayExpression(pathAry); break; default: obj = t.objectExpression(pathAry); break; } return obj;
+ 7 other calls in file
GitHub: IFreeOvO/i18n-cli
53 54 55 56 57 58 59 60 61 62
} else { newValue = t.identifier(tempValue) } ObjectPropertyArr.push(t.objectProperty(t.identifier(k), newValue)) }) const ast = t.objectExpression(ObjectPropertyArr) return ast } // 判断节点是否是props属性的默认值
+ 3 other calls in file
GitHub: groupon/swagql
171 172 173 174 175 176 177 178 179 180
FETCH: t.identifier('FETCH'), VERIFY_AUTH_STATUS: t.identifier('VERIFY_AUTH_STATUS'), }); const postfix = buildPostfix({ QUERY_FIELDS: this.queryFields.size ? t.objectExpression(Array.from(this.queryFields.values())) : t.nullLiteral(), MUTATION_FIELDS: this.mutationFields.size ? t.objectExpression(Array.from(this.mutationFields.values())) : t.nullLiteral(),
+ 3 other calls in file
131 132 133 134 135 136 137 138 139 140
t.memberExpression( t.identifier('Object'), t.identifier('assign') ), [ t.objectExpression([]), t.identifier('state'), t.objectExpression([ t.objectProperty( t.identifier(name),
+ 5 other calls in file
GitHub: alijk-fe/motor-rax
209 210 211 212 213 214 215 216 217 218
t.objectProperty(params[0], params[0]), t.objectProperty(params[1], params[1]) ]; const loopFnBody = t.blockStatement([ t.returnStatement( t.objectExpression(properties) ) ]); const mapCallExpression = t.callExpression( t.memberExpression(forNode, t.identifier('map')),
+ 7 other calls in file
GitHub: fangkyi03/spack-new
95 96 97 98 99 100 101 102 103 104
const viewtype = node.arguments[0].value.replace('An','') const attrsTemplate = babel.template('{attrs:{viewtype:%%viewtype%%}}') node.arguments[0] = types.identifier('Container') const arg1 = Object.assign({},node.arguments[1]) if (node.arguments.length == 1) { node.arguments[1] = types.objectExpression([ types.objectProperty(types.stringLiteral('attrs'), types.objectExpression( [ types.objectProperty(types.stringLiteral('viewtype'), types.stringLiteral(viewtype)),
+ 47 other calls in file
37 38 39 40 41 42 43 44 45 46
}, VariableDeclarator: (path) => { const { node } = path; if (t.isIdentifier(node.id, { name: 'attributes' })) { const properties = processAttributesProperties(definition.attributes); node.init = t.objectExpression(properties); } if (t.isIdentifier(node.id, { name: 'options' })) { const properties = processOptionsProperties(
+ 15 other calls in file
GitHub: alijk-fe/motor-rax
163 164 165 166 167 168 169 170 171 172
? genTagIdExp(tagIdExpression) : t.stringLiteral(tagId); const updatePropsArgs = [ argPIDExp, t.objectExpression(propMaps) ]; const callUpdateProps = t.expressionStatement(t.callExpression(updateProps, updatePropsArgs)); if (propMaps.length > 0) { const targetNode = parentNode || fnBody;
83 84 85 86 87 88 89 90 91 92
} function transformComponent(component, scope) { // Build the props object to pass as an expression to // the render function or the constructor for our component const propsOExp = t.objectExpression([]); if (component.children.length) { propsOExp.properties.push(t.objectProperty(t.identifier('children'), t.arrayExpression(component.children.map(element => transformNode(element, scope))))); } transformAttributes(propsOExp, component);
+ 7 other calls in file
GitHub: Leman-li/tanfu.js
298 299 300 301 302 303 304 305 306 307 308 309
return TanFuArrayExpression; }(); function ObjectExpression(object) { var expression = t.objectExpression([]); Object.keys(object).forEach(function (key) { expression.properties.push(t.objectProperty(t.identifier(key), getLiteral(object[key]))); }); return expression;
+ 8 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)