feat(cdlEventHandler): detect TOGGLE Off and emit *_defined globals

Record parameters marked with "TOGGLE Off" by adding an optional
toggleOffParameterNames field and including an extra <var>_defined global
for each such parameter in createCdlEventHandlerSnippet.

To support this, the parser was changed from a brace-delta approach to a
token-based scanner so nesting and the association between a PARAM and its
TOGGLE can be tracked reliably. Added unit tests that verify detection,
case-insensitivity, and ignoring TOGGLE occurrences inside comments/strings
or other events.
This commit is contained in:
Christoph Brandau
2026-09-21 20:51:02 +02:00
parent 757b885f28
commit 547da644ce
2 changed files with 83 additions and 30 deletions
+51
View File
@@ -0,0 +1,51 @@
const assert = require("node:assert/strict")
const fs = require("node:fs")
const path = require("node:path")
const vm = require("node:vm")
const { test } = require("node:test")
const { transformSync } = require("esbuild")
const source = fs.readFileSync(path.join(__dirname, "../client/src/common/cdlEventHandler.ts"), "utf8")
const compiled = transformSync(source, { loader: "ts", format: "cjs" }).code
const context = { module: { exports: {} } }
vm.runInNewContext(compiled, context)
const { cdlEventHandlerAtLine, createCdlEventHandlerSnippet } = context.module.exports
test("TOGGLE Off adds the defined globals to the event handler", () => {
const cdl = `EVENT GDM_header
{
PARAM product_status {
TYPE o
OPTIONS "Serie", "Prototyp"
}
PARAM stm_param_mpf_name
{
TYPE s
TOGGLE Off
}
PARAM stm_param_wks_path {
TYPE s
TOGGLE Off
}
}`
const snippet = createCdlEventHandlerSnippet(cdlEventHandlerAtLine(cdl, 0))
assert.match(snippet, /global mom_stm_param_mpf_name_defined/)
assert.match(snippet, /global mom_stm_param_wks_path_defined/)
assert.match(snippet, /global mom_product_status\n/)
assert.doesNotMatch(snippet, /mom_product_status_defined/)
})
test("toggle detection ignores comments, strings, other events, and TOGGLE On", () => {
const cdl = `EVENT first {
PARAM plain { TYPE s UI_LABEL "TOGGLE Off { ignored }" }
PARAM enabled { TOGGLE On }
PARAM commented { TYPE s # TOGGLE Off
}
PARAM mom_disabled { TOGGLE off }
}
EVENT second { PARAM other { TOGGLE Off } }`
const snippet = createCdlEventHandlerSnippet(cdlEventHandlerAtLine(cdl, 0))
assert.match(snippet, /global mom_disabled_defined/)
assert.equal((snippet.match(/_defined/g) ?? []).length, 1)
assert.doesNotMatch(snippet, /mom_mom_|mom_other/)
})