8000 feat: add auto-fix suggestion for require-img-alt rule by simplicityf · Pull Request #366 · yeonjuan/html-eslint · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add auto-fix suggestion for require-img-alt rule #366

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
merged 10 commits into from
Jun 7, 2025

Conversation

simplicityf
Copy link
Contributor
@simplicityf simplicityf commented Jun 2, 2025

Checklist

Description

Enhances the require-img-alt rule by adding an auto-fix suggestion that inserts an empty alt="" attribute when elements are missing the alt attribute.

Changes Made

  • Added auto-fix suggestion: When an tag lacks an alt attribute, the rule now provides a suggestion to insert alt=""
    Enhanced error reporting: Uses ESLint's suggest property to offer quick fixes
    Comprehensive test coverage: Added test cases covering both standalone HTML and template literal contexts

  • Test Cases
    Before (Invalid):
    html
    After (Suggested Fix):
    html

  • Features
    Works with self-closing and regular tags
    Supports substitute attributes (configurable via options)
    Compatible with template literals
    Maintains existing functionality while adding helpful auto-fix suggestions

  • Implementation Details
    Uses fixer.insertTextBefore(node.openEnd, ' alt="..."') to insert the attribute
    Preserves existing validation logic
    Follows ESLint's suggestion API patterns
    Includes proper TypeScript definitions and JSDoc comments

** This enhancement improves developer experience by providing quick fixes for accessibility issues while maintaining the rule's core functionality.

@simplicityf
Copy link
Contributor Author

@yeonjuan, please review

@yeonjuan yeonjuan requested review from Copilot and yeonjuan and removed request for Copilot June 2, 2025 12:09
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

This pull request enhances the require-img-alt rule by adding an auto-fix suggestion that inserts an empty alt attribute for tags missing one. Key changes include the addition of a fixer suggestion in the rule, updates to test cases to verify the auto-fix, and modifications to the rule's meta to enable suggestions.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/eslint-plugin/tests/rules/require-img-alt.test.js Updated test cases to incorporate auto-fix suggestion and adjusted string formatting for template literals
packages/eslint-plugin/lib/rules/require-img-alt.js Modified rule metadata to reflect suggestion type and added a new suggestion to insert an empty alt attribute

@yeonjuan
Copy link
Owner
yeonjuan commented Jun 2, 2025

Hi @simplicityf Thanks for your contribution. The format check in CI did not pass.
Please run yarn format and commit the changed files.

@simplicityf
Copy link
Contributor Author

@yeonjuan , what changes are you suggesting?? I don't understand you

Have you check the recent commit?

@yeonjuan
Copy link
Owner
yeonjuan commented Jun 4, 2025

@simplicityf

what changes are you suggesting??

#366 (comment)

Have you check the recent commit?

Yes I saw your latest commit.
The require-img-alt rule includes a substitute option (see line 55). This option allows specifying alternative attributes that can serve as substitutes for the alt attribute. If an attribute matching the substitute option is present, the rule assumes that the alt attribute is effectively provided.

I propose that no suggestion auto-fix should be offered when the substitute option is specified.

            suggest: substitute.length ? null : [
              {
                messageId: MESSAGE_IDS.INSERT_EMPTY,
                fix(fixer) {
                  return fixer.insertTextBefore(node.openEnd, ' alt=""');
                },
              },
            ],

@simplicityf
Copy link
Contributor Author

@simplicityf

what changes are you suggesting??

#366 (comment)

Have you check the recent commit?

Yes I saw your latest commit. The require-img-alt rule includes a substitute option (see line 55). This option allows specifying alternative attributes that can serve as substitutes for the alt attribute. If an attribute matching the substitute option is present, the rule assumes that the alt attribute is effectively provided.

I propose that no suggestion auto-fix should be offered when the substitute option is specified.

            suggest: substitute.length ? null : [
              {
                messageId: MESSAGE_IDS.INSERT_EMPTY,
                fix(fixer) {
                  return fixer.insertTextBefore(node.openEnd, ' alt=""');
                },
              },
            ],

@yeonjuan, please check now I have made the changes, it aligns well with the rules now.

@simplicityf
Copy link
Contributor Author

@yeonjuan, I am waiting for your review

@simplicityf
Copy link
Contributor Author

