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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 _core() {
  14. const data = require("@babel/core");
  15. _core = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _default = (0, _helperPluginUtils().declare)((api, options) => {
  21. api.assertVersion(7);
  22. const {
  23. loose = false,
  24. useBuiltIns = false
  25. } = options;
  26. if (typeof loose !== "boolean") {
  27. throw new Error(`.loose must be a boolean or undefined`);
  28. }
  29. const arrayOnlySpread = loose;
  30. function getExtendsHelper(file) {
  31. return useBuiltIns ? _core().types.memberExpression(_core().types.identifier("Object"), _core().types.identifier("assign")) : file.addHelper("extends");
  32. }
  33. function variableDeclarationHasPattern(node) {
  34. for (const declar of node.declarations) {
  35. if (_core().types.isPattern(declar.id)) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. function hasRest(pattern) {
  42. for (const elem of pattern.elements) {
  43. if (_core().types.isRestElement(elem)) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. function hasObjectRest(pattern) {
  50. for (const elem of pattern.properties) {
  51. if (_core().types.isRestElement(elem)) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. const STOP_TRAVERSAL = {};
  58. const arrayUnpackVisitor = (node, ancestors, state) => {
  59. if (!ancestors.length) {
  60. return;
  61. }
  62. if (_core().types.isIdentifier(node) && _core().types.isReferenced(node, ancestors[ancestors.length - 1]) && state.bindings[node.name]) {
  63. state.deopt = true;
  64. throw STOP_TRAVERSAL;
  65. }
  66. };
  67. class DestructuringTransformer {
  68. constructor(opts) {
  69. this.blockHoist = opts.blockHoist;
  70. this.operator = opts.operator;
  71. this.arrays = {};
  72. this.nodes = opts.nodes || [];
  73. this.scope = opts.scope;
  74. this.kind = opts.kind;
  75. this.arrayOnlySpread = opts.arrayOnlySpread;
  76. this.addHelper = opts.addHelper;
  77. }
  78. buildVariableAssignment(id, init) {
  79. let op = this.operator;
  80. if (_core().types.isMemberExpression(id)) op = "=";
  81. let node;
  82. if (op) {
  83. node = _core().types.expressionStatement(_core().types.assignmentExpression(op, id, _core().types.cloneNode(init) || this.scope.buildUndefinedNode()));
  84. } else {
  85. node = _core().types.variableDeclaration(this.kind, [_core().types.variableDeclarator(id, _core().types.cloneNode(init))]);
  86. }
  87. node._blockHoist = this.blockHoist;
  88. return node;
  89. }
  90. buildVariableDeclaration(id, init) {
  91. const declar = _core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.cloneNode(id), _core().types.cloneNode(init))]);
  92. declar._blockHoist = this.blockHoist;
  93. return declar;
  94. }
  95. push(id, _init) {
  96. const init = _core().types.cloneNode(_init);
  97. if (_core().types.isObjectPattern(id)) {
  98. this.pushObjectPattern(id, init);
  99. } else if (_core().types.isArrayPattern(id)) {
  100. this.pushArrayPattern(id, init);
  101. } else if (_core().types.isAssignmentPattern(id)) {
  102. this.pushAssignmentPattern(id, init);
  103. } else {
  104. this.nodes.push(this.buildVariableAssignment(id, init));
  105. }
  106. }
  107. toArray(node, count) {
  108. if (this.arrayOnlySpread || _core().types.isIdentifier(node) && this.arrays[node.name]) {
  109. return node;
  110. } else {
  111. return this.scope.toArray(node, count);
  112. }
  113. }
  114. pushAssignmentPattern({
  115. left,
  116. right
  117. }, valueRef) {
  118. const tempId = this.scope.generateUidIdentifierBasedOnNode(valueRef);
  119. this.nodes.push(this.buildVariableDeclaration(tempId, valueRef));
  120. const tempConditional = _core().types.conditionalExpression(_core().types.binaryExpression("===", _core().types.cloneNode(tempId), this.scope.buildUndefinedNode()), right, _core().types.cloneNode(tempId));
  121. if (_core().types.isPattern(left)) {
  122. let patternId;
  123. let node;
  124. if (this.kind === "const") {
  125. patternId = this.scope.generateUidIdentifier(tempId.name);
  126. node = this.buildVariableDeclaration(patternId, tempConditional);
  127. } else {
  128. patternId = tempId;
  129. node = _core().types.expressionStatement(_core().types.assignmentExpression("=", _core().types.cloneNode(tempId), tempConditional));
  130. }
  131. this.nodes.push(node);
  132. this.push(left, patternId);
  133. } else {
  134. this.nodes.push(this.buildVariableAssignment(left, tempConditional));
  135. }
  136. }
  137. pushObjectRest(pattern, objRef, spreadProp, spreadPropIndex) {
  138. const keys = [];
  139. let allLiteral = true;
  140. for (let i = 0; i < pattern.properties.length; i++) {
  141. const prop = pattern.properties[i];
  142. if (i >= spreadPropIndex) break;
  143. if (_core().types.isRestElement(prop)) continue;
  144. const key = prop.key;
  145. if (_core().types.isIdentifier(key) && !prop.computed) {
  146. keys.push(_core().types.stringLiteral(key.name));
  147. } else if (_core().types.isTemplateLiteral(prop.key)) {
  148. keys.push(_core().types.cloneNode(prop.key));
  149. } else if (_core().types.isLiteral(key)) {
  150. keys.push(_core().types.stringLiteral(String(key.value)));
  151. } else {
  152. keys.push(_core().types.cloneNode(key));
  153. allLiteral = false;
  154. }
  155. }
  156. let value;
  157. if (keys.length === 0) {
  158. value = _core().types.callExpression(getExtendsHelper(this), [_core().types.objectExpression([]), _core().types.cloneNode(objRef)]);
  159. } else {
  160. let keyExpression = _core().types.arrayExpression(keys);
  161. if (!allLiteral) {
  162. keyExpression = _core().types.callExpression(_core().types.memberExpression(keyExpression, _core().types.identifier("map")), [this.addHelper("toPropertyKey")]);
  163. }
  164. value = _core().types.callExpression(this.addHelper(`objectWithoutProperties${loose ? "Loose" : ""}`), [_core().types.cloneNode(objRef), keyExpression]);
  165. }
  166. this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
  167. }
  168. pushObjectProperty(prop, propRef) {
  169. if (_core().types.isLiteral(prop.key)) prop.computed = true;
  170. const pattern = prop.value;
  171. const objRef = _core().types.memberExpression(_core().types.cloneNode(propRef), prop.key, prop.computed);
  172. if (_core().types.isPattern(pattern)) {
  173. this.push(pattern, objRef);
  174. } else {
  175. this.nodes.push(this.buildVariableAssignment(pattern, objRef));
  176. }
  177. }
  178. pushObjectPattern(pattern, objRef) {
  179. if (!pattern.properties.length) {
  180. this.nodes.push(_core().types.expressionStatement(_core().types.callExpression(this.addHelper("objectDestructuringEmpty"), [objRef])));
  181. }
  182. if (pattern.properties.length > 1 && !this.scope.isStatic(objRef)) {
  183. const temp = this.scope.generateUidIdentifierBasedOnNode(objRef);
  184. this.nodes.push(this.buildVariableDeclaration(temp, objRef));
  185. objRef = temp;
  186. }
  187. if (hasObjectRest(pattern)) {
  188. let copiedPattern;
  189. for (let i = 0; i < pattern.properties.length; i++) {
  190. const prop = pattern.properties[i];
  191. if (_core().types.isRestElement(prop)) {
  192. break;
  193. }
  194. const key = prop.key;
  195. if (prop.computed && !this.scope.isPure(key)) {
  196. const name = this.scope.generateUidIdentifierBasedOnNode(key);
  197. this.nodes.push(this.buildVariableDeclaration(name, key));
  198. if (!copiedPattern) {
  199. copiedPattern = pattern = Object.assign({}, pattern, {
  200. properties: pattern.properties.slice()
  201. });
  202. }
  203. copiedPattern.properties[i] = Object.assign({}, copiedPattern.properties[i], {
  204. key: name
  205. });
  206. }
  207. }
  208. }
  209. for (let i = 0; i < pattern.properties.length; i++) {
  210. const prop = pattern.properties[i];
  211. if (_core().types.isRestElement(prop)) {
  212. this.pushObjectRest(pattern, objRef, prop, i);
  213. } else {
  214. this.pushObjectProperty(prop, objRef);
  215. }
  216. }
  217. }
  218. canUnpackArrayPattern(pattern, arr) {
  219. if (!_core().types.isArrayExpression(arr)) return false;
  220. if (pattern.elements.length > arr.elements.length) return;
  221. if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) {
  222. return false;
  223. }
  224. for (const elem of pattern.elements) {
  225. if (!elem) return false;
  226. if (_core().types.isMemberExpression(elem)) return false;
  227. }
  228. for (const elem of arr.elements) {
  229. if (_core().types.isSpreadElement(elem)) return false;
  230. if (_core().types.isCallExpression(elem)) return false;
  231. if (_core().types.isMemberExpression(elem)) return false;
  232. }
  233. const bindings = _core().types.getBindingIdentifiers(pattern);
  234. const state = {
  235. deopt: false,
  236. bindings
  237. };
  238. try {
  239. _core().types.traverse(arr, arrayUnpackVisitor, state);
  240. } catch (e) {
  241. if (e !== STOP_TRAVERSAL) throw e;
  242. }
  243. return !state.deopt;
  244. }
  245. pushUnpackedArrayPattern(pattern, arr) {
  246. for (let i = 0; i < pattern.elements.length; i++) {
  247. const elem = pattern.elements[i];
  248. if (_core().types.isRestElement(elem)) {
  249. this.push(elem.argument, _core().types.arrayExpression(arr.elements.slice(i)));
  250. } else {
  251. this.push(elem, arr.elements[i]);
  252. }
  253. }
  254. }
  255. pushArrayPattern(pattern, arrayRef) {
  256. if (!pattern.elements) return;
  257. if (this.canUnpackArrayPattern(pattern, arrayRef)) {
  258. return this.pushUnpackedArrayPattern(pattern, arrayRef);
  259. }
  260. const count = !hasRest(pattern) && pattern.elements.length;
  261. const toArray = this.toArray(arrayRef, count);
  262. if (_core().types.isIdentifier(toArray)) {
  263. arrayRef = toArray;
  264. } else {
  265. arrayRef = this.scope.generateUidIdentifierBasedOnNode(arrayRef);
  266. this.arrays[arrayRef.name] = true;
  267. this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray));
  268. }
  269. for (let i = 0; i < pattern.elements.length; i++) {
  270. let elem = pattern.elements[i];
  271. if (!elem) continue;
  272. let elemRef;
  273. if (_core().types.isRestElement(elem)) {
  274. elemRef = this.toArray(arrayRef);
  275. elemRef = _core().types.callExpression(_core().types.memberExpression(elemRef, _core().types.identifier("slice")), [_core().types.numericLiteral(i)]);
  276. elem = elem.argument;
  277. } else {
  278. elemRef = _core().types.memberExpression(arrayRef, _core().types.numericLiteral(i), true);
  279. }
  280. this.push(elem, elemRef);
  281. }
  282. }
  283. init(pattern, ref) {
  284. if (!_core().types.isArrayExpression(ref) && !_core().types.isMemberExpression(ref)) {
  285. const memo = this.scope.maybeGenerateMemoised(ref, true);
  286. if (memo) {
  287. this.nodes.push(this.buildVariableDeclaration(memo, _core().types.cloneNode(ref)));
  288. ref = memo;
  289. }
  290. }
  291. this.push(pattern, ref);
  292. return this.nodes;
  293. }
  294. }
  295. return {
  296. name: "transform-destructuring",
  297. visitor: {
  298. ExportNamedDeclaration(path) {
  299. const declaration = path.get("declaration");
  300. if (!declaration.isVariableDeclaration()) return;
  301. if (!variableDeclarationHasPattern(declaration.node)) return;
  302. const specifiers = [];
  303. for (const name of Object.keys(path.getOuterBindingIdentifiers(path))) {
  304. specifiers.push(_core().types.exportSpecifier(_core().types.identifier(name), _core().types.identifier(name)));
  305. }
  306. path.replaceWith(declaration.node);
  307. path.insertAfter(_core().types.exportNamedDeclaration(null, specifiers));
  308. },
  309. ForXStatement(path) {
  310. const {
  311. node,
  312. scope
  313. } = path;
  314. const left = node.left;
  315. if (_core().types.isPattern(left)) {
  316. const temp = scope.generateUidIdentifier("ref");
  317. node.left = _core().types.variableDeclaration("var", [_core().types.variableDeclarator(temp)]);
  318. path.ensureBlock();
  319. if (node.body.body.length === 0 && path.isCompletionRecord()) {
  320. node.body.body.unshift(_core().types.expressionStatement(scope.buildUndefinedNode()));
  321. }
  322. node.body.body.unshift(_core().types.expressionStatement(_core().types.assignmentExpression("=", left, temp)));
  323. return;
  324. }
  325. if (!_core().types.isVariableDeclaration(left)) return;
  326. const pattern = left.declarations[0].id;
  327. if (!_core().types.isPattern(pattern)) return;
  328. const key = scope.generateUidIdentifier("ref");
  329. node.left = _core().types.variableDeclaration(left.kind, [_core().types.variableDeclarator(key, null)]);
  330. const nodes = [];
  331. const destructuring = new DestructuringTransformer({
  332. kind: left.kind,
  333. scope: scope,
  334. nodes: nodes,
  335. arrayOnlySpread,
  336. addHelper: name => this.addHelper(name)
  337. });
  338. destructuring.init(pattern, key);
  339. path.ensureBlock();
  340. const block = node.body;
  341. block.body = nodes.concat(block.body);
  342. },
  343. CatchClause({
  344. node,
  345. scope
  346. }) {
  347. const pattern = node.param;
  348. if (!_core().types.isPattern(pattern)) return;
  349. const ref = scope.generateUidIdentifier("ref");
  350. node.param = ref;
  351. const nodes = [];
  352. const destructuring = new DestructuringTransformer({
  353. kind: "let",
  354. scope: scope,
  355. nodes: nodes,
  356. arrayOnlySpread,
  357. addHelper: name => this.addHelper(name)
  358. });
  359. destructuring.init(pattern, ref);
  360. node.body.body = nodes.concat(node.body.body);
  361. },
  362. AssignmentExpression(path) {
  363. const {
  364. node,
  365. scope
  366. } = path;
  367. if (!_core().types.isPattern(node.left)) return;
  368. const nodes = [];
  369. const destructuring = new DestructuringTransformer({
  370. operator: node.operator,
  371. scope: scope,
  372. nodes: nodes,
  373. arrayOnlySpread,
  374. addHelper: name => this.addHelper(name)
  375. });
  376. let ref;
  377. if (path.isCompletionRecord() || !path.parentPath.isExpressionStatement()) {
  378. ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref");
  379. nodes.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(ref, node.right)]));
  380. if (_core().types.isArrayExpression(node.right)) {
  381. destructuring.arrays[ref.name] = true;
  382. }
  383. }
  384. destructuring.init(node.left, ref || node.right);
  385. if (ref) {
  386. if (path.parentPath.isArrowFunctionExpression()) {
  387. path.replaceWith(_core().types.blockStatement([]));
  388. nodes.push(_core().types.returnStatement(_core().types.cloneNode(ref)));
  389. } else {
  390. nodes.push(_core().types.expressionStatement(_core().types.cloneNode(ref)));
  391. }
  392. }
  393. path.replaceWithMultiple(nodes);
  394. },
  395. VariableDeclaration(path) {
  396. const {
  397. node,
  398. scope,
  399. parent
  400. } = path;
  401. if (_core().types.isForXStatement(parent)) return;
  402. if (!parent || !path.container) return;
  403. if (!variableDeclarationHasPattern(node)) return;
  404. const nodeKind = node.kind;
  405. const nodes = [];
  406. let declar;
  407. for (let i = 0; i < node.declarations.length; i++) {
  408. declar = node.declarations[i];
  409. const patternId = declar.init;
  410. const pattern = declar.id;
  411. const destructuring = new DestructuringTransformer({
  412. blockHoist: node._blockHoist,
  413. nodes: nodes,
  414. scope: scope,
  415. kind: node.kind,
  416. arrayOnlySpread,
  417. addHelper: name => this.addHelper(name)
  418. });
  419. if (_core().types.isPattern(pattern)) {
  420. destructuring.init(pattern, patternId);
  421. if (+i !== node.declarations.length - 1) {
  422. _core().types.inherits(nodes[nodes.length - 1], declar);
  423. }
  424. } else {
  425. nodes.push(_core().types.inherits(destructuring.buildVariableAssignment(declar.id, _core().types.cloneNode(declar.init)), declar));
  426. }
  427. }
  428. let tail = null;
  429. const nodesOut = [];
  430. for (const node of nodes) {
  431. if (tail !== null && _core().types.isVariableDeclaration(node)) {
  432. tail.declarations.push(...node.declarations);
  433. } else {
  434. node.kind = nodeKind;
  435. nodesOut.push(node);
  436. tail = _core().types.isVariableDeclaration(node) ? node : null;
  437. }
  438. }
  439. for (const nodeOut of nodesOut) {
  440. if (!nodeOut.declarations) continue;
  441. for (const declaration of nodeOut.declarations) {
  442. const {
  443. name
  444. } = declaration.id;
  445. if (scope.bindings[name]) {
  446. scope.bindings[name].kind = nodeOut.kind;
  447. }
  448. }
  449. }
  450. if (nodesOut.length === 1) {
  451. path.replaceWith(nodesOut[0]);
  452. } else {
  453. path.replaceWithMultiple(nodesOut);
  454. }
  455. }
  456. }
  457. };
  458. });
  459. exports.default = _default;