Skip to content

Commit f6041d7

Browse files
authored
fix(lint): preserve opening-tag source bounds after multiline closes (#3917)
Capture explicit source name bounds before attributes are parsed. Preserve malformed names, Unicode lowercase expansion, and existing implied-tag ranges while retaining malformed-attribute lint failures.
1 parent dc6d78b commit f6041d7

3 files changed

Lines changed: 125 additions & 4 deletions

File tree

‎packages/lint/src/rules/core.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,30 @@ describe("core rules", () => {
962962
).toBeUndefined();
963963
});
964964

965+
it.each([
966+
["adjacent", `<span>A</span><span>B</span>`],
967+
["spaces", `<span>A</span ><span>B</span>`],
968+
["newline", `<span>A</span\n ><span>B</span>`],
969+
])(
970+
"does not flag valid sibling spans when the closing tag uses %s whitespace",
971+
async (_label, body) => {
972+
const result = await lintHyperframeHtml(compositionWithBodyPrefix(body));
973+
expect(
974+
result.findings.find((f) => f.code === "unclosed_tag_swallowed_element"),
975+
).toBeUndefined();
976+
},
977+
);
978+
979+
it.each([`<span class="first" <span>B</span>`, `<span data-label=first <strong>B</strong>`])(
980+
"still flags a malformed span start tag that swallows its next element",
981+
async (body) => {
982+
const result = await lintHyperframeHtml(compositionWithBodyPrefix(body));
983+
const finding = result.findings.find((f) => f.code === "unclosed_tag_swallowed_element");
984+
expect(finding?.severity).toBe("error");
985+
expect(finding?.snippet).toContain("<span");
986+
},
987+
);
988+
965989
it("does not flag a legitimate attribute value containing a raw <", async () => {
966990
const html = compositionWithBodyPrefix(`<div data-expr="x < y">hi</div>`);
967991
const result = await lintHyperframeHtml(html);

‎packages/lint/src/utils.test.ts‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,81 @@
11
import { describe, it, expect } from "vitest";
2-
import { stripCssComments, stripJsComments, stripJsStringLiterals } from "./utils.js";
2+
import {
3+
parseHtmlStructure,
4+
stripCssComments,
5+
stripJsComments,
6+
stripJsStringLiterals,
7+
} from "./utils.js";
38

49
const scan = (src: string) => stripJsStringLiterals(stripJsComments(src));
510
const findsRaf = (src: string) => /requestAnimationFrame\s*\(/.test(scan(src));
611

12+
describe("parseHtmlStructure source ranges", () => {
13+
it("does not include ignored markup inside a preceding malformed closing tag", () => {
14+
const source = "<p>x</p ignored<noise><span>y</span>";
15+
const tags = parseHtmlStructure(source).tags;
16+
expect(tags[1]).toMatchObject({ name: "span", raw: "<span>", attrs: "", index: 22 });
17+
});
18+
19+
it("keeps a less-than inside a malformed tag name in the original source range", () => {
20+
const tags = parseHtmlStructure("<div<foo>body</div<foo>").tags;
21+
expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([
22+
{ name: "div<foo", raw: "<div<foo>", attrs: "", index: 0 },
23+
]);
24+
});
25+
26+
it("uses the original name boundary when Unicode lowercasing changes its length", () => {
27+
const source = '<Aİİİ data-check="<" data-flag=x>body</Aİİİ>';
28+
const tags = parseHtmlStructure(source).tags;
29+
expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([
30+
{
31+
name: "ai̇i̇i̇",
32+
raw: '<Aİİİ data-check="<" data-flag=x>',
33+
attrs: ' data-check="<" data-flag=x',
34+
index: 0,
35+
},
36+
]);
37+
});
38+
39+
it("keeps malformed names and quoted less-than values after a multiline close", () => {
40+
const source = '<p>x</p\n ><div<foo data-expr="a < b">y</div<foo>';
41+
const tags = parseHtmlStructure(source).tags;
42+
expect(tags[1]).toMatchObject({
43+
name: "div<foo",
44+
raw: '<div<foo data-expr="a < b">',
45+
attrs: ' data-expr="a < b"',
46+
index: 11,
47+
});
48+
});
49+
50+
it("does not reuse implied-open origins for the following explicit tag", () => {
51+
const source = '</p></br><Aİ data-expr="x < y">text</Aİ>';
52+
const tags = parseHtmlStructure(source).tags;
53+
expect(tags.map(({ name, raw, attrs, index }) => ({ name, raw, attrs, index }))).toEqual([
54+
{ name: "p", raw: "</p>", attrs: "p", index: 0 },
55+
{ name: "br", raw: "</br>", attrs: "r", index: 4 },
56+
{ name: "ai̇", raw: '<Aİ data-expr="x < y">', attrs: ' data-expr="x < y"', index: 9 },
57+
]);
58+
});
59+
60+
it("does not use apparent tags in comments or raw-text scripts as an opening origin", () => {
61+
const source = '<!-- <fake> --><script>const text = "<fake>";</script\n ><div<foo>x</div<foo>';
62+
const tags = parseHtmlStructure(source).tags;
63+
expect(tags.map(({ name, raw, attrs }) => ({ name, raw, attrs }))).toEqual([
64+
{ name: "script", raw: "<script>", attrs: "" },
65+
{ name: "div<foo", raw: "<div<foo>", attrs: "" },
66+
]);
67+
});
68+
69+
it("starts each open tag at its own < after a multiline closing tag", () => {
70+
const source = `<span data-expr="x < y">A</span\n ><span>B</span>`;
71+
const tags = parseHtmlStructure(source).tags;
72+
expect(tags.map(({ raw, index }) => ({ raw, index }))).toEqual([
73+
{ raw: `<span data-expr="x < y">`, index: 0 },
74+
{ raw: `<span>`, index: source.indexOf("<span>") },
75+
]);
76+
});
77+
});
78+
779
describe("stripJsStringLiterals", () => {
880
it("blanks a call the composition only renders as text", () => {
981
expect(findsRaf('const CODE = "requestAnimationFrame(step);";')).toBe(false);

‎packages/lint/src/utils.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,37 @@ export function parseHtmlStructure(source: string): {
6969
contentStart: number;
7070
index: number;
7171
}> = [];
72+
let explicitOpenTag: { index: number; nameEnd: number } | null = null;
7273
const parser: Parser = new Parser(
7374
{
74-
onopentag(name) {
75-
const index = parser.startIndex;
75+
onopentagname(name) {
76+
// startIndex can still point into the preceding close. Bound this scan by
77+
// HTML name delimiters, not '<' (which can occur in a malformed name).
78+
// Keep the raw name end too: Unicode lowercasing can change UTF-16 length.
79+
let tokenStart = parser.endIndex - 1;
80+
while (
81+
tokenStart >= parser.startIndex &&
82+
!/[\t\n\f\r />]/.test(source.charAt(tokenStart))
83+
) {
84+
tokenStart -= 1;
85+
}
86+
const index = source.indexOf("<", tokenStart + 1);
87+
explicitOpenTag =
88+
index >= 0 &&
89+
index < parser.endIndex &&
90+
source.slice(index + 1, parser.endIndex).toLowerCase() === name
91+
? { index, nameEnd: parser.endIndex }
92+
: null;
93+
},
94+
onopentag(name, _attrs, isImplied) {
95+
const origin = !isImplied ? explicitOpenTag : null;
96+
const index = origin?.index ?? parser.startIndex;
97+
explicitOpenTag = null;
7698
const raw = source.slice(index, parser.endIndex + 1);
77-
const attrs = raw.slice(name.length + 1, -1).replace(/\s*\/$/, "");
99+
const rawAttrs = origin
100+
? source.slice(origin.nameEnd, parser.endIndex)
101+
: raw.slice(name.length + 1, -1);
102+
const attrs = rawAttrs.replace(/\s*\/$/, "");
78103
const tag = { raw, name, attrs, index };
79104
tags.push(tag);
80105
const sameNameStack = openTagsByName.get(name) ?? [];

0 commit comments

Comments
 (0)