4307607ba56cd34332a561435e739744c8e67ec4645ccb9feac8d717efd301ed8df321fb8b65e4f2ba402d194dcb6f73424e98c02f512896bae2827b8d40c5 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if (value < 0) {
  53. return (0, _index.unaryExpression)("-", (0, _index.bigIntLiteral)(-value));
  54. } else {
  55. return (0, _index.bigIntLiteral)(value);
  56. }
  57. }
  58. if (isRegExp(value)) {
  59. const pattern = value.source;
  60. const flags = /\/([a-z]*)$/.exec(value.toString())[1];
  61. return (0, _index.regExpLiteral)(pattern, flags);
  62. }
  63. if (Array.isArray(value)) {
  64. return (0, _index.arrayExpression)(value.map(valueToNode));
  65. }
  66. if (isPlainObject(value)) {
  67. const props = [];
  68. for (const key of Object.keys(value)) {
  69. let nodeKey,
  70. computed = false;
  71. if ((0, _isValidIdentifier.default)(key)) {
  72. if (key === "__proto__") {
  73. computed = true;
  74. nodeKey = (0, _index.stringLiteral)(key);
  75. } else {
  76. nodeKey = (0, _index.identifier)(key);
  77. }
  78. } else {
  79. nodeKey = (0, _index.stringLiteral)(key);
  80. }
  81. props.push((0, _index.objectProperty)(nodeKey, valueToNode(value[key]), computed));
  82. }
  83. return (0, _index.objectExpression)(props);
  84. }
  85. throw new Error("don't know how to turn this value into a node");
  86. }
  87. //# sourceMappingURL=valueToNode.js.map