mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:37:36 +00:00
LibJS: Allow invalid string in tagged template literals
Since tagged template literals can inspect the raw string it is not a syntax error to have invalid escapes. However the cooked value should be `undefined`. We accomplish this by tracking whether parse_string_literal fails and then using a NullLiteral (since UndefinedLiteral is not a thing) and finally converting null in tagged template execution to undefined.
This commit is contained in:
parent
0f9434a02c
commit
e5adc51e27
5 changed files with 67 additions and 8 deletions
|
@ -105,4 +105,29 @@ describe("tagged template literal functionality", () => {
|
|||
expect(raw[1]).toHaveLength(5);
|
||||
expect(raw[1]).toBe("\\nbar");
|
||||
});
|
||||
|
||||
test("invalid escapes give undefined cooked values but can be accesed in raw form", () => {
|
||||
let calls = 0;
|
||||
let lastValue = null;
|
||||
function noCookedButRaw(values) {
|
||||
++calls;
|
||||
expect(values).not.toBeNull();
|
||||
expect(values.raw).toHaveLength(1);
|
||||
expect(values.raw[0].length).toBeGreaterThan(0);
|
||||
expect(values.raw[0].charAt(0)).toBe("\\");
|
||||
expect(values[0]).toBeUndefined();
|
||||
lastValue = values.raw[0];
|
||||
}
|
||||
noCookedButRaw`\u`;
|
||||
expect(calls).toBe(1);
|
||||
expect(lastValue).toBe("\\u");
|
||||
|
||||
noCookedButRaw`\01`;
|
||||
expect(calls).toBe(2);
|
||||
expect(lastValue).toBe("\\01");
|
||||
|
||||
noCookedButRaw`\u{10FFFFF}`;
|
||||
expect(calls).toBe(3);
|
||||
expect(lastValue).toBe("\\u{10FFFFF}");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -63,3 +63,9 @@ test("line continuation in literals (not characters)", () => {
|
|||
test("reference error from expressions", () => {
|
||||
expect(() => `${b}`).toThrowWithMessage(ReferenceError, "'b' is not defined");
|
||||
});
|
||||
|
||||
test("invalid escapes should give syntax error", () => {
|
||||
expect("`\\u`").not.toEval();
|
||||
expect("`\\01`").not.toEval();
|
||||
expect("`\\u{10FFFFF}`").not.toEval();
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue