Skip to content

Commit ebbb607

Browse files
committed
fix: improve no-html rule's tag location reporting
1 parent 614ef3e commit ebbb607

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

src/rules/no-html.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { findOffsets } from "../util.js";
2424
// Helpers
2525
//-----------------------------------------------------------------------------
2626

27-
const htmlTagPattern = /<([a-z0-9]+(?:-[a-z0-9]+)*)/giu;
27+
const htmlTagPattern = /<([a-z0-9]+(?:-[a-z0-9]+)*)[^>]*>/giu;
2828

2929
//-----------------------------------------------------------------------------
3030
// Rule Definition
@@ -75,6 +75,7 @@ export default {
7575
let match;
7676

7777
while ((match = htmlTagPattern.exec(node.value)) !== null) {
78+
const fullMatch = match[0];
7879
const tagName = match[1];
7980
const { lineOffset, columnOffset } = findOffsets(
8081
node.value,
@@ -84,9 +85,16 @@ export default {
8485
line: node.position.start.line + lineOffset,
8586
column: node.position.start.column + columnOffset,
8687
};
88+
89+
const firstLineEnd = fullMatch.indexOf("\n");
90+
const endColumn =
91+
firstLineEnd === -1
92+
? start.column + fullMatch.length
93+
: start.column + firstLineEnd;
94+
8795
const end = {
8896
line: start.line,
89-
column: start.column + match[0].length + 1,
97+
column: endColumn,
9098
};
9199

92100
if (allowed.size === 0 || !allowed.has(tagName)) {

tests/rules/no-html.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,112 @@ ruleTester.run("no-html", rule, {
130130
},
131131
],
132132
},
133+
{
134+
code: "<span >Content</span>",
135+
errors: [
136+
{
137+
messageId: "disallowedElement",
138+
line: 1,
139+
column: 1,
140+
endLine: 1,
141+
endColumn: 8,
142+
data: { name: "span" },
143+
},
144+
],
145+
},
146+
{
147+
code: '<div id="foo">Some content</div>',
148+
errors: [
149+
{
150+
messageId: "disallowedElement",
151+
line: 1,
152+
column: 1,
153+
endLine: 1,
154+
endColumn: 15,
155+
data: {
156+
name: "div",
157+
},
158+
},
159+
],
160+
},
161+
{
162+
code: '<p class="text-center" data-attr="value">Content</p>',
163+
errors: [
164+
{
165+
messageId: "disallowedElement",
166+
line: 1,
167+
column: 1,
168+
endLine: 1,
169+
endColumn: 42,
170+
data: {
171+
name: "p",
172+
},
173+
},
174+
],
175+
},
176+
{
177+
code: '<span\nclass="highlight"\ndata-id="123">Text</span>',
178+
errors: [
179+
{
180+
messageId: "disallowedElement",
181+
line: 1,
182+
column: 1,
183+
endLine: 1,
184+
endColumn: 6,
185+
data: {
186+
name: "span",
187+
},
188+
},
189+
],
190+
},
191+
{
192+
code: '<div class="test" >Content</div>',
193+
errors: [
194+
{
195+
messageId: "disallowedElement",
196+
line: 1,
197+
column: 1,
198+
endLine: 1,
199+
endColumn: 24,
200+
data: { name: "div" },
201+
},
202+
],
203+
},
204+
{
205+
code: '<input type="text" placeholder="Enter text" />',
206+
errors: [
207+
{
208+
messageId: "disallowedElement",
209+
line: 1,
210+
column: 1,
211+
endLine: 1,
212+
endColumn: 47,
213+
data: {
214+
name: "input",
215+
},
216+
},
217+
],
218+
},
219+
{
220+
code: '<a href="#"><span class="highlight">Link</span></a>',
221+
errors: [
222+
{
223+
messageId: "disallowedElement",
224+
line: 1,
225+
column: 1,
226+
endLine: 1,
227+
endColumn: 13,
228+
data: { name: "a" },
229+
},
230+
{
231+
messageId: "disallowedElement",
232+
line: 1,
233+
column: 13,
234+
endLine: 1,
235+
endColumn: 37,
236+
data: { name: "span" },
237+
},
238+
],
239+
},
133240
],
134241
});

0 commit comments

Comments
 (0)