How to use the parseSync function from @babel/core

Find comprehensive JavaScript @babel/core.parseSync code examples handpicked from public code repositorys.

@babel/core.parseSync is a function that takes a string of code as input and returns the corresponding AST (Abstract Syntax Tree) representation of the code.

157
158
159
160
161
162
163
164
165
166
const transformImportProComponents = (sourceCode, options = {}) => {
  // 通过 recast 处理 ast,保证输出文件的代码风格和输入前保持一致
  const ast = parse(sourceCode, {
    parser: {
      parse: (source) => {
        const ast = parseSync(source, {
          parserOpts: {
            sourceType: 'module',
            plugins: ['jsx', 'typescript'],
            // recast uses this
fork icon34
star icon253
watch icon13

+ 11 other calls in file

211
212
213
214
215
216
217
218
219
220

```js
const babel = require('@babel/core')
const traverse = require('@babel/traverse').default

const ast = babel.parseSync(code)

let depth = 0
traverse(ast, {
  enter(path) {
fork icon4
star icon11
watch icon2

+ 5 other calls in file

How does @babel/core.parseSync work?

@babel/core.parseSync is a method in the Babel core package that synchronously parses a given code string into an abstract syntax tree (AST), which represents the structure of the code in a hierarchical form. The method accepts options that can customize the parser behavior, such as the ECMAScript version to parse or the source type (e.g., script or module).

159
160
161
162
163
164
165
166
167
168

```js
const babel = require('@babel/core');
const traverse = require('@babel/traverse').default;

const ast = babel.parseSync(code);

let depth = 0;
traverse(ast, {
  enter(path) {
fork icon15
star icon2
watch icon2

+ 4 other calls in file

40
41
42
43
44
45
46
47
48
49
// if (isRoot && importDepend) {
//   return importDepend
// }
const context = fs.readFileSync(dirPath,'utf-8')
const tranform = babel.transform(context)
const ast = babel.parseSync(tranform.code)
const nameTemplate = babel.template(`window.%%name%% = %%name%%`)
const reactDOM = babel.template(`ReactDOM.render(React.createElement(%%name%%,{},{}),document.getElementById('root'))`)
traverse(ast,{
  ImportDeclaration(path) {
fork icon3
star icon0
watch icon1

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
const babel = require("@babel/core");

const code = "const answer = 42;";
const options = { ast: true };

const ast = babel.parseSync(code, options);

console.log(ast);

In this example, we first import the @babel/core module. Then we define a string code containing some JavaScript code. We also define an options object with the ast property set to true to request the resulting AST to be returned. Finally, we call babel.parseSync with the code and options, which returns the AST object that we log to the console.

740
741
742
743
744
745
746
747
748
749
750
const tokenTypesMapping = new Map();
const tokenTypeSourcePath = "./packages/babel-parser/src/tokenizer/types.ts";


function getTokenTypesMapping() {
  if (tokenTypesMapping.size === 0) {
    const tokenTypesAst = parseSync(
      fs.readFileSync(tokenTypeSourcePath, {
        encoding: "utf-8",
      }),
      {
fork icon0
star icon3
watch icon0

211
212
213
214
215
216
217
218
219
220
};
const sourceAst =
  isTypeScriptSource(filename) ||
  isTSXSource(filename) ||
  !options.hermesParser
    ? parseSync(src, babelConfig)
    : require('hermes-parser').parse(src, {
        babel: true,
        sourceType: babelConfig.sourceType,
      });
fork icon613
star icon0
watch icon99