-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'eslint-plugin-primer-react': minor | ||
| --- | ||
|
|
||
| Detect HTML anchor elements (`<a>`) in `a11y-link-in-text-block` rule |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,15 @@ | ||||||||||||
| const {isPrimerComponent} = require('../utils/is-primer-component') | ||||||||||||
| const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name') | ||||||||||||
| const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute') | ||||||||||||
| const {isHTMLElement} = require('../utils/is-html-element') | ||||||||||||
|
|
||||||||||||
| module.exports = { | ||||||||||||
| meta: { | ||||||||||||
| docs: { | ||||||||||||
| url: require('../url')(module), | ||||||||||||
| }, | ||||||||||||
| type: 'problem', | ||||||||||||
| fixable: 'code', | ||||||||||||
| schema: [ | ||||||||||||
| { | ||||||||||||
| properties: { | ||||||||||||
|
|
@@ -20,14 +22,101 @@ module.exports = { | |||||||||||
| messages: { | ||||||||||||
| linkInTextBlock: | ||||||||||||
| 'Links should have the inline prop if it appear in a text block and only uses color to distinguish itself from surrounding text.', | ||||||||||||
| htmlAnchorInTextBlock: | ||||||||||||
| 'HTML anchor elements in text blocks should use the Link component from @primer/react instead.', | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| create(context) { | ||||||||||||
| const sourceCode = context.sourceCode ?? context.getSourceCode() | ||||||||||||
|
|
||||||||||||
| // Helper function to check if a node is in a text block | ||||||||||||
| const isNodeInTextBlock = node => { | ||||||||||||
| let siblings = node.parent.children | ||||||||||||
| if (!siblings || siblings.length === 0) return false | ||||||||||||
|
|
||||||||||||
| // Filter out whitespace nodes | ||||||||||||
| siblings = siblings.filter(childNode => { | ||||||||||||
| return ( | ||||||||||||
| !(childNode.type === 'JSXText' && /^\s+$/.test(childNode.value)) && | ||||||||||||
| !( | ||||||||||||
| childNode.type === 'JSXExpressionContainer' && | ||||||||||||
| childNode.expression.type === 'Literal' && | ||||||||||||
| /^\s+$/.test(childNode.expression.value) | ||||||||||||
| ) && | ||||||||||||
| !(childNode.type === 'Literal' && /^\s+$/.test(childNode.value)) | ||||||||||||
| ) | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
| const index = siblings.findIndex(childNode => { | ||||||||||||
| return childNode.range === node.range | ||||||||||||
|
||||||||||||
| return childNode.range === node.range | |
| return ( | |
| childNode.range[0] === node.range[0] && | |
| childNode.range[1] === node.range[1] | |
| ) |
Copilot
AI
May 23, 2025
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.
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}>).
| const openingTag = `<Link ${attributes}>` | |
| const openingTag = `<Link inline ${attributes}>` |
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.
Documentation should also mention that
<a>elements with anidattribute are skipped by the rule, since the code filters onidsimilarly toclassName.