df83ccb7cd
Documentation: - Expands the "Migrating Existing Config" section of ESLint doc to explain in more detail - Minor changes to the "Ignoring ESLint" doc Bug fixes: - Adds `browser` and `node` environments to `eslint-config-next`. Closes #26319 - Fixes `no-document-import` rule. Closes #26159 - Fixes `no-page-custom-font` rule. Closes #26160 #26894
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
const NodeAttributes = require('../utils/nodeAttributes.js')
|
|
|
|
module.exports = {
|
|
meta: {
|
|
docs: {
|
|
description:
|
|
'Recommend adding custom font in a custom document and not in a specific page',
|
|
recommended: true,
|
|
},
|
|
},
|
|
create: function (context) {
|
|
let documentImportName
|
|
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
|
|
}
|
|
}
|
|
},
|
|
JSXOpeningElement(node) {
|
|
const documentClass = context
|
|
.getAncestors()
|
|
.find(
|
|
(ancestorNode) =>
|
|
ancestorNode.type === 'ClassDeclaration' &&
|
|
ancestorNode.superClass &&
|
|
ancestorNode.superClass.name === documentImportName
|
|
)
|
|
|
|
if (documentClass || node.name.name !== 'link') {
|
|
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) {
|
|
context.report({
|
|
node,
|
|
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.',
|
|
})
|
|
}
|
|
},
|
|
}
|
|
},
|
|
}
|