How to use the program function from @babel/types

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

188
189
190
191
192
193
194
195
196
197
  ...[].concat(prefix),
  ...this.inputTypes.astNodes,
  ...this.outputTypes.astNodes,
  ...[].concat(postfix),
];
const program = t.program(body);
program.leadingComments = [
  {
    type: 'CommentLine',
    value: ' Generated by SwagQL. Do not edit by hand.',
fork icon3
star icon46
watch icon11

288
289
290
291
292
293
294
295
296
297
298
299
  return Array.from(flags.values());
}


function concatPrograms(inputs) {
  // Concatentate programs.
  const resultProgram = babelTypes.program([]);
  const result = babelTypes.file(resultProgram, [], null);


  for (const input of inputs) {
    const ast = input.ast.program;
fork icon46
star icon35
watch icon0

31
32
33
34
35
36
37
38
39
40
      ),
    ]),
    t.exportDefaultDeclaration(t.identifier(componentName)),
  ]

  return t.program(body, [], 'module')
}

const generateJSXElement = (componentName, children = null) =>
  t.JSXElement(
fork icon0
star icon3
watch icon1

+ 3 other calls in file

56
57
58
59
60
61
62
63
64
65
66
67
  const exportFn = options.esModule
    ? generateDefaultExport
    : generateModuleExport;
  statements.push(exportFn(PIPELINE_VAR));


  const program = t.program(statements);
  const ast = t.file(program);


  return ast;
}
fork icon1
star icon2
watch icon1

+ 57 other calls in file

65
66
67
68
69
70
71
72
73
  path,
  to: pascalCase(to),
}));

const ast = t.file(
  t.program([...statement(items), variable(items), logger(items)]),
);

const { code } = generator.default(ast, {}, '');
fork icon0
star icon2
watch icon0

+ 4 other calls in file

123
124
125
126
127
128
129
130
131
132
if (asset.cacheData.isES6Module) {
  path.unshiftContainer('body', [ESMODULE_TEMPLATE()]);
}

path.replaceWith(
  t.program([
    WRAPPER_TEMPLATE({
      NAME: getExportsIdentifier(asset),
      BODY: path.node.body,
    }),
fork icon0
star icon2
watch icon1

+ 7 other calls in file

44
45
46
47
48
49
50
51
52
53
  } else {
    inner.push(node);
  }
});

const program = t.program([
  ...nodes,
  t.expressionStatement(
    t.callExpression(
      t.memberExpression(
fork icon0
star icon1
watch icon1

+ 58 other calls in file

254
255
256
257
258
259
260
261
262
getBlockStatement(children = []) {
  return t.blockStatement(children);
}

visitFile_input(ctx) {
  return t.program(
    flatten(ctx.children.map(child => this.visitChildren(child)))
  );
}
fork icon0
star icon1
watch icon0

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }


function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }


const root = (h, node) => t.program((0, _all.default)(h, node));


exports.root = root;


const comment = (h, node, parent) => {
fork icon0
star icon1
watch icon0

42
43
44
45
46
47
48
49
50
51
if (this.hasBuilt) return;

this.hasBuilt = true;

let fullDestination = `${this.outputPath}/${this.destination}`;
let declaration = t.program([
  Object.assign(
    t.variableDeclaration('const', [
      t.variableDeclarator(
        Object.assign(t.identifier('config'), {
fork icon0
star icon1
watch icon0

+ 11 other calls in file

26
27
28
29
30
31
32
33
34
35
  vueParseObj.descriptor.template.content
);
jsAst = initState(jsAst, store);
templateAst = initTemplate(templateAst, null, new Set());
const { code: jsCode } = generator(
  t.program(
    [
      ...jsAst.program.body.filter((item) => t.isImportDeclaration(item)),
      t.functionDeclaration(
        t.identifier("VueTest"),
fork icon0
star icon0
watch icon1

+ 213 other calls in file

305
306
307
308
309
310
311
312
313
314
    operationTypes.push(t.objectTypeProperty(t.identifier('rawResponse'), t.genericTypeAnnotation(t.identifier("".concat(node.name, "RawResponse")))));
    babelNodes.push(rawResponseType);
  }

  babelNodes.push(exportType(node.name, exactObjectTypeAnnotation(operationTypes)));
  return t.program(babelNodes);
},
Fragment: function Fragment(node) {
  var selections = flattenArray(
  /* $FlowFixMe: selections have already been transformed */
fork icon0
star icon0
watch icon2

440
441
442
443
444
445
446
447
448
449
const type = isPluralFragment
  ? readOnlyArrayOfType(baseType)
  : baseType;
state.runtimeImports.add('FragmentReference');

return t.program([
  ...getFragmentImports(state),
  ...getEnumDefinitions(schema, state),
  importTypes(Array.from(state.runtimeImports).sort(), 'relay-runtime'),
  ...fragmentTypes,
fork icon0
star icon0
watch icon1

270
271
272
273
274
275
276
277
278
// set default name
const pathFragments = outputFile.split('/');
if (!json.name) {
  json.name = pathFragments[pathFragments.length - 1].replace('.js', '');
}
const ast = t.program([createClass(json), createExport(json)]);
const output = generate(ast);

const commentBefore = '/**\n\n\tThis code is generated.\n\tFor more information see generation/README.md.\n*/\n\n';
fork icon0
star icon0
watch icon0

+ 11 other calls in file

184
185
186
187
188
189
190
191
192
193
194
    SKIPPED: skippedArray,
    SOFTSKIPPED: softSkippedArray,
    SLOPPY: sloppyArray,
  });


  const resultProgram = babelTypes.program(statements);
  const code = babelGenerator(resultProgram, { comments: true }).code;
  const prettyCode = prettier.format(code, { parser: "babel" });
  fs.writeFileSync('generated/exceptions.js', prettyCode);
}
fork icon0
star icon0
watch icon0

+ 2 other calls in file

56
57
58
59
60
61
62
63
64
65
        } else {
            throw "No declaration for node";
        }
    }
    if (imports.length) {
        let p = types.program(imports);
        let f = types.file(p);
        ex_path.replaceWith(f);
    }
}
fork icon0
star icon0
watch icon1

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)