-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint.rules.js
More file actions
298 lines (274 loc) · 12.9 KB
/
eslint.rules.js
File metadata and controls
298 lines (274 loc) · 12.9 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* Copyright 2026 Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Custom async page loader rule
export const noAsyncPageLoaderRule = {
meta: {
type: 'suggestion',
docs: {
description:
"Recommend avoiding async 'loader' and 'clientLoader' functions or returning Promises in page routes for better performance",
category: 'Best Practices',
},
schema: [],
messages: {
asyncOrPromiseLoader:
"For better performance in page routes consider avoiding asynchronous 'loader' or 'clientLoader' functions or returning Promises",
},
},
create(context) {
function checkFunction(node) {
// Flag async functions
if (node.async) {
context.report({ node, messageId: 'asyncOrPromiseLoader' });
return;
}
// Flag return statements returning new Promise(...)
if (node.body && node.body.type === 'BlockStatement') {
for (const stmt of node.body.body) {
if (
stmt.type === 'ReturnStatement' &&
stmt.argument &&
stmt.argument.type === 'NewExpression' &&
stmt.argument.callee.type === 'Identifier' &&
stmt.argument.callee.name === 'Promise'
) {
context.report({ node: stmt, messageId: 'asyncOrPromiseLoader' });
}
}
}
}
function handleExportedLoader(node, loaderFn) {
if (loaderFn) checkFunction(loaderFn);
}
function isLoaderFunction(name) {
return name === 'loader' || name === 'clientLoader';
}
return {
// Named exports
ExportNamedDeclaration(node) {
if (!node.declaration) return;
// export function loader() {} or export function clientLoader() {}
if (node.declaration.type === 'FunctionDeclaration') {
if (node.declaration.id?.name && isLoaderFunction(node.declaration.id.name)) {
handleExportedLoader(node, node.declaration);
}
}
// export const loader = () => {} or export const clientLoader = () => {} or function expression
if (node.declaration.type === 'VariableDeclaration') {
for (const declarator of node.declaration.declarations) {
if (
declarator.id.type === 'Identifier' &&
isLoaderFunction(declarator.id.name) &&
declarator.init &&
(declarator.init.type === 'ArrowFunctionExpression' ||
declarator.init.type === 'FunctionExpression')
) {
handleExportedLoader(node, declarator.init);
}
}
}
},
// Default export object: export default { loader: ..., clientLoader: ... }
ExportDefaultDeclaration(node) {
if (node.declaration.type === 'ObjectExpression') {
for (const prop of node.declaration.properties) {
if (
(prop.type === 'Property' || prop.type === 'ObjectProperty') &&
((prop.key.type === 'Identifier' && isLoaderFunction(prop.key.name)) ||
(prop.key.type === 'Literal' && isLoaderFunction(prop.key.value)))
) {
const value = prop.value;
if (value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression') {
handleExportedLoader(node, value);
}
}
}
}
},
};
},
};
// Custom no client actions rule
export const noClientActionsRule = {
meta: {
type: 'problem',
docs: {
description:
'For security reasons, it is recommended to use server actions instead of client actions in routes.',
category: 'Best Practices',
},
messages: {
noClientActions:
'For security reasons, it is recommended to use server actions instead of client actions in routes.',
},
schema: [], // no options
},
create(context) {
const name = 'clientAction';
return {
ExportNamedDeclaration(node) {
if (node.declaration) {
// Case: export function clientAction() {}
if (node.declaration.type === 'FunctionDeclaration' && node.declaration.id?.name === name) {
context.report({ node: node.declaration.id, messageId: 'noClientActions' });
}
// Case: export const clientAction = ...
if (node.declaration.type === 'VariableDeclaration') {
for (const d of node.declaration.declarations) {
if (d.id.name === name) {
context.report({ node: d.id, messageId: 'noClientActions' });
}
}
}
}
// Case: export { clientAction } / export { foo as clientAction }
if (node.specifiers) {
for (const s of node.specifiers) {
if (s.exported && s.exported.type === 'Identifier' && s.exported.name === name) {
context.report({ node: s.exported, messageId: 'noClientActions' });
}
}
}
},
};
},
};
// Custom no client loaders rule
export const noClientLoadersRule = {
meta: {
type: 'problem',
docs: {
description:
'For better perceived performance and security reasons, it is recommended to use use server loaders instead of client loaders in routes.',
category: 'Best Practices',
},
messages: {
noClientLoaders:
'For better perceived performance and security reasons, it is recommended to use use server loaders instead of client loaders in routes.',
},
schema: [], // no options
},
create(context) {
const name = 'clientLoader';
return {
ExportNamedDeclaration(node) {
if (node.declaration) {
// Case: export function clientLoader() {}
if (node.declaration.type === 'FunctionDeclaration' && node.declaration.id?.name === name) {
context.report({ node: node.declaration.id, messageId: 'noClientLoaders' });
}
// Case: export const clientLoader = ...
if (node.declaration.type === 'VariableDeclaration') {
for (const d of node.declaration.declarations) {
if (d.id.name === name) {
context.report({ node: d.id, messageId: 'noClientLoaders' });
}
}
}
}
// Case: export { clientLoader } / export { foo as clientLoader }
if (node.specifiers) {
for (const s of node.specifiers) {
if (s.exported && s.exported.type === 'Identifier' && s.exported.name === name) {
context.report({ node: s.exported, messageId: 'noClientLoaders' });
}
}
}
},
};
},
};
// Custom color linting rule
export const colorLinterRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow hardcoded Tailwind color utilities',
category: 'Best Practices',
recommended: true,
},
fixable: null,
schema: [],
messages: {
hardcodedColor: 'Hardcoded color utility "{{className}}" detected. Use themeable Shadcn utilities instead.',
},
},
create(context) {
return {
// Only check className attributes and cn() calls, not all strings
JSXAttribute(node) {
if (node.name.name === 'className' && node.value) {
if (node.value.type === 'Literal' && typeof node.value.value === 'string') {
checkForHardcodedColors(node.value.value, context, node.value);
} else if (node.value.type === 'JSXExpressionContainer' && node.value.expression) {
if (
node.value.expression.type === 'Literal' &&
typeof node.value.expression.value === 'string'
) {
checkForHardcodedColors(node.value.expression.value, context, node.value.expression);
}
}
}
},
// Check cn() function calls
CallExpression(node) {
if (node.callee.name === 'cn' || (node.callee.property && node.callee.property.name === 'cn')) {
node.arguments.forEach((arg) => {
if (arg.type === 'Literal' && typeof arg.value === 'string') {
checkForHardcodedColors(arg.value, context, arg);
}
});
}
},
};
},
};
function checkForHardcodedColors(content, context, node) {
const classes = content.split(/\s+/);
classes.forEach((className) => {
if (!className) return;
if (isHardcodedColor(className)) {
context.report({
node,
messageId: 'hardcodedColor',
data: { className },
});
}
});
}
function isHardcodedColor(className) {
const forbiddenPatterns = [
/^bg-(gray|red|green|blue|yellow|purple|pink|indigo|teal|orange|cyan|slate|zinc|neutral|stone|emerald|amber|lime|rose|violet|fuchsia|sky)-(50|100|200|300|400|500|600|700|800|900|950)$/,
/^text-(gray|red|green|blue|yellow|purple|pink|indigo|teal|orange|cyan|slate|zinc|neutral|stone|emerald|amber|lime|rose|violet|fuchsia|sky)-(50|100|200|300|400|500|600|700|800|900|950)$/,
/^border-(gray|red|green|blue|yellow|purple|pink|indigo|teal|orange|cyan|slate|zinc|neutral|stone|emerald|amber|lime|rose|violet|fuchsia|sky)-(50|100|200|300|400|500|600|700|800|900|950)$/,
/^ring-(gray|red|green|blue|yellow|purple|pink|indigo|teal|orange|cyan|slate|zinc|neutral|stone|emerald|amber|lime|rose|violet|fuchsia|sky)-(50|100|200|300|400|500|600|700|800|900|950)$/,
/^(bg|text|border|ring|shadow|divide|placeholder|accent|caret|decoration|outline|fill|stroke)-(white|black|transparent|current)$/,
];
const isForbidden = forbiddenPatterns.some((pattern) => pattern.test(className));
if (!isForbidden) {
return false;
}
const allowedPatterns = [
/^(bg|text|border|ring|shadow|divide|placeholder|accent|caret|decoration|outline|fill|stroke)-(background|foreground|card|card-foreground|popover|popover-foreground|primary|primary-foreground|secondary|secondary-foreground|muted|muted-foreground|accent|accent-foreground|destructive|border|input|ring|chart-1|chart-2|chart-3|chart-4|chart-5|sidebar|sidebar-foreground|sidebar-primary|sidebar-primary-foreground|sidebar-accent|sidebar-accent-foreground|sidebar-border|sidebar-ring)$/,
/^(bg|text|border|ring|shadow|divide|placeholder|accent|caret|decoration|outline|fill|stroke)-\[var\(--[^)]+\)\]$/,
/^(bg|text|border|ring|shadow|divide|placeholder|accent|caret|decoration|outline|fill|stroke)-\[\$\{[^}]+\}\]$/,
/^(bg|text|border|ring|shadow|divide|placeholder|accent|caret|decoration|outline|fill|stroke)-\[--[^\]]+\]$/,
// Valid CSS keywords - these should always be allowed
/^(bg|text|border|ring|shadow|divide|placeholder|accent|caret|decoration|outline|fill|stroke)-(transparent|current)$/,
];
const isAllowed = allowedPatterns.some((pattern) => pattern.test(className));
return !isAllowed;
}