about summary refs log tree commit diff
path: root/editors/code
diff options
context:
space:
mode:
authorveetaha <veetaha2@gmail.com>2020-04-11 15:23:07 +0300
committerveetaha <veetaha2@gmail.com>2020-04-11 15:40:49 +0300
commit12e23bd60b9c3dde24bb9fa597ad88705abd2b76 (patch)
tree80196efd964aaaf7d3da56c5d46b441df9ccbf07 /editors/code
parent0ecdba20df41a800222d0fd864843843feb6e875 (diff)
downloadrust-12e23bd60b9c3dde24bb9fa597ad88705abd2b76.tar.gz
rust-12e23bd60b9c3dde24bb9fa597ad88705abd2b76.zip
vscode: fix typing bug in config
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/config.ts39
1 files changed, 30 insertions, 9 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 21c1c9f232e..35a05131c88 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -66,23 +66,44 @@ export class Config {
         return vscode.workspace.getConfiguration(this.rootSection);
     }
 
-    get serverPath() { return this.cfg.get<null | string>("serverPath")!; }
-    get channel() { return this.cfg.get<UpdatesChannel>("updates.channel")!; }
-    get askBeforeDownload() { return this.cfg.get<boolean>("updates.askBeforeDownload")!; }
-    get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; }
+    /**
+     * Beware that postfix `!` operator erases both `null` and `undefined`.
+     * This is why the following doesn't work as expected:
+     *
+     * ```ts
+     * const nullableNum = vscode
+     *  .workspace
+     *  .getConfiguration
+     *  .getConfiguration("rust-analyer")
+     *  .get<number | null>(path)!;
+     *
+     * // What happens is that type of `nullableNum` is `number` but not `null | number`:
+     * const fullFledgedNum: number = nullableNum;
+     * ```
+     * So this getter handles this quirk by not requiring the caller to use postfix `!`
+     */
+    private get<T>(path: string): T {
+        return this.cfg.get<T>(path)!;
+    }
+
+    get serverPath() { return this.get<null | string>("serverPath"); }
+    get channel() { return this.get<UpdatesChannel>("updates.channel"); }
+    get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
+    get traceExtension() { return this.get<boolean>("trace.extension"); }
+
 
     get inlayHints() {
         return {
-            typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!,
-            parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!,
-            chainingHints: this.cfg.get<boolean>("inlayHints.chainingHints")!,
-            maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!,
+            typeHints: this.get<boolean>("inlayHints.typeHints"),
+            parameterHints: this.get<boolean>("inlayHints.parameterHints"),
+            chainingHints: this.get<boolean>("inlayHints.chainingHints"),
+            maxLength: this.get<null | number>("inlayHints.maxLength"),
         };
     }
 
     get checkOnSave() {
         return {
-            command: this.cfg.get<string>("checkOnSave.command")!,
+            command: this.get<string>("checkOnSave.command"),
         };
     }
 }