shadow-utils.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 = exports.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. function collectShadowedParamsNames(param, functionScope, shadowedParams) {
  26. for (const name of Object.keys(param.getBindingIdentifiers())) {
  27. var _functionScope$bindin;
  28. const constantViolations = (_functionScope$bindin = functionScope.bindings[name]) == null ? void 0 : _functionScope$bindin.constantViolations;
  29. if (constantViolations) {
  30. for (const redeclarator of constantViolations) {
  31. const node = redeclarator.node;
  32. switch (node.type) {
  33. case "VariableDeclarator":
  34. {
  35. if (node.init === null) {
  36. const declaration = redeclarator.parentPath;
  37. if (!declaration.parentPath.isFor() || declaration.parentPath.get("body") === declaration) {
  38. redeclarator.remove();
  39. break;
  40. }
  41. }
  42. shadowedParams.add(name);
  43. break;
  44. }
  45. case "FunctionDeclaration":
  46. shadowedParams.add(name);
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. function buildScopeIIFE(shadowedParams, body) {
  54. const args = [];
  55. const params = [];
  56. for (const name of shadowedParams) {
  57. args.push(_core.types.identifier(name));
  58. params.push(_core.types.identifier(name));
  59. }
  60. return _core.types.returnStatement(_core.types.callExpression(_core.types.arrowFunctionExpression(params, body), args));
  61. }
  62. //# sourceMappingURL=shadow-utils.js.map