-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/rules/__tests__/enforce-button-for-link-with-nohref.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
'Links without href and other side effects are not accessible. Use a Button instead.', | ||
}, | ||
fixable: 'code', | ||
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. 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. Positive FeedbackNegative Feedback |
||
}, | ||
|
||
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', | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.