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

LibJS: Parse slashes after reserved identifiers correctly

Previously we were unable to parse code like `yield/2` because `/2`
was parsed as a regex. At the same time `for (a in / b/)` was parsed
as a division.

This is solved by defaulting to division in the lexer, but calling
`force_slash_as_regex()` from the parser whenever an IdentifierName
is parsed as a ReservedWord.
This commit is contained in:
Simon Wanner 2023-05-28 00:08:52 +02:00 committed by Andreas Kling
parent b0bd1e5eb5
commit a2efecac03
6 changed files with 65 additions and 25 deletions

View file

@ -210,6 +210,12 @@ describe("in- and exports", () => {
test("can have top level using declarations which trigger at the end of running a module", () => {
expectModulePassed("./top-level-dispose.mjs");
});
test("can export default a RegExp", () => {
const result = expectModulePassed("./default-regexp-export.mjs");
expect(result.default).toBeInstanceOf(RegExp);
expect(result.default.toString()).toBe(/foo/.toString());
});
});
describe("loops", () => {

View file

@ -0,0 +1,3 @@
export default /foo/;
export let passed = true;

View file

@ -19,7 +19,30 @@ test("slash token resolution in lexer", () => {
expect("a.instanceof / b").toEval();
expect("class A { #name; d = a.#name / b; }").toEval();
// FIXME: Even more 'reserved' words are valid however the cases below do still need to pass.
//expect("a.void / b").toEval();
expect("async / b").toEval();
expect("a.delete / b").toEval();
expect("delete / b/").toEval();
expect("a.in / b").toEval();
expect("for (a in / b/) {}").toEval();
expect("a.instanceof / b").toEval();
expect("a instanceof / b/").toEval();
expect("new / b/").toEval();
expect("null / b").toEval();
expect("for (a of / b/) {}").toEval();
expect("a.return / b").toEval();
expect("function foo() { return / b/ }").toEval();
expect("throw / b/").toEval();
expect("a.typeof / b").toEval();
expect("a.void / b").toEval();
expect("void / b/").toEval();
expect("await / b").toEval();
expect("await / b/").not.toEval();
expect("async function foo() { await / b }").not.toEval();
expect("async function foo() { await / b/ }").toEval();
expect("yield / b").toEval();
expect("yield / b/").not.toEval();
expect("function* foo() { yield / b }").not.toEval();
expect("function* foo() { yield / b/ }").toEval();
});