Blame view

node_modules/babel-eslint/lib/babylon-to-espree/toAST.js 2.62 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  "use strict";
  
  var t = require("@babel/types");
  var convertComments = require("./convertComments");
  
  module.exports = function(ast, traverse, code) {
    var state = { source: code };
  
    // Monkey patch visitor keys in order to be able to traverse the estree nodes
    t.VISITOR_KEYS.Property = t.VISITOR_KEYS.ObjectProperty;
    t.VISITOR_KEYS.MethodDefinition = [
      "key",
      "value",
      "decorators",
      "returnType",
      "typeParameters",
    ];
  
    traverse(ast, astTransformVisitor, null, state);
  
    delete t.VISITOR_KEYS.Property;
    delete t.VISITOR_KEYS.MethodDefinition;
  };
  
  var astTransformVisitor = {
    noScope: true,
    enter(path) {
      var node = path.node;
  
      // private var to track original node type
      node._babelType = node.type;
  
      if (node.innerComments) {
        node.trailingComments = node.innerComments;
        delete node.innerComments;
      }
  
      if (node.trailingComments) {
        convertComments(node.trailingComments);
      }
  
      if (node.leadingComments) {
        convertComments(node.leadingComments);
      }
    },
    exit(path) {
      var node = path.node;
  
      if (path.isJSXText()) {
        node.type = "Literal";
      }
  
      if (
        path.isRestElement() &&
        path.parent &&
        path.parent.type === "ObjectPattern"
      ) {
        node.type = "ExperimentalRestProperty";
      }
  
      if (
        path.isSpreadElement() &&
        path.parent &&
        path.parent.type === "ObjectExpression"
      ) {
        node.type = "ExperimentalSpreadProperty";
      }
  
      if (path.isTypeParameter()) {
        node.type = "Identifier";
        node.typeAnnotation = node.bound;
        delete node.bound;
      }
  
      // flow: prevent "no-undef"
      // for "Component" in: "let x: React.Component"
      if (path.isQualifiedTypeIdentifier()) {
        delete node.id;
      }
      // for "b" in: "var a: { b: Foo }"
      if (path.isObjectTypeProperty()) {
        delete node.key;
      }
      // for "indexer" in: "var a: {[indexer: string]: number}"
      if (path.isObjectTypeIndexer()) {
        delete node.id;
      }
      // for "param" in: "var a: { func(param: Foo): Bar };"
      if (path.isFunctionTypeParam()) {
        delete node.name;
      }
  
      // modules
  
      if (path.isImportDeclaration()) {
        delete node.isType;
      }
  
      // template string range fixes
      if (path.isTemplateLiteral()) {
        for (var j = 0; j < node.quasis.length; j++) {
          var q = node.quasis[j];
          q.range[0] -= 1;
          if (q.tail) {
            q.range[1] += 1;
          } else {
            q.range[1] += 2;
          }
          q.loc.start.column -= 1;
          if (q.tail) {
            q.loc.end.column += 1;
          } else {
            q.loc.end.column += 2;
          }
        }
      }
    },
  };