hook-o-gnese 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +182 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +237 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/engine-DJFFKwTZ.mjs +114 -0
- package/dist/engine-DJFFKwTZ.mjs.map +1 -0
- package/dist/engine.d.mts +27 -0
- package/dist/engine.mjs +2 -0
- package/dist/index.d.mts +3993 -0
- package/dist/index.mjs +357 -0
- package/dist/index.mjs.map +1 -0
- package/dist/registry-iRG6wil9.mjs +460 -0
- package/dist/registry-iRG6wil9.mjs.map +1 -0
- package/package.json +55 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { t as ALL_RULES } from "./registry-iRG6wil9.mjs";
|
|
2
|
+
//#region node_modules/.deno/@oxlint+plugins@1.63.0/node_modules/@oxlint/plugins/index.js
|
|
3
|
+
const EMPTY_VISITOR = {};
|
|
4
|
+
/**
|
|
5
|
+
* Convert a plugin which used Oxlint's `createOnce` API to also work with ESLint.
|
|
6
|
+
*
|
|
7
|
+
* If any of the plugin's rules use the Oxlint alternative `createOnce` API,
|
|
8
|
+
* add ESLint-compatible `create` methods to those rules, which delegate to `createOnce`.
|
|
9
|
+
* This makes the plugin compatible with ESLint.
|
|
10
|
+
*
|
|
11
|
+
* The `plugin` object passed in is mutated in-place.
|
|
12
|
+
*
|
|
13
|
+
* @param plugin - Plugin to convert
|
|
14
|
+
* @returns Plugin with all rules having `create` method
|
|
15
|
+
* @throws {Error} If `plugin` is not an object, or `plugin.rules` is not an object
|
|
16
|
+
*/
|
|
17
|
+
function eslintCompatPlugin(plugin) {
|
|
18
|
+
if (typeof plugin != "object" || !plugin) throw Error("Plugin must be an object");
|
|
19
|
+
let { rules } = plugin;
|
|
20
|
+
if (typeof rules != "object" || !rules) throw Error("Plugin must have an object as `rules` property");
|
|
21
|
+
let afterHooksState = new AfterHooksState();
|
|
22
|
+
for (let ruleName in rules) Object.hasOwn(rules, ruleName) && convertRule(rules[ruleName], afterHooksState);
|
|
23
|
+
return plugin;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Class containing state for tracking if any `after` hooks for a plugin's rules need to be called.
|
|
27
|
+
*
|
|
28
|
+
* # Aims
|
|
29
|
+
*
|
|
30
|
+
* Aims are:
|
|
31
|
+
* 1. `after` hook of each rule runs after all other AST visit functions, and CFG event handlers.
|
|
32
|
+
* 2. `after` hooks for *all* a plugin's rules run after *all* that plugin's rules have completed visiting AST.
|
|
33
|
+
* 3. `after` hooks for *all* a plugin's rules run before *any* of plugin's rules begin linting another file.
|
|
34
|
+
* 4. In the case of an error during AST traversal, `after` hooks are always still run.
|
|
35
|
+
*
|
|
36
|
+
* The above exactly matches the behavior when running a `createOnce` rule in Oxlint.
|
|
37
|
+
*
|
|
38
|
+
* # Why this is important
|
|
39
|
+
*
|
|
40
|
+
* All the complication comes from ensuring `after` hooks run even after an error during AST traversal.
|
|
41
|
+
*
|
|
42
|
+
* In ESLint CLI, an error will crash the process, so it doesn't particularly matter if `after` hooks run or not,
|
|
43
|
+
* but language servers will typically swallow errors, and keep the process running.
|
|
44
|
+
*
|
|
45
|
+
* Rules using `before` and `after` hooks will often rely on both hooks running in a predictable order,
|
|
46
|
+
* to maintain some internal state. For example, they may use `before` and `after` hooks to maintain a per-file
|
|
47
|
+
* cache of data which is shared between rules. The cache use case is why rule (2) above is important.
|
|
48
|
+
*
|
|
49
|
+
* Below is an example of using `before` and `after` hooks to maintain a per-file cache, shared between rules.
|
|
50
|
+
* It relies on all `before` hooks running before any rule starts visiting the AST,
|
|
51
|
+
* and all `after` hooks running after all rules have finished visiting the AST.
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* let cache: Data | null = null;
|
|
55
|
+
*
|
|
56
|
+
* let numRunningRules = 0;
|
|
57
|
+
*
|
|
58
|
+
* const setupCache = (context) => {
|
|
59
|
+
* if (cache === null) cache = new Data(context);
|
|
60
|
+
* numRunningRules++;
|
|
61
|
+
* };
|
|
62
|
+
*
|
|
63
|
+
* const teardownCache = () => {
|
|
64
|
+
* numRunningRules--;
|
|
65
|
+
* if (numRunningRules === 0) cache = null;
|
|
66
|
+
* };
|
|
67
|
+
*
|
|
68
|
+
* const rule1 = {
|
|
69
|
+
* createOnce(context) {
|
|
70
|
+
* return {
|
|
71
|
+
* before() {
|
|
72
|
+
* setupCache(context);
|
|
73
|
+
* },
|
|
74
|
+
* Identifier(node) {
|
|
75
|
+
* // Use `cache`
|
|
76
|
+
* },
|
|
77
|
+
* after: teardownCache,
|
|
78
|
+
* };
|
|
79
|
+
* },
|
|
80
|
+
* };
|
|
81
|
+
*
|
|
82
|
+
* const rule2 = {
|
|
83
|
+
* // Same as above
|
|
84
|
+
* };
|
|
85
|
+
*
|
|
86
|
+
* const rule3 = {
|
|
87
|
+
* // Same as above
|
|
88
|
+
* };
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* If `after` hooks did not always run, the next lint run could get stale state, and malfunction.
|
|
92
|
+
* If `after` hooks ran in the wrong order (e.g. after some `before` hooks for next file),
|
|
93
|
+
* `numRunningRules` would never get to 0, and cache would never be cleared.
|
|
94
|
+
*
|
|
95
|
+
* Note that because all rules run together in a single AST traversal, if a rule from plugin X throws an error,
|
|
96
|
+
* it can disrupt rules from plugin Y. This would make it hard to debug.
|
|
97
|
+
*
|
|
98
|
+
* # Mechanism
|
|
99
|
+
*
|
|
100
|
+
* ## Initialization
|
|
101
|
+
*
|
|
102
|
+
* Rules with an `after` hook register themselves by:
|
|
103
|
+
*
|
|
104
|
+
* 1. Calling `registerResetFunction` to register a function to run `after` hook and clean up internal state.
|
|
105
|
+
* This call adds the reset fn to `resetFunctions`, and adds `AFTER_HOOK_INACTIVE` to `pendingStates`.
|
|
106
|
+
* 2. Adding an `onCodePathEnd` CFG event handler to the visitor which calls `ruleFinished` at end of AST traversal.
|
|
107
|
+
*
|
|
108
|
+
* ## Per-file setup
|
|
109
|
+
*
|
|
110
|
+
* Before linting a file, `create` will call `setupAfterHook` which is created by `createContextAndVisitor`.
|
|
111
|
+
* This registers that the `after` hook for the rule needs to run, by setting `pendingStates[ruleIndex]`
|
|
112
|
+
* to `AFTER_HOOK_PENDING`, and incrementing `pendingCount`.
|
|
113
|
+
*
|
|
114
|
+
* If a cleanup microtask has not been scheduled yet, one is scheduled now (see reason below).
|
|
115
|
+
*
|
|
116
|
+
* ## Normal operation
|
|
117
|
+
*
|
|
118
|
+
* AST traversal for each rule ends with `ruleFinished` hook being called from `onCodePathEnd` CFG event handler.
|
|
119
|
+
* It increments `lintFinishedCount`. If `lintFinishedCount` equals `pendingCount`, all rules have finished linting
|
|
120
|
+
* the file, and `reset` is called, which calls all the pending `after` hooks.
|
|
121
|
+
*
|
|
122
|
+
* ## Error handling
|
|
123
|
+
*
|
|
124
|
+
* If an error is thrown during AST traversal, we ensure that `after` hooks are still run by 2 mechanisms:
|
|
125
|
+
*
|
|
126
|
+
* ### 1. Next microtick
|
|
127
|
+
*
|
|
128
|
+
* Before any rules began linting files, a microtask was scheduled, which runs on next micro-tick.
|
|
129
|
+
* All language servers we're aware of run each lint task in a separate tick, so this microtask will run in next tick
|
|
130
|
+
* after a linting run, before the next lint task starts.
|
|
131
|
+
*
|
|
132
|
+
* If the linting run completed successfully, the microtask does nothing.
|
|
133
|
+
*
|
|
134
|
+
* But if an error was thrown during AST traversal, this will be visible from the state of `pendingCount`.
|
|
135
|
+
* The microtask will run any `after` hooks which need to be run, and reset state to reflect that there are
|
|
136
|
+
* no more pending `after` hooks.
|
|
137
|
+
*
|
|
138
|
+
* ### 2. Fallback: Next lint run
|
|
139
|
+
*
|
|
140
|
+
* Before linting any file, the state of `pendingCount` is checked.
|
|
141
|
+
* If any `after` hooks are still pending, they are run immediately.
|
|
142
|
+
* They're run before the `context` objects in `createOnce` closures are updated to the next file,
|
|
143
|
+
* so they run with access to the old `context` object from the last file.
|
|
144
|
+
*
|
|
145
|
+
* This fallback should not be required, but it's included as "belt and braces", to handle if any language server
|
|
146
|
+
* or other environment running ESLint programmatically, does not pause a tick between linting runs.
|
|
147
|
+
*/
|
|
148
|
+
var AfterHooksState = class {
|
|
149
|
+
resetFunctions = [];
|
|
150
|
+
pendingStates = [];
|
|
151
|
+
pendingCount = 0;
|
|
152
|
+
lintFinishedCount = 0;
|
|
153
|
+
resetIsScheduled = !1;
|
|
154
|
+
sourceCode = null;
|
|
155
|
+
resetMicrotask = this.resetMicrotaskImpl.bind(this);
|
|
156
|
+
/**
|
|
157
|
+
* Register a function to run `after` hook for a rule, and reset state.
|
|
158
|
+
* @param reset - Function to run `after` hook and reset state
|
|
159
|
+
* @returns Index of rule
|
|
160
|
+
*/
|
|
161
|
+
registerResetFunction(reset) {
|
|
162
|
+
let { pendingStates } = this, index = pendingStates.length;
|
|
163
|
+
return pendingStates.push(0), this.resetFunctions.push(reset), index;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Register that a rule with `after` hook has completed linting a file.
|
|
167
|
+
* Called by `onCodePathEnd` CFG event handler which is added to visitor for rules with `after` hooks.
|
|
168
|
+
*
|
|
169
|
+
* If all rules with an `after` hook which needs to be run have completed linting the file, run all `after` hooks.
|
|
170
|
+
*/
|
|
171
|
+
ruleFinished() {
|
|
172
|
+
this.lintFinishedCount++, this.lintFinishedCount === this.pendingCount && this.reset(!1);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Call all reset functions where corresponding entry in `pendingStates` is `AFTER_HOOK_PENDING`.
|
|
176
|
+
* Should only be called when some `after` hooks are pending.
|
|
177
|
+
*
|
|
178
|
+
* @param ignoreErrors - `true` to catch and silently ignore any errors which occur in `after` hooks.
|
|
179
|
+
* `false` to throw them,
|
|
180
|
+
* @throws {unknown} If `ignoreErrors` is `false` and an error occurs in any `after` hooks.
|
|
181
|
+
*/
|
|
182
|
+
reset(ignoreErrors) {
|
|
183
|
+
this.pendingCount;
|
|
184
|
+
let { resetFunctions, pendingStates } = this, hooksLen = pendingStates.length, hasError = !1, error;
|
|
185
|
+
for (let i = 0; i < hooksLen; i++) if (pendingStates[i] !== 0) {
|
|
186
|
+
pendingStates[i] = 0;
|
|
187
|
+
try {
|
|
188
|
+
resetFunctions[i]();
|
|
189
|
+
} catch (e) {
|
|
190
|
+
hasError === !1 && (hasError = !0, error = e);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (this.pendingCount = 0, this.lintFinishedCount = 0, this.sourceCode = null, hasError === !0 && ignoreErrors === !1) throw error;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Schedule a microtask to run `reset` functions.
|
|
197
|
+
*/
|
|
198
|
+
scheduleReset() {
|
|
199
|
+
queueMicrotask(this.resetMicrotask), this.resetIsScheduled = !0;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Function which is scheduled as the cleanup microtask.
|
|
203
|
+
* `scheduleReset` uses `resetMicrotask` which is this method bound to `this`.
|
|
204
|
+
*/
|
|
205
|
+
resetMicrotaskImpl() {
|
|
206
|
+
this.resetIsScheduled = !1, this.pendingCount !== 0 && this.reset(!0);
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Convert a rule.
|
|
211
|
+
*
|
|
212
|
+
* The `rule` object passed in is mutated in-place.
|
|
213
|
+
*
|
|
214
|
+
* @param rule - Rule to convert
|
|
215
|
+
* @param afterHooksState - State of `after` hooks
|
|
216
|
+
* @throws {Error} If `rule` is not an object
|
|
217
|
+
*/
|
|
218
|
+
function convertRule(rule, afterHooksState) {
|
|
219
|
+
if (typeof rule != "object" || !rule) throw Error("Rule must be an object");
|
|
220
|
+
if ("create" in rule) return;
|
|
221
|
+
let context = null, visitor, beforeHook, setupAfterHook;
|
|
222
|
+
rule.create = (eslintContext) => {
|
|
223
|
+
context === null && ({context, visitor, beforeHook, setupAfterHook} = createContextAndVisitor(rule, afterHooksState));
|
|
224
|
+
let eslintFileContext = Object.getPrototypeOf(eslintContext);
|
|
225
|
+
if (setupAfterHook !== null) {
|
|
226
|
+
let { sourceCode } = eslintFileContext;
|
|
227
|
+
afterHooksState.sourceCode !== sourceCode && (afterHooksState.sourceCode = sourceCode, afterHooksState.pendingCount !== 0 && afterHooksState.reset(!0));
|
|
228
|
+
}
|
|
229
|
+
return Object.defineProperties(context, {
|
|
230
|
+
id: { value: eslintContext.id },
|
|
231
|
+
options: { value: eslintContext.options },
|
|
232
|
+
report: { value: eslintContext.report }
|
|
233
|
+
}), Object.setPrototypeOf(context, eslintFileContext), beforeHook !== null && beforeHook() === !1 ? EMPTY_VISITOR : (setupAfterHook !== null && (setupAfterHook(eslintFileContext.sourceCode.ast), afterHooksState.resetIsScheduled === !1 && afterHooksState.scheduleReset()), visitor);
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
const FILE_CONTEXT = Object.freeze({
|
|
237
|
+
get filename() {
|
|
238
|
+
throw Error("Cannot access `context.filename` in `createOnce`");
|
|
239
|
+
},
|
|
240
|
+
getFilename() {
|
|
241
|
+
throw Error("Cannot call `context.getFilename` in `createOnce`");
|
|
242
|
+
},
|
|
243
|
+
get physicalFilename() {
|
|
244
|
+
throw Error("Cannot access `context.physicalFilename` in `createOnce`");
|
|
245
|
+
},
|
|
246
|
+
getPhysicalFilename() {
|
|
247
|
+
throw Error("Cannot call `context.getPhysicalFilename` in `createOnce`");
|
|
248
|
+
},
|
|
249
|
+
get cwd() {
|
|
250
|
+
throw Error("Cannot access `context.cwd` in `createOnce`");
|
|
251
|
+
},
|
|
252
|
+
getCwd() {
|
|
253
|
+
throw Error("Cannot call `context.getCwd` in `createOnce`");
|
|
254
|
+
},
|
|
255
|
+
get sourceCode() {
|
|
256
|
+
throw Error("Cannot access `context.sourceCode` in `createOnce`");
|
|
257
|
+
},
|
|
258
|
+
getSourceCode() {
|
|
259
|
+
throw Error("Cannot call `context.getSourceCode` in `createOnce`");
|
|
260
|
+
},
|
|
261
|
+
get languageOptions() {
|
|
262
|
+
throw Error("Cannot access `context.languageOptions` in `createOnce`");
|
|
263
|
+
},
|
|
264
|
+
get settings() {
|
|
265
|
+
throw Error("Cannot access `context.settings` in `createOnce`");
|
|
266
|
+
},
|
|
267
|
+
extend(extension) {
|
|
268
|
+
return Object.freeze(Object.assign(Object.create(this), extension));
|
|
269
|
+
},
|
|
270
|
+
get parserOptions() {
|
|
271
|
+
throw Error("Cannot access `context.parserOptions` in `createOnce`");
|
|
272
|
+
},
|
|
273
|
+
get parserPath() {
|
|
274
|
+
throw Error("Cannot access `context.parserPath` in `createOnce`");
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
/**
|
|
278
|
+
* Call `createOnce` method of rule, and return `Context`, `Visitor`, and `beforeHook` (if any).
|
|
279
|
+
*
|
|
280
|
+
* @param rule - Rule with `createOnce` method
|
|
281
|
+
* @param afterHooksState - State of `after` hooks
|
|
282
|
+
* @returns Object with `context`, `visitor`, and `beforeHook` properties,
|
|
283
|
+
* and `setupAfterHook` function if visitor has an `after` hook
|
|
284
|
+
*/
|
|
285
|
+
function createContextAndVisitor(rule, afterHooksState) {
|
|
286
|
+
let { createOnce } = rule;
|
|
287
|
+
if (createOnce == null) throw Error("Rules must define either a `create` or `createOnce` method");
|
|
288
|
+
if (typeof createOnce != "function") throw Error("Rule `createOnce` property must be a function");
|
|
289
|
+
let context = Object.create(FILE_CONTEXT, {
|
|
290
|
+
id: {
|
|
291
|
+
value: null,
|
|
292
|
+
enumerable: !0,
|
|
293
|
+
configurable: !0
|
|
294
|
+
},
|
|
295
|
+
options: {
|
|
296
|
+
value: null,
|
|
297
|
+
enumerable: !0,
|
|
298
|
+
configurable: !0
|
|
299
|
+
},
|
|
300
|
+
report: {
|
|
301
|
+
value() {
|
|
302
|
+
throw Error("Cannot report errors in `createOnce`");
|
|
303
|
+
},
|
|
304
|
+
enumerable: !0,
|
|
305
|
+
configurable: !0
|
|
306
|
+
}
|
|
307
|
+
}), { before: beforeHook, after: afterHook, ...visitor } = createOnce.call(rule, context);
|
|
308
|
+
if (beforeHook === void 0) beforeHook = null;
|
|
309
|
+
else if (beforeHook !== null && typeof beforeHook != "function") throw Error("`before` property of visitor must be a function if defined");
|
|
310
|
+
let setupAfterHook = null;
|
|
311
|
+
if (afterHook != null) {
|
|
312
|
+
if (typeof afterHook != "function") throw Error("`after` property of visitor must be a function if defined");
|
|
313
|
+
let program = null, ruleIndex = afterHooksState.registerResetFunction(() => {
|
|
314
|
+
program = null, afterHook();
|
|
315
|
+
});
|
|
316
|
+
setupAfterHook = (ast) => {
|
|
317
|
+
program = ast, afterHooksState.pendingStates[ruleIndex] = 1, afterHooksState.pendingCount++;
|
|
318
|
+
};
|
|
319
|
+
let onCodePathEnd = visitor.onCodePathEnd;
|
|
320
|
+
visitor.onCodePathEnd = onCodePathEnd == null ? function(_codePath, node) {
|
|
321
|
+
node === program && afterHooksState.ruleFinished();
|
|
322
|
+
} : function(codePath, node) {
|
|
323
|
+
onCodePathEnd.call(this, codePath, node), node === program && afterHooksState.ruleFinished();
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
context,
|
|
328
|
+
visitor,
|
|
329
|
+
beforeHook,
|
|
330
|
+
setupAfterHook
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
//#endregion
|
|
334
|
+
//#region src/index.ts
|
|
335
|
+
const plugin = eslintCompatPlugin({
|
|
336
|
+
meta: { name: "hook-o-gnese" },
|
|
337
|
+
rules: ALL_RULES
|
|
338
|
+
});
|
|
339
|
+
const recommended = {
|
|
340
|
+
jsPlugins: ["./node_modules/hook-o-gnese/dist/index.mjs"],
|
|
341
|
+
options: {
|
|
342
|
+
typeAware: true,
|
|
343
|
+
typeCheck: true
|
|
344
|
+
},
|
|
345
|
+
rules: {
|
|
346
|
+
"hook-o-gnese/no-fat-effects": "warn",
|
|
347
|
+
"hook-o-gnese/state-scatter": "warn",
|
|
348
|
+
"hook-o-gnese/hook-coupling": "error",
|
|
349
|
+
"hook-o-gnese/custom-hook-depth": ["warn", { maxDepth: 3 }],
|
|
350
|
+
"typescript/no-floating-promises": "error",
|
|
351
|
+
"typescript/no-misused-promises": "error"
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
//#endregion
|
|
355
|
+
export { plugin as default, recommended };
|
|
356
|
+
|
|
357
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../node_modules/.deno/@oxlint+plugins@1.63.0/node_modules/@oxlint/plugins/index.js","../src/index.ts"],"sourcesContent":["//#region src-js/package/define.ts\n/**\n* Define a plugin.\n*\n* No-op function, just to provide type safety. Input is passed through unchanged.\n*\n* @param plugin - Plugin to define\n* @returns Same plugin as passed in\n*/\nfunction definePlugin(plugin) {\n\treturn plugin;\n}\n/**\n* Define a rule.\n*\n* No-op function, just to provide type safety. Input is passed through unchanged.\n*\n* @param rule - Rule to define\n* @returns Same rule as passed in\n*/\nfunction defineRule(rule) {\n\treturn rule;\n}\n//#endregion\n//#region src-js/package/compat.ts\nconst EMPTY_VISITOR = {};\n/**\n* Convert a plugin which used Oxlint's `createOnce` API to also work with ESLint.\n*\n* If any of the plugin's rules use the Oxlint alternative `createOnce` API,\n* add ESLint-compatible `create` methods to those rules, which delegate to `createOnce`.\n* This makes the plugin compatible with ESLint.\n*\n* The `plugin` object passed in is mutated in-place.\n*\n* @param plugin - Plugin to convert\n* @returns Plugin with all rules having `create` method\n* @throws {Error} If `plugin` is not an object, or `plugin.rules` is not an object\n*/\nfunction eslintCompatPlugin(plugin) {\n\tif (typeof plugin != \"object\" || !plugin) throw Error(\"Plugin must be an object\");\n\tlet { rules } = plugin;\n\tif (typeof rules != \"object\" || !rules) throw Error(\"Plugin must have an object as `rules` property\");\n\tlet afterHooksState = new AfterHooksState();\n\tfor (let ruleName in rules) Object.hasOwn(rules, ruleName) && convertRule(rules[ruleName], afterHooksState);\n\treturn plugin;\n}\n/**\n* Class containing state for tracking if any `after` hooks for a plugin's rules need to be called.\n*\n* # Aims\n*\n* Aims are:\n* 1. `after` hook of each rule runs after all other AST visit functions, and CFG event handlers.\n* 2. `after` hooks for *all* a plugin's rules run after *all* that plugin's rules have completed visiting AST.\n* 3. `after` hooks for *all* a plugin's rules run before *any* of plugin's rules begin linting another file.\n* 4. In the case of an error during AST traversal, `after` hooks are always still run.\n*\n* The above exactly matches the behavior when running a `createOnce` rule in Oxlint.\n*\n* # Why this is important\n*\n* All the complication comes from ensuring `after` hooks run even after an error during AST traversal.\n*\n* In ESLint CLI, an error will crash the process, so it doesn't particularly matter if `after` hooks run or not,\n* but language servers will typically swallow errors, and keep the process running.\n*\n* Rules using `before` and `after` hooks will often rely on both hooks running in a predictable order,\n* to maintain some internal state. For example, they may use `before` and `after` hooks to maintain a per-file\n* cache of data which is shared between rules. The cache use case is why rule (2) above is important.\n*\n* Below is an example of using `before` and `after` hooks to maintain a per-file cache, shared between rules.\n* It relies on all `before` hooks running before any rule starts visiting the AST,\n* and all `after` hooks running after all rules have finished visiting the AST.\n*\n* ```ts\n* let cache: Data | null = null;\n*\n* let numRunningRules = 0;\n*\n* const setupCache = (context) => {\n* if (cache === null) cache = new Data(context);\n* numRunningRules++;\n* };\n*\n* const teardownCache = () => {\n* numRunningRules--;\n* if (numRunningRules === 0) cache = null;\n* };\n*\n* const rule1 = {\n* createOnce(context) {\n* return {\n* before() {\n* setupCache(context);\n* },\n* Identifier(node) {\n* // Use `cache`\n* },\n* after: teardownCache,\n* };\n* },\n* };\n*\n* const rule2 = {\n* // Same as above\n* };\n*\n* const rule3 = {\n* // Same as above\n* };\n* ```\n*\n* If `after` hooks did not always run, the next lint run could get stale state, and malfunction.\n* If `after` hooks ran in the wrong order (e.g. after some `before` hooks for next file),\n* `numRunningRules` would never get to 0, and cache would never be cleared.\n*\n* Note that because all rules run together in a single AST traversal, if a rule from plugin X throws an error,\n* it can disrupt rules from plugin Y. This would make it hard to debug.\n*\n* # Mechanism\n*\n* ## Initialization\n*\n* Rules with an `after` hook register themselves by:\n*\n* 1. Calling `registerResetFunction` to register a function to run `after` hook and clean up internal state.\n* This call adds the reset fn to `resetFunctions`, and adds `AFTER_HOOK_INACTIVE` to `pendingStates`.\n* 2. Adding an `onCodePathEnd` CFG event handler to the visitor which calls `ruleFinished` at end of AST traversal.\n*\n* ## Per-file setup\n*\n* Before linting a file, `create` will call `setupAfterHook` which is created by `createContextAndVisitor`.\n* This registers that the `after` hook for the rule needs to run, by setting `pendingStates[ruleIndex]`\n* to `AFTER_HOOK_PENDING`, and incrementing `pendingCount`.\n*\n* If a cleanup microtask has not been scheduled yet, one is scheduled now (see reason below).\n*\n* ## Normal operation\n*\n* AST traversal for each rule ends with `ruleFinished` hook being called from `onCodePathEnd` CFG event handler.\n* It increments `lintFinishedCount`. If `lintFinishedCount` equals `pendingCount`, all rules have finished linting\n* the file, and `reset` is called, which calls all the pending `after` hooks.\n*\n* ## Error handling\n*\n* If an error is thrown during AST traversal, we ensure that `after` hooks are still run by 2 mechanisms:\n*\n* ### 1. Next microtick\n*\n* Before any rules began linting files, a microtask was scheduled, which runs on next micro-tick.\n* All language servers we're aware of run each lint task in a separate tick, so this microtask will run in next tick\n* after a linting run, before the next lint task starts.\n*\n* If the linting run completed successfully, the microtask does nothing.\n*\n* But if an error was thrown during AST traversal, this will be visible from the state of `pendingCount`.\n* The microtask will run any `after` hooks which need to be run, and reset state to reflect that there are\n* no more pending `after` hooks.\n*\n* ### 2. Fallback: Next lint run\n*\n* Before linting any file, the state of `pendingCount` is checked.\n* If any `after` hooks are still pending, they are run immediately.\n* They're run before the `context` objects in `createOnce` closures are updated to the next file,\n* so they run with access to the old `context` object from the last file.\n*\n* This fallback should not be required, but it's included as \"belt and braces\", to handle if any language server\n* or other environment running ESLint programmatically, does not pause a tick between linting runs.\n*/\nvar AfterHooksState = class {\n\tresetFunctions = [];\n\tpendingStates = [];\n\tpendingCount = 0;\n\tlintFinishedCount = 0;\n\tresetIsScheduled = !1;\n\tsourceCode = null;\n\tresetMicrotask = this.resetMicrotaskImpl.bind(this);\n\t/**\n\t* Register a function to run `after` hook for a rule, and reset state.\n\t* @param reset - Function to run `after` hook and reset state\n\t* @returns Index of rule\n\t*/\n\tregisterResetFunction(reset) {\n\t\tlet { pendingStates } = this, index = pendingStates.length;\n\t\treturn pendingStates.push(0), this.resetFunctions.push(reset), index;\n\t}\n\t/**\n\t* Register that a rule with `after` hook has completed linting a file.\n\t* Called by `onCodePathEnd` CFG event handler which is added to visitor for rules with `after` hooks.\n\t*\n\t* If all rules with an `after` hook which needs to be run have completed linting the file, run all `after` hooks.\n\t*/\n\truleFinished() {\n\t\tthis.lintFinishedCount++, this.lintFinishedCount === this.pendingCount && this.reset(!1);\n\t}\n\t/**\n\t* Call all reset functions where corresponding entry in `pendingStates` is `AFTER_HOOK_PENDING`.\n\t* Should only be called when some `after` hooks are pending.\n\t*\n\t* @param ignoreErrors - `true` to catch and silently ignore any errors which occur in `after` hooks.\n\t* `false` to throw them,\n\t* @throws {unknown} If `ignoreErrors` is `false` and an error occurs in any `after` hooks.\n\t*/\n\treset(ignoreErrors) {\n\t\tthis.pendingCount;\n\t\tlet { resetFunctions, pendingStates } = this, hooksLen = pendingStates.length, hasError = !1, error;\n\t\tfor (let i = 0; i < hooksLen; i++) if (pendingStates[i] !== 0) {\n\t\t\tpendingStates[i] = 0;\n\t\t\ttry {\n\t\t\t\tresetFunctions[i]();\n\t\t\t} catch (e) {\n\t\t\t\thasError === !1 && (hasError = !0, error = e);\n\t\t\t}\n\t\t}\n\t\tif (this.pendingCount = 0, this.lintFinishedCount = 0, this.sourceCode = null, hasError === !0 && ignoreErrors === !1) throw error;\n\t}\n\t/**\n\t* Schedule a microtask to run `reset` functions.\n\t*/\n\tscheduleReset() {\n\t\tqueueMicrotask(this.resetMicrotask), this.resetIsScheduled = !0;\n\t}\n\t/**\n\t* Function which is scheduled as the cleanup microtask.\n\t* `scheduleReset` uses `resetMicrotask` which is this method bound to `this`.\n\t*/\n\tresetMicrotaskImpl() {\n\t\tthis.resetIsScheduled = !1, this.pendingCount !== 0 && this.reset(!0);\n\t}\n};\n/**\n* Convert a rule.\n*\n* The `rule` object passed in is mutated in-place.\n*\n* @param rule - Rule to convert\n* @param afterHooksState - State of `after` hooks\n* @throws {Error} If `rule` is not an object\n*/\nfunction convertRule(rule, afterHooksState) {\n\tif (typeof rule != \"object\" || !rule) throw Error(\"Rule must be an object\");\n\tif (\"create\" in rule) return;\n\tlet context = null, visitor, beforeHook, setupAfterHook;\n\trule.create = (eslintContext) => {\n\t\tcontext === null && ({context, visitor, beforeHook, setupAfterHook} = createContextAndVisitor(rule, afterHooksState));\n\t\tlet eslintFileContext = Object.getPrototypeOf(eslintContext);\n\t\tif (setupAfterHook !== null) {\n\t\t\tlet { sourceCode } = eslintFileContext;\n\t\t\tafterHooksState.sourceCode !== sourceCode && (afterHooksState.sourceCode = sourceCode, afterHooksState.pendingCount !== 0 && afterHooksState.reset(!0));\n\t\t}\n\t\treturn Object.defineProperties(context, {\n\t\t\tid: { value: eslintContext.id },\n\t\t\toptions: { value: eslintContext.options },\n\t\t\treport: { value: eslintContext.report }\n\t\t}), Object.setPrototypeOf(context, eslintFileContext), beforeHook !== null && beforeHook() === !1 ? EMPTY_VISITOR : (setupAfterHook !== null && (setupAfterHook(eslintFileContext.sourceCode.ast), afterHooksState.resetIsScheduled === !1 && afterHooksState.scheduleReset()), visitor);\n\t};\n}\nconst FILE_CONTEXT = Object.freeze({\n\tget filename() {\n\t\tthrow Error(\"Cannot access `context.filename` in `createOnce`\");\n\t},\n\tgetFilename() {\n\t\tthrow Error(\"Cannot call `context.getFilename` in `createOnce`\");\n\t},\n\tget physicalFilename() {\n\t\tthrow Error(\"Cannot access `context.physicalFilename` in `createOnce`\");\n\t},\n\tgetPhysicalFilename() {\n\t\tthrow Error(\"Cannot call `context.getPhysicalFilename` in `createOnce`\");\n\t},\n\tget cwd() {\n\t\tthrow Error(\"Cannot access `context.cwd` in `createOnce`\");\n\t},\n\tgetCwd() {\n\t\tthrow Error(\"Cannot call `context.getCwd` in `createOnce`\");\n\t},\n\tget sourceCode() {\n\t\tthrow Error(\"Cannot access `context.sourceCode` in `createOnce`\");\n\t},\n\tgetSourceCode() {\n\t\tthrow Error(\"Cannot call `context.getSourceCode` in `createOnce`\");\n\t},\n\tget languageOptions() {\n\t\tthrow Error(\"Cannot access `context.languageOptions` in `createOnce`\");\n\t},\n\tget settings() {\n\t\tthrow Error(\"Cannot access `context.settings` in `createOnce`\");\n\t},\n\textend(extension) {\n\t\treturn Object.freeze(Object.assign(Object.create(this), extension));\n\t},\n\tget parserOptions() {\n\t\tthrow Error(\"Cannot access `context.parserOptions` in `createOnce`\");\n\t},\n\tget parserPath() {\n\t\tthrow Error(\"Cannot access `context.parserPath` in `createOnce`\");\n\t}\n});\n/**\n* Call `createOnce` method of rule, and return `Context`, `Visitor`, and `beforeHook` (if any).\n*\n* @param rule - Rule with `createOnce` method\n* @param afterHooksState - State of `after` hooks\n* @returns Object with `context`, `visitor`, and `beforeHook` properties,\n* and `setupAfterHook` function if visitor has an `after` hook\n*/\nfunction createContextAndVisitor(rule, afterHooksState) {\n\tlet { createOnce } = rule;\n\tif (createOnce == null) throw Error(\"Rules must define either a `create` or `createOnce` method\");\n\tif (typeof createOnce != \"function\") throw Error(\"Rule `createOnce` property must be a function\");\n\tlet context = Object.create(FILE_CONTEXT, {\n\t\tid: {\n\t\t\tvalue: null,\n\t\t\tenumerable: !0,\n\t\t\tconfigurable: !0\n\t\t},\n\t\toptions: {\n\t\t\tvalue: null,\n\t\t\tenumerable: !0,\n\t\t\tconfigurable: !0\n\t\t},\n\t\treport: {\n\t\t\tvalue() {\n\t\t\t\tthrow Error(\"Cannot report errors in `createOnce`\");\n\t\t\t},\n\t\t\tenumerable: !0,\n\t\t\tconfigurable: !0\n\t\t}\n\t}), { before: beforeHook, after: afterHook, ...visitor } = createOnce.call(rule, context);\n\tif (beforeHook === void 0) beforeHook = null;\n\telse if (beforeHook !== null && typeof beforeHook != \"function\") throw Error(\"`before` property of visitor must be a function if defined\");\n\tlet setupAfterHook = null;\n\tif (afterHook != null) {\n\t\tif (typeof afterHook != \"function\") throw Error(\"`after` property of visitor must be a function if defined\");\n\t\tlet program = null, ruleIndex = afterHooksState.registerResetFunction(() => {\n\t\t\tprogram = null, afterHook();\n\t\t});\n\t\tsetupAfterHook = (ast) => {\n\t\t\tprogram = ast, afterHooksState.pendingStates[ruleIndex] = 1, afterHooksState.pendingCount++;\n\t\t};\n\t\tlet onCodePathEnd = visitor.onCodePathEnd;\n\t\tvisitor.onCodePathEnd = onCodePathEnd == null ? function(_codePath, node) {\n\t\t\tnode === program && afterHooksState.ruleFinished();\n\t\t} : function(codePath, node) {\n\t\t\tonCodePathEnd.call(this, codePath, node), node === program && afterHooksState.ruleFinished();\n\t\t};\n\t}\n\treturn {\n\t\tcontext,\n\t\tvisitor,\n\t\tbeforeHook,\n\t\tsetupAfterHook\n\t};\n}\n//#endregion\nexport { definePlugin, defineRule, eslintCompatPlugin };\n","import { eslintCompatPlugin, type Plugin } from \"@oxlint/plugins\";\nimport { ALL_RULES } from \"./rules/registry.ts\";\n\nconst plugin: Plugin = eslintCompatPlugin({\n meta: { name: \"hook-o-gnese\" },\n rules: ALL_RULES,\n});\n\nexport const recommended = {\n jsPlugins: [\"./node_modules/hook-o-gnese/dist/index.mjs\"],\n options: { typeAware: true, typeCheck: true },\n rules: {\n \"hook-o-gnese/no-fat-effects\": \"warn\",\n \"hook-o-gnese/state-scatter\": \"warn\",\n \"hook-o-gnese/hook-coupling\": \"error\",\n \"hook-o-gnese/custom-hook-depth\": [\"warn\", { maxDepth: 3 }],\n \"typescript/no-floating-promises\": \"error\",\n \"typescript/no-misused-promises\": \"error\",\n },\n};\n\nexport default plugin;\n"],"x_google_ignoreList":[0],"mappings":";;AAyBA,MAAM,gBAAgB,EAAE;;;;;;;;;;;;;;AAcxB,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