Skip to content

Commit

Permalink
fix type safety for extension
Browse files Browse the repository at this point in the history
  • Loading branch information
inferrinizzard committed Apr 16, 2024
1 parent a0e399a commit 45c06ce
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/quicktype-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
jsonInputForTargetLanguage,
RendererOptions,
Options,
inferenceFlagNames
inferenceFlagNames,
isLanguageName
} from "quicktype-core";
import { schemaForTypeScriptSources } from "quicktype-typescript-input";

Expand Down Expand Up @@ -62,15 +63,16 @@ async function pickTargetLanguage(): Promise<TargetLanguagePick> {
const languageChoices = defaultTargetLanguages.map(l => l.displayName).sort();
let chosenName = await vscode.window.showQuickPick(languageChoices);
const cancelled = chosenName === undefined;
if (chosenName === undefined) {
chosenName = "typescript";
if (chosenName === undefined || !isLanguageName(chosenName)) {
return { cancelled, lang: languageNamed("typescript") };
}
return { cancelled, lang: languageNamed(chosenName)! };
return { cancelled, lang: languageNamed(chosenName) };
}

async function getTargetLanguage(editor: vscode.TextEditor): Promise<TargetLanguagePick> {
const documentLanguage = editor.document.languageId;
const currentLanguage = languageNamed(documentLanguage);
const languageName = isLanguageName(documentLanguage) ? documentLanguage : "typescript";
const currentLanguage = languageNamed(languageName);
if (currentLanguage !== undefined) {
return {
cancelled: false,
Expand Down Expand Up @@ -343,8 +345,9 @@ function deduceTargetLanguage(): TargetLanguage {
}
const sorted = Array.from(counts).sort(([_na, ca], [_nb, cb]) => cb - ca);
for (const [name] of sorted) {
const lang = languageNamed(name);
if (lang !== undefined) return lang;
if (isLanguageName(name)) {
return languageNamed(name);
}
}
return languageNamed("typescript")!;
}
Expand Down Expand Up @@ -450,7 +453,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
);

const maybeName = extensionContext.workspaceState.get<string>(lastTargetLanguageUsedKey);
if (typeof maybeName === "string") {
if (typeof maybeName === "string" && isLanguageName(maybeName)) {
explicitlySetTargetLanguage = languageNamed(maybeName);
}
}
Expand Down

0 comments on commit 45c06ce

Please sign in to comment.