How to use the file function from @babel/types

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

@babel/types.file is an object that represents a Babel AST of an entire file.

17
18
19
20
21
22
23
24
25
    const file = t.file(t.program([nodeOrPath]));
    traverse(file, visitor);
  } else {
    // Expression should be wrapped in expression statement.
    // Only can traverse file path.
    const file = t.file(t.program([t.expressionStatement(nodeOrPath)]));
    traverse(file, visitor);
  }
};
fork icon32
star icon66
watch icon12

+ 15 other calls in file

289
290
291
292
293
294
295
296
297
298
299
300
}


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;
    resultProgram.body = resultProgram.body.concat(ast.body);
fork icon46
star icon35
watch icon0

How does @babel/types.file work?

@babel/types.file is a function in the Babel Types package that creates an AST (Abstract Syntax Tree) node representing a parsed JavaScript file. It contains information such as the file name, program body, comments, and any attached source maps.

101
102
103
104
105
106
107
108
109
110

  files['index.js'].push(...m.source);
  files['index.js'].push(template.ast(`export { ${m.name} };`));
}

files['index.js'] = t.file(t.program(files['index.js']));
files['index.js'] = generate(files['index.js']).code;

const defaultVirtualOptions = {
  // Each of our Icon modules use the "./Icon.js" path to import this base
fork icon0
star icon5
watch icon5

+ 11 other calls in file

142
143
144
145
146
147
148
149
150
151
    }
    return [required, [requiredObject]].map((req) => createType(req));
  })
  .reduce((acc, curr) => [...acc, ...curr], []);

const prog = t.file(t.program([stringableType, validValueType, ...types]));

const generated = generate(prog, {});

const code = `/**
fork icon1
star icon3
watch icon2

+ 30 other calls in file

Ai Example

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

const program = t.program([
  // your code here
]);

const ast = t.file(
  // the ast node representing the module's code
  program,
  // sourceType of the module
  "module",
  // whether the AST represents an ECMAScript module
  true
);

console.log(ast);

This will create an AST node representing a JavaScript module, with the provided program as its body.

57
58
59
60
61
62
63
64
65
66
67
68
    ? 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

134
135
136
137
138
139
140
141
142
143
  });
}

function evaluateExpression(node) {
  // Wrap the node in a standalone program so we can traverse it
  node = types.file(types.program([types.expressionStatement(node)]));

  // Find the first expression and evaluate it.
  let res = null;
  traverse(node, {
fork icon0
star icon2
watch icon2

+ 3 other calls in file

64
65
66
67
68
69
70
71
72
73
  source,
  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

525
526
527
528
529
530
531
532
533
534
    return {${exposed.join(', ')}};
  `);
}

try {
  let ast = t.file(t.program(this.statements));
  let {code: output} = concat(this, ast);

  if (!this.options.minify) {
    output = '\n' + output + '\n';
fork icon1
star icon1
watch icon2

226
227
228
229
230
231
232
233
234
235
  });
}

const fn = () => {
  const file = {
    ast: t.file(helper.ast())
  };

  if (fileClass) {
    return new fileClass({
fork icon0
star icon1
watch icon1

+ 48 other calls in file

31
32
33
34
35
36
37
38
39
40
importAllName,
dependencyMapName);

const factory = functionFromProgram(fileAst.program, params);
const def = t.callExpression(t.identifier('__d'), [factory]);
const ast = t.file(t.program([t.expressionStatement(def)]));

const requireName = renameRequires(ast);

return { ast, requireName };
fork icon1
star icon0
watch icon1

+ 3 other calls in file

123
124
125
126
127
128
129
130
131
132
133
134
  });
}


function evaluateExpression(node) {
  // Wrap the node in a standalone program so we can traverse it
  node = types.file(types.program([types.expressionStatement(node)])); // Find the first expression and evaluate it.


  let res = null;
  traverse(node, {
    Expression(path) {
fork icon0
star icon0
watch icon1

57
58
59
60
61
62
63
64
65
66
                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

207
208
209
210
211
212
213
214
215
216
  statements.push(...source);

  // Export statement
  statements.push(template.ast(`export default ${moduleName};`));

  const file = t.file(t.program(statements));
  const { code } = generate(file);

  return code;
}
fork icon0
star icon0
watch icon1

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