Skip to content

Conversation

coliff
Copy link
Member

@coliff coliff commented Jun 22, 2025

This pull request introduces comprehensive support for custom HTMLHint rules, including examples, documentation, and CLI enhancements. The changes aim to make it easier for users to create, configure, and use custom rules to extend HTMLHint's functionality.

Custom Rule Examples and Implementation:

  • examples/custom-rules/example-rule.js: Added an example custom rule that checks for accessibility attributes on images and validates class names against a configurable pattern. The rule demonstrates integration with the HTML parser and reporter.
  • examples/custom-rules/README.md: Documented the example custom rule, including its purpose, usage, configuration, and testing instructions.

Documentation Updates:

This comment was marked as resolved.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds comprehensive documentation for creating and using custom rules in HTMLHint, including a new documentation page, examples, and updates to CLI and options documentation. The documentation is well-structured and detailed.

My review focuses on improving the correctness and robustness of the provided code examples. The key suggestions are:

  • Using arrow functions in event listeners to ensure the correct this context.
  • Handling rule options more safely, especially for falsy values like 0.
  • Improving performance and robustness in the example rule by caching RegExp objects and handling potential errors.

These changes will ensure the examples follow best practices and are more reliable for users to copy and adapt.

id: 'max-attributes',
description: 'Elements should not have more than the specified number of attributes',
init: function(parser, reporter, options) {
const maxAttrs = options || 5; // Default to 5 if no options provided
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The line const maxAttrs = options || 5; is not robust for handling options. If a user configures "max-attributes": 0 in their .htmlhintrc, options will be 0, and 0 || 5 will evaluate to 5. This is likely not the user's intent.

A safer way to provide a default value is to check the type of options or check specifically for undefined.

      const maxAttrs = typeof options === 'number' ? options : 5; // Default to 5 if no options provided

@coliff coliff merged commit 9ca2b99 into main Jun 22, 2025
15 checks passed
@coliff coliff deleted the dev/coliff/add-custom-rules-docs branch June 22, 2025 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant