scope.js 878 B

123456789101112131415161718192021222324
  1. import { createEmptyMap, assign } from './map.js';
  2. /**
  3. * Create a new scope which can access the parent scope,
  4. * but does not affect it when written. This is suitable for variable definitions
  5. * within a block node, or function definition.
  6. *
  7. * If parent scope has a createSubScope method, it delegates to that. Otherwise,
  8. * creates an empty map, and copies the parent scope to it, adding in
  9. * the remaining `args`.
  10. *
  11. * @param {Map} parentScope
  12. * @param {...any} args
  13. * @returns {Map}
  14. */
  15. export function createSubScope(parentScope) {
  16. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  17. args[_key - 1] = arguments[_key];
  18. }
  19. if (typeof parentScope.createSubScope === 'function') {
  20. return assign(parentScope.createSubScope(), ...args);
  21. }
  22. return assign(createEmptyMap(), parentScope, ...args);
  23. }