unicode-identifier-escape.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. test("basic escapes", () => {
  2. var foo = {};
  3. foo.brown = 12389;
  4. expect(foo.brown).toBe(12389);
  5. expect(foo.br\u006fwn).toBe(12389);
  6. expect(foo.br\u{6f}wn).toBe(12389);
  7. expect(foo.\u{62}\u{72}\u{6f}\u{77}\u{6e}).toBe(12389);
  8. });
  9. test("non-ascii escapes", () => {
  10. var foo = {};
  11. foo.𝓑𝓻𝓸𝔀𝓷 = 12389;
  12. expect(foo.𝓑𝓻𝓸𝔀𝓷).toBe(12389);
  13. expect(foo.𝓑𝓻\u{1d4f8}𝔀𝓷).toBe(12389);
  14. expect(foo.\u{1d4d1}\u{1d4fb}\u{1d4f8}\u{1d500}\u{1d4f7}).toBe(12389);
  15. // U-16 High surrogate pair is allowed in string but not in identifier.
  16. expect("foo.𝓑𝓻\ud835\udcf8𝔀𝓷").toEval();
  17. expect("foo.𝓑𝓻\\ud835\\udcf8𝔀𝓷").not.toEval();
  18. });
  19. describe("escaped keywords", () => {
  20. // We must double escape the slashes here else the strings already convert
  21. // the escaped characters (and string is more lenient).
  22. test("keywords cannot be used in an escaped form", () => {
  23. expect("\\u{69}\\u{66}(true) throw 'Should fail'").not.toEval();
  24. expect("wh\\u{69}le(true) throw 'Should fail'").not.toEval();
  25. expect("l\\u{65}t a = 3;").not.toEval();
  26. expect("function *G(){ yiel\\0064 3; }").not.toEval();
  27. });
  28. test("escaped keywords cannot be used as standalone variables", () => {
  29. expect("var fu\\u{6e}ction = 4").not.toEval();
  30. expect("var \\u0077ith = 4").not.toEval();
  31. });
  32. test("'yield' and 'let' can be escaped as variables", () => {
  33. var l\u{65}t = 3;
  34. var yi\u0065ld = 5;
  35. expect(let).toBe(3);
  36. expect(yield).toBe(5);
  37. });
  38. test("'let' cannot be used in a lexical declaration but 'yield' can", () => {
  39. expect("const l\\u{65}t = 3;").not.toEval();
  40. const yi\u0065ld = 5;
  41. expect(yield).toBe(5);
  42. });
  43. test("escaped 'yield' and 'let' variables are not allowed in strict mode", () => {
  44. expect("function f() { 'use strict'; var l\\u{65}t = 3; }").not.toEval();
  45. expect("function g() { 'use strict'; var yi\u0065ld = 5; }").not.toEval();
  46. });
  47. test("cannot use escaped 'yield' variable or label in generator context", () => {
  48. expect("function *g() { var yi\u0065ld = 5; }").not.toEval();
  49. expect("function *g() { yi\u0065ld: 5; }").not.toEval();
  50. });
  51. test("can use escaped 'let' variable and label in generator context", () => {
  52. expect("function *i() { var \\u{6c}et = 6; }").toEval();
  53. expect("function *j() { \\u{6c}et: 6; }").toEval();
  54. });
  55. test("can use keywords in some contexts", () => {
  56. var obj = {
  57. \u{69}\u{66}: 3,
  58. wh\u{69}le() {
  59. return 4;
  60. },
  61. ca\u0073e: "case",
  62. get true() {
  63. return false;
  64. },
  65. };
  66. expect(obj.\u{69}f).toBe(3);
  67. expect(obj.whi\u{6c}e()).toBe(4);
  68. expect(obj.\u{63}ase).toBe("case");
  69. expect(obj.\u0074r\u{0000075}e).toBeFalse();
  70. });
  71. });