Skip to content

Commit

Permalink
Passing tests, update configs
Browse files Browse the repository at this point in the history
  • Loading branch information
khiga8 committed May 20, 2024
1 parent c815ec1 commit d41e2cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
'primer-react/new-color-css-vars': 'error',
'primer-react/a11y-explicit-heading': 'error',
'primer-react/no-deprecated-props': 'warn',
'primer-react/a11y-link-in-text-block': 'error',
},
settings: {
github: {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'new-color-css-vars': require('./rules/new-color-css-vars'),
'a11y-explicit-heading': require('./rules/a11y-explicit-heading'),
'no-deprecated-props': require('./rules/no-deprecated-props'),
'a11y-link-in-text-block': require('./rules/a11y-link-in-text-block'),
},
configs: {
recommended: require('./configs/recommended'),
Expand Down
10 changes: 8 additions & 2 deletions src/rules/__tests__/a11y-link-in-text-block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@ ruleTester.run('a11y-link-in-text-block', rule, {
`,
errors: [{messageId: 'linkInTextBlock'}],
},
{
code: `import {Link} from '@primer/react';
<p>bla blah<Link inline={false}>Link level 1</Link></p>
`,
errors: [{messageId: 'linkInTextBlock'}],
},
{
code: `import {Link} from '@primer/react';
<Box>Something something{' '}
<Link>Link level 1</Link>
<Link>Link level 1</Link>
</Box>
`,
errors: [{messageId: 'linkInTextBlock'}],
},
{
code: `import {Link} from '@primer/react';
<><blah blah blah{' '}
<>blah blah blah{' '}
<Link>Link level 1</Link></>;
`,
errors: [{messageId: 'linkInTextBlock'}],
Expand Down
12 changes: 10 additions & 2 deletions src/rules/a11y-link-in-text-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ module.exports = {
},
],
messages: {
linkInTextBlock: 'Heading must have an explicit heading level applied through the `as` prop.',
linkInTextBlock: '<Link> that are used within a text block should have the inline prop.',
},
},
create(context) {
return {
JSXElement(node) {
const name = getJSXOpeningElementName(node.openingElement)
if (isPrimerComponent(node.openingElement.name, context.getScope(node)) && name === 'Link') {
const siblings = node.parent.children
let siblings = node.parent.children
siblings = siblings.filter(childNode => {
return !(
(childNode.type === 'JSXExpressionContainer' &&
childNode.expression.type === 'Literal' &&
/^\s+$/.test(childNode.expression.raw)) ||
(childNode.type === 'Literal' && /^\s+$/.test(childNode.raw))
)
})
if (siblings.length > 0) {
const index = siblings.findIndex(childNode => {
return childNode.range === node.range
Expand Down

0 comments on commit d41e2cb

Please sign in to comment.