1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:07:44 +00:00

LibJS: Implement tagged template literals (foobar)

To make processing tagged template literals easier, template literals
will now add one empty StringLiteral before and after each template
expression *if* there's no other string - e.g.:

`${foo}` -> "", foo, ""
`test${foo}${bar}test` -> "test", foo, "", bar, "test"

This also matches the behaviour of many other parsers.
This commit is contained in:
Linus Groh 2020-05-06 10:17:35 +01:00 committed by Andreas Kling
parent eea62dd365
commit 4d20cf57db
4 changed files with 161 additions and 3 deletions

View file

@ -0,0 +1,88 @@
load("test-common.js");
try {
assertThrowsError(() => {
foo`bar${baz}`;
}, {
error: ReferenceError,
message: "'foo' not known"
});
assertThrowsError(() => {
function foo() { }
foo`bar${baz}`;
}, {
error: ReferenceError,
message: "'baz' not known"
});
assertThrowsError(() => {
undefined``````;
}, {
error: TypeError,
message: "undefined is not a function"
});
function test1(strings) {
assert(strings instanceof Array);
assert(strings.length === 1);
assert(strings[0] === "");
return 42;
}
assert(test1`` === 42);
function test2(s) {
return function (strings) {
assert(strings instanceof Array);
assert(strings.length === 1);
assert(strings[0] === "bar");
return s + strings[0];
}
}
assert(test2("foo")`bar` === "foobar");
var test3 = {
foo(strings, p1) {
assert(strings instanceof Array);
assert(strings.length === 2);
assert(strings[0] === "");
assert(strings[1] === "");
assert(p1 === "bar");
}
};
test3.foo`${"bar"}`;
function test4(strings, p1) {
assert(strings instanceof Array);
assert(strings.length === 2);
assert(strings[0] === "foo");
assert(strings[1] === "");
assert(p1 === 42);
}
var bar = 42;
test4`foo${bar}`;
function test5(strings, p1, p2) {
assert(strings instanceof Array);
assert(strings.length === 3);
assert(strings[0] === "foo");
assert(strings[1] === "baz");
assert(strings[2] === "");
assert(p1 === 42);
assert(p2 === "qux");
return (strings, value) => `${value}${strings[0]}`;
}
var bar = 42;
assert(test5`foo${bar}baz${"qux"}``test${123}` === "123test");
function review(strings, name, rating) {
return `${strings[0]}**${name}**${strings[1]}_${rating}_${strings[2]}`;
}
var name = "SerenityOS";
var rating = "great";
assert(review`${name} is a ${rating} project!` === "**SerenityOS** is a _great_ project!");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}