web 3d图形渲染器
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.

252 lines
8.3 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.NullableTypeAnnotation = NullableTypeAnnotation;
  6. exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
  7. exports.UpdateExpression = UpdateExpression;
  8. exports.ObjectExpression = ObjectExpression;
  9. exports.DoExpression = DoExpression;
  10. exports.Binary = Binary;
  11. exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation;
  12. exports.TSAsExpression = TSAsExpression;
  13. exports.TSTypeAssertion = TSTypeAssertion;
  14. exports.TSIntersectionType = exports.TSUnionType = TSUnionType;
  15. exports.TSInferType = TSInferType;
  16. exports.BinaryExpression = BinaryExpression;
  17. exports.SequenceExpression = SequenceExpression;
  18. exports.AwaitExpression = exports.YieldExpression = YieldExpression;
  19. exports.ClassExpression = ClassExpression;
  20. exports.UnaryLike = UnaryLike;
  21. exports.FunctionExpression = FunctionExpression;
  22. exports.ArrowFunctionExpression = ArrowFunctionExpression;
  23. exports.ConditionalExpression = ConditionalExpression;
  24. exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression;
  25. exports.AssignmentExpression = AssignmentExpression;
  26. exports.LogicalExpression = LogicalExpression;
  27. var t = _interopRequireWildcard(require("@babel/types"));
  28. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  29. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  30. const PRECEDENCE = {
  31. "||": 0,
  32. "??": 0,
  33. "&&": 1,
  34. "|": 2,
  35. "^": 3,
  36. "&": 4,
  37. "==": 5,
  38. "===": 5,
  39. "!=": 5,
  40. "!==": 5,
  41. "<": 6,
  42. ">": 6,
  43. "<=": 6,
  44. ">=": 6,
  45. in: 6,
  46. instanceof: 6,
  47. ">>": 7,
  48. "<<": 7,
  49. ">>>": 7,
  50. "+": 8,
  51. "-": 8,
  52. "*": 9,
  53. "/": 9,
  54. "%": 9,
  55. "**": 10
  56. };
  57. const isClassExtendsClause = (node, parent) => (t.isClassDeclaration(parent) || t.isClassExpression(parent)) && parent.superClass === node;
  58. const hasPostfixPart = (node, parent) => (t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) && parent.object === node || (t.isCallExpression(parent) || t.isOptionalCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node || t.isTaggedTemplateExpression(parent) && parent.tag === node || t.isTSNonNullExpression(parent);
  59. function NullableTypeAnnotation(node, parent) {
  60. return t.isArrayTypeAnnotation(parent);
  61. }
  62. function FunctionTypeAnnotation(node, parent, printStack) {
  63. return t.isUnionTypeAnnotation(parent) || t.isIntersectionTypeAnnotation(parent) || t.isArrayTypeAnnotation(parent) || t.isTypeAnnotation(parent) && t.isArrowFunctionExpression(printStack[printStack.length - 3]);
  64. }
  65. function UpdateExpression(node, parent) {
  66. return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
  67. }
  68. function ObjectExpression(node, parent, printStack) {
  69. return isFirstInStatement(printStack, {
  70. considerArrow: true
  71. });
  72. }
  73. function DoExpression(node, parent, printStack) {
  74. return isFirstInStatement(printStack);
  75. }
  76. function Binary(node, parent) {
  77. if (node.operator === "**" && t.isBinaryExpression(parent, {
  78. operator: "**"
  79. })) {
  80. return parent.left === node;
  81. }
  82. if (isClassExtendsClause(node, parent)) {
  83. return true;
  84. }
  85. if (hasPostfixPart(node, parent) || t.isUnaryLike(parent) || t.isAwaitExpression(parent)) {
  86. return true;
  87. }
  88. if (t.isBinary(parent)) {
  89. const parentOp = parent.operator;
  90. const parentPos = PRECEDENCE[parentOp];
  91. const nodeOp = node.operator;
  92. const nodePos = PRECEDENCE[nodeOp];
  93. if (parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent) || parentPos > nodePos) {
  94. return true;
  95. }
  96. }
  97. }
  98. function UnionTypeAnnotation(node, parent) {
  99. return t.isArrayTypeAnnotation(parent) || t.isNullableTypeAnnotation(parent) || t.isIntersectionTypeAnnotation(parent) || t.isUnionTypeAnnotation(parent);
  100. }
  101. function TSAsExpression() {
  102. return true;
  103. }
  104. function TSTypeAssertion() {
  105. return true;
  106. }
  107. function TSUnionType(node, parent) {
  108. return t.isTSArrayType(parent) || t.isTSOptionalType(parent) || t.isTSIntersectionType(parent) || t.isTSUnionType(parent) || t.isTSRestType(parent);
  109. }
  110. function TSInferType(node, parent) {
  111. return t.isTSArrayType(parent) || t.isTSOptionalType(parent);
  112. }
  113. function BinaryExpression(node, parent) {
  114. return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent));
  115. }
  116. function SequenceExpression(node, parent) {
  117. if (t.isForStatement(parent) || t.isThrowStatement(parent) || t.isReturnStatement(parent) || t.isIfStatement(parent) && parent.test === node || t.isWhileStatement(parent) && parent.test === node || t.isForInStatement(parent) && parent.right === node || t.isSwitchStatement(parent) && parent.discriminant === node || t.isExpressionStatement(parent) && parent.expression === node) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. function YieldExpression(node, parent) {
  123. return t.isBinary(parent) || t.isUnaryLike(parent) || hasPostfixPart(node, parent) || t.isAwaitExpression(parent) && t.isYieldExpression(node) || t.isConditionalExpression(parent) && node === parent.test || isClassExtendsClause(node, parent);
  124. }
  125. function ClassExpression(node, parent, printStack) {
  126. return isFirstInStatement(printStack, {
  127. considerDefaultExports: true
  128. });
  129. }
  130. function UnaryLike(node, parent) {
  131. return hasPostfixPart(node, parent) || t.isBinaryExpression(parent, {
  132. operator: "**",
  133. left: node
  134. }) || isClassExtendsClause(node, parent);
  135. }
  136. function FunctionExpression(node, parent, printStack) {
  137. return isFirstInStatement(printStack, {
  138. considerDefaultExports: true
  139. });
  140. }
  141. function ArrowFunctionExpression(node, parent) {
  142. return t.isExportDeclaration(parent) || ConditionalExpression(node, parent);
  143. }
  144. function ConditionalExpression(node, parent) {
  145. if (t.isUnaryLike(parent) || t.isBinary(parent) || t.isConditionalExpression(parent, {
  146. test: node
  147. }) || t.isAwaitExpression(parent) || t.isTSTypeAssertion(parent) || t.isTSAsExpression(parent)) {
  148. return true;
  149. }
  150. return UnaryLike(node, parent);
  151. }
  152. function OptionalMemberExpression(node, parent) {
  153. return t.isCallExpression(parent, {
  154. callee: node
  155. }) || t.isMemberExpression(parent, {
  156. object: node
  157. });
  158. }
  159. function AssignmentExpression(node, parent) {
  160. if (t.isObjectPattern(node.left)) {
  161. return true;
  162. } else {
  163. return ConditionalExpression(node, parent);
  164. }
  165. }
  166. function LogicalExpression(node, parent) {
  167. switch (node.operator) {
  168. case "||":
  169. if (!t.isLogicalExpression(parent)) return false;
  170. return parent.operator === "??" || parent.operator === "&&";
  171. case "&&":
  172. return t.isLogicalExpression(parent, {
  173. operator: "??"
  174. });
  175. case "??":
  176. return t.isLogicalExpression(parent) && parent.operator !== "??";
  177. }
  178. }
  179. function isFirstInStatement(printStack, {
  180. considerArrow = false,
  181. considerDefaultExports = false
  182. } = {}) {
  183. let i = printStack.length - 1;
  184. let node = printStack[i];
  185. i--;
  186. let parent = printStack[i];
  187. while (i >= 0) {
  188. if (t.isExpressionStatement(parent, {
  189. expression: node
  190. }) || considerDefaultExports && t.isExportDefaultDeclaration(parent, {
  191. declaration: node
  192. }) || considerArrow && t.isArrowFunctionExpression(parent, {
  193. body: node
  194. })) {
  195. return true;
  196. }
  197. if (hasPostfixPart(node, parent) && !t.isNewExpression(parent) || t.isSequenceExpression(parent) && parent.expressions[0] === node || t.isConditional(parent, {
  198. test: node
  199. }) || t.isBinary(parent, {
  200. left: node
  201. }) || t.isAssignmentExpression(parent, {
  202. left: node
  203. })) {
  204. node = parent;
  205. i--;
  206. parent = printStack[i];
  207. } else {
  208. return false;
  209. }
  210. }
  211. return false;
  212. }