-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Escape deprecation messages (#2951)
- Loading branch information
1 parent
3aa8812
commit 58f6d3f
Showing
7 changed files
with
473 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
Sources/ApolloCodegenLib/TemplateString+AvailabilityDeprecated.swift
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
Sources/ApolloCodegenLib/TemplateString+DeprecationMessage.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
extension TemplateString.StringInterpolation { | ||
|
||
mutating func appendInterpolation( | ||
deprecationReason: String?, | ||
config: ApolloCodegen.ConfigurationContext | ||
) { | ||
guard | ||
config.options.warningsOnDeprecatedUsage == .include, | ||
let escapedDeprecationReason = deprecationReason?.escapedSwiftStringSpecialCharacters() | ||
else { | ||
removeLineIfEmpty() | ||
return | ||
} | ||
|
||
appendInterpolation(""" | ||
@available(*, deprecated, message: \"\(escapedDeprecationReason)\") | ||
""") | ||
} | ||
|
||
mutating func appendInterpolation( | ||
field: String, | ||
argument: String, | ||
warningReason: String | ||
) { | ||
let escapedWarningReason = warningReason.escapedSwiftStringSpecialCharacters() | ||
|
||
appendInterpolation(""" | ||
#warning("Argument '\(argument)' of field '\(field)' is deprecated. \ | ||
Reason: '\(escapedWarningReason)'") | ||
""") | ||
} | ||
} | ||
|
||
extension String { | ||
/// Replaces specific escaped characters so they are written into the rendered deprecation | ||
/// message as escaped characters to be correctly rendered to the user in an Xcode warning | ||
/// (e.g., `\"` becomes `\\\"`). | ||
/// | ||
/// String literals can include the following special characters: `\0` (null character), | ||
/// `\\` (backslash), `\t` (horizontal tab), `\n` (line feed), `\r` (carriage return), | ||
/// `\"` (double quotation mark) and `\'` (single quotation mark). | ||
func escapedSwiftStringSpecialCharacters() -> String { | ||
var escapedString = String() | ||
escapedString.reserveCapacity(self.count) | ||
|
||
forEach { character in | ||
switch (character) { | ||
case "\0": escapedString.append(#"\0"#) | ||
case "\\": escapedString.append(#"\\"#) | ||
case "\t": escapedString.append(#"\t"#) | ||
case "\n": escapedString.append(#"\n"#) | ||
case "\r": escapedString.append(#"\r"#) | ||
case "\"": escapedString.append(#"\""#) | ||
case "\'": escapedString.append(#"\'"#) | ||
default: escapedString.append(character) | ||
} | ||
} | ||
|
||
return escapedString | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.