-
Notifications
You must be signed in to change notification settings - Fork 11
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 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comparing arrays with
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
}) | ||||||||||||
|
||||||||||||
const prevSibling = siblings[index - 1] | ||||||||||||
const nextSibling = siblings[index + 1] | ||||||||||||
|
||||||||||||
const prevSiblingIsText = prevSibling && prevSibling.type === 'JSXText' | ||||||||||||
const nextSiblingIsText = nextSibling && nextSibling.type === 'JSXText' | ||||||||||||
|
||||||||||||
// If there's text on either side | ||||||||||||
if (prevSiblingIsText || nextSiblingIsText) { | ||||||||||||
// Skip if the only text adjacent to the link is a period | ||||||||||||
if (!prevSiblingIsText && /^\s*\.+\s*$/.test(nextSibling.value)) { | ||||||||||||
return false | ||||||||||||
} | ||||||||||||
return true | ||||||||||||
} | ||||||||||||
|
||||||||||||
return false | ||||||||||||
} | ||||||||||||
|
||||||||||||
return { | ||||||||||||
JSXElement(node) { | ||||||||||||
const name = getJSXOpeningElementName(node.openingElement) | ||||||||||||
if ( | ||||||||||||
const parentName = node.parent.openingElement?.name?.name | ||||||||||||
const parentsToSkip = ['Heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'] | ||||||||||||
|
||||||||||||
// Check for HTML anchor elements | ||||||||||||
if (isHTMLElement(node.openingElement) && name === 'a' && node.parent.children) { | ||||||||||||
// Skip if anchor is nested inside of a heading | ||||||||||||
if (parentsToSkip.includes(parentName)) return | ||||||||||||
|
||||||||||||
// Skip if anchor has className (might have distinguishing styles) | ||||||||||||
const classNameAttribute = getJSXOpeningElementAttribute(node.openingElement, 'className') | ||||||||||||
if (classNameAttribute) return | ||||||||||||
|
||||||||||||
// Skip if anchor has an ID (might have distinguishing styles) | ||||||||||||
const idAttribute = getJSXOpeningElementAttribute(node.openingElement, 'id') | ||||||||||||
if (idAttribute) return | ||||||||||||
|
||||||||||||
// Check for anchor in text block | ||||||||||||
if (isNodeInTextBlock(node)) { | ||||||||||||
// Skip if anchor child is a JSX element | ||||||||||||
const jsxElementChildren = node.children.filter(child => child.type === 'JSXElement') | ||||||||||||
if (jsxElementChildren.length > 0) return | ||||||||||||
|
||||||||||||
// Report and autofix | ||||||||||||
context.report({ | ||||||||||||
node, | ||||||||||||
messageId: 'htmlAnchorInTextBlock', | ||||||||||||
fix(fixer) { | ||||||||||||
// Get all attributes from the anchor to transfer to Link | ||||||||||||
const attributes = node.openingElement.attributes.map(attr => sourceCode.getText(attr)).join(' ') | ||||||||||||
|
||||||||||||
// Create the Link component opening and closing tags | ||||||||||||
const openingTag = `<Link ${attributes}>` | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When converting HTML anchors to
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
const closingTag = '</Link>' | ||||||||||||
|
||||||||||||
// Apply fixes to the opening and closing tags | ||||||||||||
const openingFix = fixer.replaceText(node.openingElement, openingTag) | ||||||||||||
const closingFix = fixer.replaceText(node.closingElement, closingTag) | ||||||||||||
|
||||||||||||
return [openingFix, closingFix] | ||||||||||||
}, | ||||||||||||
}) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
// Check for Primer Link component | ||||||||||||
else if ( | ||||||||||||
isPrimerComponent(node.openingElement.name, sourceCode.getScope(node)) && | ||||||||||||
name === 'Link' && | ||||||||||||
node.parent.children | ||||||||||||
|
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 anid
attribute are skipped by the rule, since the code filters onid
similarly toclassName
.Copilot uses AI. Check for mistakes.