Skip to content

Commit

Permalink
type guard helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
harrydowning committed Aug 28, 2024
1 parent 49b37c0 commit cb8ef5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
17 changes: 8 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ export const SUB_INCLUDE_CONFIG = "include";
export const INCLUDE_CONFIG = `${packageJson.name}.${SUB_INCLUDE_CONFIG}`;
export const VERSION_STATE = "version";

export type Languages = {
[key: string]: {
name: string;
scopeName: string;
stripIndent: boolean;
};
};

/* eslint sort-keys: ["error", "asc"] */
export const LANGUAGES = {
bat: "source.batchfile",
Expand Down Expand Up @@ -146,12 +154,3 @@ export const LANGUAGES = {
xsl: "text.xml.xsl",
yaml: "source.yaml",
};
/* eslint-disable sort-keys */

export type Languages = {
[key: string]: {
name: string;
scopeName: string;
stripIndent: boolean;
};
};
29 changes: 11 additions & 18 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
import { LANGUAGES, Languages } from "./constants";
import { InjectionGrammar } from "./injection-grammar";
import { Package } from "./package";
import { hasBoolKey, hasStringKey } from "./utils";

const normalizeLanguages = (languages: { [key: string]: unknown }) => {
const normalizedLanguages: Languages = {};
const parseLanguages = (languages: { [key: string]: unknown }): Languages => {
const parsedLanguages: Languages = {};
for (const id in languages) {
let language = languages[id];
if (typeof language === "string") {
language = { scopeName: language };
}

if (typeof language === "object" && language !== null) {
if (!("scopeName" in language)) {
if (!hasStringKey(language, "scopeName")) {
continue;
}

if (typeof language.scopeName !== "string") {
continue;
}

normalizedLanguages[id] = {
name:
"name" in language && typeof language.name === "string"
? language.name
: id,
parsedLanguages[id] = {
name: hasStringKey(language, "name") ? language.name : id,
scopeName: language.scopeName,
stripIndent:
"stripIndent" in language && typeof language.stripIndent === "boolean"
? language.stripIndent
: false,
stripIndent: hasBoolKey(language, "stripIndent")
? language.stripIndent
: false,
};
}
}
return normalizedLanguages;
return parsedLanguages;
};

export const generateFiles = (languages = LANGUAGES) => {
const parsedLanguages = normalizeLanguages(languages);
const parsedLanguages = parseLanguages(languages);
const grammars = [
new InjectionGrammar("source.yaml", parsedLanguages),
new InjectionGrammar("source.github-actions-workflow", parsedLanguages),
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function hasKey<K extends string>(
obj: object,
key: K,
): obj is { [P in K]: unknown } {
return key in obj;
}

export function hasStringKey<K extends string>(
obj: object,
key: K,
): obj is { [P in K]: string } {
return hasKey(obj, key) && typeof obj[key] === "string";
}

export function hasBoolKey<K extends string>(
obj: object,
key: K,
): obj is { [P in K]: boolean } {
return hasKey(obj, key) && typeof obj[key] === "boolean";
}

0 comments on commit cb8ef5d

Please sign in to comment.