1 line
2.5 KiB
JSON
1 line
2.5 KiB
JSON
{"ast":null,"code":"\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.default = valueToNode;\n\nvar _isPlainObject = _interopRequireDefault(require(\"lodash/isPlainObject\"));\n\nvar _isRegExp = _interopRequireDefault(require(\"lodash/isRegExp\"));\n\nvar _isValidIdentifier = _interopRequireDefault(require(\"../validators/isValidIdentifier\"));\n\nvar _generated = require(\"../builders/generated\");\n\nfunction _interopRequireDefault(obj) {\n return obj && obj.__esModule ? obj : {\n default: obj\n };\n}\n\nfunction valueToNode(value) {\n if (value === undefined) {\n return (0, _generated.identifier)(\"undefined\");\n }\n\n if (value === true || value === false) {\n return (0, _generated.booleanLiteral)(value);\n }\n\n if (value === null) {\n return (0, _generated.nullLiteral)();\n }\n\n if (typeof value === \"string\") {\n return (0, _generated.stringLiteral)(value);\n }\n\n if (typeof value === \"number\") {\n var result;\n\n if (Number.isFinite(value)) {\n result = (0, _generated.numericLiteral)(Math.abs(value));\n } else {\n var numerator;\n\n if (Number.isNaN(value)) {\n numerator = (0, _generated.numericLiteral)(0);\n } else {\n numerator = (0, _generated.numericLiteral)(1);\n }\n\n result = (0, _generated.binaryExpression)(\"/\", numerator, (0, _generated.numericLiteral)(0));\n }\n\n if (value < 0 || Object.is(value, -0)) {\n result = (0, _generated.unaryExpression)(\"-\", result);\n }\n\n return result;\n }\n\n if ((0, _isRegExp.default)(value)) {\n var pattern = value.source;\n var flags = value.toString().match(/\\/([a-z]+|)$/)[1];\n return (0, _generated.regExpLiteral)(pattern, flags);\n }\n\n if (Array.isArray(value)) {\n return (0, _generated.arrayExpression)(value.map(valueToNode));\n }\n\n if ((0, _isPlainObject.default)(value)) {\n var props = [];\n\n for (var _i = 0, _Object$keys = Object.keys(value); _i < _Object$keys.length; _i++) {\n var key = _Object$keys[_i];\n var nodeKey = void 0;\n\n if ((0, _isValidIdentifier.default)(key)) {\n nodeKey = (0, _generated.identifier)(key);\n } else {\n nodeKey = (0, _generated.stringLiteral)(key);\n }\n\n props.push((0, _generated.objectProperty)(nodeKey, valueToNode(value[key])));\n }\n\n return (0, _generated.objectExpression)(props);\n }\n\n throw new Error(\"don't know how to turn this value into a node\");\n}","map":null,"metadata":{},"sourceType":"script"} |