syntax.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*!
  2. {
  3. "name": "ES5 Syntax",
  4. "property": "es5syntax",
  5. "notes": [{
  6. "name": "ECMAScript 5.1 Language Specification",
  7. "href": "https://www.ecma-international.org/ecma-262/5.1/"
  8. }, {
  9. "name": "original implementation of detect code",
  10. "href": "https://kangax.github.io/compat-table/es5/"
  11. }],
  12. "authors": ["Ron Waldon (@jokeyrhyme)"],
  13. "warnings": ["This detect uses `eval()`, so CSP may be a problem."],
  14. "tags": ["es5"]
  15. }
  16. !*/
  17. /* DOC
  18. Check if browser accepts ECMAScript 5 syntax.
  19. */
  20. define(['Modernizr'], function(Modernizr) {
  21. Modernizr.addTest('es5syntax', function() {
  22. var value, obj, stringAccess, getter, setter, reservedWords, zeroWidthChars;
  23. try {
  24. /* eslint no-eval: "off" */
  25. // Property access on strings
  26. stringAccess = eval('"foobar"[3] === "b"');
  27. // Getter in property initializer
  28. getter = eval('({ get x(){ return 1 } }).x === 1');
  29. eval('({ set x(v){ value = v; } }).x = 1');
  30. // Setter in property initializer
  31. setter = value === 1;
  32. // Reserved words as property names
  33. eval('obj = ({ if: 1 })');
  34. reservedWords = obj['if'] === 1;
  35. // Zero-width characters in identifiers
  36. zeroWidthChars = eval('_\u200c\u200d = true');
  37. return stringAccess && getter && setter && reservedWords && zeroWidthChars;
  38. } catch (ignore) {
  39. return false;
  40. }
  41. });
  42. });