LibJS: Do not consume "with" tokens in import statements as identifiers

The "with" statement is its own token (TokenType::With), and thus would
fail to parse as an identifier. We've already asserted that the token
we are parsing is "with" or "assert", so just consume it.
This commit is contained in:
Timothy Flynn 2025-01-20 11:43:41 -05:00 committed by Andreas Kling
parent 2c3077d929
commit 47ba231a9b
Notes: github-actions[bot] 2025-01-21 13:59:36 +00:00
3 changed files with 37 additions and 2 deletions

View file

@ -4589,7 +4589,7 @@ ModuleRequest Parser::parse_module_request()
return request;
VERIFY(m_state.current_token.original_value().is_one_of("with"sv, "assert"sv));
consume(TokenType::Identifier);
consume();
consume(TokenType::CurlyOpen);
while (!done() && !match(TokenType::CurlyClose)) {

View file

@ -0,0 +1,2 @@
import json from "./json-module.json" with { type: "json" };
export default json;

View file

@ -1,5 +1,5 @@
describe("basic behavior", () => {
test("can import json modules", () => {
test("can import json modules (with import function)", () => {
let passed = false;
let error = null;
let result = null;
@ -31,4 +31,37 @@ describe("basic behavior", () => {
expect(jsonResult).toHaveProperty("array", [1, 2, 3]);
expect(jsonResult).toHaveProperty("map", { innerValue: "innerValue" });
});
test("can import json modules (with import statement)", () => {
let passed = false;
let error = null;
let result = null;
import("./json-module.mjs")
.then(jsonObj => {
passed = true;
result = jsonObj;
})
.catch(err => {
error = err;
});
runQueuedPromiseJobs();
if (error) throw error;
console.log(JSON.stringify(result));
expect(passed).toBeTrue();
expect(result).not.toBeNull();
expect(result).not.toBeUndefined();
const jsonResult = result.default;
expect(jsonResult).not.toBeNull();
expect(jsonResult).not.toBeUndefined();
expect(jsonResult).toHaveProperty("value", "value");
expect(jsonResult).toHaveProperty("array", [1, 2, 3]);
expect(jsonResult).toHaveProperty("map", { innerValue: "innerValue" });
});
});