Skip to content

Add rule for Link to not be allowed without href #364

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

Merged
merged 4 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/rules/__tests__/enforce-button-for-link-with-nohref.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const rule = require('../enforce-button-for-link-with-nohref')
const {RuleTester} = require('eslint')

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
})

ruleTester.run('enforce-button-for-link-with-nohref', rule, {
valid: [
// Link with href attribute
`import {Link} from '@primer/react';
<Link href="https://example.com">Valid Link</Link>`,

// Link with href and inline prop
`import {Link} from '@primer/react';
<Link inline href="https://example.com">Valid Inline Link</Link>`,

// Link with href and className
`import {Link} from '@primer/react';
<Link className="some-class" href="https://example.com">Valid Link with Class</Link>`,

// Link with href, inline, and className
`import {Link} from '@primer/react';
<Link className="some-class" inline href="https://example.com">Valid Inline Link with Class</Link>`,

// Link with href as variable
`import {Link} from '@primer/react';
const url = '/about';
<Link href={url}>About</Link>`,

// Button component (not Link)
`import {Button} from '@primer/react';
<Button onClick={handleClick}>Click me</Button>`,

// Regular HTML link (not Primer Link)
`<a onClick={handleClick}>Click me</a>`,

// Link from different package
`import {Link} from 'react-router-dom';
<Link to="/about">About</Link>`,
],
invalid: [
{
code: `import {Link} from '@primer/react';
<Link>Invalid Link without href</Link>`,
errors: [
{
messageId: 'noLinkWithoutHref',
},
],
},
{
code: `import {Link} from '@primer/react';
<Link className="some-class">Invalid Link with class but no href</Link>`,
errors: [
{
messageId: 'noLinkWithoutHref',
},
],
},
{
code: `import {Link} from '@primer/react';
<Link inline>Invalid inline Link without href</Link>`,
errors: [
{
messageId: 'noLinkWithoutHref',
},
],
},
{
code: `import {Link} from '@primer/react';
<Link onClick={handleClick}>Invalid Link with onClick but no href</Link>`,
errors: [
{
messageId: 'noLinkWithoutHref',
},
],
},
],
})
43 changes: 43 additions & 0 deletions src/rules/enforce-button-for-link-with-nohref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const url = require('../url')
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
const {isPrimerComponent} = require('../utils/is-primer-component')

module.exports = {
meta: {
type: 'error',
docs: {
description: 'Disallow usage of Link component without href',
recommended: true,
url: url(module),
},
messages: {
noLinkWithoutHref:

Check failure on line 15 in src/rules/enforce-button-for-link-with-nohref.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎·······`

This comment was marked as resolved.

'Links without href and other side effects are not accessible. Use a Button instead.',
},
fixable: 'code',
Copy link
Preview

Copilot AI Jul 8, 2025

Choose a reason for hiding this comment

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

The rule is marked as fixable but no fix function is implemented. Either implement the fix logic or remove the 'fixable' property.

Copilot uses AI. Check for mistakes.

},

create(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode()
return {
JSXElement(node) {
const openingElement = node.openingElement
const elementName = getJSXOpeningElementName(openingElement)

// Check if this is a Link component from @primer/react
if (elementName === 'Link' && isPrimerComponent(openingElement.name, sourceCode.getScope(node))) {
// Check if the Link has an href attribute
const hrefAttribute = getJSXOpeningElementAttribute(openingElement, 'href')

if (!hrefAttribute) {
context.report({
node: openingElement,
messageId: 'noLinkWithoutHref',
})
}
}
},
}
},
}
Loading