swift-syntax
swift-syntax copied to clipboard
Add Identifier wrapper that strips backticks from token text
Motivation
Following the issue opened in https://github.com/apple/swift-syntax/issues/1936, take the code below as an example:
let x = y.`z`()
When wanting to pull out the character
`z`
you will have to manually strip the backslashes to get the z
character.
The backslashes are completely valid in this case but often using the Swift syntax you might want to remove the backslashes to get the sanitised name of the token for your own purposes which can introduce unnecessary boilerplate.
Solution
Thinking forward, this new Identifier
type allows an abstraction over tokens to further sanitise/strip data out, for example, backticks (as in this PR) or future updates like:
- combined characters, e.g.
é
-
#
symbols such as in#if
- comment blocks like
/*
,///
or//
With the changes in this PR we can take the same example code:
let x = y.`z`()
And by calling [token].identifier.name
we can get z
without the backticks.
Alternatives considered
Stripping the backslashes when returning the .text
is one solution, but the backticks are a valid part of the text and so wouldn't be an accurate way of returning the source code
Trimming the backticks as part of trimmedDescription
is a valid and simpler solution IMO, but doesn't allow for future implementations like the above under the Identifier
abstraction