How to use the parseModule function from esprima

Find comprehensive JavaScript esprima.parseModule code examples handpicked from public code repositorys.

58
59
60
61
62
63
64
65
66
67
68
 */
function getPackageExportNames(packagePath) {
  const packageMeta = getPackageMeta(packagePath);
  const packageModulePath = path.join(packagePath, packageMeta.module);
  const moduleFileSource = fs.readFileSync(packageModulePath, "utf8");
  const { body } = esprima.parseModule(moduleFileSource);


  return body.reduce((result, statement) => {
    if (statement.type === "ExportDefaultDeclaration") {
      return result.concat(["default"]);
fork icon115
star icon160
watch icon0

+ 3 other calls in file

120
121
122
123
124
125
126
127
128
129
130


function parseFile(fileName) {
  const file = trimHashbang(fs.readFileSync(fileName, "utf-8"));
  let ast;
  try {
    ast = esprima.parseModule(file, {
      range: true,
      tokens: true,
      comment: true,
      loc: true
fork icon0
star icon1
watch icon0

3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
    }
    nodeIdCounter++;
}
// parse
try {
    var root = esprima.parseModule(sourceCode, {
        loc: true,
        range: true,
        tolerant: true,
        attachComment: true
fork icon0
star icon0
watch icon0

+ 6 other calls in file

13
14
15
16
17
18
19
20
21
22
23
let objectCreateCall;
let ratingClassDecl;
let fieldDeclInRatingCls = false;


try {
    esprima.parseModule(source, {}, function (node) {
        if (node.type == 'VariableDeclarator' &&
            node.id.name == 'player' &&
            node.init.type == 'ObjectExpression') {
            playerDef = node;
fork icon0
star icon0
watch icon0

12
13
14
15
16
17
18
19
20
21
it("should use `const` for `transformLineEnding`, `LineEndings`, and `LineEndingReplacements` @refactor-const", () => {
  // const transformLineEnding = ...

  let foundTransformLineEndingConst = false;

  esprima.parseModule(source, {}, node => {
    if (
      node.type === "VariableDeclaration" &&
      node.declarations.some(
        declaration => declaration.id.name === "transformLineEnding"
fork icon0
star icon0
watch icon0

+ 2 other calls in file

10
11
12
13
14
15
16
17
18
19
it("should replace uses of `var` @refactor-let", () => {
  // let buffer = ...

  let foundLetBuffer = false;

  esprima.parseModule(cliSource, {}, node => {
    if (
      node.type === "VariableDeclaration" &&
      node.declarations.some(
        declaration => declaration.id.name === "buffer"
fork icon0
star icon0
watch icon0

+ 3 other calls in file

0
1
2
3
4
5
6
7
8
9
10
let esprima = require('esprima'); // 把源代码转成抽象语法树
let estraverse = require('estraverse');
let escodegen = require('escodegen');
// 原代码就是ascii字符串
let sourceCode = `function ast(){}`;
let ast = esprima.parseModule(sourceCode); // 生成ast
console.log(ast);


/**
 * 遍历语法树,遍历的方式采用的是深度优先的方式
fork icon0
star icon0
watch icon0