-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add eslint plugin no-html-links (#8156)
Co-authored-by: Joshua Chen <[email protected]> Co-authored-by: Viktor Malmedal <[email protected]> Co-authored-by: sebastienlorber <[email protected]> Co-authored-by: Sébastien Lorber <[email protected]>
- Loading branch information
1 parent
81f30dd
commit 4a44877
Showing
23 changed files
with
291 additions
and
67 deletions.
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
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
90 changes: 90 additions & 0 deletions
90
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,90 @@ | ||
/** | ||
* 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>', | ||
}, | ||
{ | ||
code: '<a href="https://twitter.com/docusaurus">Twitter</a>', | ||
options: [{ignoreFullyResolved: true}], | ||
}, | ||
{ | ||
code: '<a href={`https://twitter.com/docusaurus`}>Twitter</a>', | ||
options: [{ignoreFullyResolved: true}], | ||
}, | ||
{ | ||
code: '<a href="mailto:[email protected]">Contact</a> ', | ||
options: [{ignoreFullyResolved: true}], | ||
}, | ||
{ | ||
code: '<a href="tel:123456789">Call</a>', | ||
options: [{ignoreFullyResolved: true}], | ||
}, | ||
], | ||
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, | ||
}, | ||
{ | ||
code: '<a href="mailto:[email protected]">Contact</a> ', | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: '<a href="tel:123456789">Call</a>', | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: '<a href={``}>Twitter</a>', | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: '<a href={`https://www.twitter.com/docusaurus`}>Twitter</a>', | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
code: '<a href="www.twitter.com/docusaurus">Twitter</a>', | ||
options: [{ignoreFullyResolved: true}], | ||
errors: errorsJSX, | ||
}, | ||
{ | ||
// TODO we might want to make this test pass | ||
// Can template literals be statically pre-evaluated? (Babel can do it) | ||
// eslint-disable-next-line no-template-curly-in-string | ||
code: '<a href={`https://twitter.com/${"docu" + "saurus"} ${"rex"}`}>Twitter</a>', | ||
options: [{ignoreFullyResolved: true}], | ||
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,103 @@ | ||
/** | ||
* 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 = [ | ||
{ | ||
ignoreFullyResolved: boolean; | ||
}, | ||
]; | ||
|
||
type MessageIds = 'link'; | ||
|
||
function isFullyResolvedUrl(urlString: string): boolean { | ||
try { | ||
// href gets coerced to a string when it gets rendered anyway | ||
const url = new URL(String(urlString)); | ||
if (url.protocol) { | ||
return true; | ||
} | ||
} catch (e) {} | ||
return false; | ||
} | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'no-html-links', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce using Docusaurus Link component instead of <a> tag', | ||
recommended: false, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
ignoreFullyResolved: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
link: `Do not use an \`<a>\` element to navigate. Use the \`<Link />\` component from \`@docusaurus/Link\` instead. See: ${docsUrl}`, | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
ignoreFullyResolved: false, | ||
}, | ||
], | ||
|
||
create(context, [options]) { | ||
const {ignoreFullyResolved} = options; | ||
|
||
return { | ||
JSXOpeningElement(node) { | ||
if ((node.name as TSESTree.JSXIdentifier).name !== 'a') { | ||
return; | ||
} | ||
|
||
if (ignoreFullyResolved) { | ||
const hrefAttr = node.attributes.find( | ||
(attr): attr is TSESTree.JSXAttribute => | ||
attr.type === 'JSXAttribute' && attr.name.name === 'href', | ||
); | ||
|
||
if (hrefAttr?.value?.type === 'Literal') { | ||
if (isFullyResolvedUrl(String(hrefAttr.value.value))) { | ||
return; | ||
} | ||
} | ||
if (hrefAttr?.value?.type === 'JSXExpressionContainer') { | ||
const container: TSESTree.JSXExpressionContainer = hrefAttr.value; | ||
const {expression} = container; | ||
if (expression.type === 'TemplateLiteral') { | ||
// Simple static string template literals | ||
if ( | ||
expression.expressions.length === 0 && | ||
expression.quasis.length === 1 && | ||
expression.quasis[0]?.type === 'TemplateElement' && | ||
isFullyResolvedUrl(String(expression.quasis[0].value.raw)) | ||
) { | ||
return; | ||
} | ||
// TODO add more complex TemplateLiteral cases here | ||
} | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.