-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keyword sanitization for Lens code generation (#2924) #2925
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8dd33cb
String sanitization extension function added
hrafnthor 3035cdd
Reserved keyword containing package declaration added to Lens tests
hrafnthor cdf67d4
Reserved keyword containing package declaration added to Optic tests
hrafnthor 73268ae
keyword sanitation added to qualifiedString processing
hrafnthor 84c13fc
A larger set of Kotlin keywords introduced.
hrafnthor 65751fc
Sanitation extension function simplified with signature modifications
hrafnthor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,103 @@ package arrow.optics.plugin.internals | |
*/ | ||
fun String.plusIfNotBlank(prefix: String = "", postfix: String = "") = | ||
if (isNotBlank()) "$prefix${this}$postfix" else this | ||
|
||
/** | ||
* Sanitizes each delimited section if it matches with Kotlin reserved keywords. | ||
*/ | ||
fun String.sanitizeDelimited(delimiter: String = ".", separator: String = delimiter) = | ||
if (isNotBlank() && contains(delimiter)) { | ||
splitToSequence(delimiter).joinToString(separator) { if (kotlinKeywords.contains(it)) "`$it`" else it } | ||
} else this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to consider empty and not containing delimiter as special cases, |
||
|
||
|
||
private val kotlinKeywords = setOf( | ||
// Hard keywords | ||
"as", | ||
"break", | ||
"class", | ||
"continue", | ||
"do", | ||
"else", | ||
"false", | ||
"for", | ||
"fun", | ||
"if", | ||
"in", | ||
"interface", | ||
"is", | ||
"null", | ||
"object", | ||
"package", | ||
"return", | ||
"super", | ||
"this", | ||
"throw", | ||
"true", | ||
"try", | ||
"typealias", | ||
"typeof", | ||
"val", | ||
"var", | ||
"when", | ||
"while", | ||
|
||
// Soft keywords | ||
"by", | ||
"catch", | ||
"constructor", | ||
"delegate", | ||
"dynamic", | ||
"field", | ||
"file", | ||
"finally", | ||
"get", | ||
"import", | ||
"init", | ||
"param", | ||
"property", | ||
"receiver", | ||
"set", | ||
"setparam", | ||
"where", | ||
|
||
// Modifier keywords | ||
"actual", | ||
"abstract", | ||
"annotation", | ||
"companion", | ||
"const", | ||
"crossinline", | ||
"data", | ||
"enum", | ||
"expect", | ||
"external", | ||
"final", | ||
"infix", | ||
"inline", | ||
"inner", | ||
"internal", | ||
"lateinit", | ||
"noinline", | ||
"open", | ||
"operator", | ||
"out", | ||
"override", | ||
"private", | ||
"protected", | ||
"public", | ||
"reified", | ||
"sealed", | ||
"suspend", | ||
"tailrec", | ||
"value", | ||
"vararg", | ||
|
||
// These aren't keywords anymore but still break some code if unescaped. | ||
// https://youtrack.jetbrains.com/issue/KT-52315 | ||
"header", | ||
"impl", | ||
|
||
// Other reserved keywords | ||
"yield", | ||
) |
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
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
2 changes: 2 additions & 0 deletions
2
arrow-libs/optics/arrow-optics-ksp-plugin/src/test/kotlin/arrow/optics/plugin/Utils.kt
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might read better if
asStringSanitized
was an extension ofName
(but if you prefer this way, it's also fine)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it would be more readable as well as limit the scope of the function to relevant parts i.e KSName rather than any String. Will change the extension signature and apply it where applicable.