Skip to content

Instantly share code, notes, and snippets.

@DamianReeves
Created May 12, 2023 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DamianReeves/252aa614f2d4cfcdb8a612b25963934a to your computer and use it in GitHub Desktop.
Save DamianReeves/252aa614f2d4cfcdb8a612b25963934a to your computer and use it in GitHub Desktop.
F# Analyzer to Restrict language
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
| _ -> [ ]
namespace TestCases
type ShouldFail = ShouldFail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment