Blame view

node_modules/babel-eslint/lib/parse.js 2.17 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  "use strict";
  
  var babylonToEspree = require("./babylon-to-espree");
  var parse = require("babylon").parse;
  var tt = require("babylon").tokTypes;
  var traverse = require("@babel/traverse").default;
  var codeFrameColumns = require("@babel/code-frame").codeFrameColumns;
  
  module.exports = function(code, options) {
    var opts = {
      codeFrame: options.hasOwnProperty("codeFrame") ? options.codeFrame : true,
      sourceType: options.sourceType,
      allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
      allowReturnOutsideFunction: true,
      allowSuperOutsideMethod: true,
      ranges: true,
      tokens: true,
      plugins: [
        "flow",
        "jsx",
        "estree",
        "asyncFunctions",
        "asyncGenerators",
        "classConstructorCall",
        "classProperties",
        "decorators",
        "doExpressions",
        "exponentiationOperator",
        "exportDefaultFrom",
        "exportNamespaceFrom",
        "functionBind",
        "functionSent",
        "objectRestSpread",
        "trailingFunctionCommas",
        "dynamicImport",
        "numericSeparator",
        "optionalChaining",
        "importMeta",
        "classPrivateProperties",
        "bigInt",
        "optionalCatchBinding",
        "throwExpressions",
        "pipelineOperator",
        "nullishCoalescingOperator",
      ],
    };
  
    var ast;
    try {
      ast = parse(code, opts);
    } catch (err) {
      if (err instanceof SyntaxError) {
        err.lineNumber = err.loc.line;
        err.column = err.loc.column;
  
        if (opts.codeFrame) {
          err.lineNumber = err.loc.line;
          err.column = err.loc.column + 1;
  
          // remove trailing "(LINE:COLUMN)" acorn message and add in esprima syntax error message start
          err.message =
            "Line " +
            err.lineNumber +
            ": " +
            err.message.replace(/ \((\d+):(\d+)\)$/, "") +
            // add codeframe
            "\n\n" +
            codeFrameColumns(
              code,
              {
                start: {
                  line: err.lineNumber,
                  column: err.column,
                },
              },
              { highlightCode: true }
            );
        }
      }
  
      throw err;
    }
  
    babylonToEspree(ast, traverse, tt, code);
  
    return ast;
  };