-
Notifications
You must be signed in to change notification settings - Fork 45
feat: new rule proposal: no-invalid-entity-351 #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: new rule proposal: no-invalid-entity-351 #371
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds a new ESLint rule no-invalid-entity
to prevent invalid HTML entities in templates and text nodes
- Implements detection logic for named and numeric entities against a JSON data source
- Provides accompanying unit tests and integrates the rule into the plugin index
- Includes documentation with usage, though examples need correction
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
packages/eslint-plugin/lib/rules/no-invalid-entity.js | Implements the rule to parse text nodes and report invalid entities |
packages/eslint-plugin/tests/rules/no-invalid-entity.test.js | Adds basic unit tests for named entities; template tests are currently empty |
packages/eslint-plugin/lib/rules/index.js | Imports and registers the new no-invalid-entity rule |
docs/rules/no-invalid-entity.md | Adds documentation and examples for using the rule |
Comments suppressed due to low confidence (3)
docs/rules/no-invalid-entity.md:28
- [nitpick] The 'Incorrect' and 'Correct' examples are identical, which can confuse readers. Update the incorrect sample to include an actual invalid entity (e.g.,
&nbsb;
) so the difference is clear.
<p>< > &    </p>
packages/eslint-plugin/tests/rules/no-invalid-entity.test.js:9
- Add valid test cases for numeric entities (e.g.,
 
and 
) to ensure both decimal and hexadecimal references are covered.
{ code: "<p>< > & </p>" }, // Valid entities
packages/eslint-plugin/tests/rules/no-invalid-entity.test.js:27
- The template parser tests are empty. Either add template-specific valid/invalid cases or remove this block if templates aren’t supported.
templateRuleTester.run("[template] no-invalid-entity", rule, {
const isHex = entityName.startsWith("x"); | ||
const numStr = isHex ? entityName.slice(2) : entityName.slice(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hexadecimal entities (e.g.,  
) aren’t detected correctly because entityName
begins with '#'
, so startsWith("x")
is always false. Strip the leading #
before testing, for example:
const raw = entityName.slice(1).toLowerCase();
const isHex = raw.startsWith('x');
const isHex = entityName.startsWith("x"); | |
const numStr = isHex ? entityName.slice(2) : entityName.slice(1); | |
const rawEntityName = entityName.slice(1).toLowerCase(); | |
const isHex = rawEntityName.startsWith("x"); | |
const numStr = isHex ? rawEntityName.slice(1) : rawEntityName; |
Copilot uses AI. Check for mistakes.
@perpetual-cosmos Thank you for your contribution. Can you please confirm that lint and format are okay before reviewing further?
When viewed in CI, the following lint errors are occurring. This means that an improper whitespace string is present in the code during typing. (https://eslint.org/docs/latest/rules/no-irregular-whitespace)
|
Thanks for the heads-up! I've removed the irregular whitespace from the relevant lines and confirmed everything is clean now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @perpetual-cosmos Before I review the details, I’d like to check these two things first.
-
It seems formatting has not been applied. Please run
yarn format
in the project root directory and commit the changes. -
The cspell CI is failing. Please add "nbnb" to the "words" field in
.cspell.json
.
You can check the spelling with theyarn check:spell
command.
Thank you for the guidance. I've addressed both points:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this, and sorry for the late review.
I've left some comments.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #371 +/- ##
==========================================
+ Coverage 98.29% 98.31% +0.02%
==========================================
Files 79 80 +1
Lines 2517 2548 +31
Branches 687 695 +8
==========================================
+ Hits 2474 2505 +31
Misses 43 43
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
@perpetual-cosmos Thank you for contributing! |
no-invalid-entity
#351Checklist
Changes
no-invalid-entity.js
rule inpackages/eslint-plugin/lib/rules/
.packages/eslint-plugin/tests/rules/no-invalid-entity.test.js
.docs/rules/no-invalid-entity.md
.entities.json
in a newpackages/eslint-plugin/lib/data
folder.Description
This PR introduces the no-invalid-entity rule to the @html-eslint/eslint-plugin package, addressing the feature request to prevent the use of invalid HTML entities in markup. The rule validates both named entities (e.g., ) and numeric entities (e.g., ) against a list of valid entities in entities.json.
##Note


While testing, I encountered a failure in the E2E test tests/e2e/index.test.js (e2e/robertakarobin) due to a mismatch in expected output. This failure is unrelated to the no-invalid-entity rule and may be due to formatting rules or test expectations. Should I attempt to resolve this issue before the PR is merged, or should I create a separate issue/PR for it?
##Thank You!
This is my first contribution to a project like this, and I want to thank the maintainers for their guidance and support throughout the process. It’s been a great learning experience, and I truly appreciate the opportunity to contribute to html-eslint. Thank you so much!