Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Possible code duplication #457

Open
Open
@BlueMagnificent

Description

@BlueMagnificent

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions