Skip to content

Commit e69cd86

Browse files
committed
refactor(linter/plugins): loadPluginImpl return an object (#14087)
Pure refactor. Centralize logic for JSON-encoding the result of loading a plugin in `loadPlugin`. `loadPluginImpl` return an object instead of a JSON-encoded string.
1 parent d8e9cc5 commit e69cd86

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,46 @@ const registeredPluginPaths = new Set<string>();
5656
// Indexed by `ruleId`, which is passed to `lintFile`.
5757
export const registeredRules: RuleAndContext[] = [];
5858

59+
// Plugin details returned to Rust
60+
interface PluginDetails {
61+
// Plugin name
62+
name: string;
63+
// Index of first rule of this plugin within `registeredRules`
64+
offset: number;
65+
// Names of rules within this plugin, in same order as in `registeredRules`
66+
ruleNames: string[];
67+
}
68+
5969
/**
6070
* Load a plugin.
6171
*
6272
* Main logic is in separate function `loadPluginImpl`, because V8 cannot optimize functions
6373
* containing try/catch.
6474
*
65-
* @param {string} path - Absolute path of plugin file
66-
* @returns {string} - JSON result
75+
* @param path - Absolute path of plugin file
76+
* @returns JSON result
6777
*/
6878
export async function loadPlugin(path: string): Promise<string> {
6979
try {
70-
return await loadPluginImpl(path);
80+
const res = await loadPluginImpl(path);
81+
return JSON.stringify({ Success: res });
7182
} catch (err) {
7283
return JSON.stringify({ Failure: getErrorMessage(err) });
7384
}
7485
}
7586

76-
async function loadPluginImpl(path: string): Promise<string> {
87+
/**
88+
* Load a plugin.
89+
*
90+
* @param path - Absolute path of plugin file
91+
* @returns - Plugin details
92+
* @throws {Error} If plugin has already been registered
93+
* @throws {TypeError} If one of plugin's rules is malformed or its `createOnce` method returns invalid visitor
94+
* @throws {*} If plugin throws an error during import
95+
*/
96+
async function loadPluginImpl(path: string): Promise<PluginDetails> {
7797
if (registeredPluginPaths.has(path)) {
78-
return JSON.stringify({
79-
Failure: 'This plugin has already been registered',
80-
});
98+
throw new Error('This plugin has already been registered. This is a bug in Oxlint. Please report it.');
8199
}
82100

83101
const { default: plugin } = (await import(path)) as { default: Plugin };
@@ -117,7 +135,7 @@ async function loadPluginImpl(path: string): Promise<string> {
117135
registeredRules.push(ruleAndContext);
118136
}
119137

120-
return JSON.stringify({ Success: { name: pluginName, offset, ruleNames } });
138+
return { name: pluginName, offset, ruleNames };
121139
}
122140

123141
/**

0 commit comments

Comments
 (0)