73b473991c
Follow-up on https://github.com/vercel/next.js/pull/38534#pullrequestreview-1035020450 This lets us use modern JS/TS syntax in ESLint rules and avoid issues like #38530 and #36693 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
200 lines
4.6 KiB
TypeScript
200 lines
4.6 KiB
TypeScript
import rule from '@next/eslint-plugin-next/dist/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 in `pages/_document.js` 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:
|
|
'Using `<link />` outside of `<Head>` will disable automatic font optimization. This is discouraged. See: https://nextjs.org/docs/messages/no-page-custom-font',
|
|
},
|
|
{
|
|
message:
|
|
'Using `<link />` outside of `<Head>` will disable automatic font optimization. This is discouraged. See: https://nextjs.org/docs/messages/no-page-custom-font',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|