Skip to content

Commit 73b3fbb

Browse files
committed
feat: add new LanguageId type to be more self documenting.
- Added new type `LanguageId` which is just an alias for `string`. By typing relevant strings as `LanguageId`, it becomes more self documenting. - Changed `languageConfigFilePaths` and `languageConfigs` class properties to define it's types on the property instead of defining the generic type of the `new Map` instance. This is just to keep things looking clean and inline with other code. - Changed `string` types to use the new `LanguageId` in `Configuration` class for: - `languageConfigFilePaths`, `languageConfigs`, `singleLineBlocksMap`, and `multiLineBlocksMap` class property Maps. - Docblock types. - `isLangIdDisabled`, `isLangIdMultiLineCommentOverridden`, `getOverriddenMultiLineComment` and `getLanguageConfig` method params. - Various variables in many methods. - `getMultiLineLanguages` and `getSingleLineLanguages` method returns.
1 parent 3e9d9a4 commit 73b3fbb

2 files changed

Lines changed: 33 additions & 28 deletions

File tree

src/configuration.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {ExtensionData} from "./extensionData";
1414
import {Settings} from "./interfaces/settings";
1515
import {ExtraSingleLineCommentStyles, LineComment, SingleLineCommentStyle} from "./interfaces/commentStyles";
1616
import {ExtensionMetaData} from "./interfaces/extensionMetaData";
17-
import {JsonObject, JsonArray, MultiLineLanguageDefinitions, SingleLineLanguageDefinitions} from "./interfaces/utils";
17+
import {JsonObject, JsonArray, LanguageId, MultiLineLanguageDefinitions, SingleLineLanguageDefinitions} from "./interfaces/utils";
1818

