Skip to content
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

An experimental tool to check regular expressions at compile time #322

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

crazytonyli
Copy link
Contributor

This is a random idea popped into my head today, not sure if it's worth pursuing, but I had fun learning swift-syntax and swift AST. We can move this tool into the bash-cache plugin or some other places, if we find this tool useful. 😸

The idea is use a special inline comment // CHECK-REGEX to check if the following string constant declaration is a valid regex. Kinda like Swift 5.7's Regex constant compile time check, except we don't have to wait for 2 or 3 years (Swift's Regex is only available on iOS 16).

Here is an example:

// CHECK-REGEX
let pattern = #"\[[^\]]+\]"#

One requirement of this check is the string constant needs to be a raw string literal (without string interpolation), otherwise we can't know the regex at compile time.

The implementation is quite simple. Iterate through all the Swift files in given directory, find a declaration that has this special inline comment, extract the regex from the expression, and finally check the regex by creating a NSRegularExpression instance with it.

Since this is an experimental tool, I didn't handle any edge cases (i.e. the inline comment is used on a non-string declaration) and the error reporting probably can be improved too (i.e. include file and line number in the error message).


  • I have considered if this change warrants release notes and have added them to the appropriate section in the CHANGELOG.md if necessary.

@crazytonyli crazytonyli requested a review from a team November 15, 2022 08:46
Copy link
Contributor

@AliSoftware AliSoftware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very cool idea and experiment! 😍 Love the idea of using Swift to create tooling and of using SwiftSyntax. 🙂

TBH I'm not sure it's going to be used much in practice tho 😕; after all if we had invalid regexes—as in, doesn't compile—we'd likely have caught it by seeing crashes at runtime at that point, and since it's opt-in, devs will have to remember to use the magic comment, while I'm not sure they will.

So TL;DR: while for a repo like WP-iOS-Shared (which is seldom updated) it can be fine to enable and merge this, I'm not sure it'd be e.g. worth adding a similar tool to all our iOS repos (e.g. WPiOS, WCiOS) or to spend too much time on this, however fun and pretty that tool is?

That being said, I'd love to see some P2 post explaining this (e.g. "Experiment: How to use SwiftSyntax to build a tool to validate code") to share the experience and knowledge and show how easy could be for us to write Swift tooling using SwiftSyntax for other similar means. I think using it can be super powerful, but as SwiftSyntax can be intimidating to anyone who never played with it at first, a P2 post could show how it can be not that scary after all 😄

@crazytonyli
Copy link
Contributor Author

@AliSoftware Agreed. In practice, when adding a new regex, not sure about others but I always go to some regex tester website to ensure I have a valid regex for my use case. I sure don't want to test the regex by building and running an iOS app. Good point about P2 by the way, I'll start a draft when I get some time 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants