GitNexus/gitnexus/test/unit/cobol-preprocessor.test.ts
Gergő Magyar d2cd0b676f
feat: add COBOL language support with regex extraction pipeline (#498)
* feat: add COBOL language support with regex extraction pipeline

Standalone COBOL processor following the markdown-processor.ts pattern:
- No LanguageProvider modification — COBOL uses regex, not tree-sitter
- No SupportedLanguages enum change — standalone processor pattern

New files:
- cobol-processor.ts — orchestrator (processCobol, isCobolFile, isJclFile)
- cobol/cobol-preprocessor.ts — regex state machine extraction (~888 LOC)
- cobol/cobol-copy-expander.ts — COPY statement expansion with circular detection
- cobol/jcl-parser.ts — JCL job/step/DD extraction
- cobol/jcl-processor.ts — JCL graph node creation

Extraction produces:
- Module nodes (PROGRAM-ID)
- Function nodes (paragraphs)
- Namespace nodes (sections)
- Property nodes (data items)
- CALLS edges (PERFORM intra-file, CALL cross-program)
- IMPORTS edges (COPY statements)
- CONTAINS edges (section → paragraph hierarchy)

Pipeline integration: single processCobol() call in Phase 2.6

54 new tests (33 COBOL + 21 JCL), all 3889 tests pass.

* docs: document custom processor pattern in pipeline.ts

Add comment block at the custom processor integration point
documenting the pattern for future non-tree-sitter language additions.

* feat(cobol): enrich graph with EXEC SQL/CICS, ENTRY points, MOVE data flow, PERFORM THRU

Maps the remaining 60% of CobolRegexResults to the graph:
- EXEC SQL blocks → CodeElement nodes + ACCESSES edges to DB tables
- EXEC CICS LINK/XCTL → CodeElement nodes + cross-program CALLS edges
- ENTRY points → Constructor nodes (registered for cross-program resolution)
- MOVE statements → ACCESSES edges (read/write data flow tracking)
- PERFORM THRU → expanded CALLS edges for range targets
- File declarations → Record nodes with assignment metadata
- Cross-program CALL 2nd pass: resolves unresolved targets after all programs processed

* test(cobol): add 26 integration tests with exact assertions + fix CICS resolution bug

Integration tests (test/integration/resolvers/cobol.test.ts):
- 26 tests covering full COBOL system extraction
- ALL assertions use exact toBe(N) — zero fuzzy assertions
- Fixtures: CUSTUPDT.cbl, AUDITLOG.cbl, CUSTDAT.cpy, RPTGEN.cbl, RUNJOBS.jcl

Bug fix (cobol-processor.ts):
- CICS LINK/XCTL cross-program resolution was broken — edges were
  created with "resolved" reason but pointing to <unresolved> targets
- Fix: use cics-link-unresolved / cics-xctl-unresolved suffix pattern
  matching the existing cobol-call-unresolved pattern
- Second-pass resolver now patches both CALL and CICS unresolved edges

All 3915 tests pass, 0 failures.

* test(cobol): exhaustive 57-test suite with strict exact assertions

Complete rewrite of COBOL integration tests using ground-truth approach:
dump the full graph, then assert EVERY node and EVERY edge.

57 tests across 9 sections:
- Node completeness: Module(3), Function(13), Namespace(2), Property(21),
  Record(1), CodeElement(8), Constructor(1) — exact sorted arrays
- Edge completeness: 22 tests covering every type+reason combination
  with exact source→target pairs
- Cross-program resolution: 6 tests verifying CALL, CICS LINK/XCTL, JCL
- COPY expansion: copybook data items in RPTGEN
- Section hierarchy: exact paragraph membership per section
- Data item ownership: exact per-module breakdown
- MOVE data flow: exact read/write pairs
- JCL integration: job/step/dataset containment
- Grand totals: CALLS(22), CONTAINS(48), IMPORTS(1), ACCESSES(7)

Fixture enhancements:
- CUSTUPDT.cbl: added INIT-SECTION + PROCESSING-SECTION, PERFORM THRU
- AUDITLOG.cbl: added ENTRY "AUDITLOG-BATCH"
- RPTGEN.cbl: added EXEC CICS XCTL

Zero fuzzy assertions — every expect uses toBe(N) or toEqual([...sorted]).

* fix(cobol): add removeRelationship API + single-quote CALL/COPY/ENTRY, PERFORM keyword skip

Phase 0A: Add removeRelationship(id) to KnowledgeGraph interface and
implementation (trivial Map.delete wrapper). Required for orphan edge
cleanup in next commit.

Phase 1A (from PR #500 review, modified):
- RE_CALL and RE_COPY_QUOTED now match both "double" and 'single' quotes
- parseSingleCopyStatement in copy-expander updated for single quotes
- PERFORM_KEYWORD_SKIP set prevents UNTIL/VARYING/WITH/TEST/FOREVER
  from being stored as false-positive perform targets
- Sequence number stripping uses /[^0-9 ]/ (preserves numeric seq numbers
  unlike PR #500's /\S/ which stripped them)
- Normalized || to ?? for regex group extraction in copy-expander

5 new graph unit tests, all 57 COBOL integration tests pass.

* fix(cobol): RE_ENTRY single-quote + remove orphan unresolved CALLS edges

Phase 1B: RE_ENTRY regex now supports both "double" and 'single' quoted
ENTRY targets. Uses named intermediates (entryName, usingClause) with ??
operator. USING capture group shifted from [2] to [3].

Phase 1C: Second-pass resolution now collects resolved orphan edge IDs
during iteration and removes them after the loop completes, using the new
graph.removeRelationship() API. Graph no longer contains phantom
<unresolved>: edges alongside their resolved replacements. CALLS count
drops from 22 to 18 (4 orphan edges removed).

* fix(cobol): Property ID collisions + O(1) Map lookup for MOVE edges

Phase 1D+3C (atomic): Property node IDs now use composite key
filePath:section:level:name instead of filePath:name. This prevents
duplicate data item names in different sections (e.g., STATUS in both
WORKING-STORAGE and LINKAGE) from silently colliding.

New generatePropertyId() helper ensures both node creation and MOVE
edge lookup use the identical key formula. buildDataItemMap() replaces
the O(n) findDataItemNode linear scan with O(1) Map lookup, built once
per file before MOVE processing.

* feat(cobol): MOVE multi-target extraction with OF/IN qualifier filtering

MOVE X TO A B C now produces write edges for all targets, not just the
first. extractMoveTargets() helper handles OF/IN qualified names
(WS-NAME OF WS-RECORD -> target is WS-NAME), subscript stripping
(WS-TABLE(I) -> WS-TABLE), and MOVE_SKIP filtering on targets.

Data model: CobolRegexResults.moves.to:string -> targets:string[]
MOVE CORRESPONDING stays single-target per COBOL standard.
Processor MOVE loop now iterates move.targets.

* feat(cobol): COPY IN/OF library, pseudotext REPLACING, dynamic CALL, PERFORM TIMES, CICS MAP unquoted

Phase 2B: COPY ... IN/OF library-name now captured as metadata in
CopyResolution (IN and OF are synonyms per COBOL-85 standard).

Phase 2C: COPY REPLACING ==pseudotext== support. Tokenizer handles
==...== delimiters alongside "quoted" strings. Pseudotext forces EXACT
type. Two-pass applyReplacing: first pass handles space-containing/
non-identifier pseudotext via global string replace; second pass handles
identifier-level LEADING/TRAILING/EXACT. New test file
cobol-copy-expander.test.ts with 10 tests.

Phase 2E: PERFORM WS-COUNT TIMES no longer produces a false-positive
perform target (checks for TIMES keyword after captured identifier).

Phase 2F: Dynamic CALL via data item (CALL WS-PROG-NAME without quotes)
now emits a CodeElement annotation node with description 'dynamic-call'
instead of silently ignoring. Adds isQuoted:boolean to call results.

Phase 3A: CICS MAP(WS-MAP-NAME) unquoted identifiers now captured.
Phase 3B: Normalized || to ?? in copy-expander (done in Phase 1A).

* feat(cobol): nested program support — capture multiple PROGRAM-IDs per file

Phase 2D: The state machine now captures all PROGRAM-IDs, not just the
first. The primary program name stays in programName; additional nested
programs go into nestedPrograms[]. The processor creates separate Module
nodes for each nested program, contained by the outer module, and
registers them in moduleNodeIds for cross-program CALL resolution.

Paragraphs/data items are not yet scoped per-program (attributed to the
outer module) — full per-program scoping is a future enhancement that
requires END PROGRAM boundary tracking in the state machine.

* test(cobol): expand integration tests for all new language features

New fixtures:
- NESTED.cbl — two PROGRAM-IDs (OUTER-PROG, INNER-PROG) for nested
  program support testing
- COPYLIB.cpy — copybook for pseudotext REPLACING test target

Modified fixtures:
- CUSTUPDT.cbl — single-quoted ENTRY 'ALTENTRY', multi-target MOVE
  (WS-AMT TO FIELD-A FIELD-B), dynamic CALL WS-PROG-NAME, COPY COPYLIB
  with pseudotext REPLACING, LINKAGE SECTION with LS-PARAM
- RPTGEN.cbl — PERFORM WS-COUNT TIMES (false-positive guard), unquoted
  MAP(WS-MAP-NAME), additional data items WS-COUNT WS-MAP-NAME

Integration test rewritten with 62 exact assertions covering:
- 5 Module, 17 Function, 33 Property, 9 CodeElement, 2 Constructor nodes
- Nested program containment (OUTER-PROG -> INNER-PROG)
- Dynamic CALL annotation (CodeElement with cobol-dynamic-call)
- Multi-target MOVE (UPDATE-BALANCE: 2 reads, 3 writes)
- Single-quoted ENTRY (ALTENTRY under CUSTUPDT)
- PERFORM TIMES guard (WS-COUNT not in CALLS)
- Orphan unresolved edge removal (zero -unresolved edges)
- Grand totals: 21 CALLS, 68 CONTAINS, 2 IMPORTS, 10 ACCESSES

* fix(cobol): pseudotext REPLACING now applies correctly via isPseudotext flag

Root cause: ==PREFIX-== matched /^[A-Z][A-Z0-9-]*$/i (trailing hyphens
allowed), routing it to the second-pass EXACT identifier match where
PREFIX-RECORD !== PREFIX- failed silently.

Fix: Propagate isPseudotext from parseReplacingClause to CopyReplacing
interface, then use it in applyReplacing first-pass condition to force
global string replacement for all pseudotext entries regardless of
whether the content looks like an identifier.

Result: COPY COPYLIB REPLACING ==PREFIX-== BY ==WS-==. now correctly
transforms PREFIX-RECORD → WS-RECORD, PREFIX-CODE → WS-CODE, etc.

* refactor(cobol): per-program scoping via boundary tracking + line-range grouping

State machine changes (minimal, ~30 lines):
- Add RE_END_PROGRAM regex for END PROGRAM program-name. detection
- Replace nestedPrograms[] with programs[] containing startLine/endLine/
  nestingDepth metadata for each PROGRAM-ID in the file
- Reset division/section/paragraph state on new PROGRAM-ID boundary
- EOF finalization flushes remaining stack entries (single-program files)
- Programs sorted by startLine (outer before inner)

Processor changes:
- Uses programs[] with line-range containment to find enclosing parent
  Module for nested programs (replaces hardcoded nestedParent logic)
- programModuleIds Map tracks Module node IDs per program name

Fixture: NESTED.cbl now includes END PROGRAM lines for both programs.

Integration test: PREFIX-* Property nodes now correctly appear as WS-*
after the pseudotext REPLACING fix from the previous commit.

* feat(cobol): free-format COBOL support (>>source free)

Auto-detects >>SOURCE FREE directive in the first 500 chars and switches
to free-format line processing:
- No column-position rules (cols 1-6 are program text, not sequence area)
- Comments use *> prefix instead of col 7 indicator
- No continuation line indicator
- Strip inline *> comments
- Skip >>SOURCE directive lines

preprocessCobolSource() skips col-1-6 stripping for free-format files.

Paragraph/section regexes relaxed from fixed 7-space prefix to flexible
whitespace with case-insensitivity (/^\s*([A-Z][A-Z0-9-]+)\.\s*$/i).
EXCLUDED_PARA_NAMES expanded with COBOL verbs (GOBACK, END-READ, etc.)
to prevent false-positive paragraph detection in free-format.

Also fixes: entry-point-scoring.ts crash when language is 'cobol'
(MERGED_ENTRY_POINT_PATTERNS[language] was undefined → optional chaining).

Benchmark on ACAS 3.01 (268 GnuCOBOL free-format programs, 10MB):
- Before: 407 nodes, 393 edges (near-empty, only file nodes)
- After:  4,297 nodes, 3,612 edges, 542 clusters, 11 flows

* fix(cobol): relax data item regexes for free-format (^\s+ to ^\s*)

RE_FD, RE_DATA_ITEM, RE_ANONYMOUS_REDEFINES, and RE_88_LEVEL all used
^\s+ which requires at least 1 leading space. In free-format mode, lines
are trimmed before processing, so data items like "01 WS-FIELD PIC X."
have no leading whitespace after trimming.

Changed to ^\s* (zero or more spaces) which works for both fixed-format
(indented lines still have spaces) and free-format (trimmed lines).

ACAS benchmark (268 GnuCOBOL programs):
- Before: 4,297 nodes, 3,612 edges (paragraphs only)
- After:  13,832 nodes, 8,615 edges (+ data items, FDs, 88-levels)

* feat(cobol): 100% structural feature coverage — GO TO, SCREEN, SD/RD, SORT, SEARCH, CANCEL, Level 66

New extractions: GO TO (CALLS edges), SCREEN SECTION data items,
SD/RD alongside FD (Record nodes), SORT/MERGE USING/GIVING (ACCESSES),
SEARCH (ACCESSES), CANCEL (CALLS), Level 66 RENAMES (Property),
IS EXTERNAL/IS GLOBAL (Property description enrichment).

ACAS: 13,951 nodes | 13,193 edges | 685 clusters | 150 flows
(+53% edges from new GO TO/SORT/SEARCH/CANCEL extractions)

* feat(cobol): enriched CICS extraction — file I/O, dynamic PROGRAM, queues, HANDLE ABEND

EXEC CICS blocks now extract:
- FILE/DATASET clause: captures VSAM file name (literal or data item ref)
  for READ/WRITE/REWRITE/DELETE/STARTBR/READNEXT/READPREV → ACCESSES edges
- PROGRAM clause: now handles unquoted variable references (dynamic CICS
  program transfer) → CodeElement annotation with cics-dynamic-program reason
- QUEUE clause: captures TS/TD queue names from WRITEQ/READQ → ACCESSES edges
- LABEL clause: captures HANDLE ABEND error handler targets → CALLS edges
- TRANSID: now handles unquoted variable references

CodeElement descriptions enriched with all captured fields (map, program,
transid, file, queue, label).

CardDemo benchmark: +49 nodes, +33 edges from enriched CICS extraction.

* feat(cobol): complete CICS command extraction — all 7 expert recommendations

From COBOL expert agent analysis:
1. ENDBR added to isRead file command list
2. LOAD added to PROGRAM edge commands (alongside LINK/XCTL)
3. Two-word commands expanded: WRITEQ/READQ/DELETEQ TS/TD, HANDLE
   ABEND/AID/CONDITION, START TRANSID
4. Queue reason differentiated: cics-queue-read/-write/-delete
5. RETURN/START TRANSID → CALLS edges to synthetic <transid> target
6. MAP → ACCESSES edges for screen traceability
7. INTO/FROM data fields extracted → ACCESSES edges to data items

Also: dataItemMap built before CICS block processing (was declared after),
CodeElement descriptions enriched with all captured CICS fields.

* test(cobol): strict exhaustive integration tests with exact edgeSet assertions

Every edge reason has exact sorted pair assertions via edgeSet(), not
just counts. Any change to extraction that adds, removes, or reorders
edges will produce a precise, descriptive failure.

Updated RPTGEN.cbl fixture with:
- GO TO EXIT-PARAGRAPH, SORT USING/GIVING, SEARCH table
- EXEC CICS READ FILE INTO, WRITEQ TS QUEUE FROM, SEND MAP FROM
- EXEC CICS HANDLE ABEND LABEL, RETURN TRANSID, XCTL PROGRAM(variable)
- ABEND-HANDLER and EXIT-PARAGRAPH paragraphs

46 tests covering 24 CALLS + 79 CONTAINS + 18 ACCESSES + 2 IMPORTS edges
across 15 distinct edge reason codes, all with exact sorted pair lists.

* fix(cobol): address 5 findings from second Claude review (compiler front-end perspective)

Finding #2: Numeric sequence numbers now stripped (changed /[^0-9 ]/ to
/\S/ in preprocessCobolSource). Lines like "000100 MAIN-PARAGRAPH." now
have cols 1-6 blanked so paragraph regex matches correctly.

Finding #11: JCL in-stream PROC ordering fixed — pre-register all PROCs
into moduleNames before step processing. Steps that EXEC a PROC defined
later in the same file now get CALLS edges.

Finding #A: PROCEDURE DIVISION USING no longer captures calling-convention
keywords (BY, VALUE, REFERENCE, CONTENT, ADDRESS, OF) as parameter names.

Finding #C: SORT/MERGE USING/GIVING now captures ALL file references
(multi-file), not just the first. Changed from single-match to section
extraction with split.

Finding #D: Section headers no longer set currentParagraph, preventing
PERFORM caller misattribution to Namespace instead of Function nodes.

* fix(cobol): address code review findings — ReDoS fix, perf, cleanup

P1 CRITICAL — ReDoS in SORT USING/GIVING:
Replaced nested-quantifier regex with safe indexOf+substring+split
approach. No backtracking possible on crafted input.

P2 — readCopy O(M) linear scan:
Added copybookByPath reverse Map for O(1) path-to-content lookup.

P3 — Dead code removal:
Deleted unused RE_SORT_USING and RE_SORT_GIVING constants.

P3 — EXCLUDED_PARA_NAMES simplification:
Replaced 20 END-* entries with startsWith('END-') prefix check.
Auto-covers future END-* verbs.

P3 — Misplaced JSDoc on removeRelationship:
Fixed comment that described removeNodesByFile instead.
Added missing JSDoc to removeNodesByFile.

Review agents: architecture-strategist, performance-oracle,
security-sentinel, code-simplicity-reviewer.

* refactor: add Cobol to SupportedLanguages with parseStrategy: standalone

New languages/cobol.ts — standalone regex processor provider with no-op
tree-sitter fields. Declares parseStrategy: 'standalone' to distinguish
from tree-sitter-based languages.

Added parseStrategy: 'tree-sitter' | 'standalone' to LanguageProviderConfig
for languages that use their own processor instead of tree-sitter.

Removed all 11 'cobol' as any casts — now uses SupportedLanguages.Cobol.
Added empty Cobol entries to entry-point-scoring and framework-detection.

* fix(cobol): 5 fixes from third Claude review + 3 regression tests

Fixes:
- Line numbers now 1-indexed in fixed-format (was 0-indexed, off-by-one
  in jump-to-definition links)
- Copybook content preprocessed before COPY expansion (sequence numbers
  and patch markers in copybooks no longer survive into expanded source)
- ENTRY USING filters calling-convention keywords (BY, VALUE, REFERENCE,
  CONTENT, ADDRESS, OF) — same fix as PROCEDURE DIVISION USING
- SORT/MERGE trailing period stripped from USING/GIVING file tokens
- Paragraph exclusion uses exact match for SECTION/DIVISION (was substring
  match that excluded valid names like CROSS-SECTION-ANALYSIS)

USING_KEYWORDS moved to module scope for reuse by both PROCEDURE DIVISION
USING and ENTRY USING handlers.

New unit tests:
- ENTRY USING BY VALUE filtering
- Paragraph names containing SECTION not excluded
- Numeric sequence numbers stripped enabling paragraph detection

* fix(cobol): address 6 findings from fourth Claude review + tests

Fourth review findings fixed:
- New #IV: PERFORM TIMES guard uses perfMatch.index instead of
  line.indexOf (prevents wrong match when target appears earlier in line)
- New #V: 88-level condition values now handle single-quoted literals
  ('Y' no longer stored with embedded quotes)
- New #I: CANCEL edges use two-pass resolution like CALL (no longer
  silently dropped when target indexed after source)
- New #3: Multi-line SORT/MERGE accumulation — sortAccum state variable
  accumulates lines until period, then extracts USING/GIVING from full
  statement (95% of production SORT statements span multiple lines)
- New #II: PROCEDURE DIVISION USING on split lines — pendingProcUsing
  flag defers parameter capture to next line if USING not on same line
- New #6 (prior): EXCLUDED_PARA_NAMES exact match for SECTION/DIVISION

Updated fixture: RPTGEN.cbl SORT now uses multi-line format with GIVING
on separate line (period-terminated). New sort-giving integration test.
ACCESSES total: 18 → 19 (new sort-giving edge from multi-line capture).

* fix(cobol): address 4 findings from fifth Claude review

Finding #B (5 reviews old): Section/paragraph node IDs now include
enclosing program name to prevent collision when nested programs share
section/paragraph names. New findOwningProgramName() helper uses
programs[] line ranges to find the innermost enclosing program.

Finding #α: pendingProcUsing now reset in the if(procUsingMatch) branch
(was only set in else branch, could leak across nested programs).

Finding #β: RE_CALL_DYNAMIC uses negative lookbehind (?<![A-Z0-9-]) to
prevent false-positive on compound identifiers like WS-CALL OCCURS.

Finding #γ: sortAccum flushed at EOF (parallel to flushSelect and
pendingFdName EOF cleanup). Prevents silent loss of SORT USING/GIVING
relationships in truncated files.