@yeonjuan confirm the changes please

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.

Thanks for the quick reflection. Overall, it looks like we need to clean up the code for the change.

@simplicityf
Copy link
Contributor Author

@yeonjuan confirm the changes please

@yeonjuan
Copy link
Owner
yeonjuan commented Jun 6, 2025

@simplicityf PR hasn't been updated, could you please push the commit?

@simplicityf
Copy link
Contributor Author

@yeonjuan

@@ -45,6 +49,8 @@ module.exports = {
],
messages: {
[MESSAGE_IDS.MISSING_ALT]: "Missing `alt` attribute at `<img>` tag",
[MESSAGE_IDS.EMPTY_ALT]: "Empty `alt` attribute at `<img>` tag",
Copy link
Owner

Choose a reason for hiding this comment

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

@simplicityf It should be removed as unused.

Copy link
Owner

Choose a reason for hiding this comment

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

@simplicityf This comment hasn't been reflected in the PR.

@@ -13,23 +13,27 @@ const { getRuleUrl } = require("./utils/rule");

const MESSAGE_IDS = {
MISSING_ALT: "missingAlt",
EMPTY_ALT: "emptyAlt",
Copy link
Owner

Choose a reason for hiding this comment

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

@simplicityf It should be removed as unused.

@simplicityf
Copy link
Contributor Author

@yeonjuan. I son't think i remeber the provious description i used. If you can point that out

@simplicityf
Copy link
Contributor Author

@yeonjuan

@simplicityf
Copy link
Contributor Author

@yeonjuan

1 similar comment
@simplicityf
Copy link
Contributor Author

@yeonjuan

@yeonjuan
Copy link
Owner
yeonjuan commented Jun 7, 2025

@simplicityf
Copy link
Contributor Author

@yeonjuan, check the latest commit

The code works, likewise the test case. I don't think I have the time for any changes again. Thanks :)

@yeonjuan
Copy link
Owner
yeonjuan commented Jun 7, 2025

@simplicityf Okay, thanks for the work. In addition to the behavior, I think code cleanup is also important for maintaining the project. Maybe you can work on it later when you have time, or someone else can take over this task. Thanks again!

@simplicityf
Copy link
Contributor Author

@yeonjuan

@@ -77,16 +96,22 @@ module.exports = {
/**
* @param {Tag} node
* @param {string[]} substitute
* @returns
* @returns {boolean}}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
* @returns {boolean}}
* @returns {boolean}

cc @simplicityf

@@ -13,14 +13,16 @@ const { getRuleUrl } = require("./utils/rule");

const MESSAGE_IDS = {
MISSING_ALT: "missingAlt",
EMPTY_ALT: "emptyAlt",
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
EMPTY_ALT: "emptyAlt",

cc @simplicityf

@@ -45,6 +48,8 @@ module.exports = {
],
messages: {
[MESSAGE_IDS.MISSING_ALT]: "Missing `alt` attribute at `<img>` tag",
[MESSAGE_IDS.EMPTY_ALT]: "Empty `alt` attribute at `<img>` tag",
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
[MESSAGE_IDS.EMPTY_ALT]: "Empty `alt` attribute at `<img>` tag",

cc @simplicityf

@simplicityf
Copy link
Contributor Author

@yeonjuan

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.

LGTM! @simplicityf Thanks for working on this.

@yeonjuan yeonjuan merged commit 85b8630 into yeonjuan:main Jun 7, 2025
4 of 5 checks passed
Copy link
codecov bot commented Jun 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.25%. Comparing base (5ba8c89) to head (892a7cf).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #366      +/-   ##
==========================================
+ Coverage   98.22%   98.25%   +0.03%     
==========================================
  Files          77       78       +1     
  Lines        2419     2466      +47     
  Branches      660      672      +12     
==========================================
+ Hits         2376     2423      +47     
  Misses         43       43              
Flag Coverage Δ
unittest 98.25% <100.00%> (+0.03%) ⬆️

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

Files with missing lines Coverage Δ
...ackages/eslint-plugin/lib/rules/require-img-alt.js 100.00% <100.00%> (+4.34%) ⬆️

... and 2 files with indirect coverage changes

🚀 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.

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.

[FEATURE] Add suggestion fixer to require-img-alt rule
2 participants
0