From 37360c5193ba9c060084d6b452dd61d2367dba68 Mon Sep 17 00:00:00 2001 From: Chinonso Chukwuogor Date: Fri, 30 Sep 2022 14:49:18 -0700 Subject: [PATCH 1/2] Use guard statements to reduce `if` nesting This uses guard statements to reduce nesting of conditions and make the code a bit more readable --- src/extension.js | 117 ++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 57 deletions(-) diff --git a/src/extension.js b/src/extension.js index 10e2c9d..4a50f74 100644 --- a/src/extension.js +++ b/src/extension.js @@ -36,64 +36,67 @@ function activate(context) { clearTimeout(timeout); } timeout = setTimeout(() => { - if (editor && (isEnabled() || force)) { - if (languagesEnabled().includes("php") && editor.document.languageId === 'php') { - currentRunner = runner(phpRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }) - } else if (languagesEnabled().includes("typescript") && editor.document.languageId === 'typescript') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.TS }) - } else if (languagesEnabled().includes("typescriptreact") && editor.document.languageId === 'typescriptreact') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.TSX }) - } else if (languagesEnabled().includes("javascript") && editor.document.languageId === 'javascript') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.JS }) - } else if (languagesEnabled().includes("javascriptreact") && editor.document.languageId === 'javascriptreact') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.JSX }) - } + if (!editor || !(isEnabled() || force)) { + return; + } + if (languagesEnabled().includes("php") && editor.document.languageId === 'php') { + currentRunner = runner(phpRunner, editor, hints => { + if (hints === false || !isEnabled()) { + return; + } + if (hints.length) { + editor.setDecorations(hintDecorationType, hints); + } else { + editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); + } + }) + } else if (languagesEnabled().includes("typescript") && editor.document.languageId === 'typescript') { + currentRunner = runner(typescriptRunner, editor, hints => { + if (hints === false || !isEnabled()) { + return; + } + if (hints.length) { + editor.setDecorations(hintDecorationType, hints); + } else { + editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); + } + + }, { language: ts.ScriptKind.TS }) + } else if (languagesEnabled().includes("typescriptreact") && editor.document.languageId === 'typescriptreact') { + currentRunner = runner(typescriptRunner, editor, hints => { + if (hints === false !! !isEnabled()) { + return + } + if (hints.length) { + editor.setDecorations(hintDecorationType, hints); + } else { + editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); + } + }, { language: ts.ScriptKind.TSX }) + } else if (languagesEnabled().includes("javascript") && editor.document.languageId === 'javascript') { + currentRunner = runner(typescriptRunner, editor, hints => { + if (hints === false || !isEnabled()) { + return; + } + if (hints.length) { + editor.setDecorations(hintDecorationType, hints); + } else { + editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); + } + }, { language: ts.ScriptKind.JS }) + } else if (languagesEnabled().includes("javascriptreact") && editor.document.languageId === 'javascriptreact') { + currentRunner = runner(typescriptRunner, editor, hints => { + if (hints === false || !isEnabled()) { + return; + } + if (hints.length) { + editor.setDecorations(hintDecorationType, hints); + } else { + editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); + } + }, { language: ts.ScriptKind.JSX }) } + }, time); } const clear = (editor) => { From 0b3bce53afae65d2f31fc4ef3707864cb3edc0bc Mon Sep 17 00:00:00 2001 From: Chinonso Chukwuogor Date: Fri, 30 Sep 2022 15:12:38 -0700 Subject: [PATCH 2/2] Use loop and config object --- src/extension.js | 80 +++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/src/extension.js b/src/extension.js index 4a50f74..bb57035 100644 --- a/src/extension.js +++ b/src/extension.js @@ -28,6 +28,30 @@ function activate(context) { ); let timeout = null; + + const runnerConfigMap = { + php: { + runner: phpRunner, + runnerOptions: undefined + }, + typescript: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.TS } + }, + typescriptreact: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.TSX } + }, + javascript: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.JS } + }, + javascriptreact: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.JSX } + } + } + const trigger = (identifier, editor, force, time = 100) => { if (currentRunner && !currentRunner.state.done) { currentRunner.reject(); @@ -39,53 +63,12 @@ function activate(context) { if (!editor || !(isEnabled() || force)) { return; } - if (languagesEnabled().includes("php") && editor.document.languageId === 'php') { - currentRunner = runner(phpRunner, editor, hints => { - if (hints === false || !isEnabled()) { - return; - } - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - }) - } else if (languagesEnabled().includes("typescript") && editor.document.languageId === 'typescript') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints === false || !isEnabled()) { - return; - } - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - }, { language: ts.ScriptKind.TS }) - } else if (languagesEnabled().includes("typescriptreact") && editor.document.languageId === 'typescriptreact') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints === false !! !isEnabled()) { - return - } - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - }, { language: ts.ScriptKind.TSX }) - } else if (languagesEnabled().includes("javascript") && editor.document.languageId === 'javascript') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints === false || !isEnabled()) { - return; - } - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - }, { language: ts.ScriptKind.JS }) - } else if (languagesEnabled().includes("javascriptreact") && editor.document.languageId === 'javascriptreact') { - currentRunner = runner(typescriptRunner, editor, hints => { + + for(const [language, config] of Object.entries(runnerConfigMap)) { + if(!languagesEnabled().includes(language) || editor.document.languageId !== language) { + continue; + } + currentRunner = runner(config.runner, editor, hints => { if (hints === false || !isEnabled()) { return; } @@ -94,7 +77,8 @@ function activate(context) { } else { editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); } - }, { language: ts.ScriptKind.JSX }) + }, config.runnerOptions) + break; } }, time);