with.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* eslint no-restricted-syntax: 0, no-with: 0, strict: 0 */
  2. var test = require('tape');
  3. var shimUnscopables = require('../');
  4. test('`with` statement', { skip: typeof Symbol !== 'function' || !Symbol.unscopables }, function (t) {
  5. // @ts-expect-error this variable is declared in case unscopables doesn't work
  6. var entries;
  7. // @ts-expect-error this variable is declared in case unscopables doesn't work
  8. var concat;
  9. // @ts-expect-error `with` unsupported
  10. with ([]) {
  11. t.equal(concat, Array.prototype.concat, 'concat is dynamically bound');
  12. t.notEqual(entries, Array.prototype.entries, 'entries is not dynamically bound');
  13. }
  14. /** @type {Record<PropertyKey, unknown>} */
  15. var obj = {
  16. foo: 1,
  17. bar: 2
  18. };
  19. // @ts-expect-error this variable is declared in case unscopables doesn't work
  20. var foo;
  21. // @ts-expect-error this variable is declared in case unscopables doesn't work
  22. var bar;
  23. obj[Symbol.unscopables] = { foo: true };
  24. // @ts-expect-error `with` unsupported
  25. with (obj) {
  26. t.equal(foo, undefined);
  27. t.equal(bar, obj.bar);
  28. }
  29. shimUnscopables('concat');
  30. // @ts-expect-error `with` unsupported
  31. with ([]) {
  32. t.notEqual(concat, Array.prototype.concat, 'concat is no longer dynamically bound');
  33. t.notEqual(entries, Array.prototype.entries, 'entries is still not dynamically bound');
  34. }
  35. t.end();
  36. });