Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

refactor(harper.js): use a stateless method instead

Elijah Potter41aeba739e23
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 553716548..8fd811f50 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -111,10 +111,6 @@ impl Linter {
self.lint_group.config.serialize(&serializer).unwrap()
}
- pub fn fill_lint_config_with_default(&mut self) {
- self.lint_group.config.fill_default_values();
- }
-
pub fn set_lint_config_from_object(&mut self, object: JsValue) -> Result<(), String> {
self.lint_group.config =
serde_wasm_bindgen::from_value(object).map_err(|v| v.to_string())?;
@@ -266,6 +262,25 @@ impl Lint {
}
}
+#[wasm_bindgen]
+pub fn get_default_lint_config_as_json() -> String {
+ let mut config = LintGroupConfig::default();
+ config.fill_default_values();
+
+ serde_json::to_string(&config).unwrap()
+}
+
+#[wasm_bindgen]
+pub fn get_default_lint_config() -> JsValue {
+ let mut config = LintGroupConfig::default();
+ config.fill_default_values();
+
+ // Important for downstream JSON serialization
+ let serializer = serde_wasm_bindgen::Serializer::json_compatible();
+
+ config.serialize(&serializer).unwrap()
+}
+
/// A struct that represents two character indices in a string: a start and an end.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[wasm_bindgen]
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
index e576a8ef7..10cfe916a 100644
--- a/packages/harper.js/src/Linter.test.ts
+++ b/packages/harper.js/src/Linter.test.ts
@@ -125,9 +125,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
test(`${linterName} default lint config has no null values`, async () => {
const linter = new Linter();
- await linter.setLintConfigToDefault();
-
- const lintConfig = await linter.getLintConfig();
+ const lintConfig = await linter.getDefaultLintConfig();
for (const value of Object.values(lintConfig)) {
expect(value).not.toBeNull();
diff --git a/packages/harper.js/src/Linter.ts b/packages/harper.js/src/Linter.ts
index 439d797e3..4976aa739 100644
--- a/packages/harper.js/src/Linter.ts
+++ b/packages/harper.js/src/Linter.ts
@@ -25,8 +25,13 @@ export default interface Linter {
/** Get the linter's current configuration. */
getLintConfig(): Promise<LintConfig>;
- /** Set the linter's current configuration to the official Harper default. */
- setLintConfigToDefault(): Promise<void>;
+ /** Get the default (unset) linter configuration as JSON.
+ * This method does not effect the caller's lint configuration, nor does it return the current one. */
+ getDefaultLintConfigAsJSON(): Promise<string>;
+
+ /** Get the default (unset) linter configuration.
+ * This method does not effect the caller's lint configuration, nor does it return the current one. */
+ getDefaultLintConfig(): Promise<LintConfig>;
/** Set the linter's current configuration. */
setLintConfig(config: LintConfig): Promise<void>;
diff --git a/packages/harper.js/src/LocalLinter.ts b/packages/harper.js/src/LocalLinter.ts
index b230a4792..03196248f 100644
--- a/packages/harper.js/src/LocalLinter.ts
+++ b/packages/harper.js/src/LocalLinter.ts
@@ -53,6 +53,18 @@ export default class LocalLinter implements Linter {
return this.inner!.get_lint_config_as_object();
}
+ async getDefaultLintConfigAsJSON(): Promise<string> {
+ const wasm = await loadWasm();
+
+ return wasm.get_default_lint_config_as_json();
+ }
+
+ async getDefaultLintConfig(): Promise<LintConfig> {
+ const wasm = await loadWasm();
+
+ return wasm.get_default_lint_config();
+ }
+
async setLintConfig(config: LintConfig): Promise<void> {
await this.initialize();
@@ -85,10 +97,4 @@ export default class LocalLinter implements Linter {
await this.initialize();
return this.inner!.get_lint_descriptions_as_json();
}
-
- async setLintConfigToDefault(): Promise<void> {
- await this.initialize();
-
- this.inner?.fill_lint_config_with_default();
- }
}
diff --git a/packages/harper.js/src/WorkerLinter/index.ts b/packages/harper.js/src/WorkerLinter/index.ts
index fa2efdd67..675d1644f 100644
--- a/packages/harper.js/src/WorkerLinter/index.ts
+++ b/packages/harper.js/src/WorkerLinter/index.ts
@@ -105,8 +105,12 @@ export default class WorkerLinter implements Linter {
return JSON.parse(await this.getLintDescriptionsAsJSON()) as Record<string, string>;
}
- setLintConfigToDefault(): Promise<void> {
- return this.rpc('setLintConfigToDefault', []);
+ getDefaultLintConfigAsJSON(): Promise<string> {
+ return this.rpc('getDefaultLintConfigAsJSON', []);
+ }
+
+ async getDefaultLintConfig(): Promise<LintConfig> {
+ return JSON.parse(await this.getDefaultLintConfigAsJSON()) as LintConfig;
}
/** Run a procedure on the remote worker. */
diff --git a/packages/web/src/lib/DefaultNeovimConfig.svelte b/packages/web/src/lib/DefaultNeovimConfig.svelte
index 97972c2d8..0fa798749 100644
--- a/packages/web/src/lib/DefaultNeovimConfig.svelte
+++ b/packages/web/src/lib/DefaultNeovimConfig.svelte
@@ -16,11 +16,10 @@
}`;
async function generateConfig() {
- await linter.setLintConfigToDefault();
- let config = await linter.getLintConfig();
+ let default_config = await linter.getDefaultLintConfig();
- let rows = Object.entries(config)
- .map(([key, value]) => `\t${key} = ${value},`)
+ let rows = Object.entries(default_config)
+ .map(([key, value]) => `\t\t\t${key} = ${value},`)
.reduce((prev, cur) => prev + '\n' + cur);
return head + rows + tail;
@@ -32,4 +31,4 @@
}
</script>
-<Button onclick={copyConfig}>Copy Default Config</Button>
+<Button onclick={copyConfig}>Copy Default Config to Clipboard</Button>