Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _helperPluginUtils() {
  7. const data = require("@babel/helper-plugin-utils");
  8. _helperPluginUtils = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _pluginSyntaxObjectRestSpread() {
  14. const data = _interopRequireDefault(require("@babel/plugin-syntax-object-rest-spread"));
  15. _pluginSyntaxObjectRestSpread = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _core() {
  21. const data = require("@babel/core");
  22. _core = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. const ZERO_REFS = (() => {
  29. const node = _core().types.identifier("a");
  30. const property = _core().types.objectProperty(_core().types.identifier("key"), node);
  31. const pattern = _core().types.objectPattern([property]);
  32. return _core().types.isReferenced(node, property, pattern) ? 1 : 0;
  33. })();
  34. var _default = (0, _helperPluginUtils().declare)((api, opts) => {
  35. api.assertVersion(7);
  36. const {
  37. useBuiltIns = false,
  38. loose = false
  39. } = opts;
  40. if (typeof loose !== "boolean") {
  41. throw new Error(".loose must be a boolean, or undefined");
  42. }
  43. function getExtendsHelper(file) {
  44. return useBuiltIns ? _core().types.memberExpression(_core().types.identifier("Object"), _core().types.identifier("assign")) : file.addHelper("extends");
  45. }
  46. function hasRestElement(path) {
  47. let foundRestElement = false;
  48. visitRestElements(path, restElement => {
  49. foundRestElement = true;
  50. restElement.stop();
  51. });
  52. return foundRestElement;
  53. }
  54. function hasObjectPatternRestElement(path) {
  55. let foundRestElement = false;
  56. visitRestElements(path, restElement => {
  57. if (restElement.parentPath.isObjectPattern()) {
  58. foundRestElement = true;
  59. restElement.stop();
  60. }
  61. });
  62. return foundRestElement;
  63. }
  64. function visitRestElements(path, visitor) {
  65. path.traverse({
  66. Expression(path) {
  67. const parentType = path.parent.type;
  68. if (parentType === "AssignmentPattern" && path.key === "right" || parentType === "ObjectProperty" && path.parent.computed && path.key === "key") {
  69. path.skip();
  70. }
  71. },
  72. RestElement: visitor
  73. });
  74. }
  75. function hasSpread(node) {
  76. for (const prop of node.properties) {
  77. if (_core().types.isSpreadElement(prop)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. function extractNormalizedKeys(path) {
  84. const props = path.node.properties;
  85. const keys = [];
  86. let allLiteral = true;
  87. for (const prop of props) {
  88. if (_core().types.isIdentifier(prop.key) && !prop.computed) {
  89. keys.push(_core().types.stringLiteral(prop.key.name));
  90. } else if (_core().types.isTemplateLiteral(prop.key)) {
  91. keys.push(_core().types.cloneNode(prop.key));
  92. } else if (_core().types.isLiteral(prop.key)) {
  93. keys.push(_core().types.stringLiteral(String(prop.key.value)));
  94. } else {
  95. keys.push(_core().types.cloneNode(prop.key));
  96. allLiteral = false;
  97. }
  98. }
  99. return {
  100. keys,
  101. allLiteral
  102. };
  103. }
  104. function replaceImpureComputedKeys(path) {
  105. const impureComputedPropertyDeclarators = [];
  106. for (const propPath of path.get("properties")) {
  107. const key = propPath.get("key");
  108. if (propPath.node.computed && !key.isPure()) {
  109. const name = path.scope.generateUidBasedOnNode(key.node);
  110. const declarator = _core().types.variableDeclarator(_core().types.identifier(name), key.node);
  111. impureComputedPropertyDeclarators.push(declarator);
  112. key.replaceWith(_core().types.identifier(name));
  113. }
  114. }
  115. return impureComputedPropertyDeclarators;
  116. }
  117. function removeUnusedExcludedKeys(path) {
  118. const bindings = path.getOuterBindingIdentifierPaths();
  119. Object.keys(bindings).forEach(bindingName => {
  120. const bindingParentPath = bindings[bindingName].parentPath;
  121. if (path.scope.getBinding(bindingName).references > ZERO_REFS || !bindingParentPath.isObjectProperty()) {
  122. return;
  123. }
  124. bindingParentPath.remove();
  125. });
  126. }
  127. function createObjectSpread(path, file, objRef) {
  128. const props = path.get("properties");
  129. const last = props[props.length - 1];
  130. _core().types.assertRestElement(last.node);
  131. const restElement = _core().types.cloneNode(last.node);
  132. last.remove();
  133. const impureComputedPropertyDeclarators = replaceImpureComputedKeys(path);
  134. const {
  135. keys,
  136. allLiteral
  137. } = extractNormalizedKeys(path);
  138. if (keys.length === 0) {
  139. return [impureComputedPropertyDeclarators, restElement.argument, _core().types.callExpression(getExtendsHelper(file), [_core().types.objectExpression([]), _core().types.cloneNode(objRef)])];
  140. }
  141. let keyExpression;
  142. if (!allLiteral) {
  143. keyExpression = _core().types.callExpression(_core().types.memberExpression(_core().types.arrayExpression(keys), _core().types.identifier("map")), [file.addHelper("toPropertyKey")]);
  144. } else {
  145. keyExpression = _core().types.arrayExpression(keys);
  146. }
  147. return [impureComputedPropertyDeclarators, restElement.argument, _core().types.callExpression(file.addHelper(`objectWithoutProperties${loose ? "Loose" : ""}`), [_core().types.cloneNode(objRef), keyExpression])];
  148. }
  149. function replaceRestElement(parentPath, paramPath) {
  150. if (paramPath.isAssignmentPattern()) {
  151. replaceRestElement(parentPath, paramPath.get("left"));
  152. return;
  153. }
  154. if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
  155. const elements = paramPath.get("elements");
  156. for (let i = 0; i < elements.length; i++) {
  157. replaceRestElement(parentPath, elements[i]);
  158. }
  159. }
  160. if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
  161. const uid = parentPath.scope.generateUidIdentifier("ref");
  162. const declar = _core().types.variableDeclaration("let", [_core().types.variableDeclarator(paramPath.node, uid)]);
  163. parentPath.ensureBlock();
  164. parentPath.get("body").unshiftContainer("body", declar);
  165. paramPath.replaceWith(_core().types.cloneNode(uid));
  166. }
  167. }
  168. return {
  169. name: "proposal-object-rest-spread",
  170. inherits: _pluginSyntaxObjectRestSpread().default,
  171. visitor: {
  172. Function(path) {
  173. const params = path.get("params");
  174. for (let i = params.length - 1; i >= 0; i--) {
  175. replaceRestElement(params[i].parentPath, params[i]);
  176. }
  177. },
  178. VariableDeclarator(path, file) {
  179. if (!path.get("id").isObjectPattern()) {
  180. return;
  181. }
  182. let insertionPath = path;
  183. const originalPath = path;
  184. visitRestElements(path.get("id"), path => {
  185. if (!path.parentPath.isObjectPattern()) {
  186. return;
  187. }
  188. if (originalPath.node.id.properties.length > 1 && !_core().types.isIdentifier(originalPath.node.init)) {
  189. const initRef = path.scope.generateUidIdentifierBasedOnNode(originalPath.node.init, "ref");
  190. originalPath.insertBefore(_core().types.variableDeclarator(initRef, originalPath.node.init));
  191. originalPath.replaceWith(_core().types.variableDeclarator(originalPath.node.id, _core().types.cloneNode(initRef)));
  192. return;
  193. }
  194. let ref = originalPath.node.init;
  195. const refPropertyPath = [];
  196. let kind;
  197. path.findParent(path => {
  198. if (path.isObjectProperty()) {
  199. refPropertyPath.unshift(path.node.key.name);
  200. } else if (path.isVariableDeclarator()) {
  201. kind = path.parentPath.node.kind;
  202. return true;
  203. }
  204. });
  205. if (refPropertyPath.length) {
  206. refPropertyPath.forEach(prop => {
  207. ref = _core().types.memberExpression(ref, _core().types.identifier(prop));
  208. });
  209. }
  210. const objectPatternPath = path.findParent(path => path.isObjectPattern());
  211. const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectSpread(objectPatternPath, file, ref);
  212. if (loose) {
  213. removeUnusedExcludedKeys(objectPatternPath);
  214. }
  215. _core().types.assertIdentifier(argument);
  216. insertionPath.insertBefore(impureComputedPropertyDeclarators);
  217. insertionPath.insertAfter(_core().types.variableDeclarator(argument, callExpression));
  218. insertionPath = insertionPath.getSibling(insertionPath.key + 1);
  219. path.scope.registerBinding(kind, insertionPath);
  220. if (objectPatternPath.node.properties.length === 0) {
  221. objectPatternPath.findParent(path => path.isObjectProperty() || path.isVariableDeclarator()).remove();
  222. }
  223. });
  224. },
  225. ExportNamedDeclaration(path) {
  226. const declaration = path.get("declaration");
  227. if (!declaration.isVariableDeclaration()) return;
  228. const hasRest = declaration.get("declarations").some(path => hasRestElement(path.get("id")));
  229. if (!hasRest) return;
  230. const specifiers = [];
  231. for (const name of Object.keys(path.getOuterBindingIdentifiers(path))) {
  232. specifiers.push(_core().types.exportSpecifier(_core().types.identifier(name), _core().types.identifier(name)));
  233. }
  234. path.replaceWith(declaration.node);
  235. path.insertAfter(_core().types.exportNamedDeclaration(null, specifiers));
  236. },
  237. CatchClause(path) {
  238. const paramPath = path.get("param");
  239. replaceRestElement(paramPath.parentPath, paramPath);
  240. },
  241. AssignmentExpression(path, file) {
  242. const leftPath = path.get("left");
  243. if (leftPath.isObjectPattern() && hasRestElement(leftPath)) {
  244. const nodes = [];
  245. const refName = path.scope.generateUidBasedOnNode(path.node.right, "ref");
  246. nodes.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.identifier(refName), path.node.right)]));
  247. const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectSpread(leftPath, file, _core().types.identifier(refName));
  248. if (impureComputedPropertyDeclarators.length > 0) {
  249. nodes.push(_core().types.variableDeclaration("var", impureComputedPropertyDeclarators));
  250. }
  251. const nodeWithoutSpread = _core().types.cloneNode(path.node);
  252. nodeWithoutSpread.right = _core().types.identifier(refName);
  253. nodes.push(_core().types.expressionStatement(nodeWithoutSpread));
  254. nodes.push(_core().types.toStatement(_core().types.assignmentExpression("=", argument, callExpression)));
  255. nodes.push(_core().types.expressionStatement(_core().types.identifier(refName)));
  256. path.replaceWithMultiple(nodes);
  257. }
  258. },
  259. ForXStatement(path) {
  260. const {
  261. node,
  262. scope
  263. } = path;
  264. const leftPath = path.get("left");
  265. const left = node.left;
  266. if (!hasObjectPatternRestElement(leftPath)) {
  267. return;
  268. }
  269. if (!_core().types.isVariableDeclaration(left)) {
  270. const temp = scope.generateUidIdentifier("ref");
  271. node.left = _core().types.variableDeclaration("var", [_core().types.variableDeclarator(temp)]);
  272. path.ensureBlock();
  273. if (node.body.body.length === 0 && path.isCompletionRecord()) {
  274. node.body.body.unshift(_core().types.expressionStatement(scope.buildUndefinedNode()));
  275. }
  276. node.body.body.unshift(_core().types.expressionStatement(_core().types.assignmentExpression("=", left, _core().types.cloneNode(temp))));
  277. } else {
  278. const pattern = left.declarations[0].id;
  279. const key = scope.generateUidIdentifier("ref");
  280. node.left = _core().types.variableDeclaration(left.kind, [_core().types.variableDeclarator(key, null)]);
  281. path.ensureBlock();
  282. node.body.body.unshift(_core().types.variableDeclaration(node.left.kind, [_core().types.variableDeclarator(pattern, _core().types.cloneNode(key))]));
  283. }
  284. },
  285. ArrayPattern(path) {
  286. const objectPatterns = [];
  287. visitRestElements(path, path => {
  288. if (!path.parentPath.isObjectPattern()) {
  289. return;
  290. }
  291. const objectPattern = path.parentPath;
  292. const uid = path.scope.generateUidIdentifier("ref");
  293. objectPatterns.push(_core().types.variableDeclarator(objectPattern.node, uid));
  294. objectPattern.replaceWith(_core().types.cloneNode(uid));
  295. path.skip();
  296. });
  297. if (objectPatterns.length > 0) {
  298. const statementPath = path.getStatementParent();
  299. statementPath.insertAfter(_core().types.variableDeclaration(statementPath.node.kind || "var", objectPatterns));
  300. }
  301. },
  302. ObjectExpression(path, file) {
  303. if (!hasSpread(path.node)) return;
  304. const args = [];
  305. let props = [];
  306. function push() {
  307. args.push(_core().types.objectExpression(props));
  308. props = [];
  309. }
  310. for (const prop of path.node.properties) {
  311. if (_core().types.isSpreadElement(prop)) {
  312. push();
  313. args.push(prop.argument);
  314. } else {
  315. props.push(prop);
  316. }
  317. }
  318. if (props.length) {
  319. push();
  320. }
  321. let helper;
  322. if (loose) {
  323. helper = getExtendsHelper(file);
  324. } else {
  325. try {
  326. helper = file.addHelper("objectSpread2");
  327. } catch (_unused) {
  328. this.file.declarations["objectSpread2"] = null;
  329. helper = file.addHelper("objectSpread");
  330. }
  331. }
  332. path.replaceWith(_core().types.callExpression(helper, args));
  333. }
  334. }
  335. };
  336. });
  337. exports.default = _default;