-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathnextjs-global-error-missing-html-body.ts
More file actions
38 lines (34 loc) · 1.63 KB
/
nextjs-global-error-missing-html-body.ts
File metadata and controls
38 lines (34 loc) · 1.63 KB
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
import { APP_DIRECTORY_PATTERN, GLOBAL_ERROR_FILE_PATTERN } from "../../constants/nextjs.js";
import { defineRule } from "../../utils/define-rule.js";
import { fileContainsJsxElements } from "../../utils/file-contains-jsx-elements.js";
import { normalizeFilename } from "../../utils/normalize-filename.js";
import type { Rule } from "../../utils/rule.js";
import type { RuleContext } from "../../utils/rule-context.js";
import type { EsTreeNodeOfType } from "../../utils/es-tree-node-of-type.js";
const REQUIRED_TAGS = ["html", "body"] as const;
export const nextjsGlobalErrorMissingHtmlBody = defineRule<Rule>({
id: "nextjs-global-error-missing-html-body",
title: "global-error.tsx missing <html>/<body>",
tags: ["test-noise"],
requires: ["nextjs"],
severity: "error",
recommendation:
"Wrap your error UI in `<html><body>...</body></html>`. The root layout is unmounted when global-error renders",
create: (context: RuleContext) => ({
Program(programNode: EsTreeNodeOfType<"Program">) {
const filename = normalizeFilename(context.filename ?? "");
if (!APP_DIRECTORY_PATTERN.test(filename)) return;
if (!GLOBAL_ERROR_FILE_PATTERN.test(filename)) return;
const foundTags = fileContainsJsxElements(programNode, REQUIRED_TAGS);
const missingTags = REQUIRED_TAGS.filter((tag) => !foundTags.has(tag)).map(
(tag) => `<${tag}>`,
);
if (missingTags.length > 0) {
context.report({
node: programNode,
message: `global-error.tsx is missing ${missingTags.join(" and ")}. The root layout unmounts on error, so this page renders broken HTML.`,
});
}
},
}),
});