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.

150 lines
4.2 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = convertFunctionParams;
  6. var _helperCallDelegate = _interopRequireDefault(require("@babel/helper-call-delegate"));
  7. var _core = require("@babel/core");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. const buildDefaultParam = (0, _core.template)(`
  10. let VARIABLE_NAME =
  11. arguments.length > ARGUMENT_KEY && arguments[ARGUMENT_KEY] !== undefined ?
  12. arguments[ARGUMENT_KEY]
  13. :
  14. DEFAULT_VALUE;
  15. `);
  16. const buildLooseDefaultParam = (0, _core.template)(`
  17. if (ASSIGNMENT_IDENTIFIER === UNDEFINED) {
  18. ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE;
  19. }
  20. `);
  21. const buildLooseDestructuredDefaultParam = (0, _core.template)(`
  22. let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ;
  23. `);
  24. const buildSafeArgumentsAccess = (0, _core.template)(`
  25. let $0 = arguments.length > $1 ? arguments[$1] : undefined;
  26. `);
  27. function isSafeBinding(scope, node) {
  28. if (!scope.hasOwnBinding(node.name)) return true;
  29. const {
  30. kind
  31. } = scope.getOwnBinding(node.name);
  32. return kind === "param" || kind === "local";
  33. }
  34. const iifeVisitor = {
  35. ReferencedIdentifier(path, state) {
  36. const {
  37. scope,
  38. node
  39. } = path;
  40. if (node.name === "eval" || !isSafeBinding(scope, node)) {
  41. state.iife = true;
  42. path.stop();
  43. }
  44. },
  45. Scope(path) {
  46. path.skip();
  47. }
  48. };
  49. function convertFunctionParams(path, loose) {
  50. const {
  51. node,
  52. scope
  53. } = path;
  54. const state = {
  55. iife: false,
  56. scope: scope
  57. };
  58. const body = [];
  59. const params = path.get("params");
  60. let firstOptionalIndex = null;
  61. for (let i = 0; i < params.length; i++) {
  62. const param = params[i];
  63. const paramIsAssignmentPattern = param.isAssignmentPattern();
  64. if (paramIsAssignmentPattern && (loose || node.kind === "set")) {
  65. const left = param.get("left");
  66. const right = param.get("right");
  67. const undefinedNode = scope.buildUndefinedNode();
  68. if (left.isIdentifier()) {
  69. body.push(buildLooseDefaultParam({
  70. ASSIGNMENT_IDENTIFIER: _core.types.cloneNode(left.node),
  71. DEFAULT_VALUE: right.node,
  72. UNDEFINED: undefinedNode
  73. }));
  74. param.replaceWith(left.node);
  75. } else if (left.isObjectPattern() || left.isArrayPattern()) {
  76. const paramName = scope.generateUidIdentifier();
  77. body.push(buildLooseDestructuredDefaultParam({
  78. ASSIGNMENT_IDENTIFIER: left.node,
  79. DEFAULT_VALUE: right.node,
  80. PARAMETER_NAME: _core.types.cloneNode(paramName),
  81. UNDEFINED: undefinedNode
  82. }));
  83. param.replaceWith(paramName);
  84. }
  85. } else if (paramIsAssignmentPattern) {
  86. if (firstOptionalIndex === null) firstOptionalIndex = i;
  87. const left = param.get("left");
  88. const right = param.get("right");
  89. if (!state.iife) {
  90. if (right.isIdentifier() && !isSafeBinding(scope, right.node)) {
  91. state.iife = true;
  92. } else {
  93. right.traverse(iifeVisitor, state);
  94. }
  95. }
  96. const defNode = buildDefaultParam({
  97. VARIABLE_NAME: left.node,
  98. DEFAULT_VALUE: right.node,
  99. ARGUMENT_KEY: _core.types.numericLiteral(i)
  100. });
  101. body.push(defNode);
  102. } else if (firstOptionalIndex !== null) {
  103. const defNode = buildSafeArgumentsAccess([param.node, _core.types.numericLiteral(i)]);
  104. body.push(defNode);
  105. } else if (param.isObjectPattern() || param.isArrayPattern()) {
  106. const uid = path.scope.generateUidIdentifier("ref");
  107. const defNode = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(param.node, uid)]);
  108. body.push(defNode);
  109. param.replaceWith(_core.types.cloneNode(uid));
  110. }
  111. if (!state.iife && !param.isIdentifier()) {
  112. param.traverse(iifeVisitor, state);
  113. }
  114. }
  115. if (body.length === 0) return false;
  116. if (firstOptionalIndex !== null) {
  117. node.params = node.params.slice(0, firstOptionalIndex);
  118. }
  119. path.ensureBlock();
  120. if (state.iife) {
  121. body.push((0, _helperCallDelegate.default)(path, scope));
  122. path.set("body", _core.types.blockStatement(body));
  123. } else {
  124. path.get("body").unshiftContainer("body", body);
  125. }
  126. return true;
  127. }