wasdk / WebAssemblyStudio

Learn, Teach, Work and Play in the WebAssembly Studio

Home Page:http://webassembly.studio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible code duplication

BlueMagnificent opened this issue · comments

There seems to be a sort of code duplication here:

let icon = "";
switch (file.type) {
case FileType.C: icon = "c-lang-file-icon"; break;
case FileType.Cpp: icon = "cpp-lang-file-icon"; break;
case FileType.JavaScript: icon = "javascript-lang-file-icon"; break;
case FileType.HTML: icon = "html-lang-file-icon"; break;
case FileType.TypeScript: icon = "typescript-lang-file-icon"; break;
case FileType.Markdown: icon = "markdown-lang-file-icon"; break;
case FileType.JSON: icon = "json-lang-file-icon"; break;
case FileType.Wasm: icon = "wasm-lang-file-icon"; break;
case FileType.Wat: icon = "wat-lang-file-icon"; break;
}

Given that the below code is to handle such case:

export function getIconForFileType(fileType: FileType): string {
if (fileType === FileType.JavaScript) {
return "javascript-lang-file-icon";
} else if (fileType === FileType.TypeScript) {
return "typescript-lang-file-icon";
} else if (fileType === FileType.C) {
return "c-lang-file-icon";
} else if (fileType === FileType.Cpp) {
return "cpp-lang-file-icon";
} else if (fileType === FileType.Rust) {
return "rust-lang-file-icon";
} else if (fileType === FileType.Markdown) {
return "markdown-lang-file-icon";
} else if (fileType === FileType.HTML) {
return "html-lang-file-icon";
} else if (fileType === FileType.CSS) {
return "css-lang-file-icon";
} else if (fileType === FileType.Directory) {
return "folder-icon";
} else if (fileType === FileType.JSON) {
return "json-lang-file-icon";
} else if (fileType === FileType.Wasm) {
return "wasm-lang-file-icon";
} else if (fileType === FileType.Wat) {
return "wat-lang-file-icon";
}
return "txt-ext-file-icon";
}

The default value for an unknown file.type in the above src\utils\Template.ts snippet is an empty string, while its equivalent in getIconForFileType of src\models\types.ts is the string "txt-ext-file-icon". However from observing rendered files in the DirectoryTree, these two default values still give the same icon.

We can correct this duplication by modifying:

import { File, FileType, Problem, Directory } from "../models";

to

import { File, FileType, Problem, Directory, getIconForFileType } from "../models";

and replacing:

let icon = "";
switch (file.type) {
case FileType.C: icon = "c-lang-file-icon"; break;
case FileType.Cpp: icon = "cpp-lang-file-icon"; break;
case FileType.JavaScript: icon = "javascript-lang-file-icon"; break;
case FileType.HTML: icon = "html-lang-file-icon"; break;
case FileType.TypeScript: icon = "typescript-lang-file-icon"; break;
case FileType.Markdown: icon = "markdown-lang-file-icon"; break;
case FileType.JSON: icon = "json-lang-file-icon"; break;
case FileType.Wasm: icon = "wasm-lang-file-icon"; break;
case FileType.Wat: icon = "wat-lang-file-icon"; break;
}

with

let icon = getIconForFileType(file.type);

It would be appropriate to note that without the above modifications the code still works well. I'm just pointing it out since it would make it easy to include additional file types later.

You should probably open a pull request.