Skip to content

Commit 8e098b6

Browse files
LeonardoLarranagaLeonardo Larrañaga
andauthored
Fix: Validate filenames consisting solely of extensions (#1802)
* Fix filename validation for files with only extensions (e.g., '.env', '.gitignore', '.editorconfig') * Support for files such as '.env.production', '.env.local' * Fixes an issue with files like '.eslintrc.js'. Now, if the file starts with a ".", every component will be checked to get a valid type. * Fixes an issue with files like '.eslintrc.js'. Now, if the file starts with a ".", every component will be checked to get a valid type. * Prioritize file extension check before handling all "." components. --------- Co-authored-by: Leonardo Larrañaga <leonardolarranaga@icloud.com>
1 parent e0009d2 commit 8e098b6

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,23 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor
4242
var name: String { url.lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) }
4343

4444
/// Returns the extension of the file or an empty string if no extension is present.
45-
var type: FileIcon.FileType { .init(rawValue: url.pathExtension) ?? .txt }
45+
var type: FileIcon.FileType {
46+
let filename = url.lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
47+
48+
/// First, check if there is a valid file extension.
49+
if let type = FileIcon.FileType(rawValue: filename) {
50+
return type
51+
} else {
52+
/// If there's not, verifies every extension for a valid type.
53+
let extensions = filename.dropFirst().components(separatedBy: ".").reversed()
54+
55+
return extensions
56+
.compactMap { FileIcon.FileType(rawValue: $0) }
57+
.first
58+
/// Returns .txt for invalid type.
59+
?? .txt
60+
}
61+
}
4662

4763
/// Returns the URL of the ``CEWorkspaceFile``
4864
let url: URL

0 commit comments

Comments
 (0)