Created
May 12, 2023 15:45
-
-
Save DamianReeves/252aa614f2d4cfcdb8a612b25963934a to your computer and use it in GitHub Desktop.
F# Analyzer to Restrict language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RestrictionAnalyzer | |
open FSharp.Analyzers.SDK | |
open FSharp.Compiler.Text | |
open FSharp.Compiler.Syntax | |
let hasTypeInNamespace (target:SynModuleOrNamespace): Message option = | |
match target with | |
| SynModuleOrNamespace(kind = SynModuleOrNamespaceKind.DeclaredNamespace; decls = decls) -> | |
let hasTypes = | |
decls | |
|> List.exists (function | |
| SynModuleDecl.Types _ -> true | |
| _ -> false) | |
if hasTypes then | |
{ | |
Type = "Restriction analyzer" | |
Message = "Types not allowed" | |
Severity = Severity.Error | |
Code = "TR001" | |
Range = Range.Zero | |
Fixes = [] | |
} |> Some | |
else | |
None | |
| _ -> None | |
[<Analyzer "RestrictionAnalyzer">] | |
let badCodeAnalyzer : Analyzer = | |
fun (context: Context) -> | |
match context.ParseTree with | |
| ParsedInput.ImplFile (ParsedImplFileInput(contents=contents)) -> | |
contents | |
|> List.choose hasTypeInNamespace | |
| _ -> [ ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TestCases | |
type ShouldFail = ShouldFail | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment