-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
feat: add eslint plugin no-html-links #8156
Merged
Merged
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2c8ff51
feat: add eslint plugin no-html-links
0ec09b0
docs: add docs for no-html-links
6e80680
fix: eslint no-html-links errors
617153a
add rel nofollow to link
695a49a
fix formatting of a tag in md
7434b77
add tests for ignoreFullyResolved
f985e86
add option ignoreFullyIgnore
237d99e
docs: update no-html-links
37e3e0b
Merge branch 'main' into eslint-no-html-links
slorber add5058
Refactor
Josh-Cena f7f6828
Merge branch 'main' into eslint-no-html-links
slorber c3a885b
prettier
slorber d50a497
Update website/docs/api/misc/eslint-plugin/no-html-links.md
slorber 72581f5
add link to <Link> in doc
slorber 41ef7a5
clarify ignoreFullyResolved
slorber bf39059
add more tests + simple template literal handling
slorber 741e6ef
empty
slorber 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
45 changes: 45 additions & 0 deletions
45
packages/eslint-plugin/src/rules/__tests__/no-html-links.test.ts
This file contains 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,45 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import rule from '../no-html-links'; | ||
import {RuleTester} from './testUtils'; | ||
|
||
const errorsJSX = [{messageId: 'link'}] as const; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-docusaurus-link', rule, { | ||
valid: [ | ||
{ | ||
code: '<Link to="/test">test</Link>', | ||
}, | ||
{ | ||
code: '<Link to="https://twitter.com/docusaurus">Twitter</Link>', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: '<a href="/test">test</a>', | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: '<a href="https://twitter.com/docusaurus" target="_blank">test</a>', | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: '<a href="https://twitter.com/docusaurus" target="_blank" rel="noopener noreferrer">test</a>', | ||
errors: errorsJSX, | ||
}, | ||
], | ||
}); |
This file contains 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
This file contains 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,48 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {createRule} from '../util'; | ||
import type {TSESTree} from '@typescript-eslint/types/dist/ts-estree'; | ||
|
||
const docsUrl = 'https://docusaurus.io/docs/docusaurus-core#link'; | ||
|
||
type Options = []; | ||
|
||
type MessageIds = 'link'; | ||
slorber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default createRule<Options, MessageIds>({ | ||
slorber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name: 'no-html-links', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce using Docusaurus Link component instead of <a> tag', | ||
recommended: false, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
link: `Do not use an \`<a>\` element to navigate. Use \`<Link />\` from \`@docusaurus/Link\` instead. See: ${docsUrl}`, | ||
}, | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
if ((node.name as TSESTree.JSXIdentifier).name !== 'a') { | ||
return; | ||
} | ||
|
||
context.report({node, messageId: 'link'}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains 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
This file contains 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
This file contains 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,24 @@ | ||
--- | ||
slug: /api/misc/@docusaurus/eslint-plugin/no-html-links | ||
--- | ||
|
||
# no-html-links | ||
|
||
Ensure that docusaurus `<Link>` is used instead of `<a>` tags. | ||
slorber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Rule Details {#details} | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```html | ||
<a href="/page">go to page!</a> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
// | ||
import Link from '@docusaurus/Link'; | ||
|
||
<Link to="/page">go to page!</Link>; | ||
``` | ||
slorber marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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
This file contains 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
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.
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.
wonder if we could also allow
<a>
for hardcoded hash links?(not all links can be evaluated but hardcoded ones could? Maybe eslint can know it starts with a hash? 🤷♂️)
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.
It's not undoable. Even for template literals, you just need to check
quasis[0][0] === "#"
.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.
Good point!
Lets say that we add this to the plugin, should it still warn (encourage) the user to use
<Link>
instead of the a tag?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.
I guess we can just ignore hashes. This is a bonus, we can merge this PR without this feature if complicated to implement.