* fix(cobol): address findings from reviews 5+6 with full test coverage

Review 5 fixes:
- #α: pendingProcUsing reset in if(procUsingMatch) branch
- #β: RE_CALL_DYNAMIC negative lookbehind prevents WS-CALL false positive
- #γ: sortAccum flushed at EOF for truncated files
- #B: Section/paragraph IDs include owning program name

Review 6 fixes:
- #P: sectionNodeIds/paraNodeIds maps use program-scoped keys
  (PROGNAME:NAME). New scopedParaLookup/scopedCallerLookup helpers.
  findContainingSection updated with programs parameter.
- #Q: RETURNING added to USING_KEYWORDS for COBOL 2002+
- #R: RE_PERFORM matches both THRU and THROUGH via alternation

New unit tests (6):
- PERFORM THROUGH captures thruTarget
- PROCEDURE DIVISION USING RETURNING filters keyword
- RE_CALL_DYNAMIC no false-match on WS-CALL compound identifier
- Multi-line SORT captures USING/GIVING from continuation lines
- PROCEDURE DIVISION USING on split line via pendingProcUsing
- Copybook preprocessing strips sequence numbers

* fix(cobol): address findings from seventh Claude review + 3 tests

Review 7 fixes:
- #i: findContainingSection only updates best when lookup succeeds
  (prevents undefined overwriting valid parent section)
- #ii: RE_PROC_SECTION handles segment numbers (SECTION 30.)
- #III: procedureUsing now stored per-program on boundary stack
  entries, propagated to programs[] output. Inner programs no longer
  overwrite outer program's parameters.
- #δ: Dynamic CANCEL (CANCEL variable) now creates CodeElement
  annotation node, matching dynamic CALL behavior. RE_CANCEL_DYNAMIC
  with negative lookbehind. cancels[] gains isQuoted field.
- #Q: RETURNING added to USING_KEYWORDS (already in prev commit)
- #R: PERFORM THROUGH already fixed (THRU|THROUGH alternation)

New unit tests:
- Nested programs carry per-program procedureUsing
- SECTION with segment number detected
- Dynamic CANCEL via data item captured with isQuoted=false

* feat(cobol): link PROCEDURE DIVISION USING to LINKAGE data items + close 4 findings

Finding #10 FIXED: procedureUsing parameters now create ACCESSES edges
with reason 'cobol-procedure-using' from Module to matching LINKAGE
SECTION Property nodes. This exposes the program's parameter contract
in the graph (e.g., AUDITLOG → LS-CUST-ID, AUDITLOG → LS-AMOUNT).

Findings closed by expert agent consensus:
- #6 COPY IN library: WONTFIX — captured metadata, no universal
  library-to-directory mapping exists. Field costs nothing and is useful
  for library queries.
- #14 SQL DELETE: WONTFIX — DB2 requires FROM; existing FROM pattern
  handles it. Bare DELETE would risk false positives.
- #E OCCURS DEPENDING ON: WONTFIX — runtime sizing concern, not
  structural. The static occurs count is sufficient for indexing.

All 39 findings from 7 Claude reviews now resolved or closed.

* fix(cobol): resolve 48 review findings across 9 review cycles

Ninth deep review resolved all remaining COBOL parser gaps identified
by 5 specialist agents (COBOL expert, architecture strategist,
TypeScript reviewer, security sentinel, code simplicity reviewer).

Fixes (P1 — critical):
- SELECT OPTIONAL now correctly skips OPTIONAL keyword (C1)
- RETURNING params excluded from PROCEDURE DIVISION USING list (C7)
- SORT GIVING no longer captures clause keywords as file names (C5)
- Extract flushSort() helper eliminating 40-line duplication (S2)
- Flush unclosed EXEC blocks at EOF matching SORT/SELECT pattern (S3)
- Guard undefined map key in jcl-processor moduleNames (S1)
- Add MAX_TOTAL_EXPANSIONS=500 to prevent exponential COPY breadth (S4)

Fixes (P2 — important):
- Quote-aware stripInlineComment for | and *> in string literals (C2+C3)
- Fixed-format literal continuation now handles quoted strings (C6)
- PROGRAM-ID detected regardless of division state for siblings (C9)

Fixes (P3 — cleanup):
- EXEC SQL INTO restricted to INSERT INTO to avoid FETCH false-pos (C8)
- Copy expander line numbers fixed from 0-based to 1-based (C11)
- Remove dead code: inInStreamProc, fileIsLiteral, expansionDepth (S7-S10)

Also fixes 8th-review findings: nested program CONTAINS attribution,
multi-PERFORM on same line, INPUT/OUTPUT PROCEDURE IS in SORT,
GO TO DEPENDING ON multi-target, MOVE CORR abbreviation, per-program
procedureUsing ACCESSES edges.

Tests: 145 COBOL tests passing (59 integration + 86 unit)
Benchmarks: CardDemo 12,323 nodes/8,893 edges (7.4s)
            ACAS 14,016 nodes/15,452 edges (9.3s, -9% faster)

* docs(cobol): update documentation for ninth review cycle fixes

Update all 4 COBOL documentation files to reflect the 16 fixes
from the ninth review cycle:

- regex-extraction.md: quote-aware comment stripping, SELECT OPTIONAL,
  RETURNING exclusion, SORT_CLAUSE_NOISE filter, flushSort() helper,
  GO TO multi-target, PROGRAM-ID division-independent detection
- copy-expansion.md: MAX_TOTAL_EXPANSIONS=500 breadth guard, 1-based
  line numbers, removed expansionDepth/warnedCircular param
- deep-indexing.md: GO TO DEPENDING ON, INPUT/OUTPUT PROCEDURE IS,
  MOVE CORR edge reasons, INSERT INTO restriction, literal continuation
- performance.md: updated benchmarks (CardDemo 12,323n/8,893e/7.4s,
  ACAS 14,016n/15,452e/9.3s), COPY breadth guard

* fix(cobol): resolve 10th review findings — nested program edge attribution

