d077026cfe8a975ebcf98a82f5b1701ac9057721.svn-base
2.25 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = valueToNode;
var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
var _isRegExp = _interopRequireDefault(require("lodash/isRegExp"));
var _isValidIdentifier = _interopRequireDefault(require("../validators/isValidIdentifier"));
var _generated = require("../builders/generated");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function valueToNode(value) {
if (value === undefined) {
return (0, _generated.identifier)("undefined");
}
if (value === true || value === false) {
return (0, _generated.booleanLiteral)(value);
}
if (value === null) {
return (0, _generated.nullLiteral)();
}
if (typeof value === "string") {
return (0, _generated.stringLiteral)(value);
}
if (typeof value === "number") {
let result;
if (Number.isFinite(value)) {
result = (0, _generated.numericLiteral)(Math.abs(value));
} else {
let numerator;
if (Number.isNaN(value)) {
numerator = (0, _generated.numericLiteral)(0);
} else {
numerator = (0, _generated.numericLiteral)(1);
}
result = (0, _generated.binaryExpression)("/", numerator, (0, _generated.numericLiteral)(0));
}
if (value < 0 || Object.is(value, -0)) {
result = (0, _generated.unaryExpression)("-", result);
}
return result;
}
if ((0, _isRegExp.default)(value)) {
const pattern = value.source;
const flags = value.toString().match(/\/([a-z]+|)$/)[1];
return (0, _generated.regExpLiteral)(pattern, flags);
}
if (Array.isArray(value)) {
return (0, _generated.arrayExpression)(value.map(valueToNode));
}
if ((0, _isPlainObject.default)(value)) {
const props = [];
for (const key of Object.keys(value)) {
let nodeKey;
if ((0, _isValidIdentifier.default)(key)) {
nodeKey = (0, _generated.identifier)(key);
} else {
nodeKey = (0, _generated.stringLiteral)(key);
}
props.push((0, _generated.objectProperty)(nodeKey, valueToNode(value[key])));
}
return (0, _generated.objectExpression)(props);
}
throw new Error("don't know how to turn this value into a node");
}