Skip to content

Instantly share code, notes, and snippets.

@Jeehut

Jeehut/lint.swift

Last active Jul 19, 2020
Embed
What would you like to do?
AnyLint custom check with autocorrection for [SafeLocalizedStringKey](https://gist.github.com/Jeehut/c8c9a8caf8dc7c02583a4a07dfbb37aa).
#!/usr/local/bin/swift-sh
import AnyLint // @Flinesoft
try Lint.logSummaryAndExit(arguments: CommandLine.arguments) {
// MARK: - Variables
let swiftAppFiles: Regex = #"^(Shared|iOS|macOS)/App/Sources/.*\.swift$"#
// MARK: - Checks
// MARK: SafeLocalizedStringKey
let unsafeLocalizedStringKeyTypes: String = [
"Button", "ColorPicker", "CommandMenu", "DatePicker", "DisclosureGroup", "Label", "Link", "NavigationLink", "Picker", "ProgressView",
"SecureField", "Stepper", "Text", "TextField", "Toggle", #"\.navigationBarTitle"#, #"\.navigationTitle"#, #"\.help"#, "WindowGroup",
].joined(separator: "|")
try Lint.checkFileContents(
checkInfo: "SafeLocalizedStringKey: Use the `safe:` overload for localization safety.",
regex: Regex(#"((?:\#(unsafeLocalizedStringKeyTypes))\(\s*)\"(?!TODO:)"#),
nonMatchingExamples: [#"Text(textVariable)"#],
includeFilters: [swiftAppFiles],
excludeFilters: [#"^Shared/App/Sources/.*/SafeLocalizedStringKey\.swift$"#],
autoCorrectReplacement: #"$1safe: ""#,
autoCorrectExamples: [
[
"before": #"Text("E-Mail")"#,
"after": #"Text(safe: "E-Mail")"#
],
[
"before": #"TextField("e.g. john@appleseed.com", text: $user.email)"#,
"after": #"TextField(safe: "e.g. john@appleseed.com", text: $user.email)"#
],
[
"before": #".navigationTitle("Edit Profile")"#,
"after": #".navigationTitle(safe: "Edit Profile")"#
]
]
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.