Skip to content

Commit ec30c7d

Browse files
authored
fix: report locations in no-multiple-h1 and require-alt-text (#551)
* fix: report locations in `no-multiple-h1` and require-alt-text * update tests to avoid problems with dedent in Bun * fix note
1 parent 71bc53e commit ec30c7d

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

β€Žsrc/util.jsβ€Ž

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ export function frontmatterHasTitle(value, pattern) {
7171
}
7272

7373
/**
74-
* Remove all HTML comments from a string.
74+
* Replaces all HTML comments with whitespace.
75+
* This preserves offsets and locations of characters
76+
* outside HTML comments by keeping line breaks and replacing
77+
* other code units with a space character.
7578
* @param {string} value The string to remove HTML comments from.
7679
* @returns {string} The string with HTML comments removed.
7780
*/
7881
export function stripHtmlComments(value) {
7982
return value.replace(htmlCommentPattern, match =>
80-
match.replace(/[^\n]/gu, " "),
83+
/* eslint-disable-next-line require-unicode-regexp
84+
-- we want to replace each code unit with a space
85+
*/
86+
match.replace(/[^\r\n]/g, " "),
8187
);
8288
}

β€Žtests/rules/no-multiple-h1.test.jsβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,5 +1202,18 @@ ruleTester.run("no-multiple-h1", rule, {
12021202
},
12031203
],
12041204
},
1205+
{
1206+
// NOTE: dedent`` converts πŸ‘πŸš€ to \u{1f44d}\u{1f680} in Bun, causing unexpected report locations
1207+
code: "<h1>Heading 1</h1>\n<!-- comment with surrogate pairs: πŸ‘πŸš€ --> <h1>Another H1</h1>",
1208+
errors: [
1209+
{
1210+
messageId: "multipleH1",
1211+
line: 2,
1212+
column: 45,
1213+
endLine: 2,
1214+
endColumn: 64,
1215+
},
1216+
],
1217+
},
12051218
],
12061219
});

β€Žtests/rules/require-alt-text.test.jsβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,18 @@ ruleTester.run("require-alt-text", rule, {
309309
},
310310
],
311311
},
312+
{
313+
// NOTE: dedent`` converts πŸ‘πŸš€ to \u{1f44d}\u{1f680} in Bun, causing unexpected report locations
314+
code: '<!-- comment with surrogate pairs: πŸ‘πŸš€ --> <img src="image.png" />',
315+
errors: [
316+
{
317+
messageId: "altTextRequired",
318+
line: 1,
319+
column: 45,
320+
endLine: 1,
321+
endColumn: 68,
322+
},
323+
],
324+
},
312325
],
313326
});

0 commit comments

Comments
Β (0)