bfbec32cacdf1c074a01425a1231df4aab8d1e527c19817f2a8a885676384ce6bc84df7324338ba65e61a323c48aaae46a76487164d88a84d8800f084a5924 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _isValidIdentifier = require("../validators/isValidIdentifier.js");
  7. var _index = require("../builders/generated/index.js");
  8. var _default = exports.default = valueToNode;
  9. const objectToString = Function.call.bind(Object.prototype.toString);
  10. function isRegExp(value) {
  11. return objectToString(value) === "[object RegExp]";
  12. }
  13. function isPlainObject(value) {
  14. if (typeof value !== "object" || value === null || Object.prototype.toString.call(value) !== "[object Object]") {
  15. return false;
  16. }
  17. const proto = Object.getPrototypeOf(value);
  18. return proto === null || Object.getPrototypeOf(proto) === null;
  19. }
  20. function valueToNode(value) {
  21. if (value === undefined) {
  22. return (0, _index.identifier)("undefined");
  23. }
  24. if (value === true || value === false) {
  25. return (0, _index.booleanLiteral)(value);
  26. }
  27. if (value === null) {
  28. return (0, _index.nullLiteral)();
  29. }
  30. if (typeof value === "string") {
  31. return (0, _index.stringLiteral)(value);
  32. }
  33. if (typeof value === "number") {
  34. let result;
  35. if (Number.isFinite(value)) {
  36. result = (0, _index.numericLiteral)(Math.abs(value));
  37. } else {
  38. let numerator;
  39. if (Number.isNaN(value)) {
  40. numerator = (0, _index.numericLiteral)(0);
  41. } else {
  42. numerator = (0, _index.numericLiteral)(1);
  43. }
  44. result = (0, _index.binaryExpression)("/", numerator, (0, _index.numericLiteral)(0));
  45. }
  46. if (value < 0 || Object.is(value, -0)) {
  47. result = (0, _index.unaryExpression)("-", result);
  48. }
  49. return result;
  50. }
  51. if (typeof value === "bigint") {
  52. return (0, _index.bigIntLiteral)(value.toString());
  53. }
  54. if (isRegExp(value)) {
  55. const pattern = value.source;
  56. const flags = /\/([a-z]*)$/.exec(value.toString())[1];
  57. return (0, _index.regExpLiteral)(pattern, flags);
  58. }
  59. if (Array.isArray(value)) {
  60. return (0, _index.arrayExpression)(value.map(valueToNode));
  61. }
  62. if (isPlainObject(value)) {
  63. const props = [];
  64. for (const key of Object.keys(value)) {
  65. let nodeKey,
  66. computed = false;
  67. if ((0, _isValidIdentifier.default)(key)) {
  68. if (key === "__proto__") {
  69. computed = true;
  70. nodeKey = (0, _index.stringLiteral)(key);
  71. } else {
  72. nodeKey = (0, _index.identifier)(key);
  73. }
  74. } else {
  75. nodeKey = (0, _index.stringLiteral)(key);
  76. }
  77. props.push((0, _index.objectProperty)(nodeKey, valueToNode(value[key]), computed));
  78. }
  79. return (0, _index.objectExpression)(props);
  80. }
  81. throw new Error("don't know how to turn this value into a node");
  82. }
  83. //# sourceMappingURL=valueToNode.js.map