Skip to content

Commit e5ef212

Browse files
committed
implement
1 parent 6157a0c commit e5ef212

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

packages/eslint-plugin/lib/rules/use-standard-html/check-content-model.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
const { getElementSpec } = require("html-standard");
1919
const { isTag } = require("../utils/node");
20-
const { shouldIgnoreChild, getNodeName } = require("./helpers");
20+
const {
21+
shouldIgnoreChild,
22+
getNodeName,
23+
getDisplayNodeName,
24+
} = require("./helpers");
2125

2226
const EXIT = false;
2327
const CONTINUE = true;
@@ -117,6 +121,10 @@ function checkContentModel(
117121
if (remain && !contentModel) {
118122
context.report({
119123
node: remain,
124+
data: {
125+
child: getDisplayNodeName(remain),
126+
parent: getDisplayNodeName(node),
127+
},
120128
messageId: MESSAGE_IDS.NOT_ALLOWED,
121129
});
122130
}
@@ -152,6 +160,10 @@ function required(model, context, state, node) {
152160
}
153161
context.report({
154162
node: node.openStart,
163+
data: {
164+
child: getDisplayNodeName(child),
165+
parent: getDisplayNodeName(node),
166+
},
155167
messageId: MESSAGE_IDS.NOT_ALLOWED,
156168
});
157169
return EXIT;

packages/eslint-plugin/lib/rules/use-standard-html/helpers.js

+24
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,34 @@ function getNodeName(node) {
3838
if (isComment(node)) {
3939
return "#comment";
4040
}
41+
return "Unknown";
42+
}
43+
44+
/**
45+
* @param {AnyHTMLNode} node
46+
* @returns {string}
47+
*/
48+
function getDisplayNodeName(node) {
49+
if (isTag(node)) {
50+
return node.name;
51+
}
52+
if (isText(node)) {
53+
return "Text";
54+
}
55+
if (isScript(node)) {
56+
return "script";
57+
}
58+
if (isStyle(node)) {
59+
return "style";
60+
}
61+
if (isComment(node)) {
62+
return "Comment";
63+
}
4164
return "#unknown";
4265
}
4366

4467
module.exports = {
4568
shouldIgnoreChild,
4669
getNodeName,
70+
getDisplayNodeName,
4771
};

packages/eslint-plugin/lib/rules/use-standard-html/use-standard-html.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ module.exports = {
4141
],
4242
messages: {
4343
[MESSAGE_IDS.REQUIRED]: "required",
44-
[MESSAGE_IDS.NOT_ALLOWED]: "not allowed",
44+
[MESSAGE_IDS.NOT_ALLOWED]:
45+
"Element '{{child}}' not allowed as child of element '{{parent}}'",
4546
},
4647
},
4748
create(context) {

packages/eslint-plugin/tests/rules/use-standard-html.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ ruleTester.run("use-standard-html", rule, {
7676
code: `<html><div></div></html>`,
7777
errors: [
7878
{
79-
messageId: "notAllowed",
79+
message: `Element 'div' not allowed as child of element 'html'`,
8080
},
8181
],
8282
},

0 commit comments

Comments
 (0)