-
Notifications
You must be signed in to change notification settings - Fork 27.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rootDir setting to eslint-plugin-next #27918
Changes from 2 commits
f9d2dfd
599c5e8
c34d9f2
d4b250b
c675a67
5602546
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type ESLint from 'eslint' | ||
|
||
declare module 'eslint' { | ||
namespace Rule { | ||
interface RuleContext extends ESLint.Rule.RuleContext { | ||
getCwd(): string | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# `@next/eslint-plugin-next` | ||
|
||
Documentation for `@next/eslint-plugin-next` can be found at: | ||
https://nextjs.org/docs/basic-features/eslint#eslint-plugin | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es2019" | ||
}, | ||
"exclude": ["node_modules"] | ||
} | ||
Comment on lines
+1
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using this with |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const NodeAttributes = require('../utils/nodeAttributes.js') | ||
const NodeAttributes = require('../utils/node-attributes.js') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed this file to align with other files in the repo. |
||
|
||
module.exports = { | ||
meta: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// @ts-check | ||
const path = require('path') | ||
const fs = require('fs') | ||
const getRootDir = require('../utils/get-root-dirs') | ||
const { | ||
getUrlFromPagesDirectories, | ||
normalizeURL, | ||
|
@@ -20,7 +22,7 @@ const fsExistsSyncCache = {} | |
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Prohibit full page refresh for nextjs pages', | ||
description: 'Prohibit full page refresh for Next.js pages', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed this was a typo. |
||
category: 'HTML', | ||
recommended: true, | ||
}, | ||
|
@@ -43,14 +45,27 @@ module.exports = { | |
], | ||
}, | ||
|
||
/** | ||
* Creates an ESLint rule listener. | ||
* | ||
* @param {import('eslint').Rule.RuleContext} context - ESLint rule context | ||
* @returns {import('eslint').Rule.RuleListener} An ESLint rule listener | ||
*/ | ||
create: function (context) { | ||
const [customPagesDirectory] = context.options | ||
const pagesDirs = customPagesDirectory | ||
? [customPagesDirectory].flat() | ||
: [ | ||
path.join(context.getCwd(), 'pages'), | ||
path.join(context.getCwd(), 'src', 'pages'), | ||
] | ||
/** @type {(string|string[])[]} */ | ||
const ruleOptions = context.options | ||
const [customPagesDirectory] = ruleOptions | ||
|
||
const rootDirs = getRootDir(context) | ||
|
||
const pagesDirs = (customPagesDirectory | ||
? [customPagesDirectory] | ||
: rootDirs.map((dir) => [ | ||
path.join(dir, 'pages'), | ||
path.join(dir, 'src', 'pages'), | ||
]) | ||
).flat() | ||
|
||
const foundPagesDirs = pagesDirs.filter((dir) => { | ||
if (fsExistsSyncCache[dir] === undefined) { | ||
fsExistsSyncCache[dir] = fs.existsSync(dir) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// @ts-check | ||
const glob = require('glob') | ||
|
||
/** | ||
* Process a Next.js root directory glob. | ||
* | ||
* @param {string} rootDir - A Next.js root directory glob. | ||
* @returns {string[]} - An array of Root directories. | ||
*/ | ||
const processRootDir = (rootDir) => { | ||
// Ensures we only match folders. | ||
if (!rootDir.endsWith('/')) rootDir += '/' | ||
return glob.sync(rootDir) | ||
} | ||
|
||
/** | ||
* Gets one or more Root | ||
* | ||
* @param {import('eslint').Rule.RuleContext} context - ESLint rule context | ||
* @returns An array of root directories. | ||
*/ | ||
const getRootDir = (context) => { | ||
let rootDirs = [context.getCwd()] | ||
|
||
/** @type {{rootDir?:string|string[]}|undefined} */ | ||
const nextSettings = context.settings.next || {} | ||
let rootDir = nextSettings.rootDir | ||
|
||
if (typeof rootDir === 'string') { | ||
rootDirs = processRootDir(rootDir) | ||
} else if (Array.isArray(rootDir)) { | ||
rootDirs = rootDir | ||
.map((dir) => (typeof dir === 'string' ? processRootDir(dir) : [])) | ||
.flat() | ||
} | ||
|
||
return rootDirs | ||
} | ||
|
||
module.exports = getRootDir |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6325,7 +6325,7 @@ caniuse-api@^3.0.0: | |
lodash.memoize "^4.1.2" | ||
lodash.uniq "^4.5.0" | ||
|
||
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001093, caniuse-lite@^1.0.30001165, caniuse-lite@^1.0.30001202, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001228: | ||
caniuse-lite@1.0.30001228, caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001093, caniuse-lite@^1.0.30001165, caniuse-lite@^1.0.30001202, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001228: | ||
version "1.0.30001228" | ||
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001228.tgz#bfdc5942cd3326fa51ee0b42fbef4da9d492a7fa" | ||
integrity sha512-QQmLOGJ3DEgokHbMSA8cj2a+geXqmnpyOFT0lhQV6P3/YOJvGDEwoedcwxEQ30gJIwIIunHIicunJ2rzK5gB2A== | ||
|
@@ -9826,6 +9826,18 @@ [email protected], glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glo | |
once "^1.3.0" | ||
path-is-absolute "^1.0.0" | ||
|
||
[email protected]: | ||
version "7.1.7" | ||
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90" | ||
integrity sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ== | ||
dependencies: | ||
fs.realpath "^1.0.0" | ||
inflight "^1.0.4" | ||
inherits "2" | ||
minimatch "^3.0.4" | ||
once "^1.3.0" | ||
path-is-absolute "^1.0.0" | ||
|
||
global-dirs@^2.0.1: | ||
version "2.1.0" | ||
resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-2.1.0.tgz#e9046a49c806ff04d6c1825e196c8f0091e8df4d" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to search for the docs when first using the plugin, hopefully this will help others.