1919
export class Configuration {
2020
/**************
@@ -30,32 +30,32 @@ export class Configuration {
3030
/**
3131
* A key:value Map object of language IDs and their config file paths.
3232
*/
33-
private languageConfigFilePaths = new Map<string, string[]>();
33+
private languageConfigFilePaths: Map<LanguageId, string[]> = new Map();
3434

3535
/**
3636
* A key:value Map object of language IDs and their configs.
3737
*/
38-
private readonly languageConfigs = new Map<string, vscode.LanguageConfiguration>();
38+
private readonly languageConfigs: Map<LanguageId, vscode.LanguageConfiguration> = new Map();
3939

4040
/**
4141
* A key:value Map object of supported and custom supported language IDs
4242
* and their single-line style comments.
4343
*
4444
* @property {string} - Map key can be either "customSupportedLanguages"
4545
* or "supportedLanguages".
46-
* @property {Map<string, SingleLineCommentStyle>} - Map value is an inner Map object of
46+
* @property {Map<LanguageId, SingleLineCommentStyle>} - Map value is an inner Map object of
4747
* language IDs and their single-line comment styles.
4848
*/
49-
private singleLineBlocksMap: Map<"customSupportedLanguages" | "supportedLanguages", Map<string, SingleLineCommentStyle>> = new Map();
49+
private singleLineBlocksMap: Map<"customSupportedLanguages" | "supportedLanguages", Map<LanguageId, SingleLineCommentStyle>> = new Map();
5050

5151
/**
5252
* A Map object of an array of supported language IDs for multi-line block comments.
5353
*
5454
* @property {string} - Map key can be either "customSupportedLanguages"
5555
* or "supportedLanguages".
56-
* @property {string[]} - Map value is an array of language IDs.
56+
* @property {LanguageId[]} - Map value is an array of language IDs.
5757
*/
58-
private multiLineBlocksMap: Map<"customSupportedLanguages" | "supportedLanguages", string[]> = new Map();
58+
private multiLineBlocksMap: Map<"customSupportedLanguages" | "supportedLanguages", LanguageId[]> = new Map();
5959

6060
/**
6161
* The directory where the auto-generated language definitions are stored.
@@ -265,20 +265,20 @@ export class Configuration {
265265

266266
/**
267267
* Is the language ID disabled?
268-
* @param {string} langId Language ID
268+
* @param {LanguageId} langId Language ID
269269
* @returns {boolean}
270270
*/
271-
public isLangIdDisabled(langId: string): boolean {
271+
public isLangIdDisabled(langId: LanguageId): boolean {
272272
return this.getConfigurationValue("disabledLanguages").includes(langId);
273273
}
274274

275275
/**
276276
* Is the multi-line comment overridden for the specified language ID?
277277
*
278-
* @param {string} langId Language ID
278+
* @param {LanguageId} langId Language ID
279279
* @returns {boolean}
280280
*/
281-
private isLangIdMultiLineCommentOverridden(langId: string): boolean {
281+
private isLangIdMultiLineCommentOverridden(langId: LanguageId): boolean {
282282
const overriddenList = this.getConfigurationValue("overrideDefaultLanguageMultiLineComments");
283283

284284
return overriddenList.hasOwnProperty(langId);
@@ -287,10 +287,10 @@ export class Configuration {
287287
/**
288288
* Get the overridden multi-line comment for the specified language ID.
289289
*
290-
* @param {string} langId Language ID
290+
* @param {LanguageId} langId Language ID
291291
* @returns {string} The overridden multi-line comment style.
292292
*/
293-
private getOverriddenMultiLineComment(langId: string): string {
293+
private getOverriddenMultiLineComment(langId: LanguageId): string {
294294
const overriddenList = this.getConfigurationValue("overrideDefaultLanguageMultiLineComments");
295295

296296
return overriddenList[langId];
@@ -349,7 +349,7 @@ export class Configuration {
349349
if (Object.hasOwn(packageJSON, "contributes") && Object.hasOwn(packageJSON.contributes, "languages")) {
350350
// Loop through the languages...
351351
for (let language of packageJSON.contributes.languages) {
352-
const langId = language.id;
352+
const langId: LanguageId = language.id;
353353
// Get the languages to skip.
354354
let skipLangs = this.getLanguagesToSkip();
355355

@@ -466,10 +466,10 @@ export class Configuration {
466466
/**
467467
* Get the config of the specified language.
468468
*
469-
* @param langId Language ID
469+
* @param {LanguageId} langId Language ID
470470
* @returns {vscode.LanguageConfiguration | undefined}
471471
*/
472-
private getLanguageConfig(langId: string): vscode.LanguageConfiguration | undefined {
472+
private getLanguageConfig(langId: LanguageId): vscode.LanguageConfiguration | undefined {
473473
if (this.languageConfigs.has(langId)) {
474474
return this.languageConfigs.get(langId);
475475
}
@@ -518,9 +518,9 @@ export class Configuration {
518518
* Get the multi-line languages from the Map.
519519
*
520520
* @param {"supportedLanguages" | "customSupportedLanguages"} key A stringed key, either `"supportedLanguages"` or `"customSupportedLanguages"`
521-
* @returns {string[]} An array of language ID strings.
521+
* @returns {LanguageId[]} An array of language ID strings.
522522
*/
523-
private getMultiLineLanguages(key: "supportedLanguages" | "customSupportedLanguages"): string[] {
523+
private getMultiLineLanguages(key: "supportedLanguages" | "customSupportedLanguages"): LanguageId[] {
524524
// The non-null assertion operator (!) ensures that the key is never undefined.
525525
return this.multiLineBlocksMap.get(key)!;
526526
}
@@ -529,9 +529,9 @@ export class Configuration {
529529
* Get the single-line languages and styles.
530530
*
531531
* @param {"supportedLanguages" | "customSupportedLanguages"} key A stringed key, either `"supportedLanguages"` or `"customSupportedLanguages"`
532-
* @returns {Map<string, SingleLineCommentStyle>} The Map of the languages and styles.
532+
* @returns {Map<LanguageId, SingleLineCommentStyle>} The Map of the languages and styles.
533533
*/
534-
private getSingleLineLanguages(key: "supportedLanguages" | "customSupportedLanguages"): Map<string, SingleLineCommentStyle> {
534+
private getSingleLineLanguages(key: "supportedLanguages" | "customSupportedLanguages"): Map<LanguageId, SingleLineCommentStyle> {
535535
// The non-null assertion operator (!) ensures that the key is never undefined.
536536
return this.singleLineBlocksMap.get(key)!;
537537
}
@@ -540,9 +540,9 @@ export class Configuration {
540540
* Set the multi-line comments language definitions.
541541
*/
542542
private setMultiLineCommentLanguageDefinitions() {
543-
let langArray: string[] = [];
543+
let langArray: LanguageId[] = [];
544544

545-
this.languageConfigs.forEach((config: vscode.LanguageConfiguration, langId: string) => {
545+
this.languageConfigs.forEach((config: vscode.LanguageConfiguration, langId: LanguageId) => {
546546
// If the config object has own property of comments AND the comments key has
547547
// own property of blockComment...
548548
if (Object.hasOwn(config, "comments") && Object.hasOwn(config.comments, "blockComment")) {
@@ -586,9 +586,9 @@ export class Configuration {
586586
* Set the single-line comments language definitions.
587587
*/
588588
private setSingleLineCommentLanguageDefinitions() {
589-
const tempMap: Map<string, SingleLineCommentStyle> = new Map();
589+
const tempMap: Map<LanguageId, SingleLineCommentStyle> = new Map();
590590

591-
this.languageConfigs.forEach((config: vscode.LanguageConfiguration, langId: string) => {
591+
this.languageConfigs.forEach((config: vscode.LanguageConfiguration, langId: LanguageId) => {
592592
// console.log(langId, config.comments.lineComment);
593593
let style: SingleLineCommentStyle | null = null;
594594

@@ -694,7 +694,7 @@ export class Configuration {
694694
/**
695695
* Sets the language configuration for a given language ID.
696696
*
697-
* @param {string} langId - The language ID for which the configuration is being set.
697+
* @param {LanguageId} langId - The language ID for which the configuration is being set.
698698
* @param {boolean} multiLine - Optional. If `true`, sets multi-line comment configuration.
699699
* @param {SingleLineCommentStyle} singleLineStyle - Optional. Specifies the style of single-line comments (e.g., `"//"`, `"#"`, `";"`).
700700
*
@@ -713,7 +713,7 @@ export class Configuration {
713713
* Note: This method ensures that the language configuration is correctly set and avoids issues
714714
* with rogue characters being inserted on new lines.
715715
*/
716-
private setLanguageConfiguration(langId: string, multiLine?: boolean, singleLineStyle?: SingleLineCommentStyle): vscode.Disposable {
716+
private setLanguageConfiguration(langId: LanguageId, multiLine?: boolean, singleLineStyle?: SingleLineCommentStyle): vscode.Disposable {
717717
const internalLangConfig: vscode.LanguageConfiguration = this.getLanguageConfig(langId);
718718
const defaultMultiLineConfig = utils.readJsonFile(`${__dirname}/../../config/default-multi-line-config.json`) as vscode.LanguageConfiguration;
719719

@@ -894,7 +894,7 @@ export class Configuration {
894894
* @param {vscode.TextEditorEdit} edit The text editor edits.
895895
*/
896896
private handleSingleLineBlock(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) {
897-
let langId = textEditor.document.languageId;
897+
let langId: LanguageId = textEditor.document.languageId;
898898
const singleLineLangs = this.getSingleLineLanguages("supportedLanguages");
899899
const customSingleLineLangs = this.getSingleLineLanguages("customSupportedLanguages");
900900

@@ -952,7 +952,7 @@ export class Configuration {
952952
* @param {vscode.TextEditor} textEditor The text editor.
953953
*/
954954
private handleChangeBladeMultiLineBlock(textEditor: vscode.TextEditor) {
955-
let langId = textEditor.document.languageId;
955+
let langId: LanguageId = textEditor.document.languageId;
956956
const extensionName = this.extensionData.get("namespace");
957957

958958
// Only carry out function if languageId is blade.

src/interfaces/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ export interface MultiLineLanguageDefinitions extends JsonObject {
3232
supportedLanguages: string[];
3333
customSupportedLanguages: string[];
3434
}
35+
36+
/**
37+
* Language ID
38+
*/
39+
export type LanguageId = string;

0 commit comments

Comments
 (0)