Get list of keywords in swift in macros

With the introduction of macros in Swift 5.9, it is possible for macros to generate declarations from user provided string data, i.e. generating Codable model from JSON. This has potential to create declarations that might conflict with Swift keywords. Does macro authors have any way to check if given string is a swift keyword?

I found this list of keywords in swift-syntax repo but it is kept private. I am unable to use this initializer in Keyword type either as getting error when trying to use SyntaxName type.

Hi there,
you can compare both the keyword provided by swiftMacro as a string and the string you are getting from the json then you can avoid conflict by using codingKeys when you find a keyword.

Hi @la-pieuvre

keyword provided by swiftMacro as a string

Can you elaborate what you mean by this? In the example I mentioned user will be providing a JSON string as input to macro and the macro has to generate Codable model from this string. How will the macro guard against field JSON key names like: default, case etc?

I think you can always wrap the field name in `...` even if it's not a keyword:

enum Example {
  case `notAKeyword`
  case `default`

So, you could just always generate the backticks to be safe.

sorry for the late answer, here you have the list of keywords : swift-syntax Documentation – Swift Package Index

I hope that helps