shadow-utils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.buildScopeIIFE = buildScopeIIFE;
  6. exports.collectShadowedParamsNames = collectShadowedParamsNames;
  7. exports.iifeVisitor = void 0;
  8. var _core = require("@babel/core");
  9. const iifeVisitor = {
  10. "ReferencedIdentifier|BindingIdentifier"(path, state) {
  11. const {
  12. scope,
  13. node
  14. } = path;
  15. const {
  16. name
  17. } = node;
  18. if (name === "eval" || scope.getBinding(name) === state.scope.parent.getBinding(name) && state.scope.hasOwnBinding(name)) {
  19. state.needsOuterBinding = true;
  20. path.stop();
  21. }
  22. },
  23. "TypeAnnotation|TSTypeAnnotation|TypeParameterDeclaration|TSTypeParameterDeclaration": path => path.skip()
  24. };
  25. exports.iifeVisitor = iifeVisitor;
  26. function collectShadowedParamsNames(param, functionScope, shadowedParams) {
  27. for (const name of Object.keys(param.getBindingIdentifiers())) {
  28. var _functionScope$bindin;
  29. const constantViolations = (_functionScope$bindin = functionScope.bindings[name]) == null ? void 0 : _functionScope$bindin.constantViolations;
  30. if (constantViolations) {
  31. for (const redeclarator of constantViolations) {
  32. const node = redeclarator.node;
  33. switch (node.type) {
  34. case "VariableDeclarator":
  35. {
  36. if (node.init === null) {
  37. const declaration = redeclarator.parentPath;
  38. if (!declaration.parentPath.isFor() || declaration.parentPath.get("body") === declaration) {
  39. redeclarator.remove();
  40. break;
  41. }
  42. }
  43. shadowedParams.add(name);
  44. break;
  45. }
  46. case "FunctionDeclaration":
  47. shadowedParams.add(name);
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. function buildScopeIIFE(shadowedParams, body) {
  55. const args = [];
  56. const params = [];
  57. for (const name of shadowedParams) {
  58. args.push(_core.types.identifier(name));
  59. params.push(_core.types.identifier(name));
  60. }
  61. return _core.types.returnStatement(_core.types.callExpression(_core.types.arrowFunctionExpression(params, body), args));
  62. }
  63. //# sourceMappingURL=shadow-utils.js.map