Skip to content

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

Merged

Conversation

perpetual-cosmos
Copy link
Contributor


Checklist

  • Implemented the no-invalid-entity rule to disallow invalid HTML entities.
  • Added tests for the rule in packages/eslint-plugin/tests/rules/no-invalid-entity.test.js.
  • Added documentation for the rule in docs/rules/no-invalid-entity.md.
  • Added entities.json data file to support entity validation.

Changes

  • Added no-invalid-entity.js rule in packages/eslint-plugin/lib/rules/.
  • Added tests in packages/eslint-plugin/tests/rules/no-invalid-entity.test.js.
  • Added documentation in docs/rules/no-invalid-entity.md.
  • Added entities.json in a new packages/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?
test1
test2

##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!

@yeonjuan yeonjuan changed the title feat:New rule proposal: no-invalid-entity-351 feat: New rule proposal: no-invalid-entity-351 Jun 7, 2025
@yeonjuan yeonjuan requested a review from Copilot June 7, 2025 14:37
Copy link
Contributor

@Copilot Copilot AI left a 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>&lt; &gt; &amp; &nbsp; &#160; &#xA0;</p>

packages/eslint-plugin/tests/rules/no-invalid-entity.test.js:9

  • Add valid test cases for numeric entities (e.g., &#160; and &#xA0;) to ensure both decimal and hexadecimal references are covered.
{ code: "<p>&lt; &gt; &amp; &nbsp;</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, {

Comment on lines 72 to 73
const isHex = entityName.startsWith("x");
const numStr = isHex ? entityName.slice(2) : entityName.slice(1);
Copy link
Preview

Copilot AI Jun 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hexadecimal entities (e.g., &#xA0;) 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');
Suggested change
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.

@yeonjuan yeonjuan changed the title feat: New rule proposal: no-invalid-entity-351 feat: new rule proposal: no-invalid-entity-351 Jun 8, 2025
@yeonjuan
Copy link
Owner

yeonjuan commented Jun 8, 2025

@perpetual-cosmos Thank you for your contribution. Can you please confirm that lint and format are okay before reviewing further?

  • yarn run lint
  • yarn run format

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)

/home/runner/work/html-eslint/html-eslint/packages/eslint-plugin/lib/rules/no-invalid-entity.js
Error:   59:40  error  Irregular whitespace not allowed  no-irregular-whitespace
Error:   70:42  error  Irregular whitespace not allowed  no-irregular-whitespace
Error:   70:47  error  Irregular whitespace not allowed  no-irregular-whitespace

@perpetual-cosmos
Copy link
Contributor Author

Thanks for the heads-up! I've removed the irregular whitespace from the relevant lines and confirmed everything is clean now.
Ran yarn lint and yarn format — both pass ✅
I'm working on the other issues Copilot mentioned. Will update once they're resolved.

Copy link
Owner

@yeonjuan yeonjuan left a 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.

  1. It seems formatting has not been applied. Please run yarn format in the project root directory and commit the changes.

  2. The cspell CI is failing. Please add "nbnb" to the "words" field in .cspell.json.
    You can check the spelling with the yarn check:spell command.

@perpetual-cosmos
Copy link
Contributor Author

Thank you for the guidance.

I've addressed both points:

Ran yarn format at the project root and committed the formatted changes.

Added "nbsb" to the "words" field in .cspell.json to resolve the spell check issue.

@yeonjuan yeonjuan self-requested a review June 10, 2025 19:14
Copy link
Owner

@yeonjuan yeonjuan left a 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.

@yeonjuan yeonjuan merged commit e2a488c into yeonjuan:main Jun 20, 2025
5 checks passed
Copy link

codecov bot commented Jun 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.31%. Comparing base (7c0dc9d) to head (57ae8e6).
Report is 1 commits behind head on main.

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              
Flag Coverage Δ
unittest 98.31% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/eslint-plugin/lib/rules/index.js 100.00% <100.00%> (ø)
...kages/eslint-plugin/lib/rules/no-invalid-entity.js 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yeonjuan
Copy link
Owner

@perpetual-cosmos Thank you for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants