Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/ScriptAnalysis/ScriptAnalysisContracts.cs
T
Joseph Doherty 73f49eaf32 fix(scripting): #1063 one compile-refusal authority for editor and deploy gate (ScriptCompileRules + the runtime's own wrapper text) — CS0161 and wrapper injection are now flagged in the editor; #1065 {{equip}} in a Calculation script is refused by the gate and by a new Calculation editor mode
#1063: the Monaco diagnostics dropped every Roslyn error located in the synthesized
wrapper as 'phantom', so CS0161 (not all code paths return a value, pinned on Run)
passed the editor and failed the deploy gate. ScriptCompileRules (Core.Scripting) now
states which diagnostics refuse (every Error, wherever located; LMX001/002 injection)
and which sources skip the compile (blank/passthrough value scripts); ScriptEvaluator
and the editor both use it, and the editor wraps with ScriptEvaluator.WrapperPreamble/
WrapperSuffix instead of its own copy. Wrapper-located refusals are drawn on the
script's last line. EditorGateParityTests runs one corpus through editor and gate in
all three modes.

#1065: a Calculation tag is raw/device-level with no equipment and its seam never
substitutes {{equip}}, so such a script deployed clean and read Bad forever. Refused
rather than substituted: EquipTokenLiteralScanner.FindInCalculation is run by
RoslynDeployScriptChecker for every Calculation use (before the passthrough shortcut,
outside the shared VT/Calc cache) and by the editor in the new 'calculation' mode
(Calculation tag editor; ScriptEdit defaults to it when a Calculation tag uses the
script).
2026-12-03 16:47:14 -05:00

71 lines
4.3 KiB
C#

using ZB.MOM.WW.OtOpcUa.Core.Scripting;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.ScriptAnalysis;
/// <summary>
/// Which seam the editor analyses a script as (lmxopcua#1025). The deploy gate compiles each script as the seam that
/// uses it, so the editor has to wrap the source the same way: a value script is
/// <c>object Run(ScriptGlobals&lt;VirtualTagContext&gt;)</c> (VirtualTag and Calculation share it); a scripted-alarm
/// predicate is <c>bool Run(ScriptGlobals&lt;AlarmPredicateContext&gt;)</c>. A Calculation script compiles as a value
/// script but is held to one more gate rule — no <c>{{equip}}</c> token (lmxopcua#1065) — so it has its own mode.
/// Carried as a string on the wire; null, blank or anything unrecognised means <see cref="Value"/>.
/// </summary>
public static class ScriptAnalysisModes
{
/// <summary>A VirtualTag value script (the default).</summary>
public const string Value = "value";
/// <summary>A scripted-alarm predicate: must return <c>bool</c>.</summary>
public const string AlarmPredicate = "alarmPredicate";
/// <summary>A Calculation-tag script: the value shape, plus the Calculation-only refusal of <c>{{equip}}</c>.</summary>
public const string Calculation = "calculation";
/// <summary>True when <paramref name="mode"/> selects the predicate shape.</summary>
/// <param name="mode">The wire value.</param>
/// <returns>Whether it names <see cref="AlarmPredicate"/>.</returns>
public static bool IsPredicate(string? mode) => string.Equals(mode, AlarmPredicate, StringComparison.OrdinalIgnoreCase);
/// <summary>True when <paramref name="mode"/> selects the Calculation rules.</summary>
/// <param name="mode">The wire value.</param>
/// <returns>Whether it names <see cref="Calculation"/>.</returns>
public static bool IsCalculation(string? mode) => string.Equals(mode, Calculation, StringComparison.OrdinalIgnoreCase);
/// <summary>The deploy-gate seam a mode analyses as — what the gate's checker is asked for the same script.</summary>
/// <param name="mode">The wire value.</param>
/// <returns>The seam.</returns>
public static ScriptContextKind KindOf(string? mode) =>
IsPredicate(mode) ? ScriptContextKind.AlarmPredicate
: IsCalculation(mode) ? ScriptContextKind.Calculation
: ScriptContextKind.VirtualTag;
/// <summary>The canonical wire value for <paramref name="mode"/> (unrecognised means <see cref="Value"/>).</summary>
/// <param name="mode">The wire value.</param>
/// <returns>One of the three constants.</returns>
public static string Normalize(string? mode) =>
IsPredicate(mode) ? AlarmPredicate : IsCalculation(mode) ? Calculation : Value;
}
// EquipmentId (optional) carries the owning-equipment context the {{equip}}/<RefName> completion +
// diagnostic need to resolve reference effective names. Null on the shared ScriptEdit page (no single
// equipment) — the equip-ref features are then inert, matching the deploy gate (which only resolves per
// owning equipment). Mode (optional) selects the seam the script is analysed as — see ScriptAnalysisModes.
public record DiagnoseRequest(string Code, string? EquipmentId = null, string? Mode = null);
public record DiagnoseResponse(IReadOnlyList<DiagnosticMarker> Markers);
public record DiagnosticMarker(int Severity, int StartLineNumber, int StartColumn,
int EndLineNumber, int EndColumn, string Message, string Code);
public record CompletionsRequest(string CodeText, int Line, int Column, string? EquipmentId = null, string? Mode = null);
public record CompletionsResponse(IReadOnlyList<CompletionItem> Items);
public record CompletionItem(string Label, string InsertText, string Detail, string Kind, int InsertTextRules = 0);
public record HoverRequest(string CodeText, int Line, int Column, string? EquipmentId = null, string? Mode = null);
public record HoverResponse(string? Markdown);
public record SignatureHelpRequest(string CodeText, int Line, int Column, string? Mode = null);
public record SignatureHelpResponse(string? Label, IReadOnlyList<SignatureHelpParameter>? Parameters, int ActiveParameter);
public record SignatureHelpParameter(string Label, string? Documentation);
public record FormatRequest(string Code);
public record FormatResponse(string Code);