-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy patheslint-plugin-hypr.mjs
More file actions
180 lines (158 loc) · 4.96 KB
/
eslint-plugin-hypr.mjs
File metadata and controls
180 lines (158 loc) · 4.96 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
const awaitTauriCommands = {
meta: {
type: "suggestion",
docs: {
description:
"Enforce await on Tauri IPC commands to prevent race conditions and freezes",
},
fixable: "code",
messages: {
missingAwait:
"Tauri command '{{name}}' must be awaited. Unawaited IPC calls can cause race conditions and freezes.",
},
},
createOnce(context) {
let tauriCommandImports;
return {
before() {
tauriCommandImports = new Set();
},
ImportDeclaration(node) {
const source = node.source.value;
const isTauriCommands =
source.startsWith("@hypr/plugin-") || source.endsWith("/tauri.gen");
if (isTauriCommands) {
for (const specifier of node.specifiers) {
if (
specifier.type === "ImportSpecifier" &&
specifier.imported.name === "commands"
) {
tauriCommandImports.add(specifier.local.name);
}
}
}
},
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.object.type === "Identifier" &&
tauriCommandImports.has(node.callee.object.name)
) {
const methodName = node.callee.property.name;
const current = node.parent;
if (current.type === "AwaitExpression") {
return;
}
if (
current.type === "ReturnStatement" ||
current.type === "ArrowFunctionExpression"
) {
return;
}
if (current.type === "ArrayExpression") {
return;
}
context.report({
node,
messageId: "missingAwait",
data: { name: methodName },
fix(fixer) {
if (current.type === "ExpressionStatement") {
const sourceCode =
context.sourceCode || context.getSourceCode();
const callText = sourceCode.getText(node);
return fixer.replaceText(node, `await ${callText}`);
}
return null;
},
});
}
},
};
},
};
const BANNED_TINYBASE_IMPORT_PREFIXES = [
"tinybase/ui-react",
"tinybase/synchronizers",
];
const BANNED_TINYBASE_EXACT_IMPORTS = new Set([
"tinybase/with-schemas",
"tinybase",
]);
const BANNED_MAIN_STORE_IMPORTS = new Set([
"~/store/tinybase/store/main",
"~/store/tinybase/hooks",
]);
const BANNED_MAIN_STORE_IMPORT_PREFIXES = ["~/store/tinybase/hooks/"];
const noRawTinybase = {
meta: {
type: "problem",
docs: {
description:
"Ban direct main-store TinyBase access outside designated boundary hooks. Consumers of ~/store/tinybase/store/main must go through domain hooks (~/<domain>/hooks/*) so the main store can be swapped in one place.",
},
messages: {
bannedImport:
"Raw TinyBase import '{{source}}' is not allowed in desktop consumer code. For main-store data, wrap reads/writes in a domain hook module (e.g. ~/<domain>/hooks/) and import from there instead.",
bannedMainStoreImport:
"Direct import of main-store module '{{source}}' is not allowed in consumer code. Use a domain hook from ~/<domain>/hooks/ instead. If this file is the boundary hook module, add its path to the rule's allowlist in .oxlintrc.json.",
},
},
create(context) {
return {
ImportDeclaration(node) {
if (node.importKind === "type") return;
const source = node.source.value;
if (typeof source !== "string") return;
if (BANNED_TINYBASE_EXACT_IMPORTS.has(source)) {
context.report({
node,
messageId: "bannedImport",
data: { source },
});
return;
}
for (const prefix of BANNED_TINYBASE_IMPORT_PREFIXES) {
if (source === prefix || source.startsWith(prefix + "/")) {
context.report({
node,
messageId: "bannedImport",
data: { source },
});
return;
}
}
const isBannedMainStore =
BANNED_MAIN_STORE_IMPORTS.has(source) ||
BANNED_MAIN_STORE_IMPORT_PREFIXES.some((prefix) =>
source.startsWith(prefix),
);
if (isBannedMainStore) {
const hasRuntimeSpecifier = node.specifiers.some(
(s) =>
s.type === "ImportNamespaceSpecifier" ||
s.type === "ImportDefaultSpecifier" ||
(s.type === "ImportSpecifier" && s.importKind !== "type"),
);
if (!hasRuntimeSpecifier) return;
context.report({
node,
messageId: "bannedMainStoreImport",
data: { source },
});
}
},
};
},
};
const plugin = {
meta: {
name: "hypr",
version: "1.0.0",
},
rules: {
"await-tauri-commands": awaitTauriCommands,
"no-raw-tinybase": noRawTinybase,
},
};
export default plugin;