index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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, () => {
  49. foundRestElement = true;
  50. path.stop();
  51. });
  52. return foundRestElement;
  53. }
  54. function visitRestElements(path, visitor) {
  55. path.traverse({
  56. Expression(path) {
  57. const parentType = path.parent.type;
  58. if (parentType === "AssignmentPattern" && path.key === "right" || parentType === "ObjectProperty" && path.parent.computed && path.key === "key") {
  59. path.skip();
  60. }
  61. },
  62. RestElement: visitor
  63. });
  64. }
  65. function hasSpread(node) {
  66. for (const prop of node.properties) {
  67. if (_core().types.isSpreadElement(prop)) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. function extractNormalizedKeys(path) {
  74. const props = path.node.properties;
  75. const keys = [];
  76. let allLiteral = true;
  77. for (const prop of props) {
  78. if (_core().types.isIdentifier(prop.key) && !prop.computed) {
  79. keys.push(_core().types.stringLiteral(prop.key.name));
  80. } else if (_core().types.isTemplateLiteral(prop.key)) {
  81. keys.push(_core().types.cloneNode(prop.key));
  82. } else if (_core().types.isLiteral(prop.key)) {
  83. keys.push(_core().types.stringLiteral(String(prop.key.value)));
  84. } else {
  85. keys.push(_core().types.cloneNode(prop.key));
  86. allLiteral = false;
  87. }
  88. }
  89. return {
  90. keys,
  91. allLiteral
  92. };
  93. }
  94. function replaceImpureComputedKeys(path) {
  95. const impureComputedPropertyDeclarators = [];
  96. for (const propPath of path.get("properties")) {
  97. const key = propPath.get("key");
  98. if (propPath.node.computed && !key.isPure()) {
  99. const name = path.scope.generateUidBasedOnNode(key.node);
  100. const declarator = _core().types.variableDeclarator(_core().types.identifier(name), key.node);
  101. impureComputedPropertyDeclarators.push(declarator);
  102. key.replaceWith(_core().types.identifier(name));
  103. }
  104. }
  105. return impureComputedPropertyDeclarators;
  106. }
  107. function removeUnusedExcludedKeys(path) {
  108. const bindings = path.getOuterBindingIdentifierPaths();
  109. Object.keys(bindings).forEach(bindingName => {
  110. const bindingParentPath = bindings[bindingName].parentPath;
  111. if (path.scope.getBinding(bindingName).references > ZERO_REFS || !bindingParentPath.isObjectProperty()) {
  112. return;
  113. }
  114. bindingParentPath.remove();
  115. });
  116. }
  117. function createObjectSpread(path, file, objRef) {
  118. const props = path.get("properties");
  119. const last = props[props.length - 1];
  120. _core().types.assertRestElement(last.node);
  121. const restElement = _core().types.cloneNode(last.node);
  122. last.remove();
  123. const impureComputedPropertyDeclarators = replaceImpureComputedKeys(path);
  124. const {
  125. keys,
  126. allLiteral
  127. } = extractNormalizedKeys(path);
  128. if (keys.length === 0) {
  129. return [impureComputedPropertyDeclarators, restElement.argument, _core().types.callExpression(getExtendsHelper(file), [_core().types.objectExpression([]), _core().types.cloneNode(objRef)])];
  130. }
  131. let keyExpression;
  132. if (!allLiteral) {
  133. keyExpression = _core().types.callExpression(_core().types.memberExpression(_core().types.arrayExpression(keys), _core().types.identifier("map")), [file.addHelper("toPropertyKey")]);
  134. } else {
  135. keyExpression = _core().types.arrayExpression(keys);
  136. }
  137. return [impureComputedPropertyDeclarators, restElement.argument, _core().types.callExpression(file.addHelper(`objectWithoutProperties${loose ? "Loose" : ""}`), [_core().types.cloneNode(objRef), keyExpression])];
  138. }
  139. function replaceRestElement(parentPath, paramPath, i, numParams) {
  140. if (paramPath.isAssignmentPattern()) {
  141. replaceRestElement(parentPath, paramPath.get("left"), i, numParams);
  142. return;
  143. }
  144. if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
  145. const elements = paramPath.get("elements");
  146. for (let i = 0; i < elements.length; i++) {
  147. replaceRestElement(parentPath, elements[i], i, elements.length);
  148. }
  149. }
  150. if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
  151. const uid = parentPath.scope.generateUidIdentifier("ref");
  152. const declar = _core().types.variableDeclaration("let", [_core().types.variableDeclarator(paramPath.node, uid)]);
  153. parentPath.ensureBlock();
  154. parentPath.get("body").unshiftContainer("body", declar);
  155. paramPath.replaceWith(_core().types.cloneNode(uid));
  156. }
  157. }
  158. return {
  159. name: "proposal-object-rest-spread",
  160. inherits: _pluginSyntaxObjectRestSpread().default,
  161. visitor: {
  162. Function(path) {
  163. const params = path.get("params");
  164. for (let i = params.length - 1; i >= 0; i--) {
  165. replaceRestElement(params[i].parentPath, params[i], i, params.length);
  166. }
  167. },
  168. VariableDeclarator(path, file) {
  169. if (!path.get("id").isObjectPattern()) {
  170. return;
  171. }
  172. let insertionPath = path;
  173. const originalPath = path;
  174. visitRestElements(path.get("id"), path => {
  175. if (!path.parentPath.isObjectPattern()) {
  176. return;
  177. }
  178. if (originalPath.node.id.properties.length > 1 && !_core().types.isIdentifier(originalPath.node.init)) {
  179. const initRef = path.scope.generateUidIdentifierBasedOnNode(originalPath.node.init, "ref");
  180. originalPath.insertBefore(_core().types.variableDeclarator(initRef, originalPath.node.init));
  181. originalPath.replaceWith(_core().types.variableDeclarator(originalPath.node.id, _core().types.cloneNode(initRef)));
  182. return;
  183. }
  184. let ref = originalPath.node.init;
  185. const refPropertyPath = [];
  186. let kind;
  187. path.findParent(path => {
  188. if (path.isObjectProperty()) {
  189. refPropertyPath.unshift(path.node.key.name);
  190. } else if (path.isVariableDeclarator()) {
  191. kind = path.parentPath.node.kind;
  192. return true;
  193. }
  194. });
  195. if (refPropertyPath.length) {
  196. refPropertyPath.forEach(prop => {
  197. ref = _core().types.memberExpression(ref, _core().types.identifier(prop));
  198. });
  199. }
  200. const objectPatternPath = path.findParent(path => path.isObjectPattern());
  201. const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectSpread(objectPatternPath, file, ref);
  202. if (loose) {
  203. removeUnusedExcludedKeys(objectPatternPath);
  204. }
  205. _core().types.assertIdentifier(argument);
  206. insertionPath.insertBefore(impureComputedPropertyDeclarators);
  207. insertionPath.insertAfter(_core().types.variableDeclarator(argument, callExpression));
  208. insertionPath = insertionPath.getSibling(insertionPath.key + 1);
  209. path.scope.registerBinding(kind, insertionPath);
  210. if (objectPatternPath.node.properties.length === 0) {
  211. objectPatternPath.findParent(path => path.isObjectProperty() || path.isVariableDeclarator()).remove();
  212. }
  213. });
  214. },
  215. ExportNamedDeclaration(path) {
  216. const declaration = path.get("declaration");
  217. if (!declaration.isVariableDeclaration()) return;
  218. const hasRest = declaration.get("declarations").some(path => hasRestElement(path.get("id")));
  219. if (!hasRest) return;
  220. const specifiers = [];
  221. for (const name of Object.keys(path.getOuterBindingIdentifiers(path))) {
  222. specifiers.push(_core().types.exportSpecifier(_core().types.identifier(name), _core().types.identifier(name)));
  223. }
  224. path.replaceWith(declaration.node);
  225. path.insertAfter(_core().types.exportNamedDeclaration(null, specifiers));
  226. },
  227. CatchClause(path) {
  228. const paramPath = path.get("param");
  229. replaceRestElement(paramPath.parentPath, paramPath);
  230. },
  231. AssignmentExpression(path, file) {
  232. const leftPath = path.get("left");
  233. if (leftPath.isObjectPattern() && hasRestElement(leftPath)) {
  234. const nodes = [];
  235. const refName = path.scope.generateUidBasedOnNode(path.node.right, "ref");
  236. nodes.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.identifier(refName), path.node.right)]));
  237. const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectSpread(leftPath, file, _core().types.identifier(refName));
  238. if (impureComputedPropertyDeclarators.length > 0) {
  239. nodes.push(_core().types.variableDeclaration("var", impureComputedPropertyDeclarators));
  240. }
  241. const nodeWithoutSpread = _core().types.cloneNode(path.node);
  242. nodeWithoutSpread.right = _core().types.identifier(refName);
  243. nodes.push(_core().types.expressionStatement(nodeWithoutSpread));
  244. nodes.push(_core().types.toStatement(_core().types.assignmentExpression("=", argument, callExpression)));
  245. nodes.push(_core().types.expressionStatement(_core().types.identifier(refName)));
  246. path.replaceWithMultiple(nodes);
  247. }
  248. },
  249. ForXStatement(path) {
  250. const {
  251. node,
  252. scope
  253. } = path;
  254. const leftPath = path.get("left");
  255. const left = node.left;
  256. if (_core().types.isObjectPattern(left) && hasRestElement(leftPath)) {
  257. const temp = scope.generateUidIdentifier("ref");
  258. node.left = _core().types.variableDeclaration("var", [_core().types.variableDeclarator(temp)]);
  259. path.ensureBlock();
  260. if (node.body.body.length === 0 && path.isCompletionRecord()) {
  261. node.body.body.unshift(_core().types.expressionStatement(scope.buildUndefinedNode()));
  262. }
  263. node.body.body.unshift(_core().types.expressionStatement(_core().types.assignmentExpression("=", left, _core().types.cloneNode(temp))));
  264. return;
  265. }
  266. if (!_core().types.isVariableDeclaration(left)) return;
  267. const pattern = left.declarations[0].id;
  268. if (!_core().types.isObjectPattern(pattern)) return;
  269. const key = scope.generateUidIdentifier("ref");
  270. node.left = _core().types.variableDeclaration(left.kind, [_core().types.variableDeclarator(key, null)]);
  271. path.ensureBlock();
  272. node.body.body.unshift(_core().types.variableDeclaration(node.left.kind, [_core().types.variableDeclarator(pattern, _core().types.cloneNode(key))]));
  273. },
  274. ObjectExpression(path, file) {
  275. if (!hasSpread(path.node)) return;
  276. const args = [];
  277. let props = [];
  278. function push() {
  279. if (!props.length) return;
  280. args.push(_core().types.objectExpression(props));
  281. props = [];
  282. }
  283. if (_core().types.isSpreadElement(path.node.properties[0])) {
  284. args.push(_core().types.objectExpression([]));
  285. }
  286. for (const prop of path.node.properties) {
  287. if (_core().types.isSpreadElement(prop)) {
  288. push();
  289. args.push(prop.argument);
  290. } else {
  291. props.push(prop);
  292. }
  293. }
  294. push();
  295. let helper;
  296. if (loose) {
  297. helper = getExtendsHelper(file);
  298. } else {
  299. helper = file.addHelper("objectSpread");
  300. }
  301. path.replaceWith(_core().types.callExpression(helper, args));
  302. }
  303. }
  304. };
  305. });
  306. exports.default = _default;