Repository navigation
Expand file tree
/
Copy patheslint.config.js
More file actions
294 lines (286 loc) · 10.3 KB
/
Copy patheslint.config.js
File metadata and controls
294 lines (286 loc) · 10.3 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
import js from "@eslint/js";
import unusedImports from "eslint-plugin-unused-imports";
import globals from "globals";
// --- Project rules: layer boundaries and extensions.gnome.org review guidelines -------------------------
const SOURCES = ["extension.js", "prefs.js", "lib/**/*.js", "prefs/**/*.js"];
const SHELL_PROCESS = ["extension.js", "lib/**/*.js"];
const PREFS_PROCESS = ["prefs.js", "prefs/**/*.js"];
const noGtkInShell = {
regex: "^gi://(Gtk|Gdk|Adw)$",
message: "Gtk/Gdk/Adw inside the GNOME Shell process. Instant rejection on extensions.gnome.org. "
+ "Prefs only, thanks.",
};
const noGnome = (where) => [
{
regex: "^gi://",
message: `${where} does not touch GNOME. Ever. `
+ "Inject a Shell adapter through the constructor instead of importing gi:// here.",
},
{
regex: "^resource://",
message: `${where} importing GNOME Shell resources? No. That is lib/shell's job.`,
},
];
const noLayer = (layers, message) => ({ regex: `(^|/)(${layers.join("|")})/`, message });
// Regex patterns are case-insensitive by default, which would match resource:///org/gnome/Shell/Extensions.
const restrictedImports = (patterns) => ["error", { patterns: patterns.map((p) => ({ ...p, caseSensitive: true })) }];
const noTimeouts = [
{
object: "GLib",
property: "timeout_add",
message: "Arbitrary timeout. React to a signal or use a BEFORE_REDRAW later like a grown-up.",
},
{
object: "GLib",
property: "timeout_add_seconds",
message: "Arbitrary timeout, now in seconds. Still no. Signals or laters.",
},
];
const noDispose = [
{
property: "run_dispose",
message: "run_dispose()? Reviewers will demand a very good comment. Destroy things properly instead.",
},
];
const noSubprocess = [
{
object: "Gio",
property: "Subprocess",
message: "Spawning processes inside GNOME Shell. Absolutely not. If you must, do it in prefs.",
},
];
const guidelineRules = {
"no-restricted-globals": [
"error",
{
name: "imports",
message: "`imports.*` is the legacy module system (Lang, Mainloop, ByteArray...). Deprecated. "
+ "Use ES modules.",
},
{ name: "setTimeout", message: "setTimeout. Seriously? React to a signal or use a BEFORE_REDRAW later." },
{ name: "setInterval", message: "setInterval. Polling GNOME Shell like it's 2009. Use signals." },
],
"no-restricted-syntax": [
"error",
{
selector: "CallExpression[optional=true]",
message: "`?.()` on a call. Either the method exists (call it) or it doesn't (handle that explicitly). "
+ "Reviewers hate this one.",
},
{
selector: "BinaryExpression[left.type='UnaryExpression'][left.operator='typeof'][right.value='function']",
message: "`typeof x === 'function'` feature sniffing. "
+ "Target the supported GNOME versions and call the API like you mean it.",
},
{
selector: "BinaryExpression[right.type='UnaryExpression'][right.operator='typeof'][left.value='function']",
message: "Feature sniffing, Yoda style. Same verdict: call the API like you mean it.",
},
],
"no-console": [
"error",
],
};
// ESLint has no core max-returns rule, so it lives here as a local plugin.
const projectPlugin = {
rules: {
"max-returns": {
meta: {
type: "suggestion",
schema: [{ type: "integer", minimum: 0 }],
messages: {
tooMany: "{{count}} return statements (max {{max}}). This function is a choose-your-own-adventure "
+ "book. Split it or use a guard and one exit.",
},
},
create(context) {
const max = context.options[0] ?? 3;
const stack = [];
const enter = (node) => stack.push({ node, count: 0 });
const exit = () => {
const { node, count } = stack.pop();
if (count > max) context.report({ node, messageId: "tooMany", data: { count, max } });
};
return {
":function": enter,
":function:exit": exit,
ReturnStatement() {
if (stack.length) stack[stack.length - 1].count++;
},
};
},
},
},
};
export default [
js.configs.recommended,
{
ignores: ["coverage/**"]
},
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
globals: {
...globals.browser,
...globals.node,
globalThis: "readonly",
log: "readonly",
logError: "readonly",
print: "readonly",
printerr: "readonly",
ARGV: "readonly"
}
},
plugins: {
"unused-imports": unusedImports,
project: projectPlugin,
},
rules: {
// Disable default no-unused-vars and use the plugin's one
"no-unused-vars": "off",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"error",
{
"vars": "all",
// Positional callbacks of GTK / Mutter signals: unused leading args are named _x
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all"
}
],
"no-undef": "error",
"no-empty": "error",
"no-constant-condition": "error",
"no-prototype-builtins": "warn",
"no-useless-escape": "warn",
"no-cond-assign": "warn",
"no-func-assign": "warn",
"no-import-assign": "warn",
"no-inner-declarations": "warn",
"no-unsafe-finally": "error",
"no-unsafe-negation": "error",
"no-dupe-keys": "error",
"preserve-caught-error": "warn",
"max-params": ["warn", 4],
"project/max-returns": ["warn", 4],
"max-len": ["warn", { "code": 120, "ignorePattern": "^(?=.*Logger\\.).{0,160}$" }],
"eqeqeq": ["error", "always"],
"no-shadow": "warn",
"prefer-const": "warn",
"no-var": "error"
}
},
{
files: SOURCES,
rules: {
...guidelineRules,
"no-restricted-properties": ["error", ...noTimeouts, ...noDispose],
},
},
{
files: ["lib/utils/logger.js"],
rules: { "no-console": "off" },
},
{
files: SHELL_PROCESS,
rules: {
"no-restricted-imports": restrictedImports([noGtkInShell]),
"no-restricted-properties": ["error", ...noTimeouts, ...noDispose, ...noSubprocess],
},
},
{
files: ["lib/domain/**/*.js"],
rules: {
"no-restricted-imports": restrictedImports([
...noGnome("The domain"),
noLayer(
["app", "shell"],
"The domain does not know lib/app or lib/shell exist. "
+ "Dependencies point inward, not wherever you feel like.",
),
]),
},
},
{
files: ["lib/app/**/*.js"],
rules: {
"no-restricted-imports": restrictedImports([
...noGnome("lib/app"),
noLayer(
["shell"],
"lib/app importing lib/shell? Backwards. Hand the Shell object in through the constructor.",
),
]),
},
},
{
files: ["lib/shell/**/*.js"],
rules: {
"no-restricted-imports": restrictedImports([
noGtkInShell,
noLayer(
["app"],
"Adapters poking into lib/app? Wire the Engine up in extension.js and keep lib/shell dumb.",
),
]),
},
},
{
// Code both processes run that needs GSettings: Gio and GLib only, which exist in both
files: ["lib/gio/**/*.js"],
rules: {
"no-restricted-imports": restrictedImports([
{
regex: "^gi://(?!(Gio|GLib)$)",
message: "lib/gio runs in the Shell and in prefs. Gio and GLib only; the rest is one process's.",
},
{
regex: "^resource://",
message: "lib/gio importing GNOME Shell resources? Prefs cannot load them. lib/shell's job.",
},
noLayer(["app", "shell"], "lib/gio is shared with prefs, which may not reach lib/app or lib/shell."),
]),
},
},
{
files: ["lib/utils/**/*.js"],
rules: {
"no-restricted-imports": restrictedImports(noGnome("lib/utils (used by prefs too)")),
},
},
{
files: PREFS_PROCESS,
rules: {
"no-restricted-imports": restrictedImports([
{
regex: "^gi://(Clutter|Meta|St|Shell)$",
message: "Clutter/Meta/St/Shell in prefs. Wrong process, it will blow up, "
+ "and reviewers will reject it. Shell side only.",
},
{
regex: "^resource:///org/gnome/shell/",
message: "resource:///org/gnome/shell is Shell-process only. Prefs cannot load it. Nice try.",
},
noLayer(
["lib/app", "lib/shell"],
"Prefs may import lib/domain and lib/utils. Leave the Engine and the Mutter adapters alone.",
),
]),
},
},
{
// GTK callbacks have fixed signatures; test helpers mirror Mutter's wide constructors.
files: [...PREFS_PROCESS, "tests/**/*.js"],
rules: { "max-params": "off" },
},
{
files: ["tests/**/*.js"],
rules: {
"max-len": "off",
"no-shadow": "off"
}
}
];