Skip to content

Commit ca5a920

Browse files
CopilotSysix
andcommitted
Add clearLoadedPlugin function for NAPI integration
- Add clearLoadedPlugin() JS function in src-js/plugins/load.ts to clear registeredRules and registeredPluginPaths - Export clearLoadedPlugin from src-js/plugins/index.ts and src-js/index.ts - Add tests for clearLoadedPlugin functionality - Function will be used by language server to reset plugin state Co-authored-by: Sysix <[email protected]>
1 parent 4e4136f commit ca5a920

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

apps/oxlint/src-js/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,19 @@ function createContextAndVisitor(rule: CreateOnceRule): {
274274

275275
return { context, visitor, beforeHook };
276276
}
277+
278+
/**
279+
* Clear all loaded plugins and rules.
280+
*
281+
* This function clears the internal state of registered plugins and rules,
282+
* allowing plugins to be reloaded from scratch. This is useful for the
283+
* language server when restarting or reloading configuration.
284+
*
285+
* Note: This function is lazy-loaded and will only be available after
286+
* the first plugin has been loaded. It will not have any effect if no
287+
* plugins have been loaded yet.
288+
*/
289+
export async function clearLoadedPlugin(): Promise<void> {
290+
const mod = await import('./plugins/index.js');
291+
mod.clearLoadedPlugin();
292+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { lintFile } from './lint.js';
2-
import { loadPlugin } from './load.js';
2+
import { clearLoadedPlugin, loadPlugin } from './load.js';
33

4-
export { lintFile, loadPlugin };
4+
export { clearLoadedPlugin, lintFile, loadPlugin };

apps/oxlint/src-js/plugins/load.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,15 @@ function conformHookFn<H>(hookFn: H | null | undefined, hookName: string): H | n
271271
if (typeof hookFn !== 'function') throw new TypeError(`\`${hookName}\` hook must be a function if provided`);
272272
return hookFn;
273273
}
274+
275+
/**
276+
* Clear all loaded plugins and rules.
277+
*
278+
* This function clears the internal state of registered plugins and rules,
279+
* allowing plugins to be reloaded from scratch. This is useful for the
280+
* language server when restarting or reloading configuration.
281+
*/
282+
export function clearLoadedPlugin(): void {
283+
registeredPluginPaths.clear();
284+
registeredRules.length = 0;
285+
}

apps/oxlint/src/run.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub type JsLintFileCb = ThreadsafeFunction<
5252
false,
5353
>;
5454

55+
56+
5557
/// NAPI entry point.
5658
///
5759
/// JS side passes in:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { clearLoadedPlugin } from '../dist/index.js';
3+
4+
/**
5+
* Test the clearLoadedPlugin function
6+
*/
7+
describe('clearLoadedPlugin', () => {
8+
it('should be exported from the main module', () => {
9+
expect(clearLoadedPlugin).toBeDefined();
10+
expect(typeof clearLoadedPlugin).toBe('function');
11+
});
12+
13+
it('should execute without errors', async () => {
14+
// Call the clear function - it should not throw
15+
await expect(clearLoadedPlugin()).resolves.toBeUndefined();
16+
});
17+
18+
it('should work when called multiple times', async () => {
19+
// Call it multiple times to ensure it doesn't error
20+
await expect(clearLoadedPlugin()).resolves.toBeUndefined();
21+
await expect(clearLoadedPlugin()).resolves.toBeUndefined();
22+
await expect(clearLoadedPlugin()).resolves.toBeUndefined();
23+
});
24+
});

0 commit comments

Comments
 (0)