版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getStaticJSONValue = exports.isUndefinedIdentifier = exports.isNumberIdentifier = exports.isExpression = void 0;
  4. function isExpression(node) {
  5. if (node.type === "JSONIdentifier" || node.type === "JSONLiteral") {
  6. const parent = node.parent;
  7. if (parent.type === "JSONProperty" && parent.key === node) {
  8. return false;
  9. }
  10. return true;
  11. }
  12. if (node.type === "JSONObjectExpression" ||
  13. node.type === "JSONArrayExpression" ||
  14. node.type === "JSONUnaryExpression" ||
  15. node.type === "JSONTemplateLiteral") {
  16. return true;
  17. }
  18. return false;
  19. }
  20. exports.isExpression = isExpression;
  21. function isNumberIdentifier(node) {
  22. return (isExpression(node) && (node.name === "Infinity" || node.name === "NaN"));
  23. }
  24. exports.isNumberIdentifier = isNumberIdentifier;
  25. function isUndefinedIdentifier(node) {
  26. return isExpression(node) && node.name === "undefined";
  27. }
  28. exports.isUndefinedIdentifier = isUndefinedIdentifier;
  29. const resolver = {
  30. Program(node) {
  31. if (node.body.length !== 1 ||
  32. node.body[0].type !== "JSONExpressionStatement") {
  33. throw new Error("Illegal argument");
  34. }
  35. return getStaticJSONValue(node.body[0]);
  36. },
  37. JSONExpressionStatement(node) {
  38. return getStaticJSONValue(node.expression);
  39. },
  40. JSONObjectExpression(node) {
  41. const object = {};
  42. for (const prop of node.properties) {
  43. Object.assign(object, getStaticJSONValue(prop));
  44. }
  45. return object;
  46. },
  47. JSONProperty(node) {
  48. const keyName = node.key.type === "JSONLiteral"
  49. ? `${node.key.value}`
  50. : node.key.name;
  51. return {
  52. [keyName]: getStaticJSONValue(node.value),
  53. };
  54. },
  55. JSONArrayExpression(node) {
  56. const array = [];
  57. for (let index = 0; index < node.elements.length; index++) {
  58. const element = node.elements[index];
  59. if (element) {
  60. array[index] = getStaticJSONValue(element);
  61. }
  62. }
  63. return array;
  64. },
  65. JSONLiteral(node) {
  66. if (node.regex) {
  67. try {
  68. return new RegExp(node.regex.pattern, node.regex.flags);
  69. }
  70. catch (_a) {
  71. return `/${node.regex.pattern}/${node.regex.flags}`;
  72. }
  73. }
  74. if (node.bigint != null) {
  75. try {
  76. return BigInt(node.bigint);
  77. }
  78. catch (_b) {
  79. return `${node.bigint}`;
  80. }
  81. }
  82. return node.value;
  83. },
  84. JSONUnaryExpression(node) {
  85. const value = getStaticJSONValue(node.argument);
  86. return node.operator === "-" ? -value : value;
  87. },
  88. JSONIdentifier(node) {
  89. if (node.name === "Infinity") {
  90. return Infinity;
  91. }
  92. if (node.name === "NaN") {
  93. return NaN;
  94. }
  95. if (node.name === "undefined") {
  96. return undefined;
  97. }
  98. throw new Error("Illegal argument");
  99. },
  100. JSONTemplateLiteral(node) {
  101. return getStaticJSONValue(node.quasis[0]);
  102. },
  103. JSONTemplateElement(node) {
  104. return node.value.cooked;
  105. },
  106. };
  107. function getStaticJSONValue(node) {
  108. return resolver[node.type](node);
  109. }
  110. exports.getStaticJSONValue = getStaticJSONValue;