How to use the variableDeclaration function from babel-types

Find comprehensive JavaScript babel-types.variableDeclaration code examples handpicked from public code repositorys.

121
122
123
124
125
126
127
128
129
130
var relativePath = path.relative(path.resolve(__dirname + '/../lib'), filename);
if (relativePath[0] !== '.') {
  relativePath = './' + relativePath;
}
statements.push(
  t.variableDeclaration('var', [
    t.variableDeclarator(
      id,
      t.callExpression(t.identifier('require'), [t.stringLiteral(relativePath)])
    ),
fork icon59
star icon89
watch icon10

+ 25 other calls in file

1
2
3
4
5
6
7
8
9
10

'use strict';
var t = require('babel-types');

var buildImportAssignment = function(id, source) {
  return t.variableDeclaration(
    'var',
    [
      t.variableDeclarator(
        t.identifier(id),
fork icon18
star icon51
watch icon5

77
78
79
80
81
82
83
84
85
86
Compiler.prototype.wrapCallExpression = function(node) {
  return t.yieldExpression(node, true);
}

Compiler.prototype.ast_variableDeclaration = function() {
    return t.variableDeclaration('var', [
          t.variableDeclarator(t.identifier('pug_mixins'),
            t.logicalExpression('||',
              t.memberExpression(t.identifier('locals'),t.identifier('pug_mixins')) ,
              t.objectExpression([]))),
fork icon5
star icon43
watch icon10

+ 7 other calls in file

229
230
231
232
233
234
235
236
237
238

有行列以及函数名,我们就需要把这些信息存到一个变量,那么就可以利用`babel-types`来生成单个变量对应AST节点了,点击[这里](https://babeljs.io/docs/en/next/babel-types.html#variabledeclaration)获取如何生成申明变量的节点,代码如下:

```js
// 生成一个变量申明的AST节点 值包含了函数的行数,列数,和函数名
let infoDeclarator = types.variableDeclaration('var', [
    types.variableDeclarator(
      types.identifier('info'),
      types.ObjectExpression([
        types.objectProperty(types.identifier('line'), types.numericLiteral(node.loc.start.line)),
fork icon5
star icon13
watch icon2

+ 23 other calls in file

110
111
112
113
114
115
116
117
118
119
    const node = path.node;

    if (node.kind === 'let' || node.kind === 'const') { 
      // 变量声明的kind, 可以是var let const
      // 然后第二个参数是声明的变量,let a, b, c这里的a, b, c就是node.declarations
      const varNode = type.variableDeclaration('var', node.declarations);
      path.replaceWith(varNode);
    }
  },
};
fork icon3
star icon11
watch icon2

+ 9 other calls in file

171
172
173
174
175
176
177
178
179
180
snapshotCall.insertAfter(
  t.ifStatement(
    t.identifier('fromTheFuture'),
    t.blockStatement(
      [
        t.variableDeclaration('let', [
          t.variableDeclarator(
            t.identifier('snapshot'),
            t.callExpression(t.identifier('restoreHeap'), [t.identifier('startFrom')]),
          ),
fork icon2
star icon5
watch icon3

+ 9 other calls in file

75
76
77
78
79
80
81
82
83
84
    t.returnStatement(t.identifier('model')),
  ]);
}

modelDefinition() {
  return t.variableDeclaration('const', [
    t.variableDeclarator(
      t.identifier('model'),
      t.callExpression(
        t.memberExpression(t.identifier('sequelize'), t.identifier('define')),
fork icon1
star icon5
watch icon2

+ 3 other calls in file

320
321
322
323
324
325
326
327
328
329
  overflow: 'auto',
};
//var ReactDOM = require('react-dom')
//要转化为什么,我们可以先通过AST查看器查看然后编写~~
function requireGenerator(varName, moduleName) {
  return types.variableDeclaration('var', [
    types.variableDeclarator(
      //t.variableDeclarator(id, init)
      //id就是identifier
      //此处的init必须是一个Expression
fork icon3
star icon23
watch icon2

+ 5 other calls in file

154
155
156
157
158
159
160
161
162
163
  t.functionExpression(c.EACH, [
    key,
    c.INDEX,
    c.LENGTH,
  ], t.blockStatement([
    t.variableDeclaration('var', [t.variableDeclarator(c.KEY, key)]),
    t.returnStatement(ConcatStringList(compile(branch.body))),
  ])),
]);
iter.name = branch.name;
fork icon17
star icon0
watch icon5

+ 3 other calls in file

198
199
200
201
202
203
204
205
206
207
  get(node, 'expression.type') === 'NewExpression' &&
  get(node, 'expression.arguments.0.value') === 'container'
) {
  const name = get(node, 'expression.callee.name');
  chartName = name === 'Heatmap' ? 'HeatMap' : `${name}Map`;
  const func = types.variableDeclaration('const', [
    types.variableDeclarator(types.identifier('config'), get(node, 'expression.arguments.1')),
  ]);
  path.replaceWith(func);
}
fork icon329
star icon0
watch icon0

145
146
147
148
149
150
151
152
153
154
  }
  return result;
}, []);

// putting the variables in one line
variables = t.variableDeclaration('var', variables.map(function (v) {
  return v.declarations[0];
}));

funcLines = [variables].concat(funcLines);
fork icon0
star icon5
watch icon1

19
20
21
22
23
24
25
26
27
28
      t.identifier(type)
    ),
    t.blockStatement([t.returnStatement(t.identifier(type))])
  );
});
const typeDefinition = t.variableDeclaration('const', [
  t.variableDeclarator(
    t.identifier(name),
    t.newExpression(t.identifier(gt.GraphQLUnionType), [
      t.objectExpression([
fork icon0
star icon3
watch icon2

+ 5 other calls in file

43
44
45
46
47
48
49
50
51
                    t.memberExpression(t.thisExpression(), getIdentifier(this.state, key))
                );
                memPath.stop();
            }
        }, { state: this.state });
        const varNode = t.variableDeclaration('const', [t.variableDeclarator(t.identifier(this.key), path.node.argument)]);
        this.statements.push(varNode);
    }
};
fork icon65
star icon0
watch icon0

174
175
176
177
178
179
180
181
182
183
    defaultSpecifier = item.local.name;
  }
});
if (defaultSpecifier) {
  astPath.replaceWith(
    t.variableDeclaration('const', [
      t.variableDeclarator(
        t.identifier(defaultSpecifier),
        t.stringLiteral(value)
      )
fork icon5
star icon49
watch icon4

+ 3 other calls in file

0
1
2
3
4
5
6
7
8
9
10
var t = require("babel-types");
var walk = require("babylon-walk");


function props(publics, propsType) {
  if (propsType === "class") {
    return t.variableDeclaration("const", [
      t.variableDeclarator(
        t.objectPattern(
          publics.map((prop) =>
            t.objectProperty(
fork icon143
star icon0
watch icon0

+ 44 other calls in file

1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
        });
        returnStatement.forEach(returnAstPath => {
            const statement = returnAstPath.node;
            const varName = returnAstPath.scope.generateUid();
            const returnValue = statement.argument;
            const pullDownRefreshNode = t.variableDeclaration('const', [t.variableDeclarator(t.identifier(varName), returnValue)]);
            returnAstPath.insertBefore(pullDownRefreshNode);
            statement.argument = astConvert_1.convertSourceStringToAstExpression(`
<PullDownRefresh
  onRefresh={this.onPullDownRefresh && this.onPullDownRefresh.bind(this)}
fork icon0
star icon0
watch icon0

+ 11 other calls in file

37
38
39
40
41
42
43
44
45
46
    var  varlist = []
    for (let i = 0; i < dataset.properties.length; ++i){
        let pp = dataset.properties[i]
        varlist.push(t.variableDeclarator(pp.key,pp.value))
    }
    dataset = t.variableDeclaration("var", varlist)    
    dataset = t.program([dataset])
}

path.stop()
fork icon0
star icon0
watch icon0

72
73
74
75
76
77
78
79
80
81
    `var tmp = ${report.toString()}`
)();
var catchBody = catchStatement.declarations[0].init.body;

// 赋值语句 值包含了函数的行列数和函数名
var infoDeclaration = types.variableDeclaration("var", [
    types.variableDeclarator(
        types.identifier("info"),
        types.ObjectExpression([
            types.objectProperty(
fork icon0
star icon0
watch icon0

115
116
117
118
119
120
121
122
123
124
path.node.arguments[1].params = path.node.arguments[1].params.concat(
  t.Identifier('req')
)

outerVariables.length && path.node.arguments[1].body.body.unshift(
  t.variableDeclaration('const',
    outerVariables.map(ov =>
      t.variableDeclarator(t.identifier(ov), t.identifier(`req.${ov}`))
    )
  )
fork icon0
star icon0
watch icon1

+ 13 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 (4076 examples)