fix(parser): uncaught mismatch between JSX opening/closing tags (#14528)

This commit is contained in:
Don Isaac
2024-10-12 22:49:45 -04:00
committed by GitHub
parent 6b8fd718c2
commit 09b031d044
5 changed files with 33 additions and 2 deletions

View File

@@ -530,7 +530,8 @@ const JSXTag = struct {
};
data: Data,
range: logger.Range,
name: string = "",
/// Empty string for fragments.
name: string,
pub fn parse(comptime P: type, p: *P) anyerror!JSXTag {
const loc = p.lexer.loc();
@@ -559,6 +560,7 @@ const JSXTag = struct {
.data = name,
}, loc) },
.range = tag_range,
.name = name,
};
}
@@ -15778,7 +15780,7 @@ fn NewParser_(
const end_tag = try JSXTag.parse(P, p);
if (!strings.eql(end_tag.name, tag.name)) {
try p.log.addRangeErrorFmt(p.source, end_tag.range, p.allocator, "Expected closing tag \\</{s}> to match opening tag \\<{s}>", .{
try p.log.addRangeErrorFmt(p.source, end_tag.range, p.allocator, "Expected closing tag \\</{s}\\> to match opening tag \\<{s}\\>", .{
end_tag.name,
tag.name,
});

View File

@@ -0,0 +1,23 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { join } from "path";
import fs from "fs";
test("JSXElement with mismatched closing tags produces a syntax error", async () => {
const files = await fs.promises.readdir(import.meta.dir);
const fixtures = files.filter(file => !file.endsWith(".test.ts")).map(fixture => join(import.meta.dir, fixture));
const bakery = fixtures.map(
fixture =>
Bun.spawn({
cmd: [bunExe(), fixture],
cwd: import.meta.dir,
stdio: ["inherit", "inherit", "inherit"],
env: bunEnv,
}).exited,
);
// all subprocesses should fail.
const exited = await Promise.all(bakery);
expect(exited).toEqual(Array.from({ length: fixtures.length }, () => 1));
});

View File

@@ -0,0 +1 @@
console.log(<div></p>);

View File

@@ -0,0 +1,2 @@
console.log(<Foo></Bar>);

View File

@@ -0,0 +1,3 @@
// mismatch where openening tag is not a valid IdentifierName, but is a valid
// JSXIdentifierName
console.log(<div-:button></p>);