-
Notifications
You must be signed in to change notification settings - Fork 277
Possible code duplication #457
Description
There seems to be a sort of code duplication here:
WebAssemblyStudio/src/utils/Template.ts
Lines 72 to 83 in 49afdef
| 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:
WebAssemblyStudio/src/models/types.ts
Lines 234 to 261 in 49afdef
| 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:
WebAssemblyStudio/src/utils/Template.ts
Line 22 in 49afdef
| import { File, FileType, Problem, Directory } from "../models"; |
to
import { File, FileType, Problem, Directory, getIconForFileType } from "../models";
and replacing:
WebAssemblyStudio/src/utils/Template.ts
Lines 72 to 83 in 49afdef
| 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.