How to use the objectPattern function from @babel/types

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

@babel/types.objectPattern is a tool used in the Babel JavaScript compiler to represent an object destructuring pattern in a JavaScript program.

72
73
74
75
76
77
78
79
80
81
  t.identifier(I8N_FUNC_NAME),
  t.identifier(I8N_FUNC_NAME),
  false,
  true,
);
let objectPattern = t.objectPattern([objectProperty]);
let callee = t.identifier(USE_TRANSLATION_FUN);
let useTranslationCall = t.callExpression(callee, []);
let tDeclarator = t.variableDeclarator(objectPattern, useTranslationCall);
const node = t.variableDeclaration('const', [tDeclarator]);
fork icon79
star icon190
watch icon15

+ 31 other calls in file

84
85
86
87
88
89
90
91
92
93
  return Array.from(this[USED].values());
}

get ast() {
  return createImport({
    GRAPHQL_IMPORTS: t.objectPattern(
      this.usedTypes.map(typeUsed =>
        t.objectProperty(
          t.identifier(typeUsed),
          t.identifier(typeUsed),
fork icon3
star icon46
watch icon11

How does @babel/types.objectPattern work?

@babel/types.objectPattern is a utility function provided by the Babel compiler that represents an object destructuring pattern in a JavaScript program. In JavaScript, object destructuring allows developers to extract properties from an object and assign them to variables. An object destructuring pattern is a shorthand way of defining the variable names and their corresponding object properties in a single line. To use @babel/types.objectPattern, developers first import the function from the @babel/types library. They can then create an object destructuring pattern using the function. The @babel/types.objectPattern function takes two arguments: an array of Property nodes and an optional AssignmentProperty node. The Property nodes represent the properties being destructured from the object, while the AssignmentProperty node represents the variable that the destructured properties should be assigned to. When Babel compiles a JavaScript program that uses object destructuring, it converts the destructuring pattern into an ObjectPattern node, using @babel/types.objectPattern to represent the pattern. Overall, @babel/types.objectPattern is a useful tool for representing object destructuring patterns in a JavaScript program, allowing developers to write concise and readable code.

14
15
16
17
18
19
20
21
22
23
const updateFuncKey = getUpdateHumpKey(name)

const ast = t.functionDeclaration(
  t.identifier(injectVariableKey), // id
  [ // params
    t.objectPattern([ // object property
      t.objectProperty(t.identifier('payload'), t.identifier('payload'))
    ]),
    t.objectPattern([ // object property
      t.objectProperty(t.identifier('call'), t.identifier('call')),
fork icon1
star icon25
watch icon1

+ 3 other calls in file

8
9
10
11
12
13
14
15
16
17
const key = objectMethodBody.key.name
const params = objectMethodBody.value.params || []
if (key === 'customRender') {
  const hasTsType = params.some(param => param.typeAnnotation)
  if (params.length && params[0].type === 'Identifier') {
    const objectPattern = t.objectPattern(params.map((param, index) => {
      const name = param.name // 参数变量
      const key = index ? (index === 1 ? 'record' : 'index') : 'text'
      return t.objectProperty(t.stringLiteral(key), t.identifier(key === name ? key : name))
    }))  
fork icon0
star icon5
watch icon2

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const t = require("@babel/types");

const ast = t.VariableDeclaration("const", [
  t.VariableDeclarator(
    t.ObjectPattern([
      t.Property(t.Identifier("x"), t.Identifier("a")),
      t.Property(t.Identifier("y"), t.Identifier("b")),
    ]),
    t.Identifier("obj")
  ),
]);

console.log(ast);

In this example, we first import the @babel/types library and define a VariableDeclaration node. We then use the VariableDeclarator function to define a new variable called obj that will hold the destructured object properties. We use the ObjectPattern function to represent the object destructuring pattern, which includes two Property nodes. These nodes specify that the x and y properties of the object should be assigned to variables called a and b, respectively. Finally, we log the resulting AST (Abstract Syntax Tree) node to the console using console.log(ast). When this code runs, it will print the following AST node to the console: javascript Copy code

66
67
68
69
70
71
72
73
74
75
76


function generateRequire(varName, moduleName) {
  return t.variableDeclaration('const', [
    t.variableDeclarator(
      Array.isArray(varName)
        ? t.objectPattern(
            varName.map(n =>
              t.objectProperty(t.identifier(n), t.identifier(n), false, true)
            )
          )
fork icon1
star icon2
watch icon1

+ 57 other calls in file

321
322
323
324
325
326
327
328
329
330
331
  })


const createTContext = context =>
  btypes.variableDeclaration('const', [
    btypes.variableDeclarator(
      btypes.objectPattern(context.properties),
      btypes.memberExpression(
        btypes.identifier('t'),
        btypes.identifier('context')
      )
fork icon0
star icon15
watch icon1

65
66
67
68
69
70
71
72
73
74
let childrenItem = compatibility.exportArgument(path)
if (!childrenItem.params.length) {
  return
}
childrenItem.params = [
  types.objectPattern([
    types.objectProperty(
      types.identifier('mode'),
      types.identifier('mode'),
      false,
fork icon0
star icon5
watch icon1

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