Skip to content

Instantly share code, notes, and snippets.

@Jeehut
Last active July 19, 2020 20:28
Show Gist options
  • Save Jeehut/c3721a53aca3b7fb8c2e14f4bda32935 to your computer and use it in GitHub Desktop.
Save Jeehut/c3721a53aca3b7fb8c2e14f4bda32935 to your computer and use it in GitHub Desktop.
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. [email protected]", text: $user.email)"#,
"after": #"TextField(safe: "e.g. [email protected]", 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