default.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.visitor = undefined;
  4. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  5. var _getIterator3 = _interopRequireDefault(_getIterator2);
  6. var _babelHelperGetFunctionArity = require("babel-helper-get-function-arity");
  7. var _babelHelperGetFunctionArity2 = _interopRequireDefault(_babelHelperGetFunctionArity);
  8. var _babelHelperCallDelegate = require("babel-helper-call-delegate");
  9. var _babelHelperCallDelegate2 = _interopRequireDefault(_babelHelperCallDelegate);
  10. var _babelTemplate = require("babel-template");
  11. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  12. var _babelTypes = require("babel-types");
  13. var t = _interopRequireWildcard(_babelTypes);
  14. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. var buildDefaultParam = (0, _babelTemplate2.default)("\n let VARIABLE_NAME =\n ARGUMENTS.length > ARGUMENT_KEY && ARGUMENTS[ARGUMENT_KEY] !== undefined ?\n ARGUMENTS[ARGUMENT_KEY]\n :\n DEFAULT_VALUE;\n");
  17. var buildCutOff = (0, _babelTemplate2.default)("\n let $0 = $1[$2];\n");
  18. function hasDefaults(node) {
  19. for (var _iterator = node.params, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  20. var _ref;
  21. if (_isArray) {
  22. if (_i >= _iterator.length) break;
  23. _ref = _iterator[_i++];
  24. } else {
  25. _i = _iterator.next();
  26. if (_i.done) break;
  27. _ref = _i.value;
  28. }
  29. var param = _ref;
  30. if (!t.isIdentifier(param)) return true;
  31. }
  32. return false;
  33. }
  34. function isSafeBinding(scope, node) {
  35. if (!scope.hasOwnBinding(node.name)) return true;
  36. var _scope$getOwnBinding = scope.getOwnBinding(node.name),
  37. kind = _scope$getOwnBinding.kind;
  38. return kind === "param" || kind === "local";
  39. }
  40. var iifeVisitor = {
  41. ReferencedIdentifier: function ReferencedIdentifier(path, state) {
  42. var scope = path.scope,
  43. node = path.node;
  44. if (node.name === "eval" || !isSafeBinding(scope, node)) {
  45. state.iife = true;
  46. path.stop();
  47. }
  48. },
  49. Scope: function Scope(path) {
  50. path.skip();
  51. }
  52. };
  53. var visitor = exports.visitor = {
  54. Function: function Function(path) {
  55. var node = path.node,
  56. scope = path.scope;
  57. if (!hasDefaults(node)) return;
  58. path.ensureBlock();
  59. var state = {
  60. iife: false,
  61. scope: scope
  62. };
  63. var body = [];
  64. var argsIdentifier = t.identifier("arguments");
  65. argsIdentifier._shadowedFunctionLiteral = path;
  66. function pushDefNode(left, right, i) {
  67. var defNode = buildDefaultParam({
  68. VARIABLE_NAME: left,
  69. DEFAULT_VALUE: right,
  70. ARGUMENT_KEY: t.numericLiteral(i),
  71. ARGUMENTS: argsIdentifier
  72. });
  73. defNode._blockHoist = node.params.length - i;
  74. body.push(defNode);
  75. }
  76. var lastNonDefaultParam = (0, _babelHelperGetFunctionArity2.default)(node);
  77. var params = path.get("params");
  78. for (var i = 0; i < params.length; i++) {
  79. var param = params[i];
  80. if (!param.isAssignmentPattern()) {
  81. if (!state.iife && !param.isIdentifier()) {
  82. param.traverse(iifeVisitor, state);
  83. }
  84. continue;
  85. }
  86. var left = param.get("left");
  87. var right = param.get("right");
  88. if (i >= lastNonDefaultParam || left.isPattern()) {
  89. var placeholder = scope.generateUidIdentifier("x");
  90. placeholder._isDefaultPlaceholder = true;
  91. node.params[i] = placeholder;
  92. } else {
  93. node.params[i] = left.node;
  94. }
  95. if (!state.iife) {
  96. if (right.isIdentifier() && !isSafeBinding(scope, right.node)) {
  97. state.iife = true;
  98. } else {
  99. right.traverse(iifeVisitor, state);
  100. }
  101. }
  102. pushDefNode(left.node, right.node, i);
  103. }
  104. for (var _i2 = lastNonDefaultParam + 1; _i2 < node.params.length; _i2++) {
  105. var _param = node.params[_i2];
  106. if (_param._isDefaultPlaceholder) continue;
  107. var declar = buildCutOff(_param, argsIdentifier, t.numericLiteral(_i2));
  108. declar._blockHoist = node.params.length - _i2;
  109. body.push(declar);
  110. }
  111. node.params = node.params.slice(0, lastNonDefaultParam);
  112. if (state.iife) {
  113. body.push((0, _babelHelperCallDelegate2.default)(path, scope));
  114. path.set("body", t.blockStatement(body));
  115. } else {
  116. path.get("body").unshiftContainer("body", body);
  117. }
  118. }
  119. };