Fix 6 findings from the 10th review (PR #498 comment #4132201110):

#A+#F: All CALL/CANCEL/CICS/ENTRY/SQL/SEARCH/file-declaration edges
now use owningModuleId() for nested program attribution instead of
the outer program's parentId. Added helper function owningModuleId()
to centralize the pattern.

#B: Added USING and GIVING to SORT_CLAUSE_NOISE set to prevent MERGE
USING + OUTPUT PROCEDURE from capturing clause keywords as file names.

#C: INPUT/OUTPUT PROCEDURE regex now captures optional THRU/THROUGH
range end paragraph, mirroring RE_PERFORM's THRU support.

#D: scopedCallerLookup fallback now uses programModuleIds.get(pgm)
instead of parentId, so PERFORM/MOVE/GOTO in nested programs with
unresolvable paragraphs fall back to the correct inner module.

#E: pendingProcUsing only set when PROCEDURE DIVISION line is NOT
period-terminated, preventing false USING expectation.

Tests: 145 passing | TypeScript clean

* fix(cobol): resolve 10th review findings — nested program edge attribution

Fix 6 findings from the 10th review (PR #498 comment #4132201110):

#A+#F: All CALL/CANCEL/CICS/ENTRY/SQL/SEARCH/file-declaration edges
now use owningModuleId() for nested program attribution instead of
the outer program's parentId. Added helper function owningModuleId()
to centralize the pattern.

#B: Added USING and GIVING to SORT_CLAUSE_NOISE set to prevent MERGE
USING + OUTPUT PROCEDURE from capturing clause keywords as file names.

#C: INPUT/OUTPUT PROCEDURE regex now captures optional THRU/THROUGH
range end paragraph, mirroring RE_PERFORM's THRU support.

#D: scopedCallerLookup fallback now uses programModuleIds.get(pgm)
instead of parentId, so PERFORM/MOVE/GOTO in nested programs with
unresolvable paragraphs fall back to the correct inner module.

#E: pendingProcUsing only set when PROCEDURE DIVISION line is NOT
period-terminated, preventing false USING expectation.

Tests: 145 passing | TypeScript clean

* fix(cobol): resolve 11th review findings — final nested program + multi-CALL gaps

#1: scopedCallerLookup(null) now uses owningModuleId(lineNum) instead
of parentId, fixing PERFORM/MOVE/GOTO before first paragraph in nested
programs.

#2+#3: CALL and CANCEL extraction now uses matchAll (global flag) to
capture multiple occurrences on the same line. Dynamic CALL/CANCEL
checked independently instead of in else branch.

#4: SORT/MERGE ACCESSES edge IDs now use owningModuleId(sort.line)
instead of parentId for nested program correctness.

#5: preprocessCobolSource free-format detection now uses first 10 lines
(consistent with extractCobolSymbolsWithRegex threshold).

#6: EXCLUDED_PARA_NAMES expanded with DISPLAY, ACCEPT, WRITE, READ,
REWRITE, DELETE, OPEN, CLOSE, RETURN, RELEASE, SORT, MERGE to prevent
false-positive paragraph detection on isolated verbs.

Also removed unused GraphNode import from cobol-processor.ts.

Tests: 145 passing | TypeScript clean

* docs(cobol): deepened full language coverage plan with research findings

3 research agents analyzed Phase 1-2 features and graph value ranking.

Key findings: cobol-call-using is #1 edge type (9.2/10); multi-line
accumulation is dominant challenge; DECLARATIVES is lowest-risk Phase 2
item; SET TO TRUE covers 80-90% of SET usage.

* feat(cobol): implement Phase 1 — high-value data flow edges

4 new extraction features that create new ACCESSES and IMPORTS edges:

1.1: EXEC SQL INCLUDE -> IMPORTS edges with reason 'sql-include'
     Handles unquoted (SQLCA), quoted ('DBRMLIB.MEMBER'), and
     underscored (CUST_TBL_DCL) member names.

1.2: CALL USING parameter extraction -> ACCESSES edges
     Extracts parameters from CALL USING clause, filtering BY/REFERENCE/
     CONTENT/VALUE/ADDRESS/OF/LENGTH/OMITTED keywords. Creates
     'cobol-call-using' ACCESSES edges (graph value: 9.2/10).

1.4: OCCURS DEPENDING ON -> ACCESSES edges with reason 'cobol-depends-on'
     Extended OCCURS regex captures DEPENDING ON field with subscript
     stripping. Creates dependency edge from table to controlling field.

1.5: VALUE clause for standard data items
     Extracts VALUE from data item clauses: quoted strings with type
     prefix (X/N/G/B), ALL literals, numerics (incl negative/decimal),
     and figurative constants. Populates Property node values.

Tests: 145 passing (+2 ACCESSES from CALL USING) | TypeScript clean

* feat(cobol): implement Phase 2 — DECLARATIVES, SET, INSPECT, EXEC DLI

4 new extraction features for error handling, data flow, and IMS/DB:

2.1: EXEC DLI (IMS/DB) -> CodeElement + ACCESSES edges
     Accumulates EXEC DLI blocks like EXEC SQL. Parses DLI verbs
     (GU, GN, ISRT, REPL, DLET, CHKP, SCHD, TERM). Extracts
     SEGMENT, PCB, INTO/FROM, PSB. Creates dli-{verb} ACCESSES
     edges to <ims>:segment Record nodes.

2.2: DECLARATIVES / USE AFTER EXCEPTION -> ACCESSES edges
     Tracks inDeclaratives state. Detects USE AFTER STANDARD
     EXCEPTION ON file-name. Creates cobol-error-handler ACCESSES
     edge from handler section to file Record.

2.3: SET statement -> ACCESSES edges
     Detects SET TO TRUE (80-90% of SET usage) and SET index
     TO/UP BY/DOWN BY. Creates cobol-set-condition / cobol-set-index
     write edges + cobol-set-read for identifier values.

2.4: INSPECT -> ACCESSES edges with multi-line accumulator
     Accumulates INSPECT until period (like SORT). Extracts inspected
     field + tally counters. Creates cobol-inspect-read/write/tally
     edges. Form detection: tallying/replacing/converting/combined.

Preprocessor: 1398 -> 1597 LOC (+199). Tests: 145 passing.

* feat(cobol): implement Phase 3 — completeness fixes

6 partial features fixed to first-class support:

3.1: CALL RETURNING -> ACCESSES write edge (cobol-call-returning)
3.2: SELECT OPTIONAL flag preserved in FileDeclaration + Record node
3.3: ALTERNATE RECORD KEY extraction (matchAll for multiple keys)
3.4: COMMON attribute on nested programs (RE_PROGRAM_ID extended)
3.5: IS EXTERNAL / IS GLOBAL as first-class boolean properties
     (removed usage string hack)
3.6: AUTHOR / DATE-WRITTEN mapped to Module node description

Tests: 145 passing | TypeScript clean

* feat(cobol): implement Phase 4 — INITIALIZE + metadata completeness

4.1: INITIALIZE statement -> ACCESSES write edge (cobol-initialize)
4.2: DATE-COMPILED and INSTALLATION paragraphs extracted and mapped
     to Module node description alongside existing AUTHOR/DATE-WRITTEN

All 4 plan phases complete. Coverage: ~95% (up from 71.9%).
Tests: 145 passing | TypeScript clean

* test(cobol): add 24 unit tests for Phase 1-4 features

Coverage for all new extraction features:

Phase 1 (8 tests):
- EXEC SQL INCLUDE (unquoted, quoted, underscored)
- CALL USING (simple, mixed modes, ADDRESS OF, OMITTED)
- CALL RETURNING
- OCCURS DEPENDING ON
- VALUE clause (string, numeric, figurative constant)

Phase 2 (10 tests):
- EXEC DLI GU/ISRT/SCHD (verb, segment, PCB, INTO, FROM, PSB)
- DECLARATIVES USE AFTER EXCEPTION (single + multiple sections)
- SET TO TRUE, SET index UP BY
- INSPECT TALLYING, INSPECT REPLACING

Phase 3-4 (6 tests):
- SELECT OPTIONAL flag
- ALTERNATE RECORD KEY
- PROGRAM-ID IS COMMON
- IS EXTERNAL / IS GLOBAL booleans
- INITIALIZE extraction
- Full programMetadata (AUTHOR, DATE-WRITTEN, DATE-COMPILED, INSTALLATION)

Total: 168 tests passing (145 + 24 - 1 removed duplicate)

* fix(cobol): use /\r?\n/ split for Windows CRLF compatibility

All 4 COBOL source files now split on /\r?\n/ instead of '\n' to
handle CRLF line endings on Windows. Previously, trailing \r in
lines caused RE_GOTO's $ anchor to fail on multi-line GO TO
DEPENDING ON statements, producing only 1 goto edge instead of 4.

Files fixed: cobol-preprocessor.ts (2 sites), cobol-processor.ts,
jcl-parser.ts, cobol-copy-expander.ts

Tests: 168 passing | TypeScript clean

* fix(cobol): resolve 12th review — dynamic CALL/CANCEL dedup + trailing anchors

#1+#2: Removed incorrect hasQuotedCall/hasQuotedCancel deduplication
guards. RE_CALL_DYNAMIC and RE_CANCEL_DYNAMIC require [A-Z] after
CALL/CANCEL, so they CANNOT match quoted targets — the guards were
both unnecessary and actively harmful, suppressing dynamic CALL/CANCEL
in ON EXCEPTION patterns.

#3+#5: Changed RE_CALL_DYNAMIC and RE_CANCEL_DYNAMIC trailing anchor
from (?:\s|\.) to (?=\s|\.|$) (lookahead). The consuming anchor
failed when the identifier was the last token on a physical line.

Tests: 168 passing | TypeScript clean

* feat(cobol): add CALL accumulator + fix SORT double-statement (#4, #6)

Finding #4: Multi-line CALL USING accumulator
Added callAccum state variable that accumulates CALL statements
spanning multiple physical lines until period or END-CALL is found.
Uses flushCallAccum() to re-extract CALL target + USING parameters
from the full accumulated statement. This fixes the silent loss of
ACCESSES parameter edges when USING appears on lines after CALL.

Finding #6: SORT double-statement on same line
After flushSort(), the code now falls through to re-check the
current line for a new SORT/MERGE start (was previously blocked
by the sortAccum === null check evaluating before flushSort ran).

Also fixed: used non-global regex for CALL detection test to avoid
the classic global regex .test() lastIndex bug.

Tests: 168 passing (+1 ACCESSES from multi-line CALL USING)

* fix(cobol): resolve 13th review — CICS LOAD, USING extraction, file scoping

#1: CICS LOAD unresolved edge no longer silently deleted in second pass.
    Changed narrow cics-link/cics-xctl check to catch-all pattern:
    rel.reason?.startsWith('cics-') && rel.reason.endsWith('-unresolved')

#2: flushCallAccum USING extraction now stops before COBOL statement
    verbs (INSPECT, SEARCH, SORT, MERGE, DISPLAY, ACCEPT, MOVE, PERFORM,
    GO TO, CALL, IF, EVALUATE). Prevents absorbing adjacent statements
    as false USING parameters in legacy pre-COBOL-85 code without END-CALL.

#3: CICS FILE Record nodes now globally-scoped (<cics-file>:FILENAME)
    instead of per-file-scoped. Enables cross-program CICS file access
    analysis, consistent with SQL table scoping (<db>:TABLE).

#4: callAccum pre-check regex now has (?<![A-Z0-9-]) lookbehind to
    prevent false activation on compound identifiers like WS-CALL-FLAG.

Tests: 168 passing | TypeScript clean

* fix(cobol): resolve 14th review — callAccum false paragraph + Area A guard

#1: callAccum continuation lines now check for COBOL statement verb
    starts (GO TO, PERFORM, MOVE, etc.) and paragraph/section headers.
    If detected, the CALL is flushed as-is and the line processed
    normally — prevents false paragraph detection and currentParagraph
    corruption from lines like "WS-ADDR." being treated as paragraphs.

#4: callAccum pre-check now guarded by currentDivision === 'procedure'
    to prevent unnecessary activations in DATA DIVISION.

#5: Fixed-format paragraph detection now rejects lines with >7 leading
    spaces (Area B indentation) as paragraph candidates. Paragraph
    names in fixed-format must start in Area A (col 8-11, max 7 spaces).
    Free-format mode is unaffected.

Tests: 168 passing | TypeScript clean

* fix(cobol): resolve 15th review — callAccum Area A + verb boundary fixes

#A: Column-position-aware paragraph detection in callAccum flush.
#B: inspectAccum early-flush on paragraph/section/verb headers.
#C: Verb boundary \b → (?:\s|$) prevents MOVE-COUNT false flush.

* test(cobol): add 17 edge-case regression tests + fix USING verb boundary

17 new tests covering all recurring review patterns:

Multi-line CALL USING (7 tests):
- Parameters on separate continuation lines (IBM mainframe style)
- No absorption of INSPECT/GO TO/paragraphs following CALL
- END-CALL scope terminator
- Hyphenated identifiers (MOVE-COUNT) not triggering false flush
- Dual quoted+dynamic CALL on same line (ON EXCEPTION)

Nested program attribution (2 tests):
- CALL in inner program within inner line range
- PERFORM before first paragraph has null caller

CRLF compatibility (1 test):
- GO TO DEPENDING ON with \r\n line endings

Area A paragraph detection (2 tests):
- Area B (>7 spaces) rejected; Area A (7 spaces) accepted

SORT/MERGE (1 test): COLLATING SEQUENCE keywords not captured
PROCEDURE USING (2 tests): RETURNING excluded, period-terminated
Comment stripping (1 test): pipe in quoted string preserved
SELECT OPTIONAL (1 test): correct file name, not OPTIONAL keyword

Bug fix: USING extraction regex verb terminators changed from
\bVERB\b to \bVERB(?=\s|$) in flushCallAccum — prevents truncation
on hyphenated identifiers like MOVE-COUNT, PERFORM-LIMIT.

Total: 185 tests passing

* test(cobol): add 32 comprehensive edge-case regression tests

13 new describe blocks covering all extraction features:

- EXEC DLI: no-SEGMENT, multi-line accumulation (2 tests)
- SET: multiple targets, DOWN BY, TO numeric (3 tests)
- INSPECT: CONVERTING, multiple counters, tallying-replacing,
  paragraph flush during accumulation (4 tests)
- DECLARATIVES: no-STANDARD keyword, I-O mode, post-END paragraphs (3)
- COPY REPLACING: pseudotext deletion ==OLD== BY ==== (1 test)
- VALUE: hex literal, negative numeric, ALL literal (3 tests)
- OCCURS: TO range, fixed-size without DEPENDING ON (2 tests)
- Dynamic CALL/CANCEL: end-of-line, multiple CANCELs (3 tests)
- EXEC SQL: INCLUDE skips tables, SELECT INTO host vars, host
  variable extraction (3 tests)
- INITIALIZE: target and caller context (1 test)
- Nested programs: sibling scoping, PROGRAM-ID without ID DIV (2)
- EXEC EOF flush: unclosed EXEC SQL flushed (1 test)
- Multi-PERFORM: IF/ELSE dual PERFORM on single line (1 test)
- IS EXTERNAL: USAGE not polluted by external flag (1 test)

Total: 215 tests passing

* fix(cobol): resolve 16th review — CANCEL in CALL block + USING boundary

#1: flushCallAccum now extracts CANCEL statements from within CALL
    ON EXCEPTION blocks. Adds RE_CANCEL + RE_CANCEL_DYNAMIC matchAll
    passes alongside existing CALL extraction.

#2: Added \bCANCEL(?=\s|$) to USING lookahead regex to prevent CANCEL
    keyword being captured as false USING parameter.

#3: Multi-line CALL start now returns immediately to prevent the CALL
    start line from simultaneously feeding sortAccum/inspectAccum.

#6: Division transitions now flush all active accumulators (callAccum,
    sortAccum, inspectAccum) to prevent state leakage across programs.

Also added CANCEL to callAccum flush trigger verb list.

Tests: 215 passing | TypeScript clean

* refactor(cobol): extract shared verb constants + resolve 17th review

Extract COBOL_STATEMENT_VERBS, RE_STATEMENT_VERB_START, and
RE_USING_PARAMS as shared constants — eliminates 4 duplicated
25-verb regex patterns.

17th review: #1 flushCallAccum before EXEC entry, #2 inspectAccum
verb parity via shared constant.

Tests: 215 passing | TypeScript clean

* test(cobol): replace all fuzzy assertions with exact toBe checks

Replaced 7 toBeGreaterThan/toBeLessThan/toBeGreaterThanOrEqual
assertions with exact toBe values:

- dataItems.length: >= 3 → toBe(3)
- calls.length: >= 1 → toBe(1)
- calls[0].line: range check → toBe(10)
- programs[].startLine/endLine: comparison → exact values
- innerA.endLine/innerB.startLine: comparison → exact values

Also added 11 new edge-case tests (accumulator flush on EXEC/division
transitions, free-format, CANCEL in CALL block, SORT THRU, verb
flush, integration).

226 tests passing — zero fuzzy assertions remain.

* fix(cobol): resolve 19th review + 15 accumulator flush tests

Fixes:
#1: END PROGRAM flushes callAccum/sortAccum/inspectAccum
#2: PROGRAM-ID sibling path flushes all accumulators
#3: Added COMPUTE/ADD/SUBTRACT/MULTIPLY/DIVIDE/STRING/UNSTRING
    to COBOL_STATEMENT_VERBS (now 32 verbs)

Tests (15 new):
- END PROGRAM flush: single + nested programs (2)
- PROGRAM-ID sibling flush (1)
- Arithmetic verb flush: COMPUTE/ADD/SUBTRACT/MULTIPLY/DIVIDE (5)
- String verb flush: STRING/UNSTRING (2)
- Arithmetic not captured as false USING params (1)
- SORT flushed at END PROGRAM (1)
- INSPECT flushed at END PROGRAM (1)
- All with exact toBe assertions (2)

Total: 239 tests passing | Zero fuzzy assertions

* fix(cobol): resolve 20th review — INITIALIZE multi-target + 2 tests

Finding 1: INITIALIZE now captures multiple targets with REPLACING
clause keyword filtering. Regex changed to lazy match stopping at
REPLACING/WITH/period boundary. Targets split on whitespace and
filtered against INITIALIZE_CLAUSE_KEYWORDS set.

Tests (2 new):
- INITIALIZE multi-target: WS-CUSTOMER WS-ORDER WS-LINE-ITEM → 3
- INITIALIZE with REPLACING: only WS-RECORD captured, not keywords

Total: 241 tests passing | TypeScript clean
2026-03-26 14:03:48 +00:00

2746 lines
106 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
preprocessCobolSource,
extractCobolSymbolsWithRegex,
} from '../../src/core/ingestion/cobol/cobol-preprocessor.js';
import type { CobolRegexResults } from '../../src/core/ingestion/cobol/cobol-preprocessor.js';
import { parseReplacingClause } from '../../src/core/ingestion/cobol/cobol-copy-expander.js';
// ---------------------------------------------------------------------------
// Helper: build COBOL source from an array of lines.
//
// The parser processes full raw lines including columns 1-6 (sequence area).
// Regexes anchored with ^\s+ (data items, FD, AUTHOR, etc.) require the line
// to start with whitespace, so test lines use spaces in cols 1-6 instead of
// numeric sequence numbers unless specifically testing sequence-number behavior.
//
// Column layout:
// 1-6: sequence/patch area (spaces or digits)
// 7: indicator (* comment, - continuation, / page break, space normal)
// 8-11: Area A (divisions, sections, paragraphs start here = 7 leading spaces)
// 12+: Area B (statements = 11+ leading spaces)
// ---------------------------------------------------------------------------
function cobol(...lines: string[]): string {
return lines.join('\n');
}
// ---------------------------------------------------------------------------
// preprocessCobolSource
// ---------------------------------------------------------------------------
describe('preprocessCobolSource', () => {
it('replaces alphabetic patch markers in cols 1-6 with spaces', () => {
const input = cobol(
'mzADD IDENTIFICATION DIVISION.',
'estero PROGRAM-ID. TEST1.',
);
const output = preprocessCobolSource(input);
const lines = output.split('\n');
expect(lines[0].substring(0, 6)).toBe(' ');
expect(lines[0].substring(6)).toBe(' IDENTIFICATION DIVISION.');
expect(lines[1].substring(0, 6)).toBe(' ');
});
it('strips numeric sequence numbers from cols 1-6', () => {
const input = cobol(
'000100 IDENTIFICATION DIVISION.',
'000200 PROGRAM-ID. TEST1.',
);
const output = preprocessCobolSource(input);
const lines = output.split('\n');
expect(lines[0]).toBe(' IDENTIFICATION DIVISION.');
expect(lines[1]).toBe(' PROGRAM-ID. TEST1.');
});
it('preserves lines shorter than 7 characters', () => {
const input = cobol('SHORT', ' ', '000100 IDENTIFICATION DIVISION.');
const output = preprocessCobolSource(input);
const lines = output.split('\n');
expect(lines[0]).toBe('SHORT');
expect(lines[1]).toBe(' ');
});
it('preserves exact line count (no lines added/removed)', () => {
const input = cobol(
'mzADD IDENTIFICATION DIVISION.',
'000200 PROGRAM-ID. TEST1.',
'patch# DATA DIVISION.',
'',
'000500 PROCEDURE DIVISION.',
);
const output = preprocessCobolSource(input);
expect(output.split('\n').length).toBe(input.split('\n').length);
});
});
// ---------------------------------------------------------------------------
// extractCobolSymbolsWithRegex
// ---------------------------------------------------------------------------
describe('extractCobolSymbolsWithRegex', () => {
// -------------------------------------------------------------------------
// PROGRAM-ID
// -------------------------------------------------------------------------
describe('PROGRAM-ID', () => {
it('extracts PROGRAM-ID from IDENTIFICATION DIVISION', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programName).toBe('TESTPROG');
});
it('captures all PROGRAM-IDs in programs array with line ranges', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER-PROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' DISPLAY "OUTER".',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER-PROG.',
' PROCEDURE DIVISION.',
' INNER-PARA.',
' DISPLAY "INNER".',
' END PROGRAM INNER-PROG.',
' END PROGRAM OUTER-PROG.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programName).toBe('OUTER-PROG');
expect(r.programs).toHaveLength(2);
expect(r.programs[0].name).toBe('OUTER-PROG');
expect(r.programs[0].nestingDepth).toBe(0);
expect(r.programs[1].name).toBe('INNER-PROG');
expect(r.programs[1].nestingDepth).toBe(1);
// INNER-PROG's startLine < endLine, contained within OUTER-PROG
expect(r.programs[0].startLine).toBe(2); // OUTER-PROG
expect(r.programs[1].startLine).toBe(7); // INNER-PROG
expect(r.programs[1].endLine).toBe(11); // END PROGRAM INNER-PROG
expect(r.programs[0].endLine).toBe(12); // END PROGRAM OUTER-PROG
});
it('returns null programName for content without PROGRAM-ID', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' AUTHOR. SOMEONE.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programName).toBeNull();
});
});
// -------------------------------------------------------------------------
// Paragraphs & Sections
// -------------------------------------------------------------------------
describe('Paragraphs & Sections', () => {
it('extracts paragraphs in PROCEDURE DIVISION (7 leading spaces)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' DISPLAY "HELLO".',
' SUB-PARA.',
' DISPLAY "WORLD".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.paragraphs).toHaveLength(2);
expect(r.paragraphs[0].name).toBe('MAIN-PARA');
expect(r.paragraphs[1].name).toBe('SUB-PARA');
});
it('extracts sections in PROCEDURE DIVISION', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' INIT-SECTION SECTION.',
' INIT-PARA.',
' DISPLAY "INIT".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sections).toHaveLength(1);
expect(r.sections[0].name).toBe('INIT-SECTION');
expect(r.paragraphs).toHaveLength(1);
expect(r.paragraphs[0].name).toBe('INIT-PARA');
});
it('excludes reserved names (DECLARATIVES, END, PROCEDURE, etc.)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' DECLARATIVES.',
' END.',
' REAL-PARA.',
' DISPLAY "OK".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.paragraphs.map(p => p.name)).toEqual(['REAL-PARA']);
});
it('does NOT treat IDENTIFICATION/ENVIRONMENT/DATA/WORKING-STORAGE as paragraphs', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' ENVIRONMENT DIVISION.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' PROCEDURE DIVISION.',
' REAL-PARA.',
' DISPLAY "OK".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const names = r.paragraphs.map(p => p.name);
expect(names).not.toContain('IDENTIFICATION');
expect(names).not.toContain('ENVIRONMENT');
expect(names).not.toContain('DATA');
expect(names).not.toContain('WORKING-STORAGE');
expect(names).toContain('REAL-PARA');
});
});
// -------------------------------------------------------------------------
// CALL / PERFORM / COPY
// -------------------------------------------------------------------------
describe('CALL / PERFORM / COPY', () => {
it('extracts CALL "PROGRAM" statements', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' CALL "SUBPROG".',
' CALL "ANOTHER".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(2);
expect(r.calls[0].target).toBe('SUBPROG');
expect(r.calls[1].target).toBe('ANOTHER');
});
it('extracts PERFORM paragraph-name with caller context', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' PERFORM SUB-PARA.',
' SUB-PARA.',
' DISPLAY "HELLO".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.performs).toHaveLength(1);
expect(r.performs[0].target).toBe('SUB-PARA');
expect(r.performs[0].caller).toBe('MAIN-PARA');
});
it('extracts PERFORM ... THRU ... statements', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' PERFORM STEP-A THRU STEP-Z.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.performs).toHaveLength(1);
expect(r.performs[0].target).toBe('STEP-A');
expect(r.performs[0].thruTarget).toBe('STEP-Z');
});
it('does NOT store PERFORM WS-COUNT TIMES as a perform target', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' PERFORM WS-COUNT TIMES.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.performs.map(p => p.target)).not.toContain('WS-COUNT');
});
it('extracts dynamic CALL (unquoted) with isQuoted=false', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' CALL WS-PROG-NAME.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('WS-PROG-NAME');
expect(r.calls[0].isQuoted).toBe(false);
});
it('quoted CALL has isQuoted=true', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' CALL "SUBPROG".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].isQuoted).toBe(true);
});
it('extracts COPY copybook (unquoted)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' COPY WSCOPY.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.copies).toHaveLength(1);
expect(r.copies[0].target).toBe('WSCOPY');
});
it('extracts COPY "copybook" (quoted)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' COPY "MY-COPY".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.copies).toHaveLength(1);
expect(r.copies[0].target).toBe('MY-COPY');
});
});
// -------------------------------------------------------------------------
// Data Division
// -------------------------------------------------------------------------
describe('Data Division', () => {
it('extracts data items with level, name, PIC, USAGE', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-RECORD.',
' 05 WS-NAME PIC X(30).',
' 05 WS-AMOUNT PIC 9(7)V99 USAGE COMP-3.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.dataItems.length).toBe(3); // WS-NAME + WS-BALANCE + WS-AMOUNT (01-level group with only period has no clauses)
const wsName = r.dataItems.find(d => d.name === 'WS-NAME');
expect(wsName).toBeDefined();
expect(wsName!.level).toBe(5);
expect(wsName!.pic).toMatch(/^X\(30\)/);
const wsAmount = r.dataItems.find(d => d.name === 'WS-AMOUNT');
expect(wsAmount).toBeDefined();
expect(wsAmount!.usage).toBe('COMP-3');
});
it('extracts 88-level condition names with values', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-STATUS PIC X.',
' 88 WS-ACTIVE VALUE "A".',
' 88 WS-INACTIVE VALUE "I".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const active = r.dataItems.find(d => d.name === 'WS-ACTIVE');
expect(active).toBeDefined();
expect(active!.level).toBe(88);
expect(active!.values).toEqual(['A']);
const inactive = r.dataItems.find(d => d.name === 'WS-INACTIVE');
expect(inactive).toBeDefined();
expect(inactive!.values).toEqual(['I']);
});
it('extracts FD entries with record name linkage', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' FILE SECTION.',
' FD EMPLOYEE-FILE.',
' 01 EMPLOYEE-RECORD.',
' 05 EMP-ID PIC 9(5).',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.fdEntries).toHaveLength(1);
expect(r.fdEntries[0].fdName).toBe('EMPLOYEE-FILE');
expect(r.fdEntries[0].recordName).toBe('EMPLOYEE-RECORD');
});
it('skips FILLER items', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-REC.',
' 05 FILLER PIC X(10).',
' 05 WS-DATA PIC X(20).',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const fillerItems = r.dataItems.filter(d => d.name === 'FILLER');
expect(fillerItems).toHaveLength(0);
expect(r.dataItems.find(d => d.name === 'WS-DATA')).toBeDefined();
});
it('correctly assigns data section (working-storage, linkage, file)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' FILE SECTION.',
' FD MY-FILE.',
' 01 FILE-REC PIC X(80).',
' WORKING-STORAGE SECTION.',
' 01 WS-VAR PIC X(10).',
' LINKAGE SECTION.',
' 01 LK-VAR PIC X(10).',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const fileRec = r.dataItems.find(d => d.name === 'FILE-REC');
expect(fileRec).toBeDefined();
expect(fileRec!.section).toBe('file');
const wsVar = r.dataItems.find(d => d.name === 'WS-VAR');
expect(wsVar).toBeDefined();
expect(wsVar!.section).toBe('working-storage');
const lkVar = r.dataItems.find(d => d.name === 'LK-VAR');
expect(lkVar).toBeDefined();
expect(lkVar!.section).toBe('linkage');
});
});
// -------------------------------------------------------------------------
// Environment Division
// -------------------------------------------------------------------------
describe('Environment Division', () => {
it('extracts SELECT ... ASSIGN TO with organization, access, record key', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' ENVIRONMENT DIVISION.',
' INPUT-OUTPUT SECTION.',
' FILE-CONTROL.',
' SELECT EMPLOYEE-FILE',
' ASSIGN TO "EMPFILE"',
' ORGANIZATION IS INDEXED',
' ACCESS MODE IS DYNAMIC',
' RECORD KEY IS EMP-ID.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.fileDeclarations).toHaveLength(1);
const fd = r.fileDeclarations[0];
expect(fd.selectName).toBe('EMPLOYEE-FILE');
expect(fd.assignTo).toBe('EMPFILE');
expect(fd.organization).toBe('INDEXED');
expect(fd.access).toBe('DYNAMIC');
expect(fd.recordKey).toBe('EMP-ID');
});
});
// -------------------------------------------------------------------------
// State Machine
// -------------------------------------------------------------------------
describe('State Machine', () => {
it('correctly transitions between divisions', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' ENVIRONMENT DIVISION.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-VAR PIC X(10).',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' DISPLAY WS-VAR.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programName).toBe('TESTPROG');
expect(r.dataItems.find(d => d.name === 'WS-VAR')).toBeDefined();
expect(r.paragraphs).toHaveLength(1);
expect(r.paragraphs[0].name).toBe('MAIN-PARA');
});
it('handles continuation lines (indicator "-" in column 7)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' CALL "VERY-LONG-PR',
' - "OGRAM-NAME".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// Continuation merges lines; at minimum verify no crash and paragraph found
expect(r.paragraphs).toHaveLength(1);
expect(r.paragraphs[0].name).toBe('MAIN-PARA');
});
it('skips comment lines (indicator "*" or "/" in column 7)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' * THIS IS A COMMENT',
' / THIS IS A PAGE BREAK COMMENT',
' CALL "REALPROG".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('REALPROG');
});
});
// -------------------------------------------------------------------------
// EXEC Blocks
// -------------------------------------------------------------------------
describe('EXEC Blocks', () => {
it('extracts EXEC SQL blocks with tables and host variables', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC SQL',
' SELECT EMP-NAME, EMP-SALARY',
' FROM EMPLOYEE',
' WHERE EMP-ID = :WS-EMP-ID',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execSqlBlocks).toHaveLength(1);
const sql = r.execSqlBlocks[0];
expect(sql.operation).toBe('SELECT');
expect(sql.tables).toContain('EMPLOYEE');
expect(sql.hostVariables).toContain('WS-EMP-ID');
});
it('extracts EXEC CICS blocks with command and MAP/PROGRAM/TRANSID', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" EXEC CICS SEND MAP('EMPMAP')",
" PROGRAM('EMPPROG')",
" TRANSID('EMPT')",
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execCicsBlocks).toHaveLength(1);
const cics = r.execCicsBlocks[0];
expect(cics.command).toBe('SEND MAP');
expect(cics.mapName).toBe('EMPMAP');
expect(cics.programName).toBe('EMPPROG');
expect(cics.transId).toBe('EMPT');
});
it('extracts EXEC CICS MAP with unquoted identifier', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC CICS SEND MAP(WS-MAP-NAME)',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execCicsBlocks).toHaveLength(1);
expect(r.execCicsBlocks[0].mapName).toBe('WS-MAP-NAME');
});
it('handles single-line EXEC SQL ... END-EXEC', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC SQL DELETE FROM ORDERS WHERE ORD-ID = :WS-ORD END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execSqlBlocks).toHaveLength(1);
expect(r.execSqlBlocks[0].operation).toBe('DELETE');
expect(r.execSqlBlocks[0].tables).toContain('ORDERS');
});
it('handles multi-line EXEC SQL ... END-EXEC', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC SQL',
' INSERT INTO AUDIT_LOG',
' VALUES (:WS-TIMESTAMP, :WS-USER)',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execSqlBlocks).toHaveLength(1);
const sql = r.execSqlBlocks[0];
expect(sql.operation).toBe('INSERT');
expect(sql.tables).toContain('AUDIT_LOG');
expect(sql.hostVariables).toContain('WS-TIMESTAMP');
expect(sql.hostVariables).toContain('WS-USER');
});
});
// -------------------------------------------------------------------------
// Linkage & Data Flow
// -------------------------------------------------------------------------
describe('Linkage & Data Flow', () => {
it('extracts PROCEDURE DIVISION USING parameters', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' LINKAGE SECTION.',
' 01 LK-PARAM1 PIC X(10).',
' 01 LK-PARAM2 PIC 9(5).',
' PROCEDURE DIVISION USING LK-PARAM1 LK-PARAM2.',
' MAIN-PARA.',
' DISPLAY LK-PARAM1.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.procedureUsing).toEqual(['LK-PARAM1', 'LK-PARAM2']);
});
it('extracts ENTRY points with USING', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' ENTRY "ALTENTRY" USING WS-PARAM1 WS-PARAM2.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.entryPoints).toHaveLength(1);
expect(r.entryPoints[0].name).toBe('ALTENTRY');
expect(r.entryPoints[0].parameters).toEqual(['WS-PARAM1', 'WS-PARAM2']);
});
it("extracts ENTRY 'ALTENTRY' with single-quoted target", () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" ENTRY 'ALTENTRY' USING WS-PARAM1.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.entryPoints).toHaveLength(1);
expect(r.entryPoints[0].name).toBe('ALTENTRY');
expect(r.entryPoints[0].parameters).toEqual(['WS-PARAM1']);
});
it('ENTRY USING filters calling-convention keywords (BY VALUE REFERENCE)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" ENTRY 'ALTENTRY' USING BY VALUE WS-AMT BY REFERENCE LS-REC.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.entryPoints).toHaveLength(1);
// BY, VALUE, REFERENCE should be filtered out — only actual parameter names remain
expect(r.entryPoints[0].parameters).toEqual(['WS-AMT', 'LS-REC']);
});
it('paragraphs with SECTION in name are NOT excluded (e.g., CROSS-SECTION-PROC)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' CROSS-SECTION-ANALYSIS.',
' DISPLAY "HELLO".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.paragraphs.map(p => p.name)).toContain('CROSS-SECTION-ANALYSIS');
});
it('PERFORM THROUGH (full spelling) captures thruTarget', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' PERFORM FIRST-PARA THROUGH LAST-PARA.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.performs).toHaveLength(1);
expect(r.performs[0].target).toBe('FIRST-PARA');
expect(r.performs[0].thruTarget).toBe('LAST-PARA');
});
it('PROCEDURE DIVISION USING RETURNING excludes return value from USING list', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION USING WS-INPUT RETURNING WS-RESULT.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// RETURNING and everything after it should be excluded — only USING parameters remain
expect(r.procedureUsing).toEqual(['WS-INPUT']);
});
it('RE_CALL_DYNAMIC does NOT false-match on WS-CALL compound identifier', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 05 WS-CALL OCCURS 10 PIC X(10).',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' DISPLAY WS-CALL.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// WS-CALL should NOT produce a dynamic CALL — it's a data item name
expect(r.calls.filter(c => !c.isQuoted)).toHaveLength(0);
});
it('multi-line SORT captures USING and GIVING from continuation lines', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SORT SORT-FILE',
' ON ASCENDING KEY WS-KEY',
' USING INPUT-FILE',
' GIVING OUTPUT-FILE.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sorts).toHaveLength(1);
expect(r.sorts[0].sortFile).toBe('SORT-FILE');
expect(r.sorts[0].usingFiles).toContain('INPUT-FILE');
expect(r.sorts[0].givingFiles).toContain('OUTPUT-FILE');
});
it('PROCEDURE DIVISION USING on split line is captured via pendingProcUsing', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION',
' USING WS-PARAM1 WS-PARAM2.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.procedureUsing).toEqual(['WS-PARAM1', 'WS-PARAM2']);
});
it('nested programs carry per-program procedureUsing', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER.',
' PROCEDURE DIVISION USING WS-OUTER-PARAM.',
' MAIN-PARA.',
' DISPLAY WS-OUTER-PARAM.',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER.',
' PROCEDURE DIVISION USING WS-INNER-PARAM.',
' INNER-PARA.',
' DISPLAY WS-INNER-PARAM.',
' END PROGRAM INNER.',
' END PROGRAM OUTER.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programs).toHaveLength(2);
const outer = r.programs.find(p => p.name === 'OUTER');
const inner = r.programs.find(p => p.name === 'INNER');
expect(outer?.procedureUsing).toEqual(['WS-OUTER-PARAM']);
expect(inner?.procedureUsing).toEqual(['WS-INNER-PARAM']);
});
it('SECTION with segment number is detected', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-SECTION SECTION 30.',
' MAIN-PARA.',
' DISPLAY "HI".',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sections.map(s => s.name)).toContain('MAIN-SECTION');
});
it('dynamic CANCEL via data item is captured with isQuoted=false', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' CANCEL WS-PGM-NAME.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.cancels).toHaveLength(1);
expect(r.cancels[0].target).toBe('WS-PGM-NAME');
expect(r.cancels[0].isQuoted).toBe(false);
});
it('copybook preprocessing strips sequence numbers before expansion', () => {
// This is tested indirectly — preprocessCobolSource is called in readCopy
const input = cobol('000100 IDENTIFICATION DIVISION.', '000200 PROGRAM-ID. TEST1.');
const output = preprocessCobolSource(input);
// Verify cols 1-6 are blanked for numeric sequences
expect(output.split('\n')[0]).toBe(' IDENTIFICATION DIVISION.');
});
it('numeric sequence numbers are stripped so paragraphs are detected', () => {
const src = preprocessCobolSource(cobol(
'000100 IDENTIFICATION DIVISION.',
'000200 PROGRAM-ID. SEQTEST.',
'000300 PROCEDURE DIVISION.',
'000400 MAIN-PARA.',
'000500 DISPLAY "HI".',
));
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programName).toBe('SEQTEST');
expect(r.paragraphs.map(p => p.name)).toEqual(['MAIN-PARA']);
});
it('extracts MOVE statements (skipping figurative constants)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' MOVE WS-SOURCE TO WS-TARGET.',
' MOVE SPACES TO WS-BLANK.',
' MOVE ZEROS TO WS-ZERO.',
' MOVE CORRESPONDING WS-REC1 TO WS-REC2.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const moveData = r.moves.map(m => ({ from: m.from, targets: m.targets, corr: m.corresponding }));
expect(moveData).toContainEqual({ from: 'WS-SOURCE', targets: ['WS-TARGET'], corr: false });
expect(moveData).toContainEqual({ from: 'WS-REC1', targets: ['WS-REC2'], corr: true });
expect(r.moves.find(m => m.from === 'SPACES')).toBeUndefined();
expect(r.moves.find(m => m.from === 'ZEROS')).toBeUndefined();
});
it('captures multiple MOVE targets: MOVE X TO A B C', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' MOVE WS-SOURCE TO WS-A WS-B WS-C.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.moves).toHaveLength(1);
expect(r.moves[0].targets).toEqual(['WS-A', 'WS-B', 'WS-C']);
});
it('MOVE CORRESPONDING is always single target', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' MOVE CORRESPONDING WS-REC1 TO WS-REC2.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.moves).toHaveLength(1);
expect(r.moves[0].targets).toEqual(['WS-REC2']);
expect(r.moves[0].corresponding).toBe(true);
});
it('MOVE handles OF-qualified names', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' MOVE WS-SRC TO WS-NAME OF WS-RECORD WS-CODE.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.moves).toHaveLength(1);
// WS-NAME OF WS-RECORD -> WS-NAME is the target; WS-CODE is a second target
expect(r.moves[0].targets).toEqual(['WS-NAME', 'WS-CODE']);
});
it('MOVE skips figurative constants in targets', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' MOVE WS-SRC TO SPACES.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// SPACES is in MOVE_SKIP, so no targets -> no move entry
expect(r.moves).toHaveLength(0);
});
});
// -------------------------------------------------------------------------
// Edge Cases
// -------------------------------------------------------------------------
describe('Edge Cases', () => {
it('empty program returns empty results', () => {
const r = extractCobolSymbolsWithRegex('', 'empty.cbl');
expect(r.programName).toBeNull();
expect(r.paragraphs).toHaveLength(0);
expect(r.sections).toHaveLength(0);
expect(r.performs).toHaveLength(0);
expect(r.calls).toHaveLength(0);
expect(r.copies).toHaveLength(0);
expect(r.dataItems).toHaveLength(0);
expect(r.fileDeclarations).toHaveLength(0);
expect(r.fdEntries).toHaveLength(0);
expect(r.execSqlBlocks).toHaveLength(0);
expect(r.execCicsBlocks).toHaveLength(0);
expect(r.procedureUsing).toHaveLength(0);
expect(r.entryPoints).toHaveLength(0);
expect(r.moves).toHaveLength(0);
});
it('extracts AUTHOR and DATE-WRITTEN from program metadata', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' AUTHOR. JOHN DOE.',
' DATE-WRITTEN. 2025-01-15.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programMetadata.author).toBe('JOHN DOE');
expect(r.programMetadata.dateWritten).toBe('2025-01-15');
});
});
// -------------------------------------------------------------------------
// Phase 1: Data Flow Features
// -------------------------------------------------------------------------
describe('Phase 1: Data Flow Features', () => {
it('EXEC SQL INCLUDE extracts member name (unquoted)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' EXEC SQL INCLUDE SQLCA END-EXEC.',
' EXEC SQL INCLUDE CUSTDCL END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const includes = r.execSqlBlocks.filter(b => b.includeMember);
expect(includes).toHaveLength(2);
expect(includes[0].includeMember).toBe('SQLCA');
expect(includes[1].includeMember).toBe('CUSTDCL');
});
it('EXEC SQL INCLUDE handles quoted and underscored member names', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
" EXEC SQL INCLUDE 'DBRMLIB.MEMBER' END-EXEC.",
' EXEC SQL INCLUDE CUST_TBL_DCL END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const includes = r.execSqlBlocks.filter(b => b.includeMember);
expect(includes).toHaveLength(2);
expect(includes[0].includeMember).toBe('DBRMLIB.MEMBER');
expect(includes[1].includeMember).toBe('CUST_TBL_DCL');
});
it('CALL USING extracts parameters', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'AUDITLOG' USING WS-CUST-ID WS-AMOUNT.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-CUST-ID', 'WS-AMOUNT']);
});
it('CALL USING filters BY REFERENCE/CONTENT/VALUE keywords', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM' USING BY REFERENCE WS-A BY CONTENT WS-B BY VALUE WS-C.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls[0].parameters).toEqual(['WS-A', 'WS-B', 'WS-C']);
});
it('CALL USING filters ADDRESS OF and OMITTED', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM' USING ADDRESS OF WS-REC OMITTED WS-FLAG.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls[0].parameters).toEqual(['WS-REC', 'WS-FLAG']);
});
it('CALL RETURNING extracts return target', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'FUNC' USING WS-INPUT RETURNING WS-RESULT.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls[0].parameters).toEqual(['WS-INPUT']);
expect(r.calls[0].returning).toBe('WS-RESULT');
});
it('OCCURS DEPENDING ON captures controlling field', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-COUNT PIC 9(4).',
' 01 WS-TABLE OCCURS 1 TO 100 DEPENDING ON WS-COUNT.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const table = r.dataItems.find(d => d.name === 'WS-TABLE');
expect(table).toBeDefined();
expect(table!.dependingOn).toBe('WS-COUNT');
expect(table!.occurs).toBe(1);
});
it('VALUE clause extracts quoted string', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
" 01 WS-STATUS PIC X VALUE 'A'.",
' 01 WS-COUNT PIC 9(4) VALUE 0.',
' 01 WS-NAME PIC X(10) VALUE SPACES.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.dataItems.find(d => d.name === 'WS-STATUS')?.values).toEqual(['A']);
expect(r.dataItems.find(d => d.name === 'WS-COUNT')?.values).toEqual(['0']);
expect(r.dataItems.find(d => d.name === 'WS-NAME')?.values).toEqual(['SPACES']);
});
});
// -------------------------------------------------------------------------
// Phase 2: IMS + Error Handling Features
// -------------------------------------------------------------------------
describe('Phase 2: IMS + Error Handling Features', () => {
it('EXEC DLI GU extracts verb, segment, PCB, and INTO', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC DLI GU USING PCB(2)',
' SEGMENT(CUSTOMER)',
' INTO(CUST-IO-AREA)',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execDliBlocks).toHaveLength(1);
expect(r.execDliBlocks[0].verb).toBe('GU');
expect(r.execDliBlocks[0].pcbNumber).toBe(2);
expect(r.execDliBlocks[0].segmentName).toBe('CUSTOMER');
expect(r.execDliBlocks[0].intoField).toBe('CUST-IO-AREA');
});
it('EXEC DLI ISRT extracts FROM field', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC DLI ISRT USING PCB(1)',
' SEGMENT(ORDER)',
' FROM(ORDER-IO-AREA)',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execDliBlocks[0].verb).toBe('ISRT');
expect(r.execDliBlocks[0].fromField).toBe('ORDER-IO-AREA');
});
it('EXEC DLI SCHD extracts PSB name', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC DLI SCHD PSB(CUSTPSB) END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execDliBlocks[0].verb).toBe('SCHD');
expect(r.execDliBlocks[0].psbName).toBe('CUSTPSB');
});
it('DECLARATIVES USE AFTER EXCEPTION extracts file binding', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' DECLARATIVES.',
' CUST-ERR SECTION.',
' USE AFTER STANDARD ERROR ON CUSTOMER-FILE.',
' CUST-ERR-PARA.',
' DISPLAY "FILE ERROR".',
' END DECLARATIVES.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.declaratives).toHaveLength(1);
expect(r.declaratives[0].sectionName).toBe('CUST-ERR');
expect(r.declaratives[0].target).toBe('CUSTOMER-FILE');
});
it('DECLARATIVES with multiple USE sections', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' DECLARATIVES.',
' ERR-A SECTION.',
' USE AFTER STANDARD EXCEPTION ON FILE-A.',
' ERR-A-PARA.',
' DISPLAY "A".',
' ERR-B SECTION.',
' USE AFTER STANDARD EXCEPTION ON INPUT.',
' ERR-B-PARA.',
' DISPLAY "B".',
' END DECLARATIVES.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.declaratives).toHaveLength(2);
expect(r.declaratives[0].target).toBe('FILE-A');
expect(r.declaratives[1].target).toBe('INPUT');
});
it('SET condition TO TRUE extracts targets', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SET END-OF-FILE TO TRUE.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sets).toHaveLength(1);
expect(r.sets[0].form).toBe('to-true');
expect(r.sets[0].targets).toEqual(['END-OF-FILE']);
});
it('SET index UP BY extracts target and value', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SET IDX-1 UP BY 1.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sets).toHaveLength(1);
expect(r.sets[0].form).toBe('up-by');
expect(r.sets[0].targets).toEqual(['IDX-1']);
expect(r.sets[0].value).toBe('1');
});
it('INSPECT TALLYING extracts field and counter', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" INSPECT WS-STRING TALLYING WS-COUNT FOR ALL 'A'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].inspectedField).toBe('WS-STRING');
expect(r.inspects[0].counters).toEqual(['WS-COUNT']);
expect(r.inspects[0].form).toBe('tallying');
});
it('INSPECT REPLACING detected', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" INSPECT WS-FIELD REPLACING ALL 'A' BY 'B'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].form).toBe('replacing');
});
});
// -------------------------------------------------------------------------
// Phase 3-4: Completeness + Niche Features
// -------------------------------------------------------------------------
describe('Phase 3-4: Completeness + Niche Features', () => {
it('SELECT OPTIONAL sets isOptional flag', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' ENVIRONMENT DIVISION.',
' INPUT-OUTPUT SECTION.',
' FILE-CONTROL.',
" SELECT OPTIONAL CUST-FILE ASSIGN TO 'CUSTFILE'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.fileDeclarations).toHaveLength(1);
expect(r.fileDeclarations[0].selectName).toBe('CUST-FILE');
expect(r.fileDeclarations[0].isOptional).toBe(true);
});
it('ALTERNATE RECORD KEY extraction', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' ENVIRONMENT DIVISION.',
' INPUT-OUTPUT SECTION.',
' FILE-CONTROL.',
" SELECT CUST-FILE ASSIGN TO 'CUSTFILE'",
' RECORD KEY IS CUST-ID',
' ALTERNATE RECORD KEY IS CUST-NAME.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.fileDeclarations[0].recordKey).toBe('CUST-ID');
expect(r.fileDeclarations[0].alternateKeys).toEqual(['CUST-NAME']);
});
it('PROGRAM-ID IS COMMON sets isCommon flag', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER-PGM.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' STOP RUN.',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER-PGM IS COMMON.',
' PROCEDURE DIVISION.',
' INNER-PARA.',
' STOP RUN.',
' END PROGRAM INNER-PGM.',
' END PROGRAM OUTER-PGM.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const inner = r.programs.find(p => p.name === 'INNER-PGM');
expect(inner).toBeDefined();
expect(inner!.isCommon).toBe(true);
const outer = r.programs.find(p => p.name === 'OUTER-PGM');
expect(outer!.isCommon).toBeFalsy();
});
it('IS EXTERNAL and IS GLOBAL as boolean properties', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-SHARED PIC X(10) IS EXTERNAL.',
' 01 WS-GLOBAL PIC X(10) IS GLOBAL.',
' 01 WS-NORMAL PIC X(10).',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.dataItems.find(d => d.name === 'WS-SHARED')?.isExternal).toBe(true);
expect(r.dataItems.find(d => d.name === 'WS-GLOBAL')?.isGlobal).toBe(true);
expect(r.dataItems.find(d => d.name === 'WS-NORMAL')?.isExternal).toBeUndefined();
expect(r.dataItems.find(d => d.name === 'WS-NORMAL')?.isGlobal).toBeUndefined();
});
it('INITIALIZE extracts target', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' INITIALIZE WS-RECORD.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.initializes).toHaveLength(1);
expect(r.initializes[0].target).toBe('WS-RECORD');
});
it('AUTHOR and DATE-WRITTEN mapped to programMetadata', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' AUTHOR. JOHN DOE.',
' DATE-WRITTEN. 2026-03-26.',
' DATE-COMPILED. 2026-03-26.',
' INSTALLATION. MAINFRAME-01.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programMetadata.author).toBe('JOHN DOE');
expect(r.programMetadata.dateWritten).toBe('2026-03-26');
expect(r.programMetadata.dateCompiled).toBe('2026-03-26');
expect(r.programMetadata.installation).toBe('MAINFRAME-01');
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: Multi-line CALL USING accumulation
// -------------------------------------------------------------------------
describe('Multi-line CALL USING accumulation', () => {
it('captures USING parameters on separate lines (IBM mainframe style)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'CUSTUPDT'",
' USING BY REFERENCE WS-CUST-ID',
' WS-CUST-NAME',
' WS-CUST-ADDR.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('CUSTUPDT');
expect(r.calls[0].parameters).toEqual(['WS-CUST-ID', 'WS-CUST-NAME', 'WS-CUST-ADDR']);
});
it('does NOT absorb next statement as USING parameter (no END-CALL)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'CUSTUPDT'",
' USING WS-PARM.',
' INSPECT WS-STATUS TALLYING WS-CNT FOR ALL SPACES.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-PARM']);
// INSPECT should be extracted separately, not absorbed
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].inspectedField).toBe('WS-STATUS');
});
it('does NOT absorb GO TO on next line', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'CUSTUPDT'",
' USING WS-PARM.',
' GO TO EXIT-PARAGRAPH.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('CUSTUPDT');
expect(r.gotos).toHaveLength(1);
expect(r.gotos[0].target).toBe('EXIT-PARAGRAPH');
});
it('does NOT create false paragraph from last USING parameter on own line', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-A',
' WS-B.',
' PERFORM NEXT-PARA.',
' NEXT-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// WS-B should NOT be a paragraph
const paraNames = r.paragraphs.map(p => p.name);
expect(paraNames).toContain('MAIN-PARA');
expect(paraNames).toContain('NEXT-PARA');
expect(paraNames).not.toContain('WS-B');
// WS-B should be captured as USING parameter
expect(r.calls[0].parameters).toContain('WS-B');
});
it('handles CALL with END-CALL scope terminator', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM' USING WS-A",
' ON EXCEPTION',
' DISPLAY "ERROR"',
' END-CALL',
' PERFORM NEXT-STEP.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-A']);
expect(r.performs).toHaveLength(1);
expect(r.performs[0].target).toBe('NEXT-STEP');
});
it('does NOT false-flush on hyphenated identifiers like MOVE-COUNT', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING MOVE-COUNT',
' PERFORM-LIMIT',
' READ-STATUS.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls[0].parameters).toEqual(['MOVE-COUNT', 'PERFORM-LIMIT', 'READ-STATUS']);
});
it('captures both quoted and dynamic CALL on same line (ON EXCEPTION)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PRIMARY' ON EXCEPTION CALL WS-FALLBACK.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(2);
expect(r.calls[0].target).toBe('PRIMARY');
expect(r.calls[0].isQuoted).toBe(true);
expect(r.calls[1].target).toBe('WS-FALLBACK');
expect(r.calls[1].isQuoted).toBe(false);
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: Nested program edge attribution
// -------------------------------------------------------------------------
describe('Nested program edge attribution', () => {
it('CALL in inner nested program attributed to inner module (not outer)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER-PGM.',
' PROCEDURE DIVISION.',
' OUTER-MAIN.',
' STOP RUN.',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER-PGM.',
' PROCEDURE DIVISION.',
' INNER-MAIN.',
" CALL 'SUBPROG'.",
' END PROGRAM INNER-PGM.',
' END PROGRAM OUTER-PGM.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// The CALL should have line number within INNER-PGM's range
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('SUBPROG');
const innerProg = r.programs.find(p => p.name === 'INNER-PGM');
expect(innerProg).toBeDefined();
expect(r.calls[0].line).toBe(10); // Line 10 in the fixture: CALL 'SUBPROG'.
});
it('PERFORM before first paragraph in nested program has correct caller', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER-PGM.',
' PROCEDURE DIVISION.',
' OUTER-MAIN.',
' STOP RUN.',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER-PGM.',
' PROCEDURE DIVISION.',
' PERFORM INNER-INIT.',
' INNER-INIT.',
' STOP RUN.',
' END PROGRAM INNER-PGM.',
' END PROGRAM OUTER-PGM.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// PERFORM before first paragraph — caller should be null (module-level)
const innerPerform = r.performs.find(p => p.target === 'INNER-INIT');
expect(innerPerform).toBeDefined();
expect(innerPerform!.caller).toBeNull();
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: CRLF / Windows line ending compatibility
// -------------------------------------------------------------------------
describe('CRLF / Windows line ending compatibility', () => {
it('GO TO DEPENDING ON works with CRLF line endings', () => {
// Simulate CRLF by using \r\n
const src = [
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' GO TO PARA-A PARA-B PARA-C',
' DEPENDING ON WS-SWITCH.',
].join('\r\n');
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.gotos).toHaveLength(3);
expect(r.gotos.map(g => g.target).sort()).toEqual(['PARA-A', 'PARA-B', 'PARA-C']);
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: Fixed-format Area A paragraph detection
// -------------------------------------------------------------------------
describe('Fixed-format Area A paragraph detection', () => {
it('rejects deeply-indented identifiers as paragraphs (Area B)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' WS-CUST-ADDR.', // Area B (>7 spaces) — NOT a paragraph
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const paraNames = r.paragraphs.map(p => p.name);
expect(paraNames).toContain('MAIN-PARA');
expect(paraNames).not.toContain('WS-CUST-ADDR');
});
it('accepts Area A indented paragraphs (7 spaces)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' REAL-PARA.', // 7 spaces — Area A, valid paragraph
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.paragraphs.map(p => p.name)).toContain('REAL-PARA');
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: SORT/MERGE edge cases
// -------------------------------------------------------------------------
describe('SORT/MERGE edge cases', () => {
it('captures SORT GIVING without spurious COLLATING SEQUENCE keywords', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SORT SORT-FILE ON ASCENDING KEY SORT-KEY',
' COLLATING SEQUENCE IS NATL',
' USING INPUT-FILE',
' GIVING OUTPUT-FILE.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sorts).toHaveLength(1);
expect(r.sorts[0].usingFiles).toEqual(['INPUT-FILE']);
// COLLATING, SEQUENCE, IS, NATL should NOT appear as giving files
expect(r.sorts[0].givingFiles).toEqual(['OUTPUT-FILE']);
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: PROCEDURE DIVISION USING edge cases
// -------------------------------------------------------------------------
describe('PROCEDURE DIVISION USING edge cases', () => {
it('excludes RETURNING value from USING parameter list', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION USING WS-INPUT RETURNING WS-RESULT.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.procedureUsing).toEqual(['WS-INPUT']);
});
it('pendingProcUsing not set for period-terminated PROCEDURE DIVISION', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.procedureUsing).toEqual([]);
// No spurious parameters from the first procedure line
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: Comment stripping edge cases
// -------------------------------------------------------------------------
describe('Comment stripping edge cases', () => {
it('pipe character inside quoted string is preserved (not treated as comment)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
" 01 WS-SEP PIC X VALUE '|'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// The data item should be extracted (not truncated by pipe)
expect(r.dataItems.find(d => d.name === 'WS-SEP')).toBeDefined();
});
});
// -------------------------------------------------------------------------
// Reviews 9-15: SELECT OPTIONAL and ALTERNATE KEY
// -------------------------------------------------------------------------
describe('SELECT OPTIONAL and ALTERNATE KEY', () => {
it('SELECT OPTIONAL captures correct file name (not OPTIONAL keyword)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' ENVIRONMENT DIVISION.',
' INPUT-OUTPUT SECTION.',
' FILE-CONTROL.',
" SELECT OPTIONAL BACKUP-FILE ASSIGN TO 'BACKUP'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.fileDeclarations).toHaveLength(1);
expect(r.fileDeclarations[0].selectName).toBe('BACKUP-FILE');
expect(r.fileDeclarations[0].isOptional).toBe(true);
});
});
// -------------------------------------------------------------------------
// Regression: EXEC DLI edge cases
// -------------------------------------------------------------------------
describe('EXEC DLI edge cases', () => {
it('EXEC DLI without SEGMENT clause (DLET/REPL)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC DLI DLET USING PCB(2) END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execDliBlocks).toHaveLength(1);
expect(r.execDliBlocks[0].verb).toBe('DLET');
expect(r.execDliBlocks[0].segmentName).toBeUndefined();
});
it('multi-line EXEC DLI accumulates correctly', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC DLI GN',
' USING PCB(1)',
' SEGMENT(ORDER)',
' INTO(ORDER-IO)',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execDliBlocks).toHaveLength(1);
expect(r.execDliBlocks[0].verb).toBe('GN');
expect(r.execDliBlocks[0].segmentName).toBe('ORDER');
expect(r.execDliBlocks[0].intoField).toBe('ORDER-IO');
});
});
// -------------------------------------------------------------------------
// Regression: SET statement edge cases
// -------------------------------------------------------------------------
describe('SET statement edge cases', () => {
it('SET multiple conditions TO TRUE', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SET COND-A COND-B COND-C TO TRUE.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sets).toHaveLength(1);
expect(r.sets[0].targets).toEqual(['COND-A', 'COND-B', 'COND-C']);
expect(r.sets[0].form).toBe('to-true');
});
it('SET index DOWN BY identifier', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SET IDX-1 DOWN BY WS-DECREMENT.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sets).toHaveLength(1);
expect(r.sets[0].form).toBe('down-by');
expect(r.sets[0].value).toBe('WS-DECREMENT');
});
it('SET index TO numeric value', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SET IDX-1 TO 5.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sets).toHaveLength(1);
expect(r.sets[0].form).toBe('to-value');
expect(r.sets[0].value).toBe('5');
});
});
// -------------------------------------------------------------------------
// Regression: INSPECT multi-line edge cases
// -------------------------------------------------------------------------
describe('INSPECT multi-line edge cases', () => {
it('INSPECT CONVERTING on single line', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" INSPECT WS-FIELD CONVERTING 'abc' TO 'ABC'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].form).toBe('converting');
expect(r.inspects[0].inspectedField).toBe('WS-FIELD');
});
it('INSPECT TALLYING with multiple counters', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' INSPECT WS-STRING TALLYING',
" WS-CNT-A FOR ALL 'A'",
" WS-CNT-B FOR ALL 'B'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].counters).toEqual(['WS-CNT-A', 'WS-CNT-B']);
});
it('INSPECT combined TALLYING and REPLACING', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' INSPECT WS-DATA',
" TALLYING WS-COUNT FOR ALL 'X'",
" REPLACING ALL 'X' BY 'Y'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].form).toBe('tallying-replacing');
});
it('real paragraph header during INSPECT flushes accumulator', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" INSPECT WS-FIELD REPLACING ALL 'A' BY 'B'",
' NEXT-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// INSPECT should be flushed, NEXT-PARA should be detected
expect(r.inspects).toHaveLength(1);
expect(r.paragraphs.map(p => p.name)).toContain('NEXT-PARA');
});
});
// -------------------------------------------------------------------------
// Regression: DECLARATIVES edge cases
// -------------------------------------------------------------------------
describe('DECLARATIVES edge cases', () => {
it('USE AFTER without STANDARD keyword (IBM extension)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' DECLARATIVES.',
' FILE-ERR SECTION.',
' USE AFTER EXCEPTION ON MASTER-FILE.',
' FILE-ERR-PARA.',
' DISPLAY "ERROR".',
' END DECLARATIVES.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.declaratives).toHaveLength(1);
expect(r.declaratives[0].target).toBe('MASTER-FILE');
});
it('USE AFTER on I-O mode (catch-all handler)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' DECLARATIVES.',
' IO-ERR SECTION.',
' USE AFTER STANDARD ERROR ON I-O.',
' IO-ERR-PARA.',
' DISPLAY "I-O ERROR".',
' END DECLARATIVES.',
' MAIN-PARA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.declaratives).toHaveLength(1);
expect(r.declaratives[0].target).toBe('I-O');
});
it('paragraphs after END DECLARATIVES are normal paragraphs', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' DECLARATIVES.',
' ERR SECTION.',
' USE AFTER STANDARD ERROR ON INPUT.',
' ERR-PARA.',
' DISPLAY "E".',
' END DECLARATIVES.',
' MAIN-PARA.',
' PERFORM PROCESS-DATA.',
' PROCESS-DATA.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const paraNames = r.paragraphs.map(p => p.name);
expect(paraNames).toContain('ERR-PARA');
expect(paraNames).toContain('MAIN-PARA');
expect(paraNames).toContain('PROCESS-DATA');
expect(r.performs).toHaveLength(1);
expect(r.performs[0].target).toBe('PROCESS-DATA');
});
});
// -------------------------------------------------------------------------
// Regression: COPY REPLACING edge cases
// -------------------------------------------------------------------------
describe('COPY REPLACING edge cases', () => {
it('pseudotext replacement with empty target (deletion)', () => {
const replacings = parseReplacingClause('==OLD-TEXT== BY ====');
expect(replacings).toHaveLength(1);
expect(replacings[0].from).toBe('OLD-TEXT');
expect(replacings[0].to).toBe('');
expect(replacings[0].isPseudotext).toBe(true);
});
});
// -------------------------------------------------------------------------
// Regression: Value clause edge cases
// -------------------------------------------------------------------------
describe('Value clause edge cases', () => {
it('VALUE with hex literal', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
" 01 WS-HEX PIC X(4) VALUE X'F1F2F3F4'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const hex = r.dataItems.find(d => d.name === 'WS-HEX');
expect(hex).toBeDefined();
expect(hex!.values).toBeDefined();
expect(hex!.values![0]).toContain('F1F2F3F4');
});
it('VALUE with negative numeric', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-NEG PIC S9(4) VALUE -1.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.dataItems.find(d => d.name === 'WS-NEG')?.values).toEqual(['-1']);
});
it('VALUE with ALL literal', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
" 01 WS-STARS PIC X(80) VALUE ALL '*'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const stars = r.dataItems.find(d => d.name === 'WS-STARS');
expect(stars?.values).toBeDefined();
expect(stars!.values![0]).toContain('*');
});
});
// -------------------------------------------------------------------------
// Regression: OCCURS DEPENDING ON edge cases
// -------------------------------------------------------------------------
describe('OCCURS DEPENDING ON edge cases', () => {
it('OCCURS with TO range', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-CNT PIC 9(4).',
' 01 WS-TBL OCCURS 1 TO 50 DEPENDING ON WS-CNT.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const tbl = r.dataItems.find(d => d.name === 'WS-TBL');
expect(tbl?.occurs).toBe(1);
expect(tbl?.dependingOn).toBe('WS-CNT');
});
it('OCCURS without DEPENDING ON (fixed-size)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-ARR OCCURS 10.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.dataItems.find(d => d.name === 'WS-ARR')?.occurs).toBe(10);
expect(r.dataItems.find(d => d.name === 'WS-ARR')?.dependingOn).toBeUndefined();
});
});
// -------------------------------------------------------------------------
// Regression: Dynamic CALL edge cases
// -------------------------------------------------------------------------
describe('Dynamic CALL edge cases', () => {
it('dynamic CALL at end of line (no trailing space or period)', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' CALL WS-PROGRAM',
' USING WS-DATA.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('WS-PROGRAM');
expect(r.calls[0].isQuoted).toBe(false);
});
it('CANCEL at end of line', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' CANCEL WS-OLD-PROG.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.cancels).toHaveLength(1);
expect(r.cancels[0].target).toBe('WS-OLD-PROG');
expect(r.cancels[0].isQuoted).toBe(false);
});
it('multiple CANCELs on same line', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CANCEL 'PROG-A' CANCEL 'PROG-B'.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.cancels).toHaveLength(2);
expect(r.cancels[0].target).toBe('PROG-A');
expect(r.cancels[1].target).toBe('PROG-B');
});
});
// -------------------------------------------------------------------------
// Regression: EXEC SQL edge cases
// -------------------------------------------------------------------------
describe('EXEC SQL edge cases', () => {
it('EXEC SQL INCLUDE does not extract tables', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' EXEC SQL INCLUDE SQLCA END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execSqlBlocks).toHaveLength(1);
expect(r.execSqlBlocks[0].includeMember).toBe('SQLCA');
expect(r.execSqlBlocks[0].tables).toHaveLength(0);
});
it('EXEC SQL SELECT INTO host variable does not capture as table', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC SQL',
' SELECT CUST_NAME INTO :WS-NAME',
' FROM CUSTOMER',
' WHERE CUST_ID = :WS-ID',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execSqlBlocks).toHaveLength(1);
// CUSTOMER should be a table, :WS-NAME should NOT
expect(r.execSqlBlocks[0].tables).toContain('CUSTOMER');
expect(r.execSqlBlocks[0].tables).not.toContain('WS-NAME');
});
it('EXEC SQL with host variables extracted', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC SQL',
' UPDATE CUSTOMER SET BALANCE = :WS-AMT',
' WHERE CUST_ID = :WS-ID',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.execSqlBlocks[0].hostVariables).toContain('WS-AMT');
expect(r.execSqlBlocks[0].hostVariables).toContain('WS-ID');
});
});
// -------------------------------------------------------------------------
// Regression: INITIALIZE extraction
// -------------------------------------------------------------------------
describe('INITIALIZE extraction', () => {
it('INITIALIZE extracts target field', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' INITIALIZE WS-CUSTOMER-REC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.initializes).toHaveLength(1);
expect(r.initializes[0].target).toBe('WS-CUSTOMER-REC');
expect(r.initializes[0].caller).toBe('MAIN-PARA');
});
it('INITIALIZE multi-target extracts all targets', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' INITIALIZE WS-CUSTOMER WS-ORDER WS-LINE-ITEM.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.initializes).toHaveLength(3);
expect(r.initializes.map(i => i.target)).toEqual(['WS-CUSTOMER', 'WS-ORDER', 'WS-LINE-ITEM']);
});
it('INITIALIZE with REPLACING clause does not capture keywords as targets', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' INITIALIZE WS-RECORD REPLACING NUMERIC BY ZEROS.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.initializes).toHaveLength(1);
expect(r.initializes[0].target).toBe('WS-RECORD');
});
});
// -------------------------------------------------------------------------
// Regression: Nested program boundary tracking
// -------------------------------------------------------------------------
describe('Nested program boundary tracking', () => {
it('sibling programs after END PROGRAM are correctly scoped', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER.',
' PROCEDURE DIVISION.',
' OUTER-MAIN.',
' STOP RUN.',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER-A.',
' PROCEDURE DIVISION.',
' A-MAIN.',
' STOP RUN.',
' END PROGRAM INNER-A.',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER-B.',
' PROCEDURE DIVISION.',
' B-MAIN.',
' STOP RUN.',
' END PROGRAM INNER-B.',
' END PROGRAM OUTER.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programs).toHaveLength(3);
expect(r.programs.map(p => p.name).sort()).toEqual(['INNER-A', 'INNER-B', 'OUTER']);
const innerA = r.programs.find(p => p.name === 'INNER-A')!;
const innerB = r.programs.find(p => p.name === 'INNER-B')!;
expect(innerA.endLine).toBe(11); // END PROGRAM INNER-A
expect(innerB.startLine).toBe(13); // PROGRAM-ID. INNER-B
});
it('PROGRAM-ID without IDENTIFICATION DIVISION header detected', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER.',
' PROCEDURE DIVISION.',
' OUTER-MAIN.',
' STOP RUN.',
' PROGRAM-ID. SIBLING.',
' PROCEDURE DIVISION.',
' SIB-MAIN.',
' STOP RUN.',
' END PROGRAM SIBLING.',
' END PROGRAM OUTER.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const names = r.programs.map(p => p.name);
expect(names).toContain('SIBLING');
expect(names).toContain('OUTER');
});
});
// -------------------------------------------------------------------------
// Regression: EXEC block EOF flush
// -------------------------------------------------------------------------
describe('EXEC block EOF flush', () => {
it('unclosed EXEC SQL is flushed at EOF', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' EXEC SQL',
' SELECT * FROM CUSTOMER',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// Should still extract even without END-EXEC
expect(r.execSqlBlocks).toHaveLength(1);
expect(r.execSqlBlocks[0].tables).toContain('CUSTOMER');
});
});
// -------------------------------------------------------------------------
// Regression: Multi-PERFORM on same line
// -------------------------------------------------------------------------
describe('Multi-PERFORM on same line', () => {
it('captures both PERFORMs in IF/ELSE on single line', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' IF WS-FLAG = 1 PERFORM PARA-A ELSE PERFORM PARA-B END-IF.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const targets = r.performs.map(p => p.target).sort();
expect(targets).toEqual(['PARA-A', 'PARA-B']);
});
});
// -------------------------------------------------------------------------
// Regression: Data item IS EXTERNAL / IS GLOBAL
// -------------------------------------------------------------------------
describe('Data item IS EXTERNAL / IS GLOBAL', () => {
it('IS EXTERNAL does not pollute usage string', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-SHARED PIC X(10) USAGE DISPLAY IS EXTERNAL.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const item = r.dataItems.find(d => d.name === 'WS-SHARED');
expect(item?.isExternal).toBe(true);
// usage should NOT contain 'external' as a string suffix
expect(item?.usage).toBe('DISPLAY');
});
});
// -------------------------------------------------------------------------
// Accumulator flush on division transitions
// -------------------------------------------------------------------------
describe('Accumulator flush on division transitions', () => {
it('callAccum flushed when EXEC SQL interrupts multi-line CALL', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'SUBPROG'",
' USING WS-PARM',
' EXEC SQL',
' SELECT * FROM CUSTOMER',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// CALL should be extracted with USING parameters (flushed before EXEC SQL)
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('SUBPROG');
expect(r.calls[0].parameters).toEqual(['WS-PARM']);
// EXEC SQL should also be extracted
expect(r.execSqlBlocks).toHaveLength(1);
expect(r.execSqlBlocks[0].tables).toContain('CUSTOMER');
});
it('callAccum flushed when EXEC CICS interrupts multi-line CALL', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'SUBPROG'",
' USING WS-DATA',
' EXEC CICS',
" LINK PROGRAM('AUDITLOG')",
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-DATA']);
expect(r.execCicsBlocks).toHaveLength(1);
});
it('callAccum flushed when EXEC DLI interrupts multi-line CALL', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'SUBPROG'",
' USING WS-KEY',
' EXEC DLI GU',
' USING PCB(1)',
' SEGMENT(CUSTOMER)',
' END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-KEY']);
expect(r.execDliBlocks).toHaveLength(1);
expect(r.execDliBlocks[0].verb).toBe('GU');
});
it('all accumulators flushed on division transition', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER-PGM.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'SUBPROG'",
' USING WS-DATA',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER-PGM.',
' PROCEDURE DIVISION.',
' INNER-MAIN.',
' STOP RUN.',
' END PROGRAM INNER-PGM.',
' END PROGRAM OUTER-PGM.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// CALL should be flushed before the new IDENTIFICATION DIVISION
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('SUBPROG');
// Both programs should be detected
expect(r.programs.map(p => p.name).sort()).toEqual(['INNER-PGM', 'OUTER-PGM']);
});
});
// -------------------------------------------------------------------------
// Free-format COBOL handling
// -------------------------------------------------------------------------
describe('Free-format COBOL handling', () => {
it('free-format source detected via >>SOURCE FREE', () => {
const src = [
'>>SOURCE FORMAT IS FREE',
'IDENTIFICATION DIVISION.',
'PROGRAM-ID. FREEPROG.',
'PROCEDURE DIVISION.',
'MAIN-PARA.',
' PERFORM PROCESS-DATA.',
'PROCESS-DATA.',
' STOP RUN.',
].join('\n');
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programName).toBe('FREEPROG');
expect(r.paragraphs).toHaveLength(2);
expect(r.performs).toHaveLength(1);
});
it('free-format *> comments stripped but not inside quotes', () => {
const src = [
'>>SOURCE FREE',
'IDENTIFICATION DIVISION.',
'PROGRAM-ID. TESTPROG.',
'DATA DIVISION.',
'WORKING-STORAGE SECTION.',
'01 WS-DATA PIC X(10). *> this is a comment',
].join('\n');
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.dataItems.find(d => d.name === 'WS-DATA')).toBeDefined();
});
});
// -------------------------------------------------------------------------
// CANCEL extraction in CALL ON EXCEPTION block
// -------------------------------------------------------------------------
describe('CANCEL extraction in CALL ON EXCEPTION block', () => {
it('CANCEL inside CALL END-CALL block is extracted', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'MAINPROG'",
' USING WS-DATA',
' ON EXCEPTION',
" CANCEL 'MAINPROG'",
" CALL 'BACKUP-PGM'",
' END-CALL.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// Both CALLs should be captured
expect(r.calls).toHaveLength(2);
expect(r.calls.map(c => c.target).sort()).toEqual(['BACKUP-PGM', 'MAINPROG']);
// CANCEL should be captured from within the CALL block
expect(r.cancels).toHaveLength(1);
expect(r.cancels[0].target).toBe('MAINPROG');
});
});
// -------------------------------------------------------------------------
// SORT INPUT PROCEDURE THRU range
// -------------------------------------------------------------------------
describe('SORT INPUT PROCEDURE THRU range', () => {
it('captures both start and thru target', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SORT SORT-FILE ON ASCENDING KEY SORT-KEY',
' INPUT PROCEDURE IS BUILD-INPUT THRU BUILD-END',
' OUTPUT PROCEDURE IS WRITE-OUTPUT.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
// INPUT PROCEDURE should produce a perform with thruTarget
const inputProc = r.performs.find(p => p.target === 'BUILD-INPUT');
expect(inputProc).toBeDefined();
expect(inputProc!.thruTarget).toBe('BUILD-END');
// OUTPUT PROCEDURE should be captured too
expect(r.performs.find(p => p.target === 'WRITE-OUTPUT')).toBeDefined();
});
});
// -------------------------------------------------------------------------
// Shared verb constant coverage
// -------------------------------------------------------------------------
describe('Shared verb constant coverage', () => {
it('COBOL_STATEMENT_VERBS flush trigger works for all major verbs', () => {
// Test that each verb in the shared constant terminates callAccum
const verbs = [
'PERFORM NEXT-PARA.', 'MOVE WS-A TO WS-B.', 'DISPLAY "HELLO".',
'GO TO EXIT-PARA.', 'INSPECT WS-X REPLACING ALL SPACES BY ZEROS.',
'SET WS-FLAG TO TRUE.', 'INITIALIZE WS-REC.', 'CANCEL WS-OLD.',
];
for (const verb of verbs) {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-PARM',
` ${verb}`,
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls.length).toBe(1);
expect(r.calls[0].parameters).toEqual(['WS-PARM']);
}
});
});
// -------------------------------------------------------------------------
// EXEC SQL INCLUDE edge cases
// -------------------------------------------------------------------------
describe('EXEC SQL INCLUDE edge cases', () => {
it('multiple EXEC SQL INCLUDEs extracted', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' EXEC SQL INCLUDE SQLCA END-EXEC.',
' EXEC SQL INCLUDE SQLDA END-EXEC.',
' EXEC SQL INCLUDE CUSTDCL END-EXEC.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
const includes = r.execSqlBlocks.filter(b => b.includeMember);
expect(includes).toHaveLength(3);
expect(includes.map(i => i.includeMember).sort()).toEqual(['CUSTDCL', 'SQLCA', 'SQLDA']);
});
});
// -------------------------------------------------------------------------
// Complete COBOL program integration
// -------------------------------------------------------------------------
describe('Complete COBOL program integration', () => {
it('extracts all construct types from a realistic program', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. FULLTEST.',
' AUTHOR. TEST AUTHOR.',
' ENVIRONMENT DIVISION.',
' INPUT-OUTPUT SECTION.',
' FILE-CONTROL.',
" SELECT CUST-FILE ASSIGN TO 'CUSTFILE'",
' ORGANIZATION IS INDEXED',
' ACCESS IS DYNAMIC',
' RECORD KEY IS CUST-ID.',
' DATA DIVISION.',
' WORKING-STORAGE SECTION.',
' 01 WS-COUNT PIC 9(4) VALUE 0.',
' 01 WS-TABLE OCCURS 10 DEPENDING ON WS-COUNT.',
' 01 WS-FLAG PIC 9 VALUE 0.',
' 88 END-OF-FILE VALUE 1.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' PERFORM PROCESS-DATA',
' SET END-OF-FILE TO TRUE',
" CALL 'SUBPROG' USING WS-COUNT.",
' PROCESS-DATA.',
" INSPECT WS-FLAG REPLACING ALL '0' BY '1'.",
' INITIALIZE WS-TABLE.',
' STOP RUN.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.programName).toBe('FULLTEST');
expect(r.programMetadata.author).toBe('TEST AUTHOR');
expect(r.fileDeclarations).toHaveLength(1);
expect(r.fileDeclarations[0].organization).toBe('INDEXED');
expect(r.dataItems.find(d => d.name === 'WS-COUNT')?.values).toEqual(['0']);
expect(r.dataItems.find(d => d.name === 'WS-TABLE')?.dependingOn).toBe('WS-COUNT');
expect(r.paragraphs).toHaveLength(2);
expect(r.performs).toHaveLength(1);
expect(r.sets).toHaveLength(1);
expect(r.sets[0].form).toBe('to-true');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-COUNT']);
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].form).toBe('replacing');
expect(r.initializes).toHaveLength(1);
});
});
// -------------------------------------------------------------------------
// Accumulator flush at END PROGRAM boundary
// -------------------------------------------------------------------------
describe('Accumulator flush at END PROGRAM boundary', () => {
it('multi-line CALL flushed at END PROGRAM', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER.',
' PROCEDURE DIVISION.',
' OUTER-MAIN.',
" CALL 'SUBPROG'",
' USING WS-DATA',
' END PROGRAM OUTER.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('SUBPROG');
expect(r.calls[0].parameters).toEqual(['WS-DATA']);
});
it('multi-line CALL flushed at END PROGRAM in nested programs', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER.',
' PROCEDURE DIVISION.',
' OUTER-MAIN.',
' STOP RUN.',
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. INNER.',
' PROCEDURE DIVISION.',
' INNER-MAIN.',
" CALL 'INNERSUB'",
' USING WS-INNER-DATA',
' END PROGRAM INNER.',
' END PROGRAM OUTER.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('INNERSUB');
expect(r.calls[0].parameters).toEqual(['WS-INNER-DATA']);
expect(r.programs).toHaveLength(2);
});
});
// -------------------------------------------------------------------------
// Accumulator flush at PROGRAM-ID sibling boundary
// -------------------------------------------------------------------------
describe('Accumulator flush at PROGRAM-ID sibling boundary', () => {
it('multi-line CALL flushed when sibling PROGRAM-ID appears without ID DIVISION', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. OUTER.',
' PROCEDURE DIVISION.',
' OUTER-MAIN.',
" CALL 'OUTERSUB'",
' USING WS-OUTER',
' PROGRAM-ID. SIBLING.',
' PROCEDURE DIVISION.',
' SIB-MAIN.',
' STOP RUN.',
' END PROGRAM SIBLING.',
' END PROGRAM OUTER.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].target).toBe('OUTERSUB');
expect(r.calls[0].parameters).toEqual(['WS-OUTER']);
const names = r.programs.map(p => p.name);
expect(names).toContain('SIBLING');
});
});
// -------------------------------------------------------------------------
// Accumulator flush on arithmetic verb boundaries
// -------------------------------------------------------------------------
describe('Accumulator flush on arithmetic verb boundaries', () => {
it('COMPUTE terminates multi-line CALL accumulation', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-INPUT',
' COMPUTE WS-TOTAL = WS-A + WS-B.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-INPUT']);
});
it('ADD terminates multi-line CALL accumulation', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-AMT',
' ADD WS-AMT TO WS-TOTAL.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-AMT']);
});
it('SUBTRACT terminates multi-line CALL accumulation', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-VAL',
' SUBTRACT WS-DISCOUNT FROM WS-TOTAL.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-VAL']);
});
it('MULTIPLY terminates multi-line CALL accumulation', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-QTY',
' MULTIPLY WS-PRICE BY WS-QTY.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-QTY']);
});
it('DIVIDE terminates multi-line CALL accumulation', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-TOTAL',
' DIVIDE WS-TOTAL BY WS-COUNT GIVING WS-AVG.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-TOTAL']);
});
it('STRING terminates multi-line CALL accumulation', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-NAME',
" STRING WS-FIRST DELIMITED BY SIZE INTO WS-FULL.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-NAME']);
});
it('UNSTRING terminates multi-line CALL accumulation', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM'",
' USING WS-LINE',
" UNSTRING WS-LINE DELIMITED BY ',' INTO WS-A WS-B.",
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
expect(r.calls[0].parameters).toEqual(['WS-LINE']);
});
});
// -------------------------------------------------------------------------
// Arithmetic verbs not captured as false USING parameters
// -------------------------------------------------------------------------
describe('Arithmetic verbs not captured as false USING parameters', () => {
it('COMPUTE after CALL USING does not pollute parameters', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
" CALL 'PGM' USING WS-INPUT.",
' COMPUTE WS-RESULT = WS-A * WS-B.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.calls).toHaveLength(1);
// Only WS-INPUT should be a parameter, not WS-RESULT/WS-A/WS-B
expect(r.calls[0].parameters).toEqual(['WS-INPUT']);
});
});
// -------------------------------------------------------------------------
// SORT accumulator flushed at program boundaries
// -------------------------------------------------------------------------
describe('SORT accumulator flushed at program boundaries', () => {
it('multi-line SORT flushed at END PROGRAM', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' SORT SORT-FILE',
' USING INPUT-FILE',
' END PROGRAM TESTPROG.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.sorts).toHaveLength(1);
expect(r.sorts[0].sortFile).toBe('SORT-FILE');
expect(r.sorts[0].usingFiles).toEqual(['INPUT-FILE']);
});
});
// -------------------------------------------------------------------------
// INSPECT accumulator flushed at program boundaries
// -------------------------------------------------------------------------
describe('INSPECT accumulator flushed at program boundaries', () => {
it('multi-line INSPECT flushed at END PROGRAM', () => {
const src = cobol(
' IDENTIFICATION DIVISION.',
' PROGRAM-ID. TESTPROG.',
' PROCEDURE DIVISION.',
' MAIN-PARA.',
' INSPECT WS-DATA',
" REPLACING ALL 'X' BY 'Y'",
' END PROGRAM TESTPROG.',
);
const r = extractCobolSymbolsWithRegex(src, 'test.cbl');
expect(r.inspects).toHaveLength(1);
expect(r.inspects[0].inspectedField).toBe('WS-DATA');
expect(r.inspects[0].form).toBe('replacing');
});
});
});