rsnext/test/unit/eslint-plugin-next/no-page-custom-font.test.ts
Joost De Cock 34dcede690
fix(eslint-plugin-next): Broken links in eslint output (#32837)
This fixes broken links in the eslint output by removing the trailing full stop.
It also makes the formatting of (the output of) the various rules consistent.

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`

> I don't think this is a bug, nor a feature, nor is it really documentation. 
> It's just a small nuisance that I bumped into and felt compelled to fix.
> I went with documentation as that seems the closest match

## What does this pull request do?

The elslint output of `eslint-plugin-next` contains useful links to the documentation about the various rules.
Unfortunately, on most (but not all) rules, those links are immediately followed by a full stop (`.`).

The terminal (or any parser) has no way of knowing that the full stop is not part of the URL. 
So it includes it and clicking the link leads to a 404 on the nextjs.org website.
 
![eslint](https://user-images.githubusercontent.com/1708494/147452577-43ad4ce7-df75-4d48-ab78-70b9b8212b7e.png)

This PR fixes that by removing the full stop. 

## But a final full stop is better grammar

I considered alternatives (such as [a zero-width space character](https://en.wikipedia.org/wiki/Zero-width_space#Prohibited_in_URLs)) in case the final full stop was part of the style guide or something.

However, as I went through the eslint rules, I notices that the messages for various rules were formatted inconsistently. 
Some with final full stop, some without.

As such, I made the all consistent with this structure:

> [message]. See: [url]

I feel this is a better solution than using the zero-width space as these sort of invisible characters 
in code can be a red flag that something fishy is going on.

I submit this pull request in the hope it will be useful, and a positive contribution to a project I have a great deal of appreciation for.
That being said, I fully understand if people would consider this a non-issue.
2021-12-28 02:18:39 +00:00

200 lines
4.6 KiB
TypeScript

import rule from '@next/eslint-plugin-next/lib/rules/no-page-custom-font'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})
const ruleTester = new RuleTester()
const filename = 'pages/_document.js'
ruleTester.run('no-page-custom-font', rule, {
valid: [
{
code: `import Document, { Html, Head } from "next/document";
class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
);
}
}
export default MyDocument;`,
filename,
},
{
code: `import NextDocument, { Html, Head } from "next/document";
class Document extends NextDocument {
render() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
);
}
}
export default Document;
`,
filename,
},
{
code: `export default function CustomDocument() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
)
}`,
filename,
},
{
code: `function CustomDocument() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
)
}
export default CustomDocument;
`,
filename,
},
{
code: `
import Document, { Html, Head } from "next/document";
class MyDocument {
render() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
);
}
}
export default MyDocument;`,
filename,
},
{
code: `export default function() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
)
}`,
filename,
},
],
invalid: [
{
code: `
import Head from 'next/head'
export default function IndexPage() {
return (
<div>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Inter"
rel="stylesheet"
/>
</Head>
<p>Hello world!</p>
</div>
)
}
`,
filename: 'pages/index.tsx',
errors: [
{
message:
'Custom fonts not added at the document level will only load for a single page. This is discouraged. See: https://nextjs.org/docs/messages/no-page-custom-font',
type: 'JSXOpeningElement',
},
],
},
{
code: `
import Head from 'next/head'
function Links() {
return (
<>
<link
href="https://fonts.googleapis.com/css2?family=Inter"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans"
rel="stylesheet"
/>
</>
)
}
export default function IndexPage() {
return (
<div>
<Head>
<Links />
</Head>
<p>Hello world!</p>
</div>
)
}
`,
filename,
errors: [
{
message:
'Rendering this <link /> not inline within <Head> of Document disables font optimization. This is discouraged. See: https://nextjs.org/docs/messages/no-page-custom-font',
},
{
message:
'Rendering this <link /> not inline within <Head> of Document disables font optimization. This is discouraged. See: https://nextjs.org/docs/messages/no-page-custom-font',
},
],
},
],
})