-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patha11y-link-in-text-block.js
More file actions
201 lines (180 loc) · 8.03 KB
/
Copy patha11y-link-in-text-block.js
File metadata and controls
201 lines (180 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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: {
skipImportCheck: {
type: 'boolean',
},
},
},
],
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
})
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)
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}>`
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
) {
// Skip if Link has className because we cannot deduce what styles are applied.
const classNameAttribute = getJSXOpeningElementAttribute(node.openingElement, 'className')
if (classNameAttribute) return
let siblings = node.parent.children
const parentName = node.parent.openingElement?.name?.name
// Skip if Link is nested inside of a heading.
const parentsToSkip = ['Heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
if (parentsToSkip.includes(parentName)) return
if (siblings.length > 0) {
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
})
const prevSibling = siblings[index - 1]
const nextSibling = siblings[index + 1]
const prevSiblingIsText = prevSibling && prevSibling.type === 'JSXText'
const nextSiblingIsText = nextSibling && nextSibling.type === 'JSXText'
if (prevSiblingIsText || nextSiblingIsText) {
// Skip if the only text adjacent to the link is a period, then skip it.
if (!prevSiblingIsText && /^\s*\.+\s*$/.test(nextSibling.value)) {
return
}
const sxAttribute = getJSXOpeningElementAttribute(node.openingElement, 'sx')
const inlineAttribute = getJSXOpeningElementAttribute(node.openingElement, 'inline')
// Skip if Link child is a JSX element.
const jsxElementChildren = node.children.filter(child => {
return child.type === 'JSXElement'
})
if (jsxElementChildren.length > 0) return
// Skip if fontWeight or fontFamily is set via the sx prop since these may technically be considered sufficiently distinguishing styles that don't use color.
if (
sxAttribute &&
sxAttribute?.value?.expression &&
sxAttribute.value.expression.type === 'ObjectExpression' &&
sxAttribute.value.expression.properties &&
sxAttribute.value.expression.properties.length > 0
) {
const fontStyleProperty = sxAttribute.value.expression.properties.filter(property => {
return property.key.name === 'fontWeight' || property.key.name === 'fontFamily'
})
if (fontStyleProperty.length > 0) return
}
if (inlineAttribute) {
if (!inlineAttribute.value) {
return
} else if (inlineAttribute.value.type === 'JSXExpressionContainer') {
if (inlineAttribute.value.expression.type === 'Literal') {
if (inlineAttribute.value.expression.value === true) {
return
}
}
}
}
context.report({
node,
messageId: 'linkInTextBlock',
})
}
}
}
},
}
},
}