File size: 6,432 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Object.defineProperty(exports, "__esModule", {
  value: true,
});

var _spacer = require("./spacer");

var _spacer2 = _interopRequireDefault(_spacer);

var _formatTreeNode = require("./formatTreeNode");

var _formatTreeNode2 = _interopRequireDefault(_formatTreeNode);

var _formatProp2 = require("./formatProp");

var _formatProp3 = _interopRequireDefault(_formatProp2);

var _mergeSiblingPlainStringChildrenReducer = require("./mergeSiblingPlainStringChildrenReducer");

var _mergeSiblingPlainStringChildrenReducer2 = _interopRequireDefault(
  _mergeSiblingPlainStringChildrenReducer
);

var _propNameSorter = require("./propNameSorter");

var _propNameSorter2 = _interopRequireDefault(_propNameSorter);

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

var compensateMultilineStringElementIndentation = function compensateMultilineStringElementIndentation(
  element,
  formattedElement,
  inline,
  lvl,
  options
) {
  var tabStop = options.tabStop;

  if (element.type === "string") {
    return formattedElement
      .split("\n")
      .map(function(line, offset) {
        if (offset === 0) {
          return line;
        }

        return "" + (0, _spacer2.default)(lvl, tabStop) + line;
      })
      .join("\n");
  }

  return formattedElement;
};

var formatOneChildren = function formatOneChildren(inline, lvl, options) {
  return function(element) {
    return compensateMultilineStringElementIndentation(
      element,
      (0, _formatTreeNode2.default)(element, inline, lvl, options),
      inline,
      lvl,
      options
    );
  };
};

var onlyPropsWithOriginalValue = function onlyPropsWithOriginalValue(
  defaultProps,
  props
) {
  return function(propName) {
    var haveDefaultValue = Object.keys(defaultProps).includes(propName);
    return (
      !haveDefaultValue ||
      (haveDefaultValue && defaultProps[propName] !== props[propName])
    );
  };
};

var isInlineAttributeTooLong = function isInlineAttributeTooLong(
  attributes,
  inlineAttributeString,
  lvl,
  tabStop,
  maxInlineAttributesLineLength
) {
  if (!maxInlineAttributesLineLength) {
    return attributes.length > 1;
  }

  return (
    (0, _spacer2.default)(lvl, tabStop).length + inlineAttributeString.length >
    maxInlineAttributesLineLength
  );
};

var shouldRenderMultilineAttr = function shouldRenderMultilineAttr(
  attributes,
  inlineAttributeString,
  containsMultilineAttr,
  inline,
  lvl,
  tabStop,
  maxInlineAttributesLineLength
) {
  return (
    (isInlineAttributeTooLong(
      attributes,
      inlineAttributeString,
      lvl,
      tabStop,
      maxInlineAttributesLineLength
    ) ||
      containsMultilineAttr) &&
    !inline
  );
};

exports.default = function(node, inline, lvl, options) {
  var type = node.type,
    _node$displayName = node.displayName,
    displayName = _node$displayName === undefined ? "" : _node$displayName,
    childrens = node.childrens,
    _node$props = node.props,
    props = _node$props === undefined ? {} : _node$props,
    _node$defaultProps = node.defaultProps,
    defaultProps = _node$defaultProps === undefined ? {} : _node$defaultProps;

  if (type !== "ReactElement") {
    throw new Error(
      'The "formatReactElementNode" function could only format node of type "ReactElement". Given:  ' +
        type
    );
  }

  var filterProps = options.filterProps,
    maxInlineAttributesLineLength = options.maxInlineAttributesLineLength,
    showDefaultProps = options.showDefaultProps,
    sortProps = options.sortProps,
    tabStop = options.tabStop;

  var out = "<" + displayName;

  var outInlineAttr = out;
  var outMultilineAttr = out;
  var containsMultilineAttr = false;

  var visibleAttributeNames = [];

  Object.keys(props)
    .filter(function(propName) {
      return filterProps.indexOf(propName) === -1;
    })
    .filter(onlyPropsWithOriginalValue(defaultProps, props))
    .forEach(function(propName) {
      return visibleAttributeNames.push(propName);
    });

  Object.keys(defaultProps)
    .filter(function(defaultPropName) {
      return filterProps.indexOf(defaultPropName) === -1;
    })
    .filter(function() {
      return showDefaultProps;
    })
    .filter(function(defaultPropName) {
      return !visibleAttributeNames.includes(defaultPropName);
    })
    .forEach(function(defaultPropName) {
      return visibleAttributeNames.push(defaultPropName);
    });

  var attributes = visibleAttributeNames.sort(
    (0, _propNameSorter2.default)(sortProps)
  );

  attributes.forEach(function(attributeName) {
    var _formatProp = (0, _formatProp3.default)(
        attributeName,
        Object.keys(props).includes(attributeName),
        props[attributeName],
        Object.keys(defaultProps).includes(attributeName),
        defaultProps[attributeName],
        inline,
        lvl,
        options
      ),
      attributeFormattedInline = _formatProp.attributeFormattedInline,
      attributeFormattedMultiline = _formatProp.attributeFormattedMultiline,
      isMultilineAttribute = _formatProp.isMultilineAttribute;

    if (isMultilineAttribute) {
      containsMultilineAttr = true;
    }

    outInlineAttr += attributeFormattedInline;
    outMultilineAttr += attributeFormattedMultiline;
  });

  outMultilineAttr += "\n" + (0, _spacer2.default)(lvl, tabStop);

  if (
    shouldRenderMultilineAttr(
      attributes,
      outInlineAttr,
      containsMultilineAttr,
      inline,
      lvl,
      tabStop,
      maxInlineAttributesLineLength
    )
  ) {
    out = outMultilineAttr;
  } else {
    out = outInlineAttr;
  }

  if (childrens && childrens.length > 0) {
    var newLvl = lvl + 1;

    out += ">";

    if (!inline) {
      out += "\n";
      out += (0, _spacer2.default)(newLvl, tabStop);
    }

    out += childrens
      .reduce(_mergeSiblingPlainStringChildrenReducer2.default, [])
      .map(formatOneChildren(inline, newLvl, options))
      .join(!inline ? "\n" + (0, _spacer2.default)(newLvl, tabStop) : "");

    if (!inline) {
      out += "\n";
      out += (0, _spacer2.default)(newLvl - 1, tabStop);
    }
    out += "</" + displayName + ">";
  } else {
    if (
      !isInlineAttributeTooLong(
        attributes,
        outInlineAttr,
        lvl,
        tabStop,
        maxInlineAttributesLineLength
      )
    ) {
      out += " ";
    }

    out += "/>";
  }

  return out;
};
//# sourceMappingURL=formatReactElementNode.js.map