babel-plugin-safe-bind.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Modified the original to use a global `safeCall`:
  3. https://babeljs.io/docs/en/babel-plugin-proposal-function-bind
  4. MIT License
  5. Copyright (c) 2014-present Sebastian McKenzie and other contributors
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. "use strict";
  24. Object.defineProperty(exports, "__esModule", {
  25. value: true
  26. });
  27. exports.default = void 0;
  28. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  29. var _pluginSyntaxFunctionBind = require("@babel/plugin-syntax-function-bind");
  30. var _core = require("@babel/core");
  31. var _default = (0, _helperPluginUtils.declare)(api => {
  32. api.assertVersion(7);
  33. function getTempId(scope) {
  34. let id = scope.path.getData("functionBind");
  35. if (id) return _core.types.cloneNode(id);
  36. id = scope.generateDeclaredUidIdentifier("context");
  37. return scope.path.setData("functionBind", id);
  38. }
  39. function getStaticContext(bind, scope) {
  40. const object = bind.object || bind.callee.object;
  41. return scope.isStatic(object) && (_core.types.isSuper(object) ? _core.types.thisExpression() : object);
  42. }
  43. function inferBindContext(bind, scope) {
  44. const staticContext = getStaticContext(bind, scope);
  45. if (staticContext) return _core.types.cloneNode(staticContext);
  46. const tempId = getTempId(scope);
  47. if (bind.object) {
  48. bind.callee = _core.types.sequenceExpression([_core.types.assignmentExpression("=", tempId, bind.object), bind.callee]);
  49. } else {
  50. bind.callee.object = _core.types.assignmentExpression("=", tempId, bind.callee.object);
  51. }
  52. return _core.types.cloneNode(tempId);
  53. }
  54. return {
  55. name: "safe-function-bind",
  56. inherits: _pluginSyntaxFunctionBind.default,
  57. visitor: {
  58. CallExpression({
  59. node,
  60. scope
  61. }) {
  62. const bind = node.callee;
  63. if (!_core.types.isBindExpression(bind)) return;
  64. // ORIGINAL:
  65. // const context = inferBindContext(bind, scope);
  66. // node.callee = _core.types.memberExpression(bind.callee, _core.types.identifier("call"));
  67. // node.arguments.unshift(context);
  68. // MODIFIED to use safeCall created in safe-globals.js:
  69. const object = bind.object || bind.callee.object;
  70. const context = scope.isStatic(object) && _core.types.isSuper(object)
  71. ? _core.types.thisExpression()
  72. : object;
  73. node.callee = _core.types.identifier("safeCall");
  74. node.arguments.unshift(bind.callee, _core.types.cloneNode(context));
  75. },
  76. BindExpression(path) {
  77. const {
  78. node,
  79. scope
  80. } = path;
  81. const context = inferBindContext(node, scope);
  82. path.replaceWith(_core.types.callExpression(_core.types.memberExpression(node.callee, _core.types.identifier("bind")), [context]));
  83. }
  84. }
  85. };
  86. });
  87. exports.default = _default;