parser-line-terminators.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. These tests deliberately produce syntax errors to check what line the parser thinks we're on.
  3. Note that line numbers are higher than you might expect as the parsed code is:
  4. function anonymous(
  5. ) {
  6. <code>
  7. }
  8. ⚠ PLEASE MAKE SURE TO NOT LET YOUR EDITOR REMOVE THE LS/PS LINE TERMINATORS!
  9. */
  10. test("LINE FEED is a line terminator", () => {
  11. expect(() => {
  12. Function("\n\n@");
  13. }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
  14. });
  15. test("CARRIAGE RETURN is a line terminator", () => {
  16. expect(() => {
  17. Function("\r\r@");
  18. }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
  19. });
  20. test("LINE SEPARATOR is a line terminator", () => {
  21. expect(() => {
  22. Function("

@");
  23. }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
  24. });
  25. test("PARAGRAPH SEPARATOR is a line terminator", () => {
  26. expect(() => {
  27. Function("

@");
  28. }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
  29. });
  30. test("CR LF is counted as only one line terminator", () => {
  31. expect(() => {
  32. Function("\r\n\r\n@");
  33. }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
  34. });
  35. test("LF/CR are not allowed in string literal", () => {
  36. expect(() => {
  37. Function(`"
  38. "`);
  39. }).toThrowWithMessage(SyntaxError, "Unexpected token UnterminatedStringLiteral");
  40. });
  41. test("LS/PS are allowed in string literal", () => {
  42. expect(`"
"`).toEval();
  43. expect(`"
"`).toEval();
  44. });
  45. test("line terminators can be mixed (but please don't)", () => {
  46. expect(() => {
  47. Function("\r
\r\n
\n\r@");
  48. }).toThrowWithMessage(SyntaxError, "line: 9, column: 1");
  49. });
  50. test("all line terminators are valid for line continuations", () => {
  51. expect(Function('return "a\\\nb"')()).toBe("ab");
  52. expect(Function('return "a\\\rb"')()).toBe("ab");
  53. expect(Function('return "a\\
b"')()).toBe("ab");
  54. expect(Function('return "a\\
b"')()).toBe("ab");
  55. });