es.regexp.flags.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var global = require('../internals/global');
  2. var DESCRIPTORS = require('../internals/descriptors');
  3. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  4. var regExpFlags = require('../internals/regexp-flags');
  5. var fails = require('../internals/fails');
  6. // babel-minify and Closure Compiler transpiles RegExp('.', 'd') -> /./d and it causes SyntaxError
  7. var RegExp = global.RegExp;
  8. var RegExpPrototype = RegExp.prototype;
  9. var FORCED = DESCRIPTORS && fails(function () {
  10. var INDICES_SUPPORT = true;
  11. try {
  12. RegExp('.', 'd');
  13. } catch (error) {
  14. INDICES_SUPPORT = false;
  15. }
  16. var O = {};
  17. // modern V8 bug
  18. var calls = '';
  19. var expected = INDICES_SUPPORT ? 'dgimsy' : 'gimsy';
  20. var addGetter = function (key, chr) {
  21. // eslint-disable-next-line es/no-object-defineproperty -- safe
  22. Object.defineProperty(O, key, { get: function () {
  23. calls += chr;
  24. return true;
  25. } });
  26. };
  27. var pairs = {
  28. dotAll: 's',
  29. global: 'g',
  30. ignoreCase: 'i',
  31. multiline: 'm',
  32. sticky: 'y'
  33. };
  34. if (INDICES_SUPPORT) pairs.hasIndices = 'd';
  35. for (var key in pairs) addGetter(key, pairs[key]);
  36. // eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
  37. var result = Object.getOwnPropertyDescriptor(RegExpPrototype, 'flags').get.call(O);
  38. return result !== expected || calls !== expected;
  39. });
  40. // `RegExp.prototype.flags` getter
  41. // https://tc39.es/ecma262/#sec-get-regexp.prototype.flags
  42. if (FORCED) defineBuiltInAccessor(RegExpPrototype, 'flags', {
  43. configurable: true,
  44. get: regExpFlags
  45. });