Skip to content

Commit 3430228

Browse files
Introduce code fix (#24)
* first shot * update README * fix sonarcloud issues * bump version * tweak README * factor out type mapping logic * cleanup * switch to local enum
1 parent 33c0d5c commit 3430228

File tree

12 files changed

+2381
-827
lines changed

12 files changed

+2381
-827
lines changed

README.md

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,91 @@
88

99
> :information_source: This component is unofficial and unsupported by OutSystems.
1010
11-
![Screenshot of Visual Studio Code displaying C# code with highlighted errors in the Problems panel, showing naming and mapping rule violations detected by a the ODC Custom Code Analyzer.](https://github.com/jonathanalgar/CustomCode-Analyzer/blob/main/screenshot.png?raw=true)
11+
![](https://github.com/jonathanalgar/CustomCode-Analyzer/blob/main/screenshot.png?raw=true)
12+
13+
_Screenshot showing the development of an ODC External Library in Visual Studio. The **ODC Custom Code Analyzer** flags several rule violations in the Problems panel. A related Quick Fix menu is open for one rule violation, offering to "Fix this type mapping" so the field correctly matches its `OSDataType.PhoneNumber`. You can get identical functionality in Visual Studio Code and Rider._
1214

1315
## Overview
1416

1517
When you want to extend your ODC apps with custom C# code, you do so with the [External Libraries SDK](https://success.outsystems.com/documentation/outsystems_developer_cloud/building_apps/extend_your_apps_with_custom_code/external_libraries_sdk_readme/). This SDK allows you to write C# code that you can call from your ODC apps.
1618

17-
Although IntelliSense in your IDE guides the available SDK decorators and their syntax, it does not guide the rules you must follow (for example, on [naming](https://www.outsystems.com/tk/redirect?g=OS-ELG-MODL-05019) and [design decisions](https://www.outsystems.com/tk/redirect?g=OS-ELG-MODL-05018)). This guidance is provided when uploading your project's built assembly to the ODC Portal, where you get feedback on rule violations. **Using this component brings that feedback forward and gives you real-time feedback on compliance with the rules as you write the code. You also get real-time guidance on best practices.**
19+
Although IntelliSense in your IDE guides the available SDK decorators and their syntax, it does not guide the rules you must follow (for example, on [naming](https://www.outsystems.com/tk/redirect?g=OS-ELG-MODL-05019) and [design decisions](https://www.outsystems.com/tk/redirect?g=OS-ELG-MODL-05018)). This guidance is provided when uploading your project's built assembly to the ODC Portal, where you get feedback on rule violations. **Using this component brings that feedback forward and gives you real-time feedback on compliance with the rules as you write the code. You also get real-time guidance on best practices and automatic fixes.**
1820

1921
### Technical Primer
2022

2123
When you upload your project's built assembly to the ODC Portal, it does not have access to the underlying code—the ODC Portal checks compliance with the rules by reflecting on the assembly's metadata.
2224

2325
This component, built from scratch, implements the rules using the rich code analysis APIs of [Roslyn](https://github.com/dotnet/roslyn), the .NET compiler.
2426

25-
#### Analyzer phases
27+
```mermaid
28+
graph TB
29+
subgraph User["👩‍💻 User"]
30+
code["Writes code"]:::userArea
31+
end
32+
33+
subgraph Roslyn["🛠 Roslyn"]
34+
ast["Generates AST<br/>and semantic model"]:::roslyn
35+
end
36+
37+
subgraph CustomCode_Analyzer["🔍 CustomCode-Analyzer"]
38+
analysis["Code analysis<br/>and fix provider"]:::analyzerArea
39+
end
40+
41+
subgraph IDE["🖥 IDE"]
42+
display["Displays diagnosis and fixes"]:::ideArea
43+
end
44+
45+
code --> ast
46+
ast --> analysis
47+
analysis --> display
48+
display --> code
49+
50+
Roslyn -."using Roslyn APIs".-> CustomCode_Analyzer
51+
```
52+
53+
#### Code analysis phases
2654

27-
The analyzer operates in two distinct phases, registered through the Roslyn `AnalysisContext`:
55+
The analyzer operates in two distinct phases, registered through the Roslyn [`AnalysisContext`](https://github.com/jonathanalgar/CustomCode-Analyzer/blob/33c0d5ce0a762236a495ebc940b688e9e14cd901/src/CustomCode-Analyzer/Analyzer.cs#L355-L364):
2856

29-
1. **Symbol analysis phase** – Triggered by `RegisterSymbolAction`, this phase performs immediate syntax and semantic analysis on individual declarations as you type. For example, when you declare a method, the analyzer instantly checks if its name starts with an underscore and reports a violation if it does (`NameBeginsWithUnderscoresRule`). These diagnostics appear immediately in your IDE's Problems window.
57+
1. **Symbol analysis phase**
58+
Triggered by [`RegisterSymbolAction`](https://github.com/jonathanalgar/CustomCode-Analyzer/blob/33c0d5ce0a762236a495ebc940b688e9e14cd901/src/CustomCode-Analyzer/Analyzer.cs#L383-L410), this phase performs immediate syntax and semantic analysis on individual declarations as you type. For example, when you declare a method, the analyzer instantly checks if its name starts with an underscore and reports a violation if it does (`NameBeginsWithUnderscoreRule`). These diagnostics appear immediately in your IDE's Problems window.
3059

31-
2. **Compilation end phase** – Registered via `RegisterCompilationEndAction`, this phase runs after all symbols have been processed and the semantic model is complete. For example, it ensures exactly one `[OSInterface]` exists across your entire codebase by maintaining a `ConcurrentDictionary` of interface declarations and validating their uniqueness (`NoSingleInterfaceRule` or `ManyInterfacesRule`). These diagnostics may appear with a slight delay as they require complete semantic analysis.
60+
2. **Compilation end phase**
61+
Registered via [`RegisterCompilationEndAction`](https://github.com/jonathanalgar/CustomCode-Analyzer/blob/33c0d5ce0a762236a495ebc940b688e9e14cd901/src/CustomCode-Analyzer/Analyzer.cs#L414-L417), this phase runs after all symbols have been processed and the semantic model is complete. For example, it ensures exactly one `[OSInterface]` exists across your project by maintaining a `ConcurrentDictionary` of interface declarations and validating their uniqueness (`NoSingleInterfaceRule` or `ManyInterfacesRule`). These diagnostics may appear with a slight delay as they require complete semantic analysis.
62+
63+
#### Code fixes for certain rules
64+
65+
Wherever we can infer a single, unambiguous fix, we provide an **automated code fix** through the Roslyn `CodeFixProvider`. Currently, code fixes are available for these rules:
66+
67+
| Rule description | OS-ELG-MODL code | Action performed |
68+
|----------------------------------|------------------|------------------------------------------------|
69+
| **Non-public OSInterface** | 05004 | Make interface `public` |
70+
| **Non-public OSStructure** | 05010 | Make struct `public` |
71+
| **Non-public OSStructureField** | 05011 | Make property/field `public` |
72+
| **Non-public OSIgnore** | 05012 | Make `[OSIgnore]` property/field `public` |
73+
| **Missing structure decoration** | 05024 | Add `[OSStructure]` attribute to struct |
74+
| **Name begins with underscores** | 05022 | Remove leading `_` in method names |
75+
| **Unsupported type mapping** | 05017 | Change field/property type to match `DataType` |
76+
77+
The code fix suggestions appear as "lightbulb" actions (or "quick actions") in your IDE. If you accept the fix, the analyzer modifies your source code to make it compliant with the respective rule.
3278

3379
## How to use
3480

35-
### Visual Studio 2022 (Enterprise, Pro and Community editions)
81+
### Visual Studio 2022 (Enterprise, Pro and, Community editions)
3682

3783
You can use the auto-updating extension from the Visual Studio Marketplace. Simply [install the extension from the Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=JonathanAlgar.CustomCodeAnalyzer).
3884

3985
If your project references the External Libraries SDK (`OutSystems.ExternalLibraries.SDK`), the extension should automatically start providing feedback on your code.
4086

41-
To ensure real-time feedback for [compilation end phase](#analyzer-phases) rules (not just during builds), you need to configure your Visual Studio's background analysis:
87+
To ensure real-time feedback for [compilation end phase](#analyzer-phases) rules (and not just at the point of build), you need to configure your Visual Studio's background analysis:
4288

4389
1. Select **Tools** > **Options**.
4490
1. From the left menu select **C#** > **Advanced**.
4591
1. Set both **Run background code analysis** for and **Show compiler errors and warnings** to **Entire solution**.
4692
1. Make sure the **Run code analysis in separate process box** is unchecked.
4793

94+
> :bulb: To ensure the extension automatically updates to the latest version, you should enable auto-updates in Visual Studio. Go to **Tools** > **Options** > **Environment** > **Extensions** and make sure **Automatically update extensions** is checked.
95+
4896
### Others
4997

5098
Add the [NuGet package](https://www.nuget.org/packages/CustomCode.Analyzer/) as a dev dependency to your ODC external libraries project:
@@ -53,21 +101,23 @@ Add the [NuGet package](https://www.nuget.org/packages/CustomCode.Analyzer/) as
53101

54102
If your project references the External Libraries SDK (`OutSystems.ExternalLibraries.SDK`), the package should automatically start providing feedback on your code.
55103

104+
The NuGet package cannot be automatically updated, so be sure to update regularlrly to get the latest features.
105+
56106
#### Visual Studio Code
57107

58-
To ensure real-time feedback for [compilation end phase](#analyzer-phases) rules (not just during builds), you need to configure your Visual Studio Code's background analysis:
108+
To ensure real-time feedback for [compilation end phase](#analyzer-phases) rules (and not just at the point of build), you need to configure your Visual Studio's background analysis:
59109

60110
1. Open the command palette (_Ctrl+Shift+P_).
61111
1. Search for "roslyn". Set the **Dotnet › Background Analysis: Analyzer Diagnostics Scope** to **fullSolution**.
62112

63-
> :bulb: Auto-updating extensions for Visual Studio Code and Rider are in the works.
113+
> :bug: For the [code fix](#code-fixes-for-certain-rules) to work, you need to be switch to using the pre-release version of the C# extension. See [this issue](https://github.com/dotnet/vscode-csharp/issues/7802).
64114
65115
## TODO
66116

67117
See [here](https://github.com/jonathanalgar/CustomCode-Analyzer/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement).
68118

69-
## Contributing
119+
## Feedback and contributions
70120

71-
Please report bugs and feature requests [here](https://github.com/jonathanalgar/CustomCode-Analyzer/issues/new/choose).
121+
Please report bugs and feature requests [here](https://github.com/jonathanalgar/CustomCode-Analyzer/issues/new/choose). Feel free to leave general feedback on the [OutSystems Community Forum post](https://www.outsystems.com/forums/discussion/100963/odc-external-libraries-custom-code-analyzer/).
72122

73-
PRs are welcome. Code quality improvements, new features (especially those unassigned and listed [here](https://github.com/jonathanalgar/CustomCode-Analyzer/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement)) and documentation improvements are all welcome 🤗 All changes to Analyzer code should pass all existing tests (`dotnet test`) and all new features should be covered by new tests.
123+
PRs are welcome. Code quality improvements, new features (especially those unassigned and listed [here](https://github.com/jonathanalgar/CustomCode-Analyzer/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement)), and documentation improvements are all welcome 🤗 All changes to Analyzer code should pass all existing tests (`dotnet test`), and all new features should be covered by new tests. Please format any new code with [csharpier](https://csharpier.com/).

screenshot.png

50.9 KB
Loading

src/CustomCode-Analyzer.Vsix/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
33
<Metadata>
4-
<Identity Id="CustomCode_Analyzer.Vsix.e1c79fe3-d19f-4bf9-9c0c-57ba1f95b494" Version="0.1.4" Language="en-US" Publisher="Jonathan Algar"/>
4+
<Identity Id="CustomCode_Analyzer.Vsix.e1c79fe3-d19f-4bf9-9c0c-57ba1f95b494" Version="0.2.0" Language="en-US" Publisher="Jonathan Algar"/>
55
<DisplayName>ODC Custom Code Analyzer</DisplayName>
66
<Description xml:space="preserve">Get feedback on your OutSytems Developer Cloud (ODC) custom C# code as you code.</Description>
77
<MoreInfo>https://github.com/jonathanalgar/CustomCode-Analyzer</MoreInfo>

0 commit comments

Comments
 (0)