-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleVisitor.cs
More file actions
78 lines (66 loc) · 3.04 KB
/
Copy pathRuleVisitor.cs
File metadata and controls
78 lines (66 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2024, 2026 James Draycott <me@racci.dev>. All Rights Reserved.
// Licensed under the AGPL-3.0-or-later License, See LICENSE in the project root
// for license information.
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation.Language;
using Compiler.Module.Compiled;
using LanguageExt;
namespace Compiler.Analyser.Rules;
public sealed class RuleVisitor(
IEnumerable<Rule> rules,
IEnumerable<Compiled> imports) : AstVisitor {
private readonly IEnumerable<Rule> Rules = rules;
private readonly IEnumerable<Compiled> Imports = imports;
private readonly Dictionary<int, Dictionary<Rule, bool>> ThreadLocalCache = [];
public readonly List<Issue> Issues = [];
public void VisitModule([NotNull] CompiledLocalModule compiledModule) {
this.ThreadLocalCache.Add(Environment.CurrentManagedThreadId, []);
try {
foreach (var rule in this.Rules) {
this.ThreadLocalCache[Environment.CurrentManagedThreadId].Add(rule, rule.SupportsModule(compiledModule));
}
compiledModule.Document.Ast.Visit(this);
} finally {
this.ThreadLocalCache.Remove(Environment.CurrentManagedThreadId);
}
}
public override AstVisitAction DefaultVisit(Ast ast) {
if (GetSuppressions(ast).IsErr(out var err, out var suppressions)) {
if (err is Issue issue) {
this.Issues.Add(issue);
} else if (err is ManyErrors errors) {
this.Issues.AddRange(errors.Errors.Cast<Issue>());
}
return AstVisitAction.SkipChildren;
}
foreach (var rule in this.Rules) {
// If the key doesn't exist assume we support it, this allows for usage outside of the visitor like in tests.
if (this.ThreadLocalCache.TryGetValue(Environment.CurrentManagedThreadId, out var threadCache)
&& threadCache.TryGetValue(rule, out var supports)
&& !supports
) continue;
if (!rule.ShouldProcess(ast, suppressions)) continue;
foreach (var issue in rule.Analyse(ast, this.Imports)) {
this.Issues.Add(issue);
}
}
return AstVisitAction.Continue;
}
public static Fin<IEnumerable<Suppression>> GetSuppressions(Ast ast) {
var paramBlock = AstHelper.FindClosestParamBlock(ast);
IEnumerable<AttributeAst> attributes;
if (paramBlock != null) {
attributes = paramBlock.Attributes;
} else {
// Check script-level attributes if no param block found
var root = AstHelper.FindRoot(ast);
if (root is ScriptBlockAst scriptBlock && scriptBlock.ParamBlock == null) {
attributes = scriptBlock.Attributes;
} else {
return Fin.Succ(Enumerable.Empty<Suppression>());
}
}
return SuppressAnalyserAttributeExt.FromAttributes(attributes)
.Map(suppressions => suppressions.Select(suppression => suppression.GetSuppression()));
}
}