Add rootDir setting to eslint-plugin-next (#27918)
## Introduction
This PR enables setting a `rootDir` for a Next.js project, and follows the same pattern used by [`@typescript-eslint/parser`](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#parseroptionsproject).
## Details
Previously, users had to pass paths to the rule itself.
```js
module.exports = {
rules: {
"@next/next/no-html-link-for-pages": [
"error",
// This could be a string, or array of strings.
"/packages/my-app/pages",
],
},
};
```
With this PR, this has been simplified (the previous implementation still works as expected).
```js
module.exports = {
settings: {
next: {
rootDir: "/packages/my-app",
},
},
rules: {
"@next/next/no-html-link-for-pages": "error",
},
};
```
Further, this rule allows the use of globs, again aligning with `@typescript-eslint/parser`.
```js
module.exports = {
settings: {
next: {
// Globs
rootDir: "/packages/*",
rootDir: "/packages/{app-a,app-b}",
// Arrays
rootDir: ["/app-a", "/app-b"],
// Arrays with globs
rootDir: ["/main-app", "/other-apps/*"],
},
};
```
This enables users to either provide per-workspace configuration with overrides, or to use globs for situations like monorepos where the apps share a domain (micro-frontends).
This doesn't solve, but improves https://github.com/vercel/next.js/issues/26330.
## Feature
- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] 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
- [x] Make sure the linting passes
2021-08-11 12:37:55 +02:00
|
|
|
const NodeAttributes = require('../utils/node-attributes.js')
|
2021-11-26 14:27:13 +01:00
|
|
|
const { sep, posix } = require('path')
|
2021-05-10 21:08:14 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
description:
|
|
|
|
'Recommend adding custom font in a custom document and not in a specific page',
|
|
|
|
recommended: true,
|
2021-10-16 08:16:41 +02:00
|
|
|
url: 'https://nextjs.org/docs/messages/no-page-custom-font',
|
2021-05-10 21:08:14 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
create: function (context) {
|
2021-11-26 14:27:13 +01:00
|
|
|
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`)
|
|
|
|
|
2021-08-05 02:58:06 +02:00
|
|
|
let documentImportName
|
2021-11-26 14:27:13 +01:00
|
|
|
let localDefaultExportName
|
|
|
|
let exportDeclarationType
|
|
|
|
|
2021-05-10 21:08:14 +02:00
|
|
|
return {
|
|
|
|
ImportDeclaration(node) {
|
|
|
|
if (node.source.value === 'next/document') {
|
2021-08-05 02:58:06 +02:00
|
|
|
const documentImport = node.specifiers.find(
|
|
|
|
({ type }) => type === 'ImportDefaultSpecifier'
|
|
|
|
)
|
|
|
|
if (documentImport && documentImport.local) {
|
|
|
|
documentImportName = documentImport.local.name
|
2021-05-10 21:08:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-11-26 14:27:13 +01:00
|
|
|
|
|
|
|
ExportDefaultDeclaration(node) {
|
|
|
|
exportDeclarationType = node.declaration.type
|
|
|
|
|
|
|
|
if (node.declaration.type === 'FunctionDeclaration') {
|
|
|
|
localDefaultExportName = node.declaration.id.name
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
node.declaration.type === 'ClassDeclaration' &&
|
|
|
|
node.declaration.superClass &&
|
|
|
|
node.declaration.superClass.name === documentImportName
|
|
|
|
) {
|
|
|
|
localDefaultExportName = node.declaration.id.name
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-05-10 21:08:14 +02:00
|
|
|
JSXOpeningElement(node) {
|
2021-11-26 14:27:13 +01:00
|
|
|
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 (!localDefaultExportName) {
|
|
|
|
// find the top level of the module
|
|
|
|
const program = ancestors.find(
|
|
|
|
(ancestor) => ancestor.type === 'Program'
|
2021-05-10 21:08:14 +02:00
|
|
|
)
|
|
|
|
|
2021-11-26 14:27:13 +01:00
|
|
|
// go over each token to find the combination of `export default <name>`
|
|
|
|
for (let i = 0; i <= program.tokens.length - 1; i++) {
|
|
|
|
if (localDefaultExportName) {
|
|
|
|
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') {
|
|
|
|
localDefaultExportName = 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 &&
|
|
|
|
ancestor.id.name === localDefaultExportName
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// function ...() {} export default ...
|
|
|
|
// class ... extends ...; export default ...
|
|
|
|
return ancestor.id && ancestor.id.name === localDefaultExportName
|
|
|
|
})
|
|
|
|
|
|
|
|
// file starts with _document and this <link /> is within the default export
|
|
|
|
if (is_Document && parentComponent) {
|
2021-05-10 21:08:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const attributes = new NodeAttributes(node)
|
|
|
|
if (!attributes.has('href') || !attributes.hasValue('href')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const hrefValue = attributes.value('href')
|
2021-05-12 14:24:42 +02:00
|
|
|
const isGoogleFont =
|
|
|
|
typeof hrefValue === 'string' &&
|
|
|
|
hrefValue.startsWith('https://fonts.googleapis.com/css')
|
2021-05-10 21:08:14 +02:00
|
|
|
|
|
|
|
if (isGoogleFont) {
|
2021-11-26 14:27:13 +01:00
|
|
|
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}`
|
|
|
|
|
2021-05-10 21:08:14 +02:00
|
|
|
context.report({
|
|
|
|
node,
|
2021-11-26 14:27:13 +01:00
|
|
|
message,
|
2021-05-10 21:08:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|