eslint-plugin-nextjs 0.1.2 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/dist/index.d.ts +3 -6
- package/dist/index.js +399 -264
- package/dist/index.js.map +1 -1
- package/package.json +18 -12
- package/dist/index.cjs +0 -1485
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -11
- package/dist/rules/google-font-display.cjs +0 -119
- package/dist/rules/google-font-display.cjs.map +0 -1
- package/dist/rules/google-font-display.d.cts +0 -8
- package/dist/rules/google-font-display.d.ts +0 -8
- package/dist/rules/google-font-display.js +0 -92
- package/dist/rules/google-font-display.js.map +0 -1
- package/dist/rules/google-font-preconnect.cjs +0 -109
- package/dist/rules/google-font-preconnect.cjs.map +0 -1
- package/dist/rules/google-font-preconnect.d.cts +0 -5
- package/dist/rules/google-font-preconnect.d.ts +0 -5
- package/dist/rules/google-font-preconnect.js +0 -82
- package/dist/rules/google-font-preconnect.js.map +0 -1
- package/dist/rules/inline-script-id.cjs +0 -94
- package/dist/rules/inline-script-id.cjs.map +0 -1
- package/dist/rules/inline-script-id.d.cts +0 -5
- package/dist/rules/inline-script-id.d.ts +0 -5
- package/dist/rules/inline-script-id.js +0 -67
- package/dist/rules/inline-script-id.js.map +0 -1
- package/dist/rules/next-script-for-ga.cjs +0 -129
- package/dist/rules/next-script-for-ga.cjs.map +0 -1
- package/dist/rules/next-script-for-ga.d.cts +0 -5
- package/dist/rules/next-script-for-ga.d.ts +0 -5
- package/dist/rules/next-script-for-ga.js +0 -102
- package/dist/rules/next-script-for-ga.js.map +0 -1
- package/dist/rules/no-assign-module-variable.cjs +0 -64
- package/dist/rules/no-assign-module-variable.cjs.map +0 -1
- package/dist/rules/no-assign-module-variable.d.cts +0 -5
- package/dist/rules/no-assign-module-variable.d.ts +0 -5
- package/dist/rules/no-assign-module-variable.js +0 -37
- package/dist/rules/no-assign-module-variable.js.map +0 -1
- package/dist/rules/no-async-client-component.cjs +0 -99
- package/dist/rules/no-async-client-component.cjs.map +0 -1
- package/dist/rules/no-async-client-component.d.cts +0 -5
- package/dist/rules/no-async-client-component.d.ts +0 -5
- package/dist/rules/no-async-client-component.js +0 -72
- package/dist/rules/no-async-client-component.js.map +0 -1
- package/dist/rules/no-before-interactive-script-outside-document.cjs +0 -94
- package/dist/rules/no-before-interactive-script-outside-document.cjs.map +0 -1
- package/dist/rules/no-before-interactive-script-outside-document.d.cts +0 -5
- package/dist/rules/no-before-interactive-script-outside-document.d.ts +0 -5
- package/dist/rules/no-before-interactive-script-outside-document.js +0 -59
- package/dist/rules/no-before-interactive-script-outside-document.js.map +0 -1
- package/dist/rules/no-css-tags.cjs +0 -70
- package/dist/rules/no-css-tags.cjs.map +0 -1
- package/dist/rules/no-css-tags.d.cts +0 -5
- package/dist/rules/no-css-tags.d.ts +0 -5
- package/dist/rules/no-css-tags.js +0 -43
- package/dist/rules/no-css-tags.js.map +0 -1
- package/dist/rules/no-document-import-in-page.cjs +0 -74
- package/dist/rules/no-document-import-in-page.cjs.map +0 -1
- package/dist/rules/no-document-import-in-page.d.cts +0 -5
- package/dist/rules/no-document-import-in-page.d.ts +0 -5
- package/dist/rules/no-document-import-in-page.js +0 -39
- package/dist/rules/no-document-import-in-page.js.map +0 -1
- package/dist/rules/no-duplicate-head.cjs +0 -87
- package/dist/rules/no-duplicate-head.cjs.map +0 -1
- package/dist/rules/no-duplicate-head.d.cts +0 -5
- package/dist/rules/no-duplicate-head.d.ts +0 -5
- package/dist/rules/no-duplicate-head.js +0 -60
- package/dist/rules/no-duplicate-head.js.map +0 -1
- package/dist/rules/no-head-element.cjs +0 -76
- package/dist/rules/no-head-element.cjs.map +0 -1
- package/dist/rules/no-head-element.d.cts +0 -5
- package/dist/rules/no-head-element.d.ts +0 -5
- package/dist/rules/no-head-element.js +0 -41
- package/dist/rules/no-head-element.js.map +0 -1
- package/dist/rules/no-head-import-in-document.cjs +0 -76
- package/dist/rules/no-head-import-in-document.cjs.map +0 -1
- package/dist/rules/no-head-import-in-document.d.cts +0 -5
- package/dist/rules/no-head-import-in-document.d.ts +0 -5
- package/dist/rules/no-head-import-in-document.js +0 -41
- package/dist/rules/no-head-import-in-document.js.map +0 -1
- package/dist/rules/no-html-link-for-pages.cjs +0 -302
- package/dist/rules/no-html-link-for-pages.cjs.map +0 -1
- package/dist/rules/no-html-link-for-pages.d.cts +0 -5
- package/dist/rules/no-html-link-for-pages.d.ts +0 -5
- package/dist/rules/no-html-link-for-pages.js +0 -267
- package/dist/rules/no-html-link-for-pages.js.map +0 -1
- package/dist/rules/no-img-element.cjs +0 -83
- package/dist/rules/no-img-element.cjs.map +0 -1
- package/dist/rules/no-img-element.d.cts +0 -5
- package/dist/rules/no-img-element.d.ts +0 -5
- package/dist/rules/no-img-element.js +0 -48
- package/dist/rules/no-img-element.js.map +0 -1
- package/dist/rules/no-page-custom-font.cjs +0 -184
- package/dist/rules/no-page-custom-font.cjs.map +0 -1
- package/dist/rules/no-page-custom-font.d.cts +0 -5
- package/dist/rules/no-page-custom-font.d.ts +0 -5
- package/dist/rules/no-page-custom-font.js +0 -159
- package/dist/rules/no-page-custom-font.js.map +0 -1
- package/dist/rules/no-script-component-in-head.cjs +0 -74
- package/dist/rules/no-script-component-in-head.cjs.map +0 -1
- package/dist/rules/no-script-component-in-head.d.cts +0 -5
- package/dist/rules/no-script-component-in-head.d.ts +0 -5
- package/dist/rules/no-script-component-in-head.js +0 -47
- package/dist/rules/no-script-component-in-head.js.map +0 -1
- package/dist/rules/no-styled-jsx-in-document.cjs +0 -78
- package/dist/rules/no-styled-jsx-in-document.cjs.map +0 -1
- package/dist/rules/no-styled-jsx-in-document.d.cts +0 -5
- package/dist/rules/no-styled-jsx-in-document.d.ts +0 -5
- package/dist/rules/no-styled-jsx-in-document.js +0 -43
- package/dist/rules/no-styled-jsx-in-document.js.map +0 -1
- package/dist/rules/no-sync-scripts.cjs +0 -64
- package/dist/rules/no-sync-scripts.cjs.map +0 -1
- package/dist/rules/no-sync-scripts.d.cts +0 -5
- package/dist/rules/no-sync-scripts.d.ts +0 -5
- package/dist/rules/no-sync-scripts.js +0 -37
- package/dist/rules/no-sync-scripts.js.map +0 -1
- package/dist/rules/no-title-in-document-head.cjs +0 -78
- package/dist/rules/no-title-in-document-head.cjs.map +0 -1
- package/dist/rules/no-title-in-document-head.d.cts +0 -5
- package/dist/rules/no-title-in-document-head.d.ts +0 -5
- package/dist/rules/no-title-in-document-head.js +0 -51
- package/dist/rules/no-title-in-document-head.js.map +0 -1
- package/dist/rules/no-typos.cjs +0 -133
- package/dist/rules/no-typos.cjs.map +0 -1
- package/dist/rules/no-typos.d.cts +0 -5
- package/dist/rules/no-typos.d.ts +0 -5
- package/dist/rules/no-typos.js +0 -98
- package/dist/rules/no-typos.js.map +0 -1
- package/dist/rules/no-unwanted-polyfillio.cjs +0 -164
- package/dist/rules/no-unwanted-polyfillio.cjs.map +0 -1
- package/dist/rules/no-unwanted-polyfillio.d.cts +0 -5
- package/dist/rules/no-unwanted-polyfillio.d.ts +0 -5
- package/dist/rules/no-unwanted-polyfillio.js +0 -137
- package/dist/rules/no-unwanted-polyfillio.js.map +0 -1
- package/dist/utils/define-rule.cjs +0 -31
- package/dist/utils/define-rule.cjs.map +0 -1
- package/dist/utils/define-rule.d.cts +0 -5
- package/dist/utils/define-rule.d.ts +0 -5
- package/dist/utils/define-rule.js +0 -6
- package/dist/utils/define-rule.js.map +0 -1
- package/dist/utils/get-root-dirs.cjs +0 -60
- package/dist/utils/get-root-dirs.cjs.map +0 -1
- package/dist/utils/get-root-dirs.d.cts +0 -8
- package/dist/utils/get-root-dirs.d.ts +0 -8
- package/dist/utils/get-root-dirs.js +0 -25
- package/dist/utils/get-root-dirs.js.map +0 -1
- package/dist/utils/node-attributes.cjs +0 -67
- package/dist/utils/node-attributes.cjs.map +0 -1
- package/dist/utils/node-attributes.d.cts +0 -15
- package/dist/utils/node-attributes.d.ts +0 -15
- package/dist/utils/node-attributes.js +0 -46
- package/dist/utils/node-attributes.js.map +0 -1
- package/dist/utils/url.cjs +0 -167
- package/dist/utils/url.cjs.map +0 -1
- package/dist/utils/url.d.cts +0 -35
- package/dist/utils/url.d.ts +0 -35
- package/dist/utils/url.js +0 -128
- package/dist/utils/url.js.map +0 -1
@@ -1,302 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __create = Object.create;
|
3
|
-
var __defProp = Object.defineProperty;
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __export = (target, all) => {
|
9
|
-
for (var name in all)
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
-
};
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
-
for (let key of __getOwnPropNames(from))
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
-
}
|
18
|
-
return to;
|
19
|
-
};
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
-
mod
|
27
|
-
));
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
-
|
30
|
-
// src/rules/no-html-link-for-pages.ts
|
31
|
-
var no_html_link_for_pages_exports = {};
|
32
|
-
__export(no_html_link_for_pages_exports, {
|
33
|
-
noHtmlLinkForPages: () => noHtmlLinkForPages
|
34
|
-
});
|
35
|
-
module.exports = __toCommonJS(no_html_link_for_pages_exports);
|
36
|
-
var fs2 = __toESM(require("fs"), 1);
|
37
|
-
var path2 = __toESM(require("path"), 1);
|
38
|
-
|
39
|
-
// src/utils/define-rule.ts
|
40
|
-
var defineRule = (rule) => rule;
|
41
|
-
|
42
|
-
// src/utils/get-root-dirs.ts
|
43
|
-
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
44
|
-
var processRootDir = (rootDir) => {
|
45
|
-
return import_fast_glob.default.globSync(rootDir.replaceAll("\\", "/"), {
|
46
|
-
onlyDirectories: true
|
47
|
-
});
|
48
|
-
};
|
49
|
-
var getRootDirs = (context) => {
|
50
|
-
let rootDirs = [context.cwd];
|
51
|
-
const nextSettings = (
|
52
|
-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
53
|
-
context.settings.next || {}
|
54
|
-
);
|
55
|
-
const rootDir = nextSettings.rootDir;
|
56
|
-
if (typeof rootDir === "string") {
|
57
|
-
rootDirs = processRootDir(rootDir);
|
58
|
-
} else if (Array.isArray(rootDir)) {
|
59
|
-
rootDirs = rootDir.map((dir) => typeof dir === "string" ? processRootDir(dir) : []).flat();
|
60
|
-
}
|
61
|
-
return rootDirs;
|
62
|
-
};
|
63
|
-
|
64
|
-
// src/utils/url.ts
|
65
|
-
var fs = __toESM(require("fs"), 1);
|
66
|
-
var path = __toESM(require("path"), 1);
|
67
|
-
var fsReadDirSyncCache = {};
|
68
|
-
var parseUrlForPages = (urlprefix, directory) => {
|
69
|
-
fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {
|
70
|
-
withFileTypes: true
|
71
|
-
});
|
72
|
-
const res = [];
|
73
|
-
fsReadDirSyncCache[directory].forEach((dirent) => {
|
74
|
-
if (/(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
75
|
-
if (/^index(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
76
|
-
res.push(
|
77
|
-
`${urlprefix}${dirent.name.replace(/^index(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
78
|
-
);
|
79
|
-
}
|
80
|
-
res.push(
|
81
|
-
`${urlprefix}${dirent.name.replace(/(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
82
|
-
);
|
83
|
-
} else {
|
84
|
-
const dirPath = path.join(directory, dirent.name);
|
85
|
-
if (dirent.isDirectory() && !dirent.isSymbolicLink()) {
|
86
|
-
res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));
|
87
|
-
}
|
88
|
-
}
|
89
|
-
});
|
90
|
-
return res;
|
91
|
-
};
|
92
|
-
var parseUrlForAppDir = (urlprefix, directory) => {
|
93
|
-
fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {
|
94
|
-
withFileTypes: true
|
95
|
-
});
|
96
|
-
const res = [];
|
97
|
-
fsReadDirSyncCache[directory].forEach((dirent) => {
|
98
|
-
if (/(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
99
|
-
if (/^page(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
100
|
-
res.push(
|
101
|
-
`${urlprefix}${dirent.name.replace(/^page(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
102
|
-
);
|
103
|
-
} else if (!/^layout(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
104
|
-
res.push(
|
105
|
-
`${urlprefix}${dirent.name.replace(/(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
106
|
-
);
|
107
|
-
}
|
108
|
-
} else {
|
109
|
-
const dirPath = path.join(directory, dirent.name);
|
110
|
-
if (dirent.isDirectory(dirPath) && !dirent.isSymbolicLink()) {
|
111
|
-
res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));
|
112
|
-
}
|
113
|
-
}
|
114
|
-
});
|
115
|
-
return res;
|
116
|
-
};
|
117
|
-
var normalizeURL = (url2) => {
|
118
|
-
if (!url2) {
|
119
|
-
return;
|
120
|
-
}
|
121
|
-
url2 = url2.split("?", 1)[0];
|
122
|
-
url2 = url2.split("#", 1)[0];
|
123
|
-
url2 = url2.replace(/(?<temp1>\/index\.html)$/, "/");
|
124
|
-
if (url2 === "") {
|
125
|
-
return url2;
|
126
|
-
}
|
127
|
-
url2 = url2.endsWith("/") ? url2 : `${url2}/`;
|
128
|
-
return url2;
|
129
|
-
};
|
130
|
-
var normalizeAppPath = (route) => ensureLeadingSlash(
|
131
|
-
route.split("/").reduce((pathname, segment, index, segments) => {
|
132
|
-
if (!segment) {
|
133
|
-
return pathname;
|
134
|
-
}
|
135
|
-
if (isGroupSegment(segment)) {
|
136
|
-
return pathname;
|
137
|
-
}
|
138
|
-
if (segment.startsWith("@")) {
|
139
|
-
return pathname;
|
140
|
-
}
|
141
|
-
if ((segment === "page" || segment === "route") && index === segments.length - 1) {
|
142
|
-
return pathname;
|
143
|
-
}
|
144
|
-
return `${pathname}/${segment}`;
|
145
|
-
}, "")
|
146
|
-
);
|
147
|
-
var getUrlFromPagesDirectories = (urlPrefix, directories) => Array.from(
|
148
|
-
// De-duplicate similar pages across multiple directories.
|
149
|
-
new Set(
|
150
|
-
directories.flatMap((directory) => parseUrlForPages(urlPrefix, directory)).map(
|
151
|
-
// Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.
|
152
|
-
(url2) => `^${normalizeURL(url2)}$`
|
153
|
-
)
|
154
|
-
)
|
155
|
-
).map((urlReg) => {
|
156
|
-
urlReg = urlReg.replaceAll(/\[.*]/g, "((?!.+?\\..+?).*?)");
|
157
|
-
return new RegExp(urlReg);
|
158
|
-
});
|
159
|
-
var getUrlFromAppDirectory = (urlPrefix, directories) => Array.from(
|
160
|
-
// De-duplicate similar pages across multiple directories.
|
161
|
-
new Set(
|
162
|
-
directories.map((directory) => parseUrlForAppDir(urlPrefix, directory)).flat().map(
|
163
|
-
// Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.
|
164
|
-
(url2) => `^${normalizeAppPath(url2)}$`
|
165
|
-
)
|
166
|
-
)
|
167
|
-
).map((urlReg) => {
|
168
|
-
urlReg = urlReg.replaceAll(/\[.*]/g, "((?!.+?\\..+?).*?)");
|
169
|
-
return new RegExp(urlReg);
|
170
|
-
});
|
171
|
-
var execOnce = (fn) => {
|
172
|
-
let used = false;
|
173
|
-
let result;
|
174
|
-
return (...args) => {
|
175
|
-
if (!used) {
|
176
|
-
used = true;
|
177
|
-
result = fn(...args);
|
178
|
-
}
|
179
|
-
return result;
|
180
|
-
};
|
181
|
-
};
|
182
|
-
var ensureLeadingSlash = (route) => route.startsWith("/") ? route : `/${route}`;
|
183
|
-
var isGroupSegment = (segment) => segment.startsWith("(") && segment.endsWith(")");
|
184
|
-
|
185
|
-
// src/rules/no-html-link-for-pages.ts
|
186
|
-
var pagesDirWarning = execOnce((pagesDirs) => {
|
187
|
-
console.warn(
|
188
|
-
`Pages directory cannot be found at ${pagesDirs.join(" or ")}. If using a custom path, please configure with the \`no-html-link-for-pages\` rule in your eslint config file.`
|
189
|
-
);
|
190
|
-
});
|
191
|
-
var fsExistsSyncCache = {};
|
192
|
-
var memoize = (fn) => {
|
193
|
-
const cache = {};
|
194
|
-
return (...args) => {
|
195
|
-
const key = JSON.stringify(args);
|
196
|
-
cache[key] ??= fn(...args);
|
197
|
-
return cache[key];
|
198
|
-
};
|
199
|
-
};
|
200
|
-
var cachedGetUrlFromPagesDirectories = memoize(getUrlFromPagesDirectories);
|
201
|
-
var cachedGetUrlFromAppDirectory = memoize(getUrlFromAppDirectory);
|
202
|
-
var url = "https://nextjs.org/docs/messages/no-html-link-for-pages";
|
203
|
-
var noHtmlLinkForPages = defineRule({
|
204
|
-
/**
|
205
|
-
* Creates an ESLint rule listener.
|
206
|
-
*/
|
207
|
-
create: (context) => {
|
208
|
-
const ruleOptions = context.options;
|
209
|
-
const [customPagesDirectory] = ruleOptions;
|
210
|
-
const rootDirs = getRootDirs(context);
|
211
|
-
const pagesDirs = (customPagesDirectory ? [customPagesDirectory] : rootDirs.map((dir) => [
|
212
|
-
path2.join(dir, "pages"),
|
213
|
-
path2.join(dir, "src", "pages")
|
214
|
-
])).flat();
|
215
|
-
const foundPagesDirs = pagesDirs.filter((dir) => {
|
216
|
-
fsExistsSyncCache[dir] ??= fs2.existsSync(dir);
|
217
|
-
return fsExistsSyncCache[dir];
|
218
|
-
});
|
219
|
-
const appDirs = rootDirs.map((dir) => [path2.join(dir, "app"), path2.join(dir, "src", "app")]).flat();
|
220
|
-
const foundAppDirs = appDirs.filter((dir) => {
|
221
|
-
fsExistsSyncCache[dir] ??= fs2.existsSync(dir);
|
222
|
-
return fsExistsSyncCache[dir];
|
223
|
-
});
|
224
|
-
if (foundPagesDirs.length === 0 && foundAppDirs.length === 0) {
|
225
|
-
pagesDirWarning(pagesDirs);
|
226
|
-
return {};
|
227
|
-
}
|
228
|
-
const pageUrls = cachedGetUrlFromPagesDirectories("/", foundPagesDirs);
|
229
|
-
const appDirUrls = cachedGetUrlFromAppDirectory("/", foundAppDirs);
|
230
|
-
const allUrlRegex = [...pageUrls, ...appDirUrls];
|
231
|
-
return {
|
232
|
-
JSXOpeningElement: (node) => {
|
233
|
-
if (node.name.name !== "a") {
|
234
|
-
return;
|
235
|
-
}
|
236
|
-
if (node.attributes.length === 0) {
|
237
|
-
return;
|
238
|
-
}
|
239
|
-
const target = node.attributes.find(
|
240
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "target"
|
241
|
-
);
|
242
|
-
if (target && target.value.value === "_blank") {
|
243
|
-
return;
|
244
|
-
}
|
245
|
-
const href = node.attributes.find(
|
246
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "href"
|
247
|
-
);
|
248
|
-
if (!href || href.value && href.value.type !== "Literal") {
|
249
|
-
return;
|
250
|
-
}
|
251
|
-
const hasDownloadAttr = node.attributes.find(
|
252
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "download"
|
253
|
-
);
|
254
|
-
if (hasDownloadAttr) {
|
255
|
-
return;
|
256
|
-
}
|
257
|
-
const hrefPath = normalizeURL(href.value.value);
|
258
|
-
if (/^(?<temp1>https?:\/\/|\/\/)/.test(hrefPath)) {
|
259
|
-
return;
|
260
|
-
}
|
261
|
-
allUrlRegex.forEach((foundUrl) => {
|
262
|
-
if (hrefPath && foundUrl.test(normalizeURL(hrefPath))) {
|
263
|
-
context.report({
|
264
|
-
message: `Do not use an \`<a>\` element to navigate to \`${hrefPath}\`. Use \`<Link />\` from \`next/link\` instead. See: ${url}`,
|
265
|
-
node
|
266
|
-
});
|
267
|
-
}
|
268
|
-
});
|
269
|
-
}
|
270
|
-
};
|
271
|
-
},
|
272
|
-
meta: {
|
273
|
-
docs: {
|
274
|
-
category: "HTML",
|
275
|
-
description: "Prevent usage of `<a>` elements to navigate to internal Next.js pages.",
|
276
|
-
recommended: true,
|
277
|
-
url
|
278
|
-
},
|
279
|
-
schema: [
|
280
|
-
{
|
281
|
-
oneOf: [
|
282
|
-
{
|
283
|
-
type: "string"
|
284
|
-
},
|
285
|
-
{
|
286
|
-
items: {
|
287
|
-
type: "string"
|
288
|
-
},
|
289
|
-
type: "array",
|
290
|
-
uniqueItems: true
|
291
|
-
}
|
292
|
-
]
|
293
|
-
}
|
294
|
-
],
|
295
|
-
type: "problem"
|
296
|
-
}
|
297
|
-
});
|
298
|
-
// Annotate the CommonJS export names for ESM import in node:
|
299
|
-
0 && (module.exports = {
|
300
|
-
noHtmlLinkForPages
|
301
|
-
});
|
302
|
-
//# sourceMappingURL=no-html-link-for-pages.cjs.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"sources":["../../src/rules/no-html-link-for-pages.ts","../../src/utils/define-rule.ts","../../src/utils/get-root-dirs.ts","../../src/utils/url.ts"],"sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\n\nimport { defineRule } from \"../utils/define-rule.js\";\nimport { getRootDirs } from \"../utils/get-root-dirs.js\";\nimport {\n execOnce,\n getUrlFromAppDirectory,\n getUrlFromPagesDirectories,\n normalizeURL,\n} from \"../utils/url.js\";\n\nconst pagesDirWarning = execOnce((pagesDirs) => {\n console.warn(\n `Pages directory cannot be found at ${pagesDirs.join(\" or \")}. ` +\n \"If using a custom path, please configure with the `no-html-link-for-pages` rule in your eslint config file.\",\n );\n});\n\n// Cache for fs.existsSync lookup.\n// Prevent multiple blocking IO requests that have already been calculated.\nconst fsExistsSyncCache: Record<string, boolean> = {};\n\nconst memoize = <T = any>(fn: (...args: any[]) => T) => {\n const cache: Record<string, T> = {};\n return (...args: any[]): T => {\n const key = JSON.stringify(args);\n cache[key] ??= fn(...args);\n return cache[key];\n };\n};\n\nconst cachedGetUrlFromPagesDirectories = memoize(getUrlFromPagesDirectories);\nconst cachedGetUrlFromAppDirectory = memoize(getUrlFromAppDirectory);\n\nconst url = \"https://nextjs.org/docs/messages/no-html-link-for-pages\";\n\nexport const noHtmlLinkForPages = defineRule({\n /**\n * Creates an ESLint rule listener.\n */\n create: (context) => {\n // @ts-expect-error initial override, TODO: fix\n const ruleOptions: (string | string[])[] = context.options;\n const [customPagesDirectory] = ruleOptions;\n\n const rootDirs = getRootDirs(context);\n\n const pagesDirs = (\n customPagesDirectory ?\n [customPagesDirectory]\n : rootDirs.map((dir) => [\n path.join(dir, \"pages\"),\n path.join(dir, \"src\", \"pages\"),\n ])).flat();\n\n const foundPagesDirs = pagesDirs.filter((dir) => {\n fsExistsSyncCache[dir] ??= fs.existsSync(dir);\n return fsExistsSyncCache[dir];\n });\n\n const appDirs = rootDirs\n .map((dir) => [path.join(dir, \"app\"), path.join(dir, \"src\", \"app\")])\n .flat();\n\n const foundAppDirs = appDirs.filter((dir) => {\n fsExistsSyncCache[dir] ??= fs.existsSync(dir);\n return fsExistsSyncCache[dir];\n });\n\n // warn if there are no pages and app directories\n if (foundPagesDirs.length === 0 && foundAppDirs.length === 0) {\n pagesDirWarning(pagesDirs);\n return {};\n }\n\n const pageUrls = cachedGetUrlFromPagesDirectories(\"/\", foundPagesDirs);\n const appDirUrls = cachedGetUrlFromAppDirectory(\"/\", foundAppDirs);\n const allUrlRegex = [...pageUrls, ...appDirUrls];\n\n return {\n JSXOpeningElement: (node: any) => {\n if (node.name.name !== \"a\") {\n return;\n }\n\n if (node.attributes.length === 0) {\n return;\n }\n\n const target = node.attributes.find(\n (attr: any) =>\n attr.type === \"JSXAttribute\" && attr.name.name === \"target\",\n );\n\n if (target && target.value.value === \"_blank\") {\n return;\n }\n\n const href = node.attributes.find(\n (attr: any) =>\n attr.type === \"JSXAttribute\" && attr.name.name === \"href\",\n );\n\n if (!href || (href.value && href.value.type !== \"Literal\")) {\n return;\n }\n\n const hasDownloadAttr = node.attributes.find(\n (attr: any) =>\n attr.type === \"JSXAttribute\" && attr.name.name === \"download\",\n );\n\n if (hasDownloadAttr) {\n return;\n }\n\n const hrefPath = normalizeURL(href.value.value);\n // Outgoing links are ignored\n\n if (/^(?<temp1>https?:\\/\\/|\\/\\/)/.test(hrefPath as string)) {\n return;\n }\n\n allUrlRegex.forEach((foundUrl) => {\n if (hrefPath && foundUrl.test(normalizeURL(hrefPath) as string)) {\n context.report({\n message: `Do not use an \\`<a>\\` element to navigate to \\`${hrefPath}\\`. Use \\`<Link />\\` from \\`next/link\\` instead. See: ${url}`,\n node,\n });\n }\n });\n },\n };\n },\n\n meta: {\n docs: {\n category: \"HTML\",\n description:\n \"Prevent usage of `<a>` elements to navigate to internal Next.js pages.\",\n recommended: true,\n url,\n },\n schema: [\n {\n oneOf: [\n {\n type: \"string\",\n },\n {\n items: {\n type: \"string\",\n },\n type: \"array\",\n uniqueItems: true,\n },\n ],\n },\n ],\n type: \"problem\",\n },\n});\n","import type { Rule } from \"eslint\";\n\nexport const defineRule = (rule: Rule.RuleModule): Rule.RuleModule => rule;\n","import type { Rule } from \"eslint\";\n\nimport fastGlob from \"fast-glob\";\n\n/**\n * Process a Next.js root directory glob.\n */\nconst processRootDir = (rootDir: string): string[] => {\n return fastGlob.globSync(rootDir.replaceAll(\"\\\\\", \"/\"), {\n onlyDirectories: true,\n });\n};\n\n/**\n * Gets one or more Root, returns an array of root directories.\n */\nexport const getRootDirs = (context: Rule.RuleContext) => {\n let rootDirs = [context.cwd];\n\n const nextSettings: { rootDir?: string | string[] } =\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n context.settings.next || {};\n const rootDir = nextSettings.rootDir;\n\n if (typeof rootDir === \"string\") {\n rootDirs = processRootDir(rootDir);\n } else if (Array.isArray(rootDir)) {\n rootDirs = rootDir\n .map((dir) => (typeof dir === \"string\" ? processRootDir(dir) : []))\n .flat();\n }\n\n return rootDirs;\n};\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\n\n// Cache for fs.readdirSync lookup.\n// Prevent multiple blocking IO requests that have already been calculated.\nconst fsReadDirSyncCache: Record<string, fs.Dirent[]> = {};\n\n/**\n * Recursively parse directory for page URLs.\n */\nconst parseUrlForPages = (urlprefix: string, directory: string) => {\n fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {\n withFileTypes: true,\n });\n const res: string[] = [];\n fsReadDirSyncCache[directory].forEach((dirent) => {\n // TODO: this should account for all page extensions\n // not just js(x) and ts(x)\n if (/(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n if (/^index(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n res.push(\n `${urlprefix}${dirent.name.replace(/^index(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n }\n res.push(\n `${urlprefix}${dirent.name.replace(/(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n } else {\n const dirPath = path.join(directory, dirent.name);\n if (dirent.isDirectory() && !dirent.isSymbolicLink()) {\n res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));\n }\n }\n });\n return res;\n};\n\n/**\n * Recursively parse app directory for URLs.\n */\nconst parseUrlForAppDir = (urlprefix: string, directory: string) => {\n fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {\n withFileTypes: true,\n });\n const res: string[] = [];\n fsReadDirSyncCache[directory].forEach((dirent) => {\n // TODO: this should account for all page extensions\n // not just js(x) and ts(x)\n if (/(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n if (/^page(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n res.push(\n `${urlprefix}${dirent.name.replace(/^page(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n } else if (!/^layout(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n res.push(\n `${urlprefix}${dirent.name.replace(/(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n }\n } else {\n const dirPath = path.join(directory, dirent.name);\n // @ts-expect-error - types suggest this shouldn't take an arg, but I don't want to change anything\n if (dirent.isDirectory(dirPath) && !dirent.isSymbolicLink()) {\n res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));\n }\n }\n });\n return res;\n};\n\n/**\n * Takes a URL and does the following things.\n * - Replaces `index.html` with `/`\n * - Makes sure all URLs are have a trailing `/`\n * - Removes query string\n */\nexport const normalizeURL = (url: string) => {\n if (!url) {\n return;\n }\n url = url.split(\"?\", 1)[0] as string;\n url = url.split(\"#\", 1)[0] as string;\n url = url.replace(/(?<temp1>\\/index\\.html)$/, \"/\");\n // Empty URLs should not be trailed with `/`, e.g. `#heading`\n if (url === \"\") {\n return url;\n }\n url = url.endsWith(\"/\") ? url : `${url}/`;\n return url;\n};\n\n/**\n * Normalizes an app route so it represents the actual request path. Essentially\n * performing the following transformations:\n *\n * - `/(dashboard)/user/[id]/page` to `/user/[id]`\n * - `/(dashboard)/account/page` to `/account`\n * - `/user/[id]/page` to `/user/[id]`\n * - `/account/page` to `/account`\n * - `/page` to `/`\n * - `/(dashboard)/user/[id]/route` to `/user/[id]`\n * - `/(dashboard)/account/route` to `/account`\n * - `/user/[id]/route` to `/user/[id]`\n * - `/account/route` to `/account`\n * - `/route` to `/`\n * - `/` to `/`\n *\n * @param route the app route to normalize\n * @returns the normalized pathname\n */\nexport const normalizeAppPath = (route: string) =>\n ensureLeadingSlash(\n route.split(\"/\").reduce((pathname, segment, index, segments) => {\n // Empty segments are ignored.\n if (!segment) {\n return pathname;\n }\n\n // Groups are ignored.\n if (isGroupSegment(segment)) {\n return pathname;\n }\n\n // Parallel segments are ignored.\n if (segment.startsWith(\"@\")) {\n return pathname;\n }\n\n // The last segment (if it's a leaf) should be ignored.\n if (\n (segment === \"page\" || segment === \"route\") &&\n index === segments.length - 1\n ) {\n return pathname;\n }\n\n return `${pathname}/${segment}`;\n }, \"\"),\n );\n\n/**\n * Gets the possible URLs from a directory.\n */\nexport const getUrlFromPagesDirectories = (\n urlPrefix: string,\n directories: string[],\n) =>\n Array.from(\n // De-duplicate similar pages across multiple directories.\n new Set(\n directories\n .flatMap((directory) => parseUrlForPages(urlPrefix, directory))\n .map(\n // Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.\n (url) => `^${normalizeURL(url)}$`,\n ),\n ),\n ).map((urlReg) => {\n urlReg = urlReg.replaceAll(/\\[.*]/g, \"((?!.+?\\\\..+?).*?)\");\n return new RegExp(urlReg);\n });\n\nexport const getUrlFromAppDirectory = (\n urlPrefix: string,\n directories: string[],\n) =>\n Array.from(\n // De-duplicate similar pages across multiple directories.\n new Set(\n directories\n .map((directory) => parseUrlForAppDir(urlPrefix, directory))\n .flat()\n .map(\n // Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.\n (url) => `^${normalizeAppPath(url)}$`,\n ),\n ),\n ).map((urlReg) => {\n urlReg = urlReg.replaceAll(/\\[.*]/g, \"((?!.+?\\\\..+?).*?)\");\n return new RegExp(urlReg);\n });\n\nexport const execOnce = <TArgs extends any[], TResult>(\n fn: (...args: TArgs) => TResult,\n): ((...args: TArgs) => TResult) => {\n let used = false;\n let result: TResult;\n\n return (...args: TArgs) => {\n if (!used) {\n used = true;\n result = fn(...args);\n }\n return result;\n };\n};\n\nconst ensureLeadingSlash = (route: string) =>\n route.startsWith(\"/\") ? route : `/${route}`;\n\nconst isGroupSegment = (segment: string) =>\n segment.startsWith(\"(\") && segment.endsWith(\")\");\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,MAAoB;AACpB,IAAAC,QAAsB;;;ACCf,IAAM,aAAa,CAAC,SAA2C;;;ACAtE,uBAAqB;AAKrB,IAAM,iBAAiB,CAAC,YAA8B;AACpD,SAAO,iBAAAC,QAAS,SAAS,QAAQ,WAAW,MAAM,GAAG,GAAG;AAAA,IACtD,iBAAiB;AAAA,EACnB,CAAC;AACH;AAKO,IAAM,cAAc,CAAC,YAA8B;AACxD,MAAI,WAAW,CAAC,QAAQ,GAAG;AAE3B,QAAM;AAAA;AAAA,IAEJ,QAAQ,SAAS,QAAQ,CAAC;AAAA;AAC5B,QAAM,UAAU,aAAa;AAE7B,MAAI,OAAO,YAAY,UAAU;AAC/B,eAAW,eAAe,OAAO;AAAA,EACnC,WAAW,MAAM,QAAQ,OAAO,GAAG;AACjC,eAAW,QACR,IAAI,CAAC,QAAS,OAAO,QAAQ,WAAW,eAAe,GAAG,IAAI,CAAC,CAAE,EACjE,KAAK;AAAA,EACV;AAEA,SAAO;AACT;;;ACjCA,SAAoB;AACpB,WAAsB;AAItB,IAAM,qBAAkD,CAAC;AAKzD,IAAM,mBAAmB,CAAC,WAAmB,cAAsB;AACjE,qBAAmB,SAAS,MAAS,eAAY,WAAW;AAAA,IAC1D,eAAe;AAAA,EACjB,CAAC;AACD,QAAM,MAAgB,CAAC;AACvB,qBAAmB,SAAS,EAAE,QAAQ,CAAC,WAAW;AAGhD,QAAI,gCAAgC,KAAK,OAAO,IAAI,GAAG;AACrD,UAAI,sCAAsC,KAAK,OA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cAAM,kBAAkB,KAAK,WAAW;AAAA,UACtC,CAAC,SACC,KAAK,SAAS,kBAAkB,KAAK,KAAK,SAAS;AAAA,QACvD;AAEA,YAAI,iBAAiB;AACnB;AAAA,QACF;AAEA,cAAM,WAAW,aAAa,KAAK,MAAM,KAAK;AAG9C,YAAI,8BAA8B,KAAK,QAAkB,GAAG;AAC1D;AAAA,QACF;AAEA,oBAAY,QAAQ,CAAC,aAAa;AAChC,cAAI,YAAY,SAAS,KAAK,aAAa,QAAQ,CAAW,GAAG;AAC/D,oBAAQ,OAAO;AAAA,cACb,SAAS,kDAAkD,QAAQ,yDAAyD,GAAG;AAAA,cAC/H;AAAA,YACF,CAAC;AAAA,UACH;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM;AAAA,IACJ,MAAM;AAAA,MACJ,UAAU;AAAA,MACV,aACE;AAAA,MACF,aAAa;AAAA,MACb;AAAA,IACF;AAAA,IACA,QAAQ;AAAA,MACN;AAAA,QACE,OAAO;AAAA,UACL;AAAA,YACE,MAAM;AAAA,UACR;AAAA,UACA;AAAA,YACE,OAAO;AAAA,cACL,MAAM;AAAA,YACR;AAAA,YACA,MAAM;AAAA,YACN,aAAa;AAAA,UACf;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,MAAM;AAAA,EACR;AACF,CAAC;","names":["fs","path","fastGlob","url"]}
|
@@ -1,267 +0,0 @@
|
|
1
|
-
// src/rules/no-html-link-for-pages.ts
|
2
|
-
import * as fs2 from "node:fs";
|
3
|
-
import * as path2 from "node:path";
|
4
|
-
|
5
|
-
// src/utils/define-rule.ts
|
6
|
-
var defineRule = (rule) => rule;
|
7
|
-
|
8
|
-
// src/utils/get-root-dirs.ts
|
9
|
-
import fastGlob from "fast-glob";
|
10
|
-
var processRootDir = (rootDir) => {
|
11
|
-
return fastGlob.globSync(rootDir.replaceAll("\\", "/"), {
|
12
|
-
onlyDirectories: true
|
13
|
-
});
|
14
|
-
};
|
15
|
-
var getRootDirs = (context) => {
|
16
|
-
let rootDirs = [context.cwd];
|
17
|
-
const nextSettings = (
|
18
|
-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
19
|
-
context.settings.next || {}
|
20
|
-
);
|
21
|
-
const rootDir = nextSettings.rootDir;
|
22
|
-
if (typeof rootDir === "string") {
|
23
|
-
rootDirs = processRootDir(rootDir);
|
24
|
-
} else if (Array.isArray(rootDir)) {
|
25
|
-
rootDirs = rootDir.map((dir) => typeof dir === "string" ? processRootDir(dir) : []).flat();
|
26
|
-
}
|
27
|
-
return rootDirs;
|
28
|
-
};
|
29
|
-
|
30
|
-
// src/utils/url.ts
|
31
|
-
import * as fs from "node:fs";
|
32
|
-
import * as path from "node:path";
|
33
|
-
var fsReadDirSyncCache = {};
|
34
|
-
var parseUrlForPages = (urlprefix, directory) => {
|
35
|
-
fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {
|
36
|
-
withFileTypes: true
|
37
|
-
});
|
38
|
-
const res = [];
|
39
|
-
fsReadDirSyncCache[directory].forEach((dirent) => {
|
40
|
-
if (/(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
41
|
-
if (/^index(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
42
|
-
res.push(
|
43
|
-
`${urlprefix}${dirent.name.replace(/^index(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
44
|
-
);
|
45
|
-
}
|
46
|
-
res.push(
|
47
|
-
`${urlprefix}${dirent.name.replace(/(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
48
|
-
);
|
49
|
-
} else {
|
50
|
-
const dirPath = path.join(directory, dirent.name);
|
51
|
-
if (dirent.isDirectory() && !dirent.isSymbolicLink()) {
|
52
|
-
res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));
|
53
|
-
}
|
54
|
-
}
|
55
|
-
});
|
56
|
-
return res;
|
57
|
-
};
|
58
|
-
var parseUrlForAppDir = (urlprefix, directory) => {
|
59
|
-
fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {
|
60
|
-
withFileTypes: true
|
61
|
-
});
|
62
|
-
const res = [];
|
63
|
-
fsReadDirSyncCache[directory].forEach((dirent) => {
|
64
|
-
if (/(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
65
|
-
if (/^page(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
66
|
-
res.push(
|
67
|
-
`${urlprefix}${dirent.name.replace(/^page(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
68
|
-
);
|
69
|
-
} else if (!/^layout(?<temp2>\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {
|
70
|
-
res.push(
|
71
|
-
`${urlprefix}${dirent.name.replace(/(?<temp2>\.(?<temp1>j|t)sx?)$/, "")}`
|
72
|
-
);
|
73
|
-
}
|
74
|
-
} else {
|
75
|
-
const dirPath = path.join(directory, dirent.name);
|
76
|
-
if (dirent.isDirectory(dirPath) && !dirent.isSymbolicLink()) {
|
77
|
-
res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));
|
78
|
-
}
|
79
|
-
}
|
80
|
-
});
|
81
|
-
return res;
|
82
|
-
};
|
83
|
-
var normalizeURL = (url2) => {
|
84
|
-
if (!url2) {
|
85
|
-
return;
|
86
|
-
}
|
87
|
-
url2 = url2.split("?", 1)[0];
|
88
|
-
url2 = url2.split("#", 1)[0];
|
89
|
-
url2 = url2.replace(/(?<temp1>\/index\.html)$/, "/");
|
90
|
-
if (url2 === "") {
|
91
|
-
return url2;
|
92
|
-
}
|
93
|
-
url2 = url2.endsWith("/") ? url2 : `${url2}/`;
|
94
|
-
return url2;
|
95
|
-
};
|
96
|
-
var normalizeAppPath = (route) => ensureLeadingSlash(
|
97
|
-
route.split("/").reduce((pathname, segment, index, segments) => {
|
98
|
-
if (!segment) {
|
99
|
-
return pathname;
|
100
|
-
}
|
101
|
-
if (isGroupSegment(segment)) {
|
102
|
-
return pathname;
|
103
|
-
}
|
104
|
-
if (segment.startsWith("@")) {
|
105
|
-
return pathname;
|
106
|
-
}
|
107
|
-
if ((segment === "page" || segment === "route") && index === segments.length - 1) {
|
108
|
-
return pathname;
|
109
|
-
}
|
110
|
-
return `${pathname}/${segment}`;
|
111
|
-
}, "")
|
112
|
-
);
|
113
|
-
var getUrlFromPagesDirectories = (urlPrefix, directories) => Array.from(
|
114
|
-
// De-duplicate similar pages across multiple directories.
|
115
|
-
new Set(
|
116
|
-
directories.flatMap((directory) => parseUrlForPages(urlPrefix, directory)).map(
|
117
|
-
// Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.
|
118
|
-
(url2) => `^${normalizeURL(url2)}$`
|
119
|
-
)
|
120
|
-
)
|
121
|
-
).map((urlReg) => {
|
122
|
-
urlReg = urlReg.replaceAll(/\[.*]/g, "((?!.+?\\..+?).*?)");
|
123
|
-
return new RegExp(urlReg);
|
124
|
-
});
|
125
|
-
var getUrlFromAppDirectory = (urlPrefix, directories) => Array.from(
|
126
|
-
// De-duplicate similar pages across multiple directories.
|
127
|
-
new Set(
|
128
|
-
directories.map((directory) => parseUrlForAppDir(urlPrefix, directory)).flat().map(
|
129
|
-
// Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.
|
130
|
-
(url2) => `^${normalizeAppPath(url2)}$`
|
131
|
-
)
|
132
|
-
)
|
133
|
-
).map((urlReg) => {
|
134
|
-
urlReg = urlReg.replaceAll(/\[.*]/g, "((?!.+?\\..+?).*?)");
|
135
|
-
return new RegExp(urlReg);
|
136
|
-
});
|
137
|
-
var execOnce = (fn) => {
|
138
|
-
let used = false;
|
139
|
-
let result;
|
140
|
-
return (...args) => {
|
141
|
-
if (!used) {
|
142
|
-
used = true;
|
143
|
-
result = fn(...args);
|
144
|
-
}
|
145
|
-
return result;
|
146
|
-
};
|
147
|
-
};
|
148
|
-
var ensureLeadingSlash = (route) => route.startsWith("/") ? route : `/${route}`;
|
149
|
-
var isGroupSegment = (segment) => segment.startsWith("(") && segment.endsWith(")");
|
150
|
-
|
151
|
-
// src/rules/no-html-link-for-pages.ts
|
152
|
-
var pagesDirWarning = execOnce((pagesDirs) => {
|
153
|
-
console.warn(
|
154
|
-
`Pages directory cannot be found at ${pagesDirs.join(" or ")}. If using a custom path, please configure with the \`no-html-link-for-pages\` rule in your eslint config file.`
|
155
|
-
);
|
156
|
-
});
|
157
|
-
var fsExistsSyncCache = {};
|
158
|
-
var memoize = (fn) => {
|
159
|
-
const cache = {};
|
160
|
-
return (...args) => {
|
161
|
-
const key = JSON.stringify(args);
|
162
|
-
cache[key] ??= fn(...args);
|
163
|
-
return cache[key];
|
164
|
-
};
|
165
|
-
};
|
166
|
-
var cachedGetUrlFromPagesDirectories = memoize(getUrlFromPagesDirectories);
|
167
|
-
var cachedGetUrlFromAppDirectory = memoize(getUrlFromAppDirectory);
|
168
|
-
var url = "https://nextjs.org/docs/messages/no-html-link-for-pages";
|
169
|
-
var noHtmlLinkForPages = defineRule({
|
170
|
-
/**
|
171
|
-
* Creates an ESLint rule listener.
|
172
|
-
*/
|
173
|
-
create: (context) => {
|
174
|
-
const ruleOptions = context.options;
|
175
|
-
const [customPagesDirectory] = ruleOptions;
|
176
|
-
const rootDirs = getRootDirs(context);
|
177
|
-
const pagesDirs = (customPagesDirectory ? [customPagesDirectory] : rootDirs.map((dir) => [
|
178
|
-
path2.join(dir, "pages"),
|
179
|
-
path2.join(dir, "src", "pages")
|
180
|
-
])).flat();
|
181
|
-
const foundPagesDirs = pagesDirs.filter((dir) => {
|
182
|
-
fsExistsSyncCache[dir] ??= fs2.existsSync(dir);
|
183
|
-
return fsExistsSyncCache[dir];
|
184
|
-
});
|
185
|
-
const appDirs = rootDirs.map((dir) => [path2.join(dir, "app"), path2.join(dir, "src", "app")]).flat();
|
186
|
-
const foundAppDirs = appDirs.filter((dir) => {
|
187
|
-
fsExistsSyncCache[dir] ??= fs2.existsSync(dir);
|
188
|
-
return fsExistsSyncCache[dir];
|
189
|
-
});
|
190
|
-
if (foundPagesDirs.length === 0 && foundAppDirs.length === 0) {
|
191
|
-
pagesDirWarning(pagesDirs);
|
192
|
-
return {};
|
193
|
-
}
|
194
|
-
const pageUrls = cachedGetUrlFromPagesDirectories("/", foundPagesDirs);
|
195
|
-
const appDirUrls = cachedGetUrlFromAppDirectory("/", foundAppDirs);
|
196
|
-
const allUrlRegex = [...pageUrls, ...appDirUrls];
|
197
|
-
return {
|
198
|
-
JSXOpeningElement: (node) => {
|
199
|
-
if (node.name.name !== "a") {
|
200
|
-
return;
|
201
|
-
}
|
202
|
-
if (node.attributes.length === 0) {
|
203
|
-
return;
|
204
|
-
}
|
205
|
-
const target = node.attributes.find(
|
206
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "target"
|
207
|
-
);
|
208
|
-
if (target && target.value.value === "_blank") {
|
209
|
-
return;
|
210
|
-
}
|
211
|
-
const href = node.attributes.find(
|
212
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "href"
|
213
|
-
);
|
214
|
-
if (!href || href.value && href.value.type !== "Literal") {
|
215
|
-
return;
|
216
|
-
}
|
217
|
-
const hasDownloadAttr = node.attributes.find(
|
218
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "download"
|
219
|
-
);
|
220
|
-
if (hasDownloadAttr) {
|
221
|
-
return;
|
222
|
-
}
|
223
|
-
const hrefPath = normalizeURL(href.value.value);
|
224
|
-
if (/^(?<temp1>https?:\/\/|\/\/)/.test(hrefPath)) {
|
225
|
-
return;
|
226
|
-
}
|
227
|
-
allUrlRegex.forEach((foundUrl) => {
|
228
|
-
if (hrefPath && foundUrl.test(normalizeURL(hrefPath))) {
|
229
|
-
context.report({
|
230
|
-
message: `Do not use an \`<a>\` element to navigate to \`${hrefPath}\`. Use \`<Link />\` from \`next/link\` instead. See: ${url}`,
|
231
|
-
node
|
232
|
-
});
|
233
|
-
}
|
234
|
-
});
|
235
|
-
}
|
236
|
-
};
|
237
|
-
},
|
238
|
-
meta: {
|
239
|
-
docs: {
|
240
|
-
category: "HTML",
|
241
|
-
description: "Prevent usage of `<a>` elements to navigate to internal Next.js pages.",
|
242
|
-
recommended: true,
|
243
|
-
url
|
244
|
-
},
|
245
|
-
schema: [
|
246
|
-
{
|
247
|
-
oneOf: [
|
248
|
-
{
|
249
|
-
type: "string"
|
250
|
-
},
|
251
|
-
{
|
252
|
-
items: {
|
253
|
-
type: "string"
|
254
|
-
},
|
255
|
-
type: "array",
|
256
|
-
uniqueItems: true
|
257
|
-
}
|
258
|
-
]
|
259
|
-
}
|
260
|
-
],
|
261
|
-
type: "problem"
|
262
|
-
}
|
263
|
-
});
|
264
|
-
export {
|
265
|
-
noHtmlLinkForPages
|
266
|
-
};
|
267
|
-
//# sourceMappingURL=no-html-link-for-pages.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"sources":["../../src/rules/no-html-link-for-pages.ts","../../src/utils/define-rule.ts","../../src/utils/get-root-dirs.ts","../../src/utils/url.ts"],"sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\n\nimport { defineRule } from \"../utils/define-rule.js\";\nimport { getRootDirs } from \"../utils/get-root-dirs.js\";\nimport {\n execOnce,\n getUrlFromAppDirectory,\n getUrlFromPagesDirectories,\n normalizeURL,\n} from \"../utils/url.js\";\n\nconst pagesDirWarning = execOnce((pagesDirs) => {\n console.warn(\n `Pages directory cannot be found at ${pagesDirs.join(\" or \")}. ` +\n \"If using a custom path, please configure with the `no-html-link-for-pages` rule in your eslint config file.\",\n );\n});\n\n// Cache for fs.existsSync lookup.\n// Prevent multiple blocking IO requests that have already been calculated.\nconst fsExistsSyncCache: Record<string, boolean> = {};\n\nconst memoize = <T = any>(fn: (...args: any[]) => T) => {\n const cache: Record<string, T> = {};\n return (...args: any[]): T => {\n const key = JSON.stringify(args);\n cache[key] ??= fn(...args);\n return cache[key];\n };\n};\n\nconst cachedGetUrlFromPagesDirectories = memoize(getUrlFromPagesDirectories);\nconst cachedGetUrlFromAppDirectory = memoize(getUrlFromAppDirectory);\n\nconst url = \"https://nextjs.org/docs/messages/no-html-link-for-pages\";\n\nexport const noHtmlLinkForPages = defineRule({\n /**\n * Creates an ESLint rule listener.\n */\n create: (context) => {\n // @ts-expect-error initial override, TODO: fix\n const ruleOptions: (string | string[])[] = context.options;\n const [customPagesDirectory] = ruleOptions;\n\n const rootDirs = getRootDirs(context);\n\n const pagesDirs = (\n customPagesDirectory ?\n [customPagesDirectory]\n : rootDirs.map((dir) => [\n path.join(dir, \"pages\"),\n path.join(dir, \"src\", \"pages\"),\n ])).flat();\n\n const foundPagesDirs = pagesDirs.filter((dir) => {\n fsExistsSyncCache[dir] ??= fs.existsSync(dir);\n return fsExistsSyncCache[dir];\n });\n\n const appDirs = rootDirs\n .map((dir) => [path.join(dir, \"app\"), path.join(dir, \"src\", \"app\")])\n .flat();\n\n const foundAppDirs = appDirs.filter((dir) => {\n fsExistsSyncCache[dir] ??= fs.existsSync(dir);\n return fsExistsSyncCache[dir];\n });\n\n // warn if there are no pages and app directories\n if (foundPagesDirs.length === 0 && foundAppDirs.length === 0) {\n pagesDirWarning(pagesDirs);\n return {};\n }\n\n const pageUrls = cachedGetUrlFromPagesDirectories(\"/\", foundPagesDirs);\n const appDirUrls = cachedGetUrlFromAppDirectory(\"/\", foundAppDirs);\n const allUrlRegex = [...pageUrls, ...appDirUrls];\n\n return {\n JSXOpeningElement: (node: any) => {\n if (node.name.name !== \"a\") {\n return;\n }\n\n if (node.attributes.length === 0) {\n return;\n }\n\n const target = node.attributes.find(\n (attr: any) =>\n attr.type === \"JSXAttribute\" && attr.name.name === \"target\",\n );\n\n if (target && target.value.value === \"_blank\") {\n return;\n }\n\n const href = node.attributes.find(\n (attr: any) =>\n attr.type === \"JSXAttribute\" && attr.name.name === \"href\",\n );\n\n if (!href || (href.value && href.value.type !== \"Literal\")) {\n return;\n }\n\n const hasDownloadAttr = node.attributes.find(\n (attr: any) =>\n attr.type === \"JSXAttribute\" && attr.name.name === \"download\",\n );\n\n if (hasDownloadAttr) {\n return;\n }\n\n const hrefPath = normalizeURL(href.value.value);\n // Outgoing links are ignored\n\n if (/^(?<temp1>https?:\\/\\/|\\/\\/)/.test(hrefPath as string)) {\n return;\n }\n\n allUrlRegex.forEach((foundUrl) => {\n if (hrefPath && foundUrl.test(normalizeURL(hrefPath) as string)) {\n context.report({\n message: `Do not use an \\`<a>\\` element to navigate to \\`${hrefPath}\\`. Use \\`<Link />\\` from \\`next/link\\` instead. See: ${url}`,\n node,\n });\n }\n });\n },\n };\n },\n\n meta: {\n docs: {\n category: \"HTML\",\n description:\n \"Prevent usage of `<a>` elements to navigate to internal Next.js pages.\",\n recommended: true,\n url,\n },\n schema: [\n {\n oneOf: [\n {\n type: \"string\",\n },\n {\n items: {\n type: \"string\",\n },\n type: \"array\",\n uniqueItems: true,\n },\n ],\n },\n ],\n type: \"problem\",\n },\n});\n","import type { Rule } from \"eslint\";\n\nexport const defineRule = (rule: Rule.RuleModule): Rule.RuleModule => rule;\n","import type { Rule } from \"eslint\";\n\nimport fastGlob from \"fast-glob\";\n\n/**\n * Process a Next.js root directory glob.\n */\nconst processRootDir = (rootDir: string): string[] => {\n return fastGlob.globSync(rootDir.replaceAll(\"\\\\\", \"/\"), {\n onlyDirectories: true,\n });\n};\n\n/**\n * Gets one or more Root, returns an array of root directories.\n */\nexport const getRootDirs = (context: Rule.RuleContext) => {\n let rootDirs = [context.cwd];\n\n const nextSettings: { rootDir?: string | string[] } =\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n context.settings.next || {};\n const rootDir = nextSettings.rootDir;\n\n if (typeof rootDir === \"string\") {\n rootDirs = processRootDir(rootDir);\n } else if (Array.isArray(rootDir)) {\n rootDirs = rootDir\n .map((dir) => (typeof dir === \"string\" ? processRootDir(dir) : []))\n .flat();\n }\n\n return rootDirs;\n};\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\n\n// Cache for fs.readdirSync lookup.\n// Prevent multiple blocking IO requests that have already been calculated.\nconst fsReadDirSyncCache: Record<string, fs.Dirent[]> = {};\n\n/**\n * Recursively parse directory for page URLs.\n */\nconst parseUrlForPages = (urlprefix: string, directory: string) => {\n fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {\n withFileTypes: true,\n });\n const res: string[] = [];\n fsReadDirSyncCache[directory].forEach((dirent) => {\n // TODO: this should account for all page extensions\n // not just js(x) and ts(x)\n if (/(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n if (/^index(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n res.push(\n `${urlprefix}${dirent.name.replace(/^index(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n }\n res.push(\n `${urlprefix}${dirent.name.replace(/(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n } else {\n const dirPath = path.join(directory, dirent.name);\n if (dirent.isDirectory() && !dirent.isSymbolicLink()) {\n res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));\n }\n }\n });\n return res;\n};\n\n/**\n * Recursively parse app directory for URLs.\n */\nconst parseUrlForAppDir = (urlprefix: string, directory: string) => {\n fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {\n withFileTypes: true,\n });\n const res: string[] = [];\n fsReadDirSyncCache[directory].forEach((dirent) => {\n // TODO: this should account for all page extensions\n // not just js(x) and ts(x)\n if (/(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n if (/^page(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n res.push(\n `${urlprefix}${dirent.name.replace(/^page(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n } else if (!/^layout(?<temp2>\\.(?<temp1>j|t)sx?)$/.test(dirent.name)) {\n res.push(\n `${urlprefix}${dirent.name.replace(/(?<temp2>\\.(?<temp1>j|t)sx?)$/, \"\")}`,\n );\n }\n } else {\n const dirPath = path.join(directory, dirent.name);\n // @ts-expect-error - types suggest this shouldn't take an arg, but I don't want to change anything\n if (dirent.isDirectory(dirPath) && !dirent.isSymbolicLink()) {\n res.push(...parseUrlForPages(`${urlprefix + dirent.name}/`, dirPath));\n }\n }\n });\n return res;\n};\n\n/**\n * Takes a URL and does the following things.\n * - Replaces `index.html` with `/`\n * - Makes sure all URLs are have a trailing `/`\n * - Removes query string\n */\nexport const normalizeURL = (url: string) => {\n if (!url) {\n return;\n }\n url = url.split(\"?\", 1)[0] as string;\n url = url.split(\"#\", 1)[0] as string;\n url = url.replace(/(?<temp1>\\/index\\.html)$/, \"/\");\n // Empty URLs should not be trailed with `/`, e.g. `#heading`\n if (url === \"\") {\n return url;\n }\n url = url.endsWith(\"/\") ? url : `${url}/`;\n return url;\n};\n\n/**\n * Normalizes an app route so it represents the actual request path. Essentially\n * performing the following transformations:\n *\n * - `/(dashboard)/user/[id]/page` to `/user/[id]`\n * - `/(dashboard)/account/page` to `/account`\n * - `/user/[id]/page` to `/user/[id]`\n * - `/account/page` to `/account`\n * - `/page` to `/`\n * - `/(dashboard)/user/[id]/route` to `/user/[id]`\n * - `/(dashboard)/account/route` to `/account`\n * - `/user/[id]/route` to `/user/[id]`\n * - `/account/route` to `/account`\n * - `/route` to `/`\n * - `/` to `/`\n *\n * @param route the app route to normalize\n * @returns the normalized pathname\n */\nexport const normalizeAppPath = (route: string) =>\n ensureLeadingSlash(\n route.split(\"/\").reduce((pathname, segment, index, segments) => {\n // Empty segments are ignored.\n if (!segment) {\n return pathname;\n }\n\n // Groups are ignored.\n if (isGroupSegment(segment)) {\n return pathname;\n }\n\n // Parallel segments are ignored.\n if (segment.startsWith(\"@\")) {\n return pathname;\n }\n\n // The last segment (if it's a leaf) should be ignored.\n if (\n (segment === \"page\" || segment === \"route\") &&\n index === segments.length - 1\n ) {\n return pathname;\n }\n\n return `${pathname}/${segment}`;\n }, \"\"),\n );\n\n/**\n * Gets the possible URLs from a directory.\n */\nexport const getUrlFromPagesDirectories = (\n urlPrefix: string,\n directories: string[],\n) =>\n Array.from(\n // De-duplicate similar pages across multiple directories.\n new Set(\n directories\n .flatMap((directory) => parseUrlForPages(urlPrefix, directory))\n .map(\n // Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.\n (url) => `^${normalizeURL(url)}$`,\n ),\n ),\n ).map((urlReg) => {\n urlReg = urlReg.replaceAll(/\\[.*]/g, \"((?!.+?\\\\..+?).*?)\");\n return new RegExp(urlReg);\n });\n\nexport const getUrlFromAppDirectory = (\n urlPrefix: string,\n directories: string[],\n) =>\n Array.from(\n // De-duplicate similar pages across multiple directories.\n new Set(\n directories\n .map((directory) => parseUrlForAppDir(urlPrefix, directory))\n .flat()\n .map(\n // Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.\n (url) => `^${normalizeAppPath(url)}$`,\n ),\n ),\n ).map((urlReg) => {\n urlReg = urlReg.replaceAll(/\\[.*]/g, \"((?!.+?\\\\..+?).*?)\");\n return new RegExp(urlReg);\n });\n\nexport const execOnce = <TArgs extends any[], TResult>(\n fn: (...args: TArgs) => TResult,\n): ((...args: TArgs) => TResult) => {\n let used = false;\n let result: TResult;\n\n return (...args: TArgs) => {\n if (!used) {\n used = true;\n result = fn(...args);\n }\n return result;\n };\n};\n\nconst ensureLeadingSlash = (route: string) =>\n route.startsWith(\"/\") ? route : `/${route}`;\n\nconst isGroupSegment = (segment: string) =>\n segment.startsWith(\"(\") && segment.endsWith(\")\");\n"],"mappings":";AAAA,YAAYA,SAAQ;AACpB,YAAYC,WAAU;;;ACCf,IAAM,aAAa,CAAC,SAA2C;;;ACAtE,OAAO,cAAc;AAKrB,IAAM,iBAAiB,CAAC,YAA8B;AACpD,SAAO,SAAS,SAAS,QAAQ,WAAW,MAAM,GAAG,GAAG;AAAA,IACtD,iBAAiB;AAAA,EACnB,CAAC;AACH;AAKO,IAAM,cAAc,CAAC,YAA8B;AACxD,MAAI,WAAW,CAAC,QAAQ,GAAG;AAE3B,QAAM;AAA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