A ReSharper plugin to detect suspicious semicolons in Razor views

I must admit, every now and then I find myself typing yet another semicolon in one of my razor views. I usually also hit the backspace key immediately after. But sometimes, a lone wolf semicolon gets through and is rendered in the resulting HTML page. I HATE this! It's the most silly bug report you'll ever spend time on. The effort-value ratio of filing the bug and resolving it is far below zero, but as it is a UI issue it most likely will get prioritized over other more important stuff. It's still waste. ReSharper is great at reducing waste, so I figured it'd be a good idea to have a ReSharper code inspection to highlight these semicolons and generate warnings. To my surprise I found this wasn't built-in so the idea of a plug-in was born.

I've built a very simple XML-minification plug-in for ReSharper before, but didn't really plan the time for building another one. The ReSharper SDK is quite awesome, though it could use some more loose-coupling and perhaps be NuGetized? I popped the idea on Twitter to see if people would think it's useful or not, and in the hopes to find someone who had the time or was willing to assist. A huge thanks to Matt Ellis (@citizenmatt) and Igal Tabachnik (@hmemcpy) for your enthousiasm and sharing some very useful pointers on how this plug-in could be implemented! Your input made it look like a no-brainer so I figured it couldn't harm to spend half-an-hour on it.

The idea was to build a custom daemon to analyze HTML tokens within a Razor file. Razor files can contain HTML, JavaScript, Razor-syntax, C#, CSS… and hence are considered a multi-language PSI file. Bear with me, it's actually pretty straightforward and there's a great sample daemon implementation included in the SDK as well. It's a matter of understanding the concepts, reading the sample code, writing some similar code and hitting F5 a few times to debug your own implementation. This plug-in is only 5 classes!

The SDK comes with a set of project and item-templates, pre-configured to provide the necessary "devenv.exe /ReSharper.Internal" instructions to unlock some ReSharper internal utilities in the Visual Studio instance you'll be debugging. The PSI Browser is a kind of document explorer and provides a ton of information on each element, character and its context within the file.

That's how I found out I only needed to inspect the HTML tokens. It also showed me that a whitespace character is a separate token, thus splitting up HTML tokens containing text. Finding the suspicious semicolon (usually at the end of a Razor statement and at the beginning of meaningful HTML tokens) is as easy as finding the HTML token that only contains a single semicolon character. This covers about 95% of semicolon mistakes I believe, which is good enough for me at the moment. I also didn't want this plug-in to generate warnings for the semicolon character in code snippets, in HTML entities (such as © for instance), or in smileys ;-).

This plug-in will provide warnings and highlighting when a suspicious semicolon has been detected in a Razor view, and a quick-fix to get rid of it ALT-ENTER-style.

Just file an issue on my GitHub repo if you find something fishy, and note that I accept pull requests (and will happily add your name to the Author's tag of the NuGet package that this plug-in will ship in).

JetBrains just announced the new ReSharper Extension Manager using a NuGet feed which is now also available as a package source preset on MyGet. For those interested: I've hooked up my GitHub repository with my MyGet feed's POST hook, so I can automatically package-n-publish the plug-in for testing and release it with a click of a button. I'll do a follow-up post on how I did this, but for those who can't wait, there's useful info in this blog post (I'm using a build.bat file to hook into the build process).

To install my ReSharper extension, you need ReSharper 8 EAP or higher. For those on an earlier version of ReSharper, check for a compatible download in my GitHub repository.

Installing ReSharper plug-ins shipped as NuGet packages and get notifications about available updates: killer feature if you ask me!

Sharing is caring

Posted by Xavier Decoster on
Last revised: 30 Apr, 2013 04:25 PM
blog comments powered by Disqus