ScLint
This repository contains a set of C# Roslyn code analyzers helping to maintain compliance with good practices while working on Sitecore projects.
Installation guide
Code analyzers
Run root\src\Roslyn\ScLint\ScLint\ScLint.Vsix\bin\Debug\ScLint.vsix VSIX file to start installation process of extension on your local environment. Install the set of code analyzers to specific Visual Studio release by selecting target version in dialog box. After successful installation you can see the extension in Tools - Extension and Updates... (Visual Studio 2017) or Extensions - Manage Extensions (Visual Studio 2019) window.
Content
List of included C# code analyzers
Repository's directory "src" contains "Roslyn" folder where C# solution "ScLint" with analyzers' source code is placed. Analyzers are located in different folders in Visual Studio solution.
# | Diagnostic ID | Task | Description | Folder name in project |
---|---|---|---|---|
1. | ScLint1 | Avoid direct SQL queries in code | Code analyzer checks if there are any strings in code with direct SQL queries. Type: warning |
DirectSqlCalls |
2. | ScLint2 | Do not use block comments in code | Code is analyzed in respect of use of block comments. There shouldn't be any this kind of comments, only one-line comments are allowed. Code fix provides a possibility to delete it automatically. Type: warning |
BlockComments |
3. | ScLint3 | Do not use hard coded paths to get Sitecore items | Rule checks variable declarations and return statements to catch all occurrences of obsolete way of referencing items - searching them by providing their paths and using following methods: GetItem, GetRootItem, SelectItems, SelectItemsUsingXPath, SelectSingleItem, SelectSingleItemUsingXPath. Code analyzer suggests to give items' guids instead of paths and to use other methods to get them. Type: warning |
HardCodedPaths |
4. | ScLint4 | Do not use GUIDs of unknown items | Rule checks if GUIDs are used as method arguments or as attributes in code - then reports a warning. GUIDs can be provided only while assigning to variables. Type: warning |
HardCodedGuids |
5. | ScLint5 | Do not use hard coded image paths | Rule checks if paths to media library items are used direct (being not wrapped in variables) in methods as parameters. This approach is reported as a warning since paths should be assigned to variables to suggest which items they are referring to. Type: warning |
HardCodedImagePaths |
Development
Code analyzers
C# solution contains separate folder for each diagnostic rule where code analyzers ([rule_name]Analyzer.cs) and code fix providers ([rule_name]CodeFixProvider.cs) are placed.
In code analyzer's file there are label strings declared (DiagnosticId, Title, Category variables). Initialize method is for launching analyzing for specific code elements (given as a second parameter in RegisterSyntaxNodeAction method) and AnalyzeNode is a scenario when given code element appeared in code. There should be also a hence call to code fix provider.
In code fix provider's file, in RegisterCodeFixesAsync method we should provide an algorithm to fix reported part of code or leave this method empty when e.g. warning report is sufficient and there is no need to change the syntax.
Disclaimer
SitecoreĀ® is a registered trademark of Sitecore Corporation.
Contributing
Your pull requests are very welcome.
License
This work is APACHE LICENSE, VERSION 2.0 licensed.