Projekt

Obecné

Profil

Stáhnout (4.25 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
exports.__esModule = true;
4

    
5
exports.default = function (opts) {
6
  var visitor = {};
7

    
8
  visitor.JSXNamespacedName = function (path) {
9
    throw path.buildCodeFrameError("Namespace tags are not supported. ReactJSX is not XML.");
10
  };
11

    
12
  visitor.JSXElement = {
13
    exit: function exit(path, file) {
14
      var callExpr = buildElementCall(path.get("openingElement"), file);
15

    
16
      callExpr.arguments = callExpr.arguments.concat(path.node.children);
17

    
18
      if (callExpr.arguments.length >= 3) {
19
        callExpr._prettyCall = true;
20
      }
21

    
22
      path.replaceWith(t.inherits(callExpr, path.node));
23
    }
24
  };
25

    
26
  return visitor;
27

    
28
  function convertJSXIdentifier(node, parent) {
29
    if (t.isJSXIdentifier(node)) {
30
      if (node.name === "this" && t.isReferenced(node, parent)) {
31
        return t.thisExpression();
32
      } else if (_esutils2.default.keyword.isIdentifierNameES6(node.name)) {
33
        node.type = "Identifier";
34
      } else {
35
        return t.stringLiteral(node.name);
36
      }
37
    } else if (t.isJSXMemberExpression(node)) {
38
      return t.memberExpression(convertJSXIdentifier(node.object, node), convertJSXIdentifier(node.property, node));
39
    }
40

    
41
    return node;
42
  }
43

    
44
  function convertAttributeValue(node) {
45
    if (t.isJSXExpressionContainer(node)) {
46
      return node.expression;
47
    } else {
48
      return node;
49
    }
50
  }
51

    
52
  function convertAttribute(node) {
53
    var value = convertAttributeValue(node.value || t.booleanLiteral(true));
54

    
55
    if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
56
      value.value = value.value.replace(/\n\s+/g, " ");
57
    }
58

    
59
    if (t.isValidIdentifier(node.name.name)) {
60
      node.name.type = "Identifier";
61
    } else {
62
      node.name = t.stringLiteral(node.name.name);
63
    }
64

    
65
    return t.inherits(t.objectProperty(node.name, value), node);
66
  }
67

    
68
  function buildElementCall(path, file) {
69
    path.parent.children = t.react.buildChildren(path.parent);
70

    
71
    var tagExpr = convertJSXIdentifier(path.node.name, path.node);
72
    var args = [];
73

    
74
    var tagName = void 0;
75
    if (t.isIdentifier(tagExpr)) {
76
      tagName = tagExpr.name;
77
    } else if (t.isLiteral(tagExpr)) {
78
      tagName = tagExpr.value;
79
    }
80

    
81
    var state = {
82
      tagExpr: tagExpr,
83
      tagName: tagName,
84
      args: args
85
    };
86

    
87
    if (opts.pre) {
88
      opts.pre(state, file);
89
    }
90

    
91
    var attribs = path.node.attributes;
92
    if (attribs.length) {
93
      attribs = buildOpeningElementAttributes(attribs, file);
94
    } else {
95
      attribs = t.nullLiteral();
96
    }
97

    
98
    args.push(attribs);
99

    
100
    if (opts.post) {
101
      opts.post(state, file);
102
    }
103

    
104
    return state.call || t.callExpression(state.callee, args);
105
  }
106

    
107
  function buildOpeningElementAttributes(attribs, file) {
108
    var _props = [];
109
    var objs = [];
110

    
111
    var useBuiltIns = file.opts.useBuiltIns || false;
112
    if (typeof useBuiltIns !== "boolean") {
113
      throw new Error("transform-react-jsx currently only accepts a boolean option for " + "useBuiltIns (defaults to false)");
114
    }
115

    
116
    function pushProps() {
117
      if (!_props.length) return;
118

    
119
      objs.push(t.objectExpression(_props));
120
      _props = [];
121
    }
122

    
123
    while (attribs.length) {
124
      var prop = attribs.shift();
125
      if (t.isJSXSpreadAttribute(prop)) {
126
        pushProps();
127
        objs.push(prop.argument);
128
      } else {
129
        _props.push(convertAttribute(prop));
130
      }
131
    }
132

    
133
    pushProps();
134

    
135
    if (objs.length === 1) {
136
      attribs = objs[0];
137
    } else {
138
      if (!t.isObjectExpression(objs[0])) {
139
        objs.unshift(t.objectExpression([]));
140
      }
141

    
142
      var helper = useBuiltIns ? t.memberExpression(t.identifier("Object"), t.identifier("assign")) : file.addHelper("extends");
143

    
144
      attribs = t.callExpression(helper, objs);
145
    }
146

    
147
    return attribs;
148
  }
149
};
150

    
151
var _esutils = require("esutils");
152

    
153
var _esutils2 = _interopRequireDefault(_esutils);
154

    
155
var _babelTypes = require("babel-types");
156

    
157
var t = _interopRequireWildcard(_babelTypes);
158

    
159
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
160

    
161
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
162

    
163
module.exports = exports["default"];
    (1-1/1)