34dcede690
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.
159 lines
4.9 KiB
JavaScript
159 lines
4.9 KiB
JavaScript
const NodeAttributes = require('../utils/node-attributes.js')
|
|
const { sep, posix } = require('path')
|
|
|
|
module.exports = {
|
|
meta: {
|
|
docs: {
|
|
description:
|
|
'Recommend adding custom font in a custom document and not in a specific page',
|
|
recommended: true,
|
|
url: 'https://nextjs.org/docs/messages/no-page-custom-font',
|
|
},
|
|
},
|
|
create: function (context) {
|
|
const paths = context.getFilename().split('pages')
|
|
const page = paths[paths.length - 1]
|
|
|
|
// outside of a file within `pages`, bail
|
|
if (!page) {
|
|
return {}
|
|
}
|
|
|
|
const is_Document =
|
|
page.startsWith(`${sep}_document`) ||
|
|
page.startsWith(`${posix.sep}_document`)
|
|
|
|
let documentImportName
|
|
let localDefaultExportId
|
|
let exportDeclarationType
|
|
|
|
return {
|
|
ImportDeclaration(node) {
|
|
if (node.source.value === 'next/document') {
|
|
const documentImport = node.specifiers.find(
|
|
({ type }) => type === 'ImportDefaultSpecifier'
|
|
)
|
|
if (documentImport && documentImport.local) {
|
|
documentImportName = documentImport.local.name
|
|
}
|
|
}
|
|
},
|
|
|
|
ExportDefaultDeclaration(node) {
|
|
exportDeclarationType = node.declaration.type
|
|
|
|
if (node.declaration.type === 'FunctionDeclaration') {
|
|
localDefaultExportId = node.declaration.id
|
|
return
|
|
}
|
|
|
|
if (
|
|
node.declaration.type === 'ClassDeclaration' &&
|
|
node.declaration.superClass &&
|
|
node.declaration.superClass.name === documentImportName
|
|
) {
|
|
localDefaultExportId = node.declaration.id
|
|
}
|
|
},
|
|
|
|
JSXOpeningElement(node) {
|
|
if (node.name.name !== 'link') {
|
|
return
|
|
}
|
|
|
|
const ancestors = context.getAncestors()
|
|
|
|
// if `export default <name>` is further down within the file after the
|
|
// currently traversed component, then `localDefaultExportName` will
|
|
// still be undefined
|
|
if (!localDefaultExportId) {
|
|
// find the top level of the module
|
|
const program = ancestors.find(
|
|
(ancestor) => ancestor.type === 'Program'
|
|
)
|
|
|
|
// go over each token to find the combination of `export default <name>`
|
|
for (let i = 0; i <= program.tokens.length - 1; i++) {
|
|
if (localDefaultExportId) {
|
|
break
|
|
}
|
|
|
|
const token = program.tokens[i]
|
|
|
|
if (token.type === 'Keyword' && token.value === 'export') {
|
|
const nextToken = program.tokens[i + 1]
|
|
|
|
if (
|
|
nextToken &&
|
|
nextToken.type === 'Keyword' &&
|
|
nextToken.value === 'default'
|
|
) {
|
|
const maybeIdentifier = program.tokens[i + 2]
|
|
|
|
if (maybeIdentifier && maybeIdentifier.type === 'Identifier') {
|
|
localDefaultExportId = { name: maybeIdentifier.value }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const parentComponent = ancestors.find((ancestor) => {
|
|
// export default class ... extends ...
|
|
if (exportDeclarationType === 'ClassDeclaration') {
|
|
return (
|
|
ancestor.type === exportDeclarationType &&
|
|
ancestor.superClass &&
|
|
ancestor.superClass.name === documentImportName
|
|
)
|
|
}
|
|
|
|
// export default function ...
|
|
if (exportDeclarationType === 'FunctionDeclaration') {
|
|
return (
|
|
ancestor.type === exportDeclarationType &&
|
|
isIdentifierMatch(ancestor.id, localDefaultExportId)
|
|
)
|
|
}
|
|
|
|
// function ...() {} export default ...
|
|
// class ... extends ...; export default ...
|
|
return isIdentifierMatch(ancestor.id, localDefaultExportId)
|
|
})
|
|
|
|
// file starts with _document and this <link /> is within the default export
|
|
if (is_Document && parentComponent) {
|
|
return
|
|
}
|
|
|
|
const attributes = new NodeAttributes(node)
|
|
if (!attributes.has('href') || !attributes.hasValue('href')) {
|
|
return
|
|
}
|
|
|
|
const hrefValue = attributes.value('href')
|
|
const isGoogleFont =
|
|
typeof hrefValue === 'string' &&
|
|
hrefValue.startsWith('https://fonts.googleapis.com/css')
|
|
|
|
if (isGoogleFont) {
|
|
const end =
|
|
'This is discouraged. See: https://nextjs.org/docs/messages/no-page-custom-font'
|
|
|
|
const message = is_Document
|
|
? `Rendering this <link /> not inline within <Head> of Document disables font optimization. ${end}`
|
|
: `Custom fonts not added at the document level will only load for a single page. ${end}`
|
|
|
|
context.report({
|
|
node,
|
|
message,
|
|
})
|
|
}
|
|
},
|
|
}
|
|
},
|
|
}
|
|
|
|
function isIdentifierMatch(id1, id2) {
|
|
return (id1 === null && id2 === null) || (id1 && id2 && id1.name === id2.name)
|
|
}
|