diff options
author | Linus Groh <mail@linusgroh.de> | 2020-10-24 13:30:57 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-24 16:34:01 +0200 |
commit | 4fb96afafc6e87b75d2e310f949b0ca7b337b050 (patch) | |
tree | e654afea4bc7dd536d3d0b46182a3ed477e57fc2 /Libraries/LibJS/Tests | |
parent | 9f036959e867e52bbec12a384e00f21ee0a07be2 (diff) | |
download | serenity-4fb96afafc6e87b75d2e310f949b0ca7b337b050.zip |
LibJS: Support LegacyOctalEscapeSequence in string literals
https://tc39.es/ecma262/#sec-additional-syntax-string-literals
The syntax and semantics of 11.8.4 is extended as follows except that
this extension is not allowed for strict mode code:
Syntax
EscapeSequence::
CharacterEscapeSequence
LegacyOctalEscapeSequence
NonOctalDecimalEscapeSequence
HexEscapeSequence
UnicodeEscapeSequence
LegacyOctalEscapeSequence::
OctalDigit [lookahead ∉ OctalDigit]
ZeroToThree OctalDigit [lookahead ∉ OctalDigit]
FourToSeven OctalDigit
ZeroToThree OctalDigit OctalDigit
ZeroToThree :: one of
0 1 2 3
FourToSeven :: one of
4 5 6 7
NonOctalDecimalEscapeSequence :: one of
8 9
This definition of EscapeSequence is not used in strict mode or when
parsing TemplateCharacter.
Note
It is possible for string literals to precede a Use Strict Directive
that places the enclosing code in strict mode, and implementations must
take care to not use this extended definition of EscapeSequence with
such literals. For example, attempting to parse the following source
text must fail:
function invalid() { "\7"; "use strict"; }
Diffstat (limited to 'Libraries/LibJS/Tests')
-rw-r--r-- | Libraries/LibJS/Tests/string-escapes.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/string-escapes.js b/Libraries/LibJS/Tests/string-escapes.js index e4abf9ec07..d8c75e9055 100644 --- a/Libraries/LibJS/Tests/string-escapes.js +++ b/Libraries/LibJS/Tests/string-escapes.js @@ -13,3 +13,32 @@ test("unicode escapes", () => { expect(`\u{1f41e}`).toBe("🐞"); expect("\u00ff").toBe(String.fromCharCode(0xff)); }); + +describe("octal escapes", () => { + test("basic functionality", () => { + expect("\1").toBe("\u0001"); + expect("\2").toBe("\u0002"); + expect("\3").toBe("\u0003"); + expect("\4").toBe("\u0004"); + expect("\5").toBe("\u0005"); + expect("\6").toBe("\u0006"); + expect("\7").toBe("\u0007"); + expect("\8").toBe("8"); + expect("\9").toBe("9"); + expect("\128").toBe("\n8"); + expect("\141bc").toBe("abc"); + expect("f\157o\142a\162").toBe("foobar"); + expect("\123\145\162\145\156\151\164\171\117\123").toBe("SerenityOS"); + }); + + test("syntax error in template literal", () => { + expect("`\\123`").not.toEval(); + }); + + test("syntax error in strict mode", () => { + expect("'use strict'; '\\123'").not.toEval(); + expect('"use strict"; "\\123"').not.toEval(); + // Special case, string literal precedes use strict directive + expect("'\\123'; somethingElse; 'use strict'").not.toEval(); + }); +}); |