File size: 5,274 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
import { defineRule } from '../utils/define-rule'
import NodeAttributes from '../utils/node-attributes'
import { sep, posix } from 'path'
import type { AST } from 'eslint'
const url = 'https://nextjs.org/docs/messages/no-page-custom-font'
function isIdentifierMatch(id1, id2) {
return (id1 === null && id2 === null) || (id1 && id2 && id1.name === id2.name)
}
export = defineRule({
meta: {
docs: {
description: 'Prevent page-only custom fonts.',
recommended: true,
url,
},
type: 'problem',
schema: [],
},
create(context) {
const { sourceCode } = context
const paths = context.filename.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 &&
'name' in node.declaration.superClass &&
node.declaration.superClass.name === documentImportName
) {
localDefaultExportId = node.declaration.id
}
},
JSXOpeningElement(node) {
if (node.name.name !== 'link') {
return
}
const ancestors = sourceCode.getAncestors(node)
// 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'
) as AST.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 &&
'superClass' in ancestor &&
ancestor.superClass &&
'name' in ancestor.superClass &&
ancestor.superClass.name === documentImportName
)
}
if ('id' in ancestor) {
// 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)
}
return false
})
// 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: ${url}`
const message = is_Document
? `Using \`<link />\` outside of \`<Head>\` will disable automatic font optimization. ${end}`
: `Custom fonts not added in \`pages/_document.js\` will only load for a single page. ${end}`
context.report({
node,
message,
})
}
},
}
},
})
|