-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathno-bad-internal-links.js
More file actions
106 lines (97 loc) · 3.04 KB
/
no-bad-internal-links.js
File metadata and controls
106 lines (97 loc) · 3.04 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
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
/**
* ESLint rule: no-bad-internal-links
*
* Enforces correct internal link format in JSX:
* - No leading/trailing whitespace in any URL
* - Internal links must start with '/'
* - Internal links must not use .html extension
* - Internal links must end with '/'
*/
module.exports = {
meta: {
type: "problem",
docs: {
description:
"Enforce correct internal link format in JSX (absolute paths, no .html, trailing slash)",
},
messages: {
whitespace:
"Link has leading or trailing whitespace: '{{value}}'. Remove the extra spaces.",
noLeadingSlash:
"Internal link '{{value}}' should start with '/'. Did you mean '/{{suggested}}'?",
htmlExtension:
"Internal link '{{value}}' should not use .html extension. Did you mean '{{suggested}}'?",
noTrailingSlash:
"Internal link '{{value}}' should end with '/'. Did you mean '{{value}}/'?",
},
},
create(context) {
return {
JSXAttribute(node) {
const attrName = node.name && node.name.name;
if (attrName !== "href" && attrName !== "to") return;
// Only handle string literals, skip expressions like {`/path/${var}`}
if (
!node.value ||
node.value.type !== "Literal" ||
typeof node.value.value !== "string"
) {
return;
}
const value = node.value.value;
// Check for leading/trailing whitespace (applies to all URLs)
if (value !== value.trim()) {
context.report({ node, messageId: "whitespace", data: { value } });
return;
}
// Skip external URLs, anchors, and protocol-relative URLs
if (
value.startsWith("http://") ||
value.startsWith("https://") ||
value.startsWith("mailto:") ||
value.startsWith("tel:") ||
value.startsWith("#") ||
value.startsWith("//")
) {
return;
}
// Skip empty strings
if (value === "") return;
// --- Internal link checks ---
// Must start with /
if (!value.startsWith("/")) {
const stripped = value.replace(/\.html?$/, "");
const suggested = stripped.endsWith("/") ? stripped : stripped + "/";
context.report({
node,
messageId: "noLeadingSlash",
data: { value, suggested },
});
return;
}
// Must not end with .html
if (/\.html?$/.test(value)) {
const suggested = value.replace(/\.html?$/, "/");
context.report({
node,
messageId: "htmlExtension",
data: { value, suggested },
});
return;
}
// Must end with / (except paths with anchors or query strings)
if (
!value.endsWith("/") &&
!value.includes("#") &&
!value.includes("?")
) {
context.report({
node,
messageId: "noTrailingSlash",
data: { value },
});
}
},
};
},
};