UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.convertExpressionToJSXIdentifier = convertExpressionToJSXIdentifier;
7exports.convertJSXExpressionToIdentifier = convertJSXExpressionToIdentifier;
8exports.convertKeyValueToJSXAttribute = convertKeyValueToJSXAttribute;
9
10var t = _interopRequireWildcard(require("@babel/types"));
11
12var _invariant = _interopRequireDefault(require("../invariant.js"));
13
14var _utils = require("./utils.js");
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
19
20/**
21 * Copyright (c) 2017-present, Facebook, Inc.
22 * All rights reserved.
23 *
24 * This source code is licensed under the BSD-style license found in the
25 * LICENSE file in the root directory of this source tree. An additional grant
26 * of patent rights can be found in the PATENTS file in the same directory.
27 */
28function convertExpressionToJSXIdentifier(expr, isRoot) {
29 switch (expr.type) {
30 case "ThisExpression":
31 (0, _invariant.default)(isRoot === false, `invalid conversion of root expression to JSXIdentifier for ThisExpression`);
32 return t.jSXIdentifier("this");
33
34 case "Identifier":
35 let name = expr.name;
36 (0, _invariant.default)( // ensure the 1st character of the string is uppercase
37 // for a component unless it is not the root
38 isRoot === false || (0, _utils.isReactComponent)(name), "invalid JSXIdentifer from Identifier, Identifier name must be uppercase");
39 return t.jSXIdentifier(name);
40
41 case "StringLiteral":
42 let value = expr.value;
43 (0, _invariant.default)( // ensure the 1st character of the string is lowercase
44 // otherwise it will appear as a component
45 value.length > 0 && value[0] === value[0].toLowerCase(), "invalid JSXIdentifer from string, strings must be lowercase");
46 return t.jSXIdentifier(value);
47
48 case "MemberExpression":
49 (0, _invariant.default)(expr.computed === false, "Cannot inline computed expressions in JSX type.");
50 return t.jSXMemberExpression(convertExpressionToJSXIdentifier(expr.object, false), convertExpressionToJSXIdentifier(expr.property, false));
51
52 default:
53 (0, _invariant.default)(false, "Invalid JSX type");
54 }
55}
56
57function convertJSXExpressionToIdentifier(expr) {
58 switch (expr.type) {
59 case "JSXIdentifier":
60 return t.identifier(expr.name);
61
62 case "JSXMemberExpression":
63 return t.memberExpression(convertJSXExpressionToIdentifier(expr.object), convertJSXExpressionToIdentifier(expr.property));
64
65 default:
66 (0, _invariant.default)(false, "Invalid JSX type");
67 }
68}
69
70function convertKeyValueToJSXAttribute(key, expr) {
71 let wrapInContainer = true;
72
73 if (expr && t.isStringLiteral(expr) && typeof expr.value === "string") {
74 let value = expr.value;
75 wrapInContainer = value.includes('"') || value.includes("'");
76 }
77
78 return t.jSXAttribute(t.jSXIdentifier(key), wrapInContainer ? t.jSXExpressionContainer(expr) : expr);
79}
80//# sourceMappingURL=jsx.js.map
\No newline at